package org.dromara.system.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.system.domain.bo.CaseCommentSettingBo;
import org.dromara.system.domain.vo.CaseCommentSettingVo;
import org.dromara.system.domain.CaseCommentSetting;
import org.dromara.system.mapper.CaseCommentSettingMapper;
import org.dromara.system.service.ICaseCommentSettingService;

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

/**
 * 病例点评配置Service业务层处理
 *
 * @author wuzhigang
 * @date 2024-01-03
 */
@RequiredArgsConstructor
@Service
public class CaseCommentSettingServiceImpl implements ICaseCommentSettingService {

    private final CaseCommentSettingMapper baseMapper;

    /**
     * 查询病例点评配置
     */
    @Override
    public CaseCommentSettingVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询病例点评配置列表
     */
    @Override
    public TableDataInfo<CaseCommentSettingVo> queryPageList(CaseCommentSettingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CaseCommentSetting> lqw = buildQueryWrapper(bo);
        Page<CaseCommentSettingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询病例点评配置列表
     */
    @Override
    public List<CaseCommentSettingVo> queryList(CaseCommentSettingBo bo) {
        LambdaQueryWrapper<CaseCommentSetting> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CaseCommentSetting> buildQueryWrapper(CaseCommentSettingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CaseCommentSetting> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCaseCommentScore()), CaseCommentSetting::getCaseCommentScore, bo.getCaseCommentScore());
        lqw.eq(StringUtils.isNotBlank(bo.getCommentTips()), CaseCommentSetting::getCommentTips, bo.getCommentTips());
        return lqw;
    }

    /**
     * 新增病例点评配置
     */
    @Override
    public Boolean insertByBo(CaseCommentSettingBo bo) {
        CaseCommentSetting add = MapstructUtils.convert(bo, CaseCommentSetting.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改病例点评配置
     */
    @Override
    public Boolean updateByBo(CaseCommentSettingBo bo) {
        CaseCommentSetting update = MapstructUtils.convert(bo, CaseCommentSetting.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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