package org.dromara.art.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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.art.domain.bo.ArtHistoryScoreControlLineBo;
import org.dromara.art.domain.vo.ArtHistoryScoreControlLineVo;
import org.dromara.art.domain.ArtHistoryScoreControlLine;
import org.dromara.art.mapper.ArtHistoryScoreControlLineMapper;
import org.dromara.art.service.IArtHistoryScoreControlLineService;

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

/**
 * 历年省控线Service业务层处理
 *
 * @author ZhouWenTao
 * @date 2026-02-20
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ArtHistoryScoreControlLineServiceImpl implements IArtHistoryScoreControlLineService {

    private final ArtHistoryScoreControlLineMapper baseMapper;

    /**
     * 查询历年省控线
     *
     * @param controlId 主键
     * @return 历年省控线
     */
    @Override
    public ArtHistoryScoreControlLineVo queryById(Long controlId){
        return baseMapper.selectVoById(controlId);
    }

    /**
     * 分页查询历年省控线列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 历年省控线分页列表
     */
    @Override
    public TableDataInfo<ArtHistoryScoreControlLineVo> queryPageList(ArtHistoryScoreControlLineBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArtHistoryScoreControlLine> lqw = buildQueryWrapper(bo);
        Page<ArtHistoryScoreControlLineVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的历年省控线列表
     *
     * @param bo 查询条件
     * @return 历年省控线列表
     */
    @Override
    public List<ArtHistoryScoreControlLineVo> queryList(ArtHistoryScoreControlLineBo bo) {
        LambdaQueryWrapper<ArtHistoryScoreControlLine> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ArtHistoryScoreControlLine> buildQueryWrapper(ArtHistoryScoreControlLineBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArtHistoryScoreControlLine> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ArtHistoryScoreControlLine::getControlId);
        lqw.eq(StringUtils.isNotBlank(bo.getProvinceCode()), ArtHistoryScoreControlLine::getProvinceCode, bo.getProvinceCode());
        lqw.like(StringUtils.isNotBlank(bo.getProvinceName()), ArtHistoryScoreControlLine::getProvinceName, bo.getProvinceName());
        lqw.eq(bo.getYear() != null, ArtHistoryScoreControlLine::getYear, bo.getYear());
        lqw.eq(StringUtils.isNotBlank(bo.getMajorCategory()), ArtHistoryScoreControlLine::getMajorCategory, bo.getMajorCategory());
        lqw.like(StringUtils.isNotBlank(bo.getBatchName()), ArtHistoryScoreControlLine::getBatchName, bo.getBatchName());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjectType()), ArtHistoryScoreControlLine::getSubjectType, bo.getSubjectType());
        lqw.eq(bo.getCultureScore() != null, ArtHistoryScoreControlLine::getCultureScore, bo.getCultureScore());
        lqw.eq(bo.getMajorScore() != null, ArtHistoryScoreControlLine::getMajorScore, bo.getMajorScore());
        lqw.eq(bo.getCultureScoreExam() != null, ArtHistoryScoreControlLine::getCultureScoreExam, bo.getCultureScoreExam());
        lqw.eq(bo.getMajorScoreExam() != null, ArtHistoryScoreControlLine::getMajorScoreExam, bo.getMajorScoreExam());
        return lqw;
    }

    /**
     * 新增历年省控线
     *
     * @param bo 历年省控线
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ArtHistoryScoreControlLineBo bo) {
        ArtHistoryScoreControlLine add = MapstructUtils.convert(bo, ArtHistoryScoreControlLine.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setControlId(add.getControlId());
        }
        return flag;
    }

    /**
     * 修改历年省控线
     *
     * @param bo 历年省控线
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ArtHistoryScoreControlLineBo bo) {
        ArtHistoryScoreControlLine update = MapstructUtils.convert(bo, ArtHistoryScoreControlLine.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除历年省控线信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
