package org.dromara.business.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.CaseFeedbackBo;
import org.dromara.business.domain.vo.CaseFeedbackVo;
import org.dromara.business.domain.CaseFeedback;
import org.dromara.business.mapper.CaseFeedbackMapper;
import org.dromara.business.service.ICaseFeedbackService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 意见反馈Service业务层处理
 *
 * @author wuzhigang
 * @date 2023-12-01
 */
@RequiredArgsConstructor
@Service
public class CaseFeedbackServiceImpl implements ICaseFeedbackService {

    private final CaseFeedbackMapper baseMapper;

    /**
     * 查询意见反馈
     */
    @Override
    public CaseFeedbackVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询意见反馈列表
     */
    @Override
    public TableDataInfo<CaseFeedbackVo> queryPageList(CaseFeedbackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CaseFeedback> lqw = buildQueryWrapper(bo);
        Page<CaseFeedbackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询意见反馈列表
     */
    @Override
    public List<CaseFeedbackVo> queryList(CaseFeedbackBo bo) {
        LambdaQueryWrapper<CaseFeedback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CaseFeedback> buildQueryWrapper(CaseFeedbackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CaseFeedback> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, CaseFeedback::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getFeedbackType()), CaseFeedback::getFeedbackType, bo.getFeedbackType());
        lqw.eq(StringUtils.isNotBlank(bo.getFeedback()), CaseFeedback::getFeedback, bo.getFeedback());
        return lqw;
    }

    /**
     * 新增意见反馈
     */
    @Override
    public Boolean insertByBo(CaseFeedbackBo bo) {
        CaseFeedback add = MapstructUtils.convert(bo, CaseFeedback.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改意见反馈
     */
    @Override
    public Boolean updateByBo(CaseFeedbackBo bo) {
        CaseFeedback update = MapstructUtils.convert(bo, CaseFeedback.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CaseFeedback entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除意见反馈
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
