package org.dromara.system.service.impl;

import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.DateUtils;
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.dromara.system.domain.bo.StudentLiveOldBo;
import org.dromara.system.domain.vo.StudentLiveOldVo;
import org.dromara.system.mapper.StudentLiveOldMapper;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.StudentInfoOldBo;
import org.dromara.system.domain.vo.StudentInfoOldVo;
import org.dromara.system.domain.StudentInfoOld;
import org.dromara.system.mapper.StudentInfoOldMapper;
import org.dromara.system.service.IStudentInfoOldService;

import java.util.*;

/**
 * 学生档案历史信息Service业务层处理
 *
 * @author Lion Li
 * @date 2023-08-16
 */
@RequiredArgsConstructor
@Service
public class StudentInfoOldServiceImpl implements IStudentInfoOldService {

    private final StudentInfoOldMapper baseMapper;
    private final StudentLiveOldMapper studentLiveOldMapper;

    /**
     * 查询学生档案历史信息
     */
    @Override
    public StudentInfoOldVo queryById(Long oldId){
        return baseMapper.selectVoById(oldId);
    }

    /**
     * 查询学生档案历史信息列表
     */
    @Override
    public TableDataInfo<StudentInfoOldVo> queryPageList(StudentInfoOldBo bo, PageQuery pageQuery) {
        if (bo.getStudentId() == null || bo.getStudentId() == 0L) {
            throw new ServiceException("参数错误");
        }
        LambdaQueryWrapper<StudentInfoOld> lqw = buildQueryWrapper(bo);
        Page<StudentInfoOldVo> result = baseMapper.selectVoMapperPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询学生档案历史信息列表
     */
    @Override
    public List<StudentInfoOldVo> queryList(StudentInfoOldBo bo) {
        LambdaQueryWrapper<StudentInfoOld> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoMapperList(lqw);
    }

    private LambdaQueryWrapper<StudentInfoOld> buildQueryWrapper(StudentInfoOldBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StudentInfoOld> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStudentId() != null, StudentInfoOld::getStudentId, bo.getStudentId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), StudentInfoOld::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), StudentInfoOld::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getAge()), StudentInfoOld::getAge, bo.getAge());
        lqw.like(StringUtils.isNotBlank(bo.getFamilyName()), StudentInfoOld::getFamilyName, bo.getFamilyName());
        lqw.eq(StringUtils.isNotBlank(bo.getSchool()), StudentInfoOld::getSchool, bo.getSchool());
        lqw.eq(StringUtils.isNotBlank(bo.getStudentNumber()), StudentInfoOld::getStudentNumber, bo.getStudentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), StudentInfoOld::getIdCard, bo.getIdCard());
        lqw.eq(StringUtils.isNotBlank(bo.getIsNearsightedness()), StudentInfoOld::getIsNearsightedness, bo.getIsNearsightedness());
        lqw.eq(StringUtils.isNotBlank(bo.getDegreeMyopia()), StudentInfoOld::getDegreeMyopia, bo.getDegreeMyopia());
        lqw.eq(StringUtils.isNotBlank(bo.getIsFarsightedness()), StudentInfoOld::getIsFarsightedness, bo.getIsFarsightedness());
        lqw.eq(StringUtils.isNotBlank(bo.getFarsightedness()), StudentInfoOld::getFarsightedness, bo.getFarsightedness());
        lqw.eq(StringUtils.isNotBlank(bo.getHeight()), StudentInfoOld::getHeight, bo.getHeight());
        lqw.eq(StringUtils.isNotBlank(bo.getWeight()), StudentInfoOld::getWeight, bo.getWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getBloodType()), StudentInfoOld::getBloodType, bo.getBloodType());
        lqw.eq(StringUtils.isNotBlank(bo.getPoliticalStatus()), StudentInfoOld::getPoliticalStatus, bo.getPoliticalStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getHealth()), StudentInfoOld::getHealth, bo.getHealth());
        lqw.eq(StringUtils.isNotBlank(bo.getIdentificationPhoto()), StudentInfoOld::getIdentificationPhoto, bo.getIdentificationPhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getMind()), StudentInfoOld::getMind, bo.getMind());
        lqw.eq(StringUtils.isNotBlank(bo.getEntrancePermit()), StudentInfoOld::getEntrancePermit, bo.getEntrancePermit());
        lqw.like(StringUtils.isNotBlank(bo.getFatherName()), StudentInfoOld::getFatherName, bo.getFatherName());
        lqw.eq(StringUtils.isNotBlank(bo.getFatherTelephone()), StudentInfoOld::getFatherTelephone, bo.getFatherTelephone());
        lqw.like(StringUtils.isNotBlank(bo.getMotherName()), StudentInfoOld::getMotherName, bo.getMotherName());
        lqw.eq(StringUtils.isNotBlank(bo.getMotherTelephone()), StudentInfoOld::getMotherTelephone, bo.getMotherTelephone());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), StudentInfoOld::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), StudentInfoOld::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), StudentInfoOld::getLatitude, bo.getLatitude());
        lqw.eq(StringUtils.isNotBlank(bo.getIsMedicalHistory()), StudentInfoOld::getIsMedicalHistory, bo.getIsMedicalHistory());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicalDescription()), StudentInfoOld::getMedicalDescription, bo.getMedicalDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getPsychologicalDescription()), StudentInfoOld::getPsychologicalDescription, bo.getPsychologicalDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getIsLset()), StudentInfoOld::getIsLset, bo.getIsLset());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPoverty()), StudentInfoOld::getIsPoverty, bo.getIsPoverty());
        lqw.eq(StringUtils.isNotBlank(bo.getEmergencyContact()), StudentInfoOld::getEmergencyContact, bo.getEmergencyContact());
        lqw.eq(StringUtils.isNotBlank(bo.getEmergencyContactTelephone()), StudentInfoOld::getEmergencyContactTelephone, bo.getEmergencyContactTelephone());
        return lqw;
    }

    /**
     * 新增学生档案历史信息
     */
    @Override
    public Boolean insertByBo(StudentInfoOldBo bo) {
        StudentInfoOld add = MapstructUtils.convert(bo, StudentInfoOld.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOldId(add.getOldId());
        }
        return flag;
    }

    /**
     * 修改学生档案历史信息
     */
    @Override
    public Boolean updateByBo(StudentInfoOldBo bo) {
        StudentInfoOld update = MapstructUtils.convert(bo, StudentInfoOld.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除学生档案历史信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R<Map<String, Object>> selectStudentInfoOldListEcharts(StudentLiveOldBo bo) {
        if (bo.getStudentId() == null || bo.getStudentId() == 0L) {
            throw new ServiceException("参数错误");
        }
        List<StudentLiveOldVo> studentLiveOldList = studentLiveOldMapper.selectStudentLiveOldListEcharts(bo);

        Map<String, Object> map = new HashMap<>();
        List<Object> x = new ArrayList<>();
        List<Object> y1 = new ArrayList<>();
        List<Object> y2 = new ArrayList<>();
        String sg = "0";
        String tz = "0";

        if (studentLiveOldList != null && studentLiveOldList.size() > 0) {
            for (int i = 0; i < studentLiveOldList.size(); i++) {
                StudentLiveOldVo liveOld = studentLiveOldList.get(i);
                if(StringUtils.isEmpty(liveOld.getHeight()) || StringUtils.isEmpty(liveOld.getWeight())){
                    continue;
                }
                if (!sg.equals(liveOld.getHeight()) || !tz.equals(liveOld.getWeight())) {
                    sg = liveOld.getHeight();
                    tz = liveOld.getWeight();
                    x.add(DateUtils.dateTime(liveOld.getUpdateTime()));
                    y1.add(liveOld.getHeight());
                    y2.add(liveOld.getWeight());
                }
            }
        }

        map.put("X", x);
        map.put("Y1", y1);
        map.put("Y2", y2);
        return R.ok("成功", map);
    }
}
