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.ArtSchoolRecruitMajorBo;
import org.dromara.art.domain.vo.ArtSchoolRecruitMajorVo;
import org.dromara.art.domain.ArtSchoolRecruitMajor;
import org.dromara.art.mapper.ArtSchoolRecruitMajorMapper;
import org.dromara.art.service.IArtSchoolRecruitMajorService;

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

/**
 * 院校招录专业Service业务层处理
 *
 * @author ZhouWenTao
 * @date 2026-02-20
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ArtSchoolRecruitMajorServiceImpl implements IArtSchoolRecruitMajorService {

    private final ArtSchoolRecruitMajorMapper baseMapper;

    /**
     * 查询院校招录专业
     *
     * @param recruitMajorId 主键
     * @return 院校招录专业
     */
    @Override
    public ArtSchoolRecruitMajorVo queryById(Long recruitMajorId){
        return baseMapper.selectVoById(recruitMajorId);
    }

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

    /**
     * 查询符合条件的院校招录专业列表
     *
     * @param bo 查询条件
     * @return 院校招录专业列表
     */
    @Override
    public List<ArtSchoolRecruitMajorVo> queryList(ArtSchoolRecruitMajorBo bo) {
        LambdaQueryWrapper<ArtSchoolRecruitMajor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ArtSchoolRecruitMajor> buildQueryWrapper(ArtSchoolRecruitMajorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArtSchoolRecruitMajor> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ArtSchoolRecruitMajor::getRecruitMajorId);
        lqw.eq(bo.getSchoolId() != null, ArtSchoolRecruitMajor::getSchoolId, bo.getSchoolId());
        lqw.eq(StringUtils.isNotBlank(bo.getSchoolCode()), ArtSchoolRecruitMajor::getSchoolCode, bo.getSchoolCode());
        lqw.like(StringUtils.isNotBlank(bo.getSchoolName()), ArtSchoolRecruitMajor::getSchoolName, bo.getSchoolName());
        lqw.eq(bo.getYear() != null, ArtSchoolRecruitMajor::getYear, bo.getYear());
        lqw.eq(bo.getMajorId() != null, ArtSchoolRecruitMajor::getMajorId, bo.getMajorId());
        lqw.eq(StringUtils.isNotBlank(bo.getMajorCode()), ArtSchoolRecruitMajor::getMajorCode, bo.getMajorCode());
        lqw.like(StringUtils.isNotBlank(bo.getMajorName()), ArtSchoolRecruitMajor::getMajorName, bo.getMajorName());
        lqw.eq(StringUtils.isNotBlank(bo.getEnrollCode()), ArtSchoolRecruitMajor::getEnrollCode, bo.getEnrollCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDataStatus()), ArtSchoolRecruitMajor::getDataStatus, bo.getDataStatus());
        lqw.like(StringUtils.isNotBlank(bo.getBatchName()), ArtSchoolRecruitMajor::getBatchName, bo.getBatchName());
        lqw.eq(StringUtils.isNotBlank(bo.getMajorType()), ArtSchoolRecruitMajor::getMajorType, bo.getMajorType());
        lqw.eq(StringUtils.isNotBlank(bo.getMajorTypeSub()), ArtSchoolRecruitMajor::getMajorTypeSub, bo.getMajorTypeSub());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjectType()), ArtSchoolRecruitMajor::getSubjectType, bo.getSubjectType());
        lqw.eq(StringUtils.isNotBlank(bo.getAdmissionWayShort()), ArtSchoolRecruitMajor::getAdmissionWayShort, bo.getAdmissionWayShort());
        lqw.eq(StringUtils.isNotBlank(bo.getAdmissionWayExternal()), ArtSchoolRecruitMajor::getAdmissionWayExternal, bo.getAdmissionWayExternal());
        lqw.eq(StringUtils.isNotBlank(bo.getAdmissionWayExternalOp()), ArtSchoolRecruitMajor::getAdmissionWayExternalOp, bo.getAdmissionWayExternalOp());
        lqw.eq(StringUtils.isNotBlank(bo.getAdmissionWayInternal()), ArtSchoolRecruitMajor::getAdmissionWayInternal, bo.getAdmissionWayInternal());
        lqw.eq(StringUtils.isNotBlank(bo.getAdmissionWayInternalOp()), ArtSchoolRecruitMajor::getAdmissionWayInternalOp, bo.getAdmissionWayInternalOp());
        lqw.eq(bo.getPlanEnroll() != null, ArtSchoolRecruitMajor::getPlanEnroll, bo.getPlanEnroll());
        lqw.eq(StringUtils.isNotBlank(bo.getMainExamSubject()), ArtSchoolRecruitMajor::getMainExamSubject, bo.getMainExamSubject());
        lqw.eq(bo.getSchoolingYears() != null, ArtSchoolRecruitMajor::getSchoolingYears, bo.getSchoolingYears());
        lqw.eq(StringUtils.isNotBlank(bo.getEnrollLimitDesc()), ArtSchoolRecruitMajor::getEnrollLimitDesc, bo.getEnrollLimitDesc());
        lqw.eq(bo.getTuitionFee() != null, ArtSchoolRecruitMajor::getTuitionFee, bo.getTuitionFee());
        lqw.eq(bo.getCultureScoreLimit() != null, ArtSchoolRecruitMajor::getCultureScoreLimit, bo.getCultureScoreLimit());
        lqw.eq(bo.getMajorScoreLimit() != null, ArtSchoolRecruitMajor::getMajorScoreLimit, bo.getMajorScoreLimit());
        lqw.eq(bo.getChineseScoreLimit() != null, ArtSchoolRecruitMajor::getChineseScoreLimit, bo.getChineseScoreLimit());
        lqw.eq(bo.getEnglishScoreLimit() != null, ArtSchoolRecruitMajor::getEnglishScoreLimit, bo.getEnglishScoreLimit());
        return lqw;
    }

    /**
     * 新增院校招录专业
     *
     * @param bo 院校招录专业
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ArtSchoolRecruitMajorBo bo) {
        ArtSchoolRecruitMajor add = MapstructUtils.convert(bo, ArtSchoolRecruitMajor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRecruitMajorId(add.getRecruitMajorId());
        }
        return flag;
    }

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

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

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