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.ArtSchoolRecruitMajorHistoryBo;
import org.dromara.art.domain.vo.ArtSchoolRecruitMajorHistoryVo;
import org.dromara.art.domain.ArtSchoolRecruitMajorHistory;
import org.dromara.art.mapper.ArtSchoolRecruitMajorHistoryMapper;
import org.dromara.art.service.IArtSchoolRecruitMajorHistoryService;

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

/**
 * 院校招录专业历年录取数据Service业务层处理
 *
 * @author ZhouWenTao
 * @date 2026-02-20
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ArtSchoolRecruitMajorHistoryServiceImpl implements IArtSchoolRecruitMajorHistoryService {

    private final ArtSchoolRecruitMajorHistoryMapper baseMapper;

    /**
     * 查询院校招录专业历年录取数据
     *
     * @param historyId 主键
     * @return 院校招录专业历年录取数据
     */
    @Override
    public ArtSchoolRecruitMajorHistoryVo queryById(Long historyId){
        return baseMapper.selectVoById(historyId);
    }

    /**
     * 分页查询院校招录专业历年录取数据列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 院校招录专业历年录取数据分页列表
     */
    @Override
    public TableDataInfo<ArtSchoolRecruitMajorHistoryVo> queryPageList(ArtSchoolRecruitMajorHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArtSchoolRecruitMajorHistory> lqw = buildQueryWrapper(bo);
        Page<ArtSchoolRecruitMajorHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的院校招录专业历年录取数据列表
     *
     * @param bo 查询条件
     * @return 院校招录专业历年录取数据列表
     */
    @Override
    public List<ArtSchoolRecruitMajorHistoryVo> queryList(ArtSchoolRecruitMajorHistoryBo bo) {
        LambdaQueryWrapper<ArtSchoolRecruitMajorHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ArtSchoolRecruitMajorHistory> buildQueryWrapper(ArtSchoolRecruitMajorHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArtSchoolRecruitMajorHistory> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ArtSchoolRecruitMajorHistory::getHistoryId);
        lqw.eq(bo.getRecruitMajorId() != null, ArtSchoolRecruitMajorHistory::getRecruitMajorId, bo.getRecruitMajorId());
        lqw.eq(bo.getSchoolId() != null, ArtSchoolRecruitMajorHistory::getSchoolId, bo.getSchoolId());
        lqw.eq(StringUtils.isNotBlank(bo.getSchoolCode()), ArtSchoolRecruitMajorHistory::getSchoolCode, bo.getSchoolCode());
        lqw.eq(StringUtils.isNotBlank(bo.getCollegeCode()), ArtSchoolRecruitMajorHistory::getCollegeCode, bo.getCollegeCode());
        lqw.like(StringUtils.isNotBlank(bo.getSchoolName()), ArtSchoolRecruitMajorHistory::getSchoolName, bo.getSchoolName());
        lqw.eq(bo.getMajorId() != null, ArtSchoolRecruitMajorHistory::getMajorId, bo.getMajorId());
        lqw.eq(StringUtils.isNotBlank(bo.getMajorCode()), ArtSchoolRecruitMajorHistory::getMajorCode, bo.getMajorCode());
        lqw.like(StringUtils.isNotBlank(bo.getMajorName()), ArtSchoolRecruitMajorHistory::getMajorName, bo.getMajorName());
        lqw.eq(StringUtils.isNotBlank(bo.getEnrollCode()), ArtSchoolRecruitMajorHistory::getEnrollCode, bo.getEnrollCode());
        lqw.eq(StringUtils.isNotBlank(bo.getMajorType()), ArtSchoolRecruitMajorHistory::getMajorType, bo.getMajorType());
        lqw.eq(StringUtils.isNotBlank(bo.getMajorTypeSub()), ArtSchoolRecruitMajorHistory::getMajorTypeSub, bo.getMajorTypeSub());
        lqw.eq(StringUtils.isNotBlank(bo.getMainExamSubject()), ArtSchoolRecruitMajorHistory::getMainExamSubject, bo.getMainExamSubject());
        lqw.eq(bo.getYear() != null, ArtSchoolRecruitMajorHistory::getYear, bo.getYear());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjectType()), ArtSchoolRecruitMajorHistory::getSubjectType, bo.getSubjectType());
        lqw.like(StringUtils.isNotBlank(bo.getBatchName()), ArtSchoolRecruitMajorHistory::getBatchName, bo.getBatchName());
        lqw.eq(StringUtils.isNotBlank(bo.getAdmissionFormula()), ArtSchoolRecruitMajorHistory::getAdmissionFormula, bo.getAdmissionFormula());
        lqw.eq(StringUtils.isNotBlank(bo.getProbabilityOperator()), ArtSchoolRecruitMajorHistory::getProbabilityOperator, bo.getProbabilityOperator());
        lqw.eq(bo.getControlScore() != null, ArtSchoolRecruitMajorHistory::getControlScore, bo.getControlScore());
        lqw.eq(bo.getAdmissionScore() != null, ArtSchoolRecruitMajorHistory::getAdmissionScore, bo.getAdmissionScore());
        lqw.eq(bo.getPlanEnroll() != null, ArtSchoolRecruitMajorHistory::getPlanEnroll, bo.getPlanEnroll());
        lqw.eq(bo.getFiledAmount() != null, ArtSchoolRecruitMajorHistory::getFiledAmount, bo.getFiledAmount());
        lqw.eq(bo.getAdmitAmount() != null, ArtSchoolRecruitMajorHistory::getAdmitAmount, bo.getAdmitAmount());
        lqw.eq(bo.getFirstChoiceAdmitAmount() != null, ArtSchoolRecruitMajorHistory::getFirstChoiceAdmitAmount, bo.getFirstChoiceAdmitAmount());
        lqw.eq(bo.getMinScoreDiff() != null, ArtSchoolRecruitMajorHistory::getMinScoreDiff, bo.getMinScoreDiff());
        lqw.eq(bo.getTuitionFee() != null, ArtSchoolRecruitMajorHistory::getTuitionFee, bo.getTuitionFee());
        return lqw;
    }

    /**
     * 新增院校招录专业历年录取数据
     *
     * @param bo 院校招录专业历年录取数据
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ArtSchoolRecruitMajorHistoryBo bo) {
        ArtSchoolRecruitMajorHistory add = MapstructUtils.convert(bo, ArtSchoolRecruitMajorHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setHistoryId(add.getHistoryId());
        }
        return flag;
    }

    /**
     * 修改院校招录专业历年录取数据
     *
     * @param bo 院校招录专业历年录取数据
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ArtSchoolRecruitMajorHistoryBo bo) {
        ArtSchoolRecruitMajorHistory update = MapstructUtils.convert(bo, ArtSchoolRecruitMajorHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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