package org.sky.scientific.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import lombok.extern.slf4j.Slf4j;
import org.sky.core.tool.utils.DateUtil;
import org.sky.core.tool.utils.Func;
import org.sky.scientific.excel.TechnicianExcel;
import org.sky.scientific.utils.ImportResult;
import org.sky.scientific.utils.ValidationUtils;
import org.sky.system.cache.DictCache;
import org.sky.system.pojo.entity.Dict;
import org.sky.system.pojo.enums.DictEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
public class TechnicianImportListener extends BaseReadListener {

	@Override
	public void invoke(Map<Integer, String> map, AnalysisContext context) {
		int rowIndex = context.readRowHolder().getRowIndex() + 1;

		ImportResult<Object> result = convertAndValidate(map, rowIndex);
		resultList.add(result);
		if (result.isSuccess()) {
			dataList.add(result.getData());
			incrementSuccessCount();
		} else {
			incrementErrorCount();
		}
	}

	private ImportResult<Object> convertAndValidate(Map<Integer, String> data, int rowIndex) {

		List<String> errors = new ArrayList<>();

		TechnicianExcel excel = new TechnicianExcel();
		excel.setXh(data.get(0));
		excel.setName(data.get(1));
		excel.setNumber(data.get(2));
		excel.setIdCard(data.get(3));

		if(Func.isBlank(excel.getNumber()) && Func.isBlank(excel.getIdCard())){
			errors.add("工号和身份证号不能都为空");
		}

		String sexName = data.get(4);
		List<String> sexList = DictCache.getList(DictEnum.SEX.getName()).stream().map(Dict::getDictValue).toList();
		if (Func.isNotBlank(sexName)) {
			if(Func.isNotEmpty(sexList) && !sexList.contains(sexName)){
				errors.add("性别错误，请重新选择");
			}else {
				excel.setGender(Integer.valueOf(DictCache.getKey(DictEnum.SEX, sexName)));
			}
		}
		String birthday = data.get(5);
		if (Func.isNotBlank(birthday) && !ValidationUtils.isValidDateFormat(birthday, DateUtil.PATTERN_DATE)) {
			errors.add("出生日期为空或格式错误，yyyy-MM-dd");
		}
		excel.setBirthday(birthday);

		excel.setEthnic(data.get(6));
		excel.setDepartment(data.get(7));
		excel.setPost(data.get(8));
		excel.setPostLocation(data.get(9));
		String nature = data.get(10);
		List<String> natureList = DictCache.getList("yonggongxingzhi").stream().map(Dict::getDictValue).toList();
		if (Func.isNotBlank(nature) && Func.isNotEmpty(natureList) && !natureList.contains(nature)) {
			errors.add("用工性质错误，请重新选择");
		}
		excel.setNature(nature);

		String hireDate = data.get(11);
		if (Func.isNotBlank(hireDate) && !ValidationUtils.isValidDateFormat(hireDate, DateUtil.PATTERN_DATE)) {
			errors.add("入职时间为空或格式错误，yyyy-MM-dd");
		}
		excel.setHireDate(hireDate);

		String education = data.get(12);
		List<String> educationList = DictCache.getList("xueli").stream().map(Dict::getDictValue).toList();
		if (Func.isNotBlank(education) && Func.isNotEmpty(educationList) && !educationList.contains(education)) {
			errors.add("最高学历错误，请重新选择");
		}
		excel.setEducation(education);

		excel.setDegree(data.get(13));
		excel.setSchoolName(data.get(14));
		excel.setMajor(data.get(15));

		String rank = data.get(16);
		if (Func.isNotBlank(rank)) {
			List<String> rankList = DictCache.getList("zhicheng").stream().map(Dict::getDictValue).toList();
			if (Func.isNotEmpty(rankList) && !rankList.contains(rank.trim())) {
				errors.add("职称错误，请重新选择");
			}
			excel.setRank(rank);
		}

		excel.setPracticingRequirement(data.get(17));
		excel.setRemark(data.get(18));

		String situation = data.get(19);
		List<String> situationList = DictCache.getList("canyanqingkuang").stream().map(Dict::getDictValue).toList();
		if (Func.isNotBlank(situation) && Func.isNotEmpty(situationList) && !situationList.contains(situation)) {
			errors.add("参研情况错误，请重新选择");
		}
		excel.setSituation(situation);

		String personnelType = data.get(20);
		List<String> personnelTypeList = DictCache.getList("renyuanleixing").stream().map(Dict::getDictValue).toList();
		if (Func.isNotBlank(personnelType) && Func.isNotEmpty(personnelTypeList) && !personnelTypeList.contains(personnelType)) {
			errors.add("人员类型错误，请重新选择");
		}
		excel.setPersonnelType(personnelType);

		errors.addAll(ValidationUtils.validate(excel));
		if (!errors.isEmpty()) {
			return ImportResult.error(rowIndex, errors, data.toString());
		}
		return ImportResult.success(rowIndex, excel);
	}
}
