
package org.sky.scientific.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.mp.base.BaseServiceImpl;
import org.sky.core.tool.utils.BeanUtil;
import org.sky.core.tool.utils.DateUtil;
import org.sky.core.tool.utils.StringUtil;
import org.sky.scientific.excel.TechnicianExcel;
import org.sky.scientific.mapper.TechnicianMapper;
import org.sky.scientific.pojo.dto.TechnicianDTO;
import org.sky.scientific.pojo.entity.TechnicianEntity;
import org.sky.scientific.pojo.vo.TechnicianVO;
import org.sky.scientific.service.ITechnicianService;
import org.sky.system.cache.DictCache;
import org.sky.system.pojo.enums.DictEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 技术人员花名册 服务实现类
 *
 * @author sky
 * @since 2025-06-10
 */
@Service
public class TechnicianServiceImpl extends BaseServiceImpl<TechnicianMapper, TechnicianEntity> implements ITechnicianService {

	@Override
	public boolean saveTechnician(TechnicianEntity technician) {
		//检查重复性
		check(technician);
		technician.setYearAndMonth(technician.getYearAndMonth());
		return this.save(technician);
	}

	@Override
	public boolean updateTechnician(TechnicianEntity technician) {
		return this.updateById(technician);
	}

	@Override
	public IPage<TechnicianVO> selectTechnicianPage(IPage<TechnicianVO> page, TechnicianDTO technician) {
		return page.setRecords(baseMapper.selectTechnicianPage(page, technician));
	}


	@Override
	public List<TechnicianExcel> exportTechnician(TechnicianDTO technician) {
		List<TechnicianExcel> technicianList = baseMapper.exportTechnicianList(technician);
		technicianList.forEach(temp -> {
			temp.setGenderName(DictCache.getValue(DictEnum.SEX, temp.getGender()));
		});
		return technicianList;
	}

	@Transactional
	@Override
	public void importTechnician(List<TechnicianExcel> data, String yearAndMonth) {
		for (TechnicianExcel excel : data) {
			TechnicianEntity technician = Objects.requireNonNull(BeanUtil.copyProperties(excel, TechnicianDTO.class));
			//转换 性别字典
			technician.setGender(Integer.valueOf(DictCache.getKey(DictEnum.SEX, excel.getGenderName())));
			technician.setYearAndMonth(yearAndMonth);
			this.saveTechnician(technician);
		}
	}

	@Transactional
	@Override
	public boolean fetchLastMonthData(TechnicianEntity technician) {
		//查询上个月的数据
		//得到上个月的年月
		String lastDate = DateUtil.format(DateUtil.minusMonths(DateUtil.parse(technician.getYearAndMonth(), "yyyyMM"), 1), "yyyyMM");
		List<TechnicianEntity> lastMonthList = this.baseMapper.selectByYearAndMonth(lastDate);
		//与本月数据对比，去重后保存至数据库
		for (TechnicianEntity temp : lastMonthList) {
			temp.setYearAndMonth(technician.getYearAndMonth());
			temp.setId(null);
			saveTechnician(temp);
		}
		return true;
	}

	private void check(TechnicianEntity technician) {
		if (StringUtil.isBlank(technician.getYearAndMonth())) {
			technician.setYearAndMonth(DateUtil.format(DateUtil.now(), "yyyyMM"));
		}
		//根据 年月 和工号或身份证 查询是否存在，存在则本次导入所有的数据都不成功
		Integer count = this.baseMapper.countByDateAndNumberOrIdentityCard(technician);
		if (count > 0) {
			throw new ServiceException("导入失败，姓名：[" + technician.getName() + "]已存在，请检查数据");
		}
	}
}
