package org.dromara.system.service.impl.opinion;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.opinion.XiaoyuanOpinion;
import org.dromara.system.domain.opinion.bo.XiaoyuanOpinionBo;
import org.dromara.system.domain.opinion.vo.XiaoyuanOpinionVo;
import org.dromara.system.mapper.opinion.XiaoyuanOpinionMapper;
import org.dromara.system.service.opinion.IXiaoyuanOpinionService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 校园建议IDService业务层处理
 *
 * @author boman
 * @date 2023-09-15
 */
@RequiredArgsConstructor
@Service
public class XiaoyuanOpinionServiceImpl implements IXiaoyuanOpinionService {

    private final XiaoyuanOpinionMapper baseMapper;

    /**
     * 查询校园建议ID
     */
    @Override
    public XiaoyuanOpinionVo queryById(Long opinionId){
        return baseMapper.selectVoById(opinionId);
    }

    /**
     * 查询校园建议ID列表
     */
    @Override
    public TableDataInfo<XiaoyuanOpinionVo> queryPageList(XiaoyuanOpinionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<XiaoyuanOpinion> lqw = buildQueryWrapper(bo);
        Page<XiaoyuanOpinionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询校园建议ID列表
     */
    @Override
    public List<XiaoyuanOpinionVo> queryList(XiaoyuanOpinionBo bo) {
        LambdaQueryWrapper<XiaoyuanOpinion> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<XiaoyuanOpinion> buildQueryWrapper(XiaoyuanOpinionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<XiaoyuanOpinion> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOpinionDetails()), XiaoyuanOpinion::getOpinionDetails, bo.getOpinionDetails());
        lqw.eq(StringUtils.isNotBlank(bo.getOpinionReply()), XiaoyuanOpinion::getOpinionReply, bo.getOpinionReply());
        lqw.eq(StringUtils.isNotBlank(bo.getOpinionType()), XiaoyuanOpinion::getOpinionType, bo.getOpinionType());
        lqw.eq(StringUtils.isNotBlank(bo.getOpinionPhoto()), XiaoyuanOpinion::getOpinionPhoto, bo.getOpinionPhoto());
        return lqw;
    }

    /**
     * 新增校园建议ID
     */
    @Override
    public Boolean insertByBo(XiaoyuanOpinionBo bo) {
        XiaoyuanOpinion add = MapstructUtils.convert(bo, XiaoyuanOpinion.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOpinionId(add.getOpinionId());
        }
        return flag;
    }

    /**
     * 修改校园建议ID
     */
    @Override
    public Boolean updateByBo(XiaoyuanOpinionBo bo) {
        XiaoyuanOpinion update = MapstructUtils.convert(bo, XiaoyuanOpinion.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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