package com.yida.data.user.config;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.util.StrUtil;
import com.yida.data.common.core.entity.Dict;
import com.yida.data.common.core.entity.user.EduStudent;
import com.yida.data.common.excel.DefaultExportExcelClient;
import com.yida.data.common.service.CommonService;
import com.yida.data.user.dto.PageStudentDTO;
import com.yida.data.user.dto.StudentExportDTO;
import com.yida.data.user.excel.SelectStudentExportData;
import com.yida.data.user.service.EduStudentService;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Objects;

/**
 * 学生信息导出处理类
 *
 * @author ZYJ
 * @date 2023/12/12 15:00
 */
public class ExportStudentClient
        extends DefaultExportExcelClient<SelectStudentExportData, StudentExportDTO, EduStudent, EduStudentService> {

    public ExportStudentClient(EduStudentService eduStudentService, RedisService redisService, CommonService commonService,
                               String exportCacheKey, String uploadUrl) {
        super(eduStudentService, redisService, commonService, exportCacheKey, uploadUrl);
    }

    @Override
    public List<EduStudent> selectExportData(SelectStudentExportData selectStudentExportData) {
        // 查询数据
        PageStudentDTO dto = new PageStudentDTO();
        BeanUtils.copyProperties(selectStudentExportData, dto);
        return this.baseService.selectStudent(dto);
    }

    @Override
    public StudentExportDTO handleSingleData(EduStudent o) {
        StudentExportDTO exportDTO = new StudentExportDTO();
        BeanUtils.copyProperties(o, exportDTO);
        // 采集状态
        String collectStatus = "未采集";
        if (Objects.nonNull(o.getCollectType()) && o.getCollectType() == 1) {
            collectStatus = "已采集";
        }
        exportDTO.setCollectStatus(collectStatus);
        // 班级信息
        if (StrUtil.isNotBlank(o.getCampusName()) && StrUtil.isNotBlank(o.getSectionName())
                && StrUtil.isNotBlank(o.getGradeName()) && StrUtil.isNotBlank(o.getClassName())) {
            exportDTO.setDeptName(o.getCampusName() + "-" + o.getSectionName() + "-" +
                    o.getGradeName() + "-" + o.getClassName());
        }
        // 政治面貌
        Dict politicsStatus = this.commonService.getDictByTypeAndValueOrLabel("politics_status", null, o.getPoliticsStatus());
        if (Objects.nonNull(politicsStatus)) {
            exportDTO.setPoliticsStatus(politicsStatus.getValue());
        } else {
            exportDTO.setPoliticsStatus("");
        }
        // 学生性别
        String sex = o.getStuSex();
        if (StrUtil.isEmpty(sex)) {
            exportDTO.setStuSex("");
        } else {
            exportDTO.setStuSex("0".equals(sex) ? "男" : "女");
        }
        // 民族
        Dict nation = this.commonService.getDictByTypeAndValueOrLabel("nation", null, o.getNation());
        if (Objects.nonNull(nation)) {
            exportDTO.setNation(nation.getValue());
        } else {
            exportDTO.setNation("");
        }
        // 学生类型
        Integer type = o.getType();
        if (Objects.nonNull(type)) {
            exportDTO.setType(type == 0 ? "走读" : "住宿");
        }
        // 学籍状态
        Dict studentStatus = this.commonService.getDictByTypeAndValueOrLabel("student_status", null, o.getStudentStatus());
        if (Objects.nonNull(studentStatus)) {
            exportDTO.setStudentStatus(studentStatus.getValue());
        } else {
            exportDTO.setStudentStatus("");
        }
        return exportDTO;
    }
}
