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.ArtSchoolDetailBo;
import org.dromara.art.domain.vo.ArtSchoolDetailVo;
import org.dromara.art.domain.ArtSchoolDetail;
import org.dromara.art.mapper.ArtSchoolDetailMapper;
import org.dromara.art.service.IArtSchoolDetailService;

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

/**
 * 学校详细信息Service业务层处理
 *
 * @author ZhouWenTao
 * @date 2026-02-17
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ArtSchoolDetailServiceImpl implements IArtSchoolDetailService {

    private final ArtSchoolDetailMapper baseMapper;

    /**
     * 查询学校详细信息
     *
     * @param detailId 主键
     * @return 学校详细信息
     */
    @Override
    public ArtSchoolDetailVo queryById(Long detailId){
        return stripJsonb(baseMapper.selectVoById(detailId));
    }

    /**
     * 分页查询学校详细信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 学校详细信息分页列表
     */
    @Override
    public TableDataInfo<ArtSchoolDetailVo> queryPageList(ArtSchoolDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArtSchoolDetail> lqw = buildQueryWrapper(bo);
        Page<ArtSchoolDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        if (result.getRecords() != null) {
            result.getRecords().forEach(this::stripJsonb);
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的学校详细信息列表
     *
     * @param bo 查询条件
     * @return 学校详细信息列表
     */
    @Override
    public List<ArtSchoolDetailVo> queryList(ArtSchoolDetailBo bo) {
        LambdaQueryWrapper<ArtSchoolDetail> lqw = buildQueryWrapper(bo);
        List<ArtSchoolDetailVo> list = baseMapper.selectVoList(lqw);
        if (list != null) {
            list.forEach(this::stripJsonb);
        }
        return list;
    }

    private LambdaQueryWrapper<ArtSchoolDetail> buildQueryWrapper(ArtSchoolDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArtSchoolDetail> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ArtSchoolDetail::getDetailId);
        lqw.eq(bo.getSchoolId() != null, ArtSchoolDetail::getSchoolId, bo.getSchoolId());
        lqw.like(StringUtils.isNotBlank(bo.getIntroduction()), ArtSchoolDetail::getIntroduction, bo.getIntroduction());
        lqw.eq(StringUtils.isNotBlank(bo.getSchoolIcon()), ArtSchoolDetail::getSchoolIcon, bo.getSchoolIcon());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), ArtSchoolDetail::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getContact()), ArtSchoolDetail::getContact, bo.getContact());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), ArtSchoolDetail::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getWebsite()), ArtSchoolDetail::getWebsite, bo.getWebsite());
        lqw.eq(StringUtils.isNotBlank(bo.getPostcode()), ArtSchoolDetail::getPostcode, bo.getPostcode());
        lqw.eq(bo.getEstablishYear() != null, ArtSchoolDetail::getEstablishYear, bo.getEstablishYear());
        lqw.eq(bo.getCampusAreaMu() != null, ArtSchoolDetail::getCampusAreaMu, bo.getCampusAreaMu());
        lqw.eq(bo.getLibraryCollection() != null, ArtSchoolDetail::getLibraryCollection, bo.getLibraryCollection());
        lqw.eq(bo.getMaleRatio() != null, ArtSchoolDetail::getMaleRatio, bo.getMaleRatio());
        lqw.eq(bo.getFemaleRatio() != null, ArtSchoolDetail::getFemaleRatio, bo.getFemaleRatio());
        lqw.eq(bo.getIs985() != null, ArtSchoolDetail::getIs985, bo.getIs985());
        lqw.eq(bo.getIs211() != null, ArtSchoolDetail::getIs211, bo.getIs211());
        lqw.eq(bo.getIsDoubleFirstClass() != null, ArtSchoolDetail::getIsDoubleFirstClass, bo.getIsDoubleFirstClass());
        lqw.eq(bo.getIsKeyUniversity() != null, ArtSchoolDetail::getIsKeyUniversity, bo.getIsKeyUniversity());
        lqw.eq(bo.getStudentCount() != null, ArtSchoolDetail::getStudentCount, bo.getStudentCount());
        lqw.eq(bo.getTeacherCount() != null, ArtSchoolDetail::getTeacherCount, bo.getTeacherCount());
        lqw.eq(bo.getMasterPoint() != null, ArtSchoolDetail::getMasterPoint, bo.getMasterPoint());
        lqw.eq(bo.getDoctorPoint() != null, ArtSchoolDetail::getDoctorPoint, bo.getDoctorPoint());
        lqw.eq(bo.getKeyMajorCount() != null, ArtSchoolDetail::getKeyMajorCount, bo.getKeyMajorCount());
        lqw.eq(bo.getEmploymentRate() != null, ArtSchoolDetail::getEmploymentRate, bo.getEmploymentRate());
        lqw.eq(bo.getSatisfactionRate() != null, ArtSchoolDetail::getSatisfactionRate, bo.getSatisfactionRate());
        lqw.eq(bo.getUnivId() != null, ArtSchoolDetail::getUnivId, bo.getUnivId());
        return lqw;
    }

    /**
     * 新增学校详细信息
     *
     * @param bo 学校详细信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ArtSchoolDetailBo bo) {
        ArtSchoolDetail add = MapstructUtils.convert(bo, ArtSchoolDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改学校详细信息
     *
     * @param bo 学校详细信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ArtSchoolDetailBo bo) {
        ArtSchoolDetail update = MapstructUtils.convert(bo, ArtSchoolDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public ArtSchoolDetail queryEntityBySchoolId(Long schoolId) {
        if (schoolId == null) {
            return null;
        }
        return baseMapper.selectOne(Wrappers.<ArtSchoolDetail>lambdaQuery()
            .eq(ArtSchoolDetail::getSchoolId, schoolId), false);
    }

    @Override
    public Boolean saveOrUpdateBySchoolId(ArtSchoolDetail detail) {
        if (detail == null || detail.getSchoolId() == null) {
            return false;
        }
        if (detail.getDetailId() != null) {
            return baseMapper.updateById(detail) > 0;
        }
        ArtSchoolDetail exist = queryEntityBySchoolId(detail.getSchoolId());
        if (exist == null) {
            return baseMapper.insert(detail) > 0;
        }
        detail.setDetailId(exist.getDetailId());
        return baseMapper.updateById(detail) > 0;
    }

    private ArtSchoolDetailVo stripJsonb(ArtSchoolDetailVo vo) {
        if (vo == null) {
            return null;
        }
        vo.setSatisfactionJson(null);
        vo.setScholarshipJson(null);
        vo.setSpecialMajorJson(null);
        vo.setEmploymentReportJson(null);
        vo.setPhotoJson(null);
        vo.setAccommodationJson(null);
        vo.setSubjectReviewsJson(null);
        vo.setResearchJson(null);
        vo.setUnivMajorsJson(null);
        vo.setUnivPostgraduateJson(null);
        return vo;
    }

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

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