
package org.sky.scientific.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.secure.utils.AuthUtil;
import org.sky.core.tool.utils.DateUtil;
import org.sky.core.tool.utils.StringUtil;
import org.sky.scientific.mapper.TechnicianMapper;
import org.sky.scientific.pojo.entity.SalaryEntity;
import org.sky.scientific.pojo.entity.TechnicianEntity;
import org.sky.scientific.pojo.vo.SalaryVO;
import org.sky.scientific.excel.SalaryExcel;
import org.sky.scientific.mapper.SalaryMapper;
import org.sky.scientific.service.ISalaryService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.sky.core.mp.base.BaseServiceImpl;
import java.util.List;

/**
 * 技术人员工资表 服务实现类
 *
 * @author Kd
 * @since 2025-06-19
 */
@Service
public class SalaryServiceImpl extends BaseServiceImpl<SalaryMapper, SalaryEntity> implements ISalaryService {

	@Resource
	private TechnicianMapper technicianMapper;

	@Override
	public IPage<SalaryVO> selectSalaryPage(IPage<SalaryVO> page, SalaryVO salary) {
		List<SalaryVO> voList = baseMapper.selectSalaryPage(page, salary);
		//如果数据空，则查询该年度整体是否有数据
//		if (voList.isEmpty()) {
//			LambdaQueryWrapper<SalaryEntity> wrapper = Wrappers.<SalaryEntity>query().lambda()
//				.eq(SalaryEntity::getYearAndMonth, salary.getYearAndMonth())
//				.eq(SalaryEntity::getTenantId, AuthUtil.getTenantId())
//				.eq(SalaryEntity::getIsDeleted, 0);
//			if (baseMapper.selectCount(wrapper) == 0) {
//				 //将本年度技术人员基础数据插入工资表，工资数据为空
//
//			}
//		}
		return page.setRecords(voList);
	}


	@Override
	public List<SalaryExcel> exportSalary(Wrapper<SalaryEntity> queryWrapper) {
		List<SalaryExcel> salaryList = baseMapper.exportSalary(queryWrapper);
		//salaryList.forEach(salary -> {
		//	salary.setTypeName(DictCache.getValue(DictEnum.YES_NO, Salary.getType()));
		//});
		return salaryList;
	}

	@Override
	public boolean saveSalary(SalaryEntity salary) {

		Integer affectNum = 0;
		if (StringUtil.isBlank(salary.getYearAndMonth())) {
			salary.setYearAndMonth(DateUtil.format(DateUtil.now(), "yyyy"));
		}
		//从技术人员花名册中根据年份和人员信息,查询人员是否存在
		TechnicianEntity technician = new TechnicianEntity();
		technician.setYearAndMonth(salary.getYearAndMonth());
		technician.setNumber(salary.getNumber());
		technician.setIdentityCard(salary.getIdentityCard());
		technician.setTenantId(AuthUtil.getTenantId());
		Integer countWithTechnician = technicianMapper.countByDateAndNumberOrIdentityCard(technician);
		//不存在则抛出异常
		if(countWithTechnician == 0){
			throw new ServiceException("[" + salary.getYearAndMonth() + "]年度内无人员[" + salary.getName() + "、" + salary.getNumber() + "、" + salary.getIdentityCard() + "]");
		}else {
			//存在则根据年份和人员信息查询工资是否存在
			LambdaQueryWrapper<SalaryEntity> wrapper = Wrappers.<SalaryEntity>query().lambda()
				.eq(SalaryEntity::getYearAndMonth, salary.getYearAndMonth())
				.eq(SalaryEntity::getNumber, salary.getNumber())
				.eq(SalaryEntity::getIdentityCard, salary.getIdentityCard())
				.eq(SalaryEntity::getTenantId, AuthUtil.getTenantId())
				.eq(SalaryEntity::getIsDeleted, 0);
			//存在则覆盖
			if (baseMapper.selectCount(wrapper) > 0) {
				LambdaUpdateWrapper<SalaryEntity> updateWrapper = Wrappers.<SalaryEntity>update().lambda()
					.eq(SalaryEntity::getYearAndMonth, salary.getYearAndMonth())
					.eq(SalaryEntity::getNumber, salary.getNumber())
					.eq(SalaryEntity::getIdentityCard, salary.getIdentityCard())
					.eq(SalaryEntity::getTenantId, AuthUtil.getTenantId())
					.eq(SalaryEntity::getIsDeleted, 0);
				affectNum = baseMapper.update(salary, updateWrapper);
			}else {
				//不存在则新增
				affectNum = baseMapper.insert(salary);
			}
		}
		return affectNum > 0;
	}

}
