
package org.sky.scientific.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.sky.core.excel.util.ExcelUtil;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.mp.base.BaseServiceImpl;
import org.sky.core.tool.utils.DateUtil;
import org.sky.core.tool.utils.Func;
import org.sky.scientific.excel.YsRyrgfyExcel;
import org.sky.scientific.mapper.TechnicianMapper;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.mapper.YsRyrgfyMapper;
import org.sky.scientific.pojo.entity.TechnicianEntity;
import org.sky.scientific.pojo.entity.YsRyrgfyEntity;
import org.sky.scientific.pojo.vo.XmVO;
import org.sky.scientific.pojo.vo.YsRyrgfyVO;
import org.sky.scientific.service.IYsRyrgfyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 项目预算-人员人工费用 服务实现类
 *
 * @author Kd
 * @since 2025-06-29
 */
@Slf4j
@AllArgsConstructor
@Service
public class YsRyrgfyServiceImpl extends BaseServiceImpl<YsRyrgfyMapper, YsRyrgfyEntity> implements IYsRyrgfyService {

	private final TechnicianMapper technicianMapper;
	private final XmMapper xmMapper;

	@Override
	public IPage<YsRyrgfyVO> selectYsRyrgfyPage(IPage<YsRyrgfyVO> page, YsRyrgfyVO ysRyrgfy) {
		List<YsRyrgfyVO> dataList = baseMapper.selectYsRyrgfyPage(page, ysRyrgfy);
		for (YsRyrgfyVO vo : dataList) {
			vo.getTechnician().setForeign(!"在职".equals(vo.getTechnician().getNature()));
		}
		return page.setRecords(dataList);
	}

	@SneakyThrows
	@Override
	@Transactional
	public void importRyrgfy(MultipartFile file, Long xmId) {
		if (Func.isNull(xmId)) {
			throw new ServiceException("请选择研发项目");
		}
		XmVO xmVO = xmMapper.selectByXmId(xmId);
		if (Func.isNull(xmVO)) {
			throw new ServiceException("研发项目不存在");
		}

		//查询项目开始时间对应月份的花名册
		List<TechnicianEntity> dbTechnicianList = technicianMapper.selectList(Wrappers.<TechnicianEntity>lambdaQuery().eq(TechnicianEntity::getYearAndMonth, DateUtil.format(xmVO.getXmkssj(), DateUtil.PATTERN_YYYYMM)));

		if (Func.isEmpty(dbTechnicianList)) {
			throw new ServiceException(xmVO.getXmkssj() + "花名册无数据");
		}

		List<YsRyrgfyExcel> dataList = ExcelUtil.read(file, 0, 3, YsRyrgfyExcel.class);

		List<YsRyrgfyEntity> dbYsList = baseMapper.selectList(Wrappers.<YsRyrgfyEntity>lambdaQuery().eq(YsRyrgfyEntity::getXmId, xmId));

		List<YsRyrgfyEntity> insertList = new ArrayList<>();
		int batchCount = 20;
		for (YsRyrgfyExcel excel : dataList) {
			YsRyrgfyEntity entity = new YsRyrgfyEntity();
			entity.setXmId(xmId);
			List<Long> list = dbTechnicianList.stream()
				.filter(technician -> Func.equals(technician.getNumber(), excel.getNumber()) && Func.equals(technician.getIdCard(), excel.getIdCard()))
				.map(TechnicianEntity::getId).toList();
			if (Func.isEmpty(list)) {
				throw new ServiceException("工号[" + excel.getName() + "]身份证[" + excel.getIdCard() + "]不存在");
			}

			if(dbYsList.stream().anyMatch(ys -> Func.equals(ys.getTechnicianId(), list.get(0)))){
				throw new ServiceException("序号["+excel.getXh()+"]的人员已在预算表中");
			}
			entity.setTechnicianId(list.get(0));
			entity.setZzsc(excel.getZzsc());
			entity.setPjrgfy(excel.getPjrgfy());
			insertList.add(entity);

			if (insertList.size() % batchCount == 0 && this.saveBatch(insertList)) {
				insertList.clear();
			}
		}

		if (Func.isNotEmpty(insertList)) {
			this.saveBatch(insertList);
		}
	}

	@Override
	public boolean saveRyrgfy(YsRyrgfyEntity ysRyrgfy) {
		//花名册是否已添加
		List<YsRyrgfyEntity> dbList = baseMapper.selectList(Wrappers.<YsRyrgfyEntity>lambdaQuery().eq(YsRyrgfyEntity::getXmId, ysRyrgfy.getXmId()));
		List<Long> dbTechnicianIdList = dbList.stream().map(YsRyrgfyEntity::getTechnicianId).toList();
		if (dbTechnicianIdList.contains(ysRyrgfy.getTechnicianId())) {
			throw new ServiceException("人员已在预算表中");
		}

		return this.save(ysRyrgfy);
	}

	@Override
	public boolean physicalDeleteByIds(List<Long> ids) {
		return baseMapper.physicalDeleteByIds(ids) > 0;
	}

	@Override
	public boolean updateRyrgfy(YsRyrgfyEntity ysRyrgfy) {
		List<YsRyrgfyEntity> dbList = baseMapper.selectList(Wrappers.<YsRyrgfyEntity>lambdaQuery().eq(YsRyrgfyEntity::getXmId, ysRyrgfy.getXmId()));
		for (YsRyrgfyEntity entity : dbList) {
			if(Func.equals(entity.getTechnicianId(), ysRyrgfy.getTechnicianId()) && !Func.equals(entity.getId(), ysRyrgfy.getId())){
				throw new ServiceException("人员已在预算表中");
			}
		}
		return this.updateById(ysRyrgfy);
	}

	@Override
	public List<YsRyrgfyExcel> exportYsRyrgfy(Wrapper<YsRyrgfyEntity> queryWrapper) {
		List<YsRyrgfyExcel> ysRyrgfyList = baseMapper.exportYsRyrgfy(queryWrapper);
		//ysRyrgfyList.forEach(ysRyrgfy -> {
		//	ysRyrgfy.setTypeName(DictCache.getValue(DictEnum.YES_NO, YsRyrgfy.getType()));
		//});
		return ysRyrgfyList;
	}
}
