/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yifu4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.yifu.cloud.plus.v1.yifu.archives.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.plugin.excel.vo.ErrorMessage;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TEmpDisabilityInfo;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TEmployeeInfo;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TEmployeeLog;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TEmployeeProject;
import com.yifu.cloud.plus.v1.yifu.archives.mapper.TEmployeeProjectMapper;
import com.yifu.cloud.plus.v1.yifu.archives.service.TEmployeeInfoService;
import com.yifu.cloud.plus.v1.yifu.archives.service.TEmployeeLogService;
import com.yifu.cloud.plus.v1.yifu.archives.service.TEmployeeProjectService;
import com.yifu.cloud.plus.v1.yifu.archives.vo.EmpDisabilityExcelVO;
import com.yifu.cloud.plus.v1.yifu.archives.vo.EmployeeProjectVO;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.exception.ErrorCodes;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.MsgUtils;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.*;

/**
 * 项目档案表
 *
 * @author huyc
 * @date 2022-06-20 09:19:40
 */
@Service
public class TEmployeeProjectServiceImpl extends ServiceImpl<TEmployeeProjectMapper, TEmployeeProject> implements TEmployeeProjectService {

	private TEmployeeInfoService tEmployeeInfoService;

	private TEmployeeLogService tEmployeeLogService;

	private final String content1 = "复档";

	private final String content2 = "复项";

	private final String content3 = "复档复项";

	private final String content4 = "减档减项";

	@Override
	public R addCheck(TEmployeeProject tEmployeeProject) {

		Map<String,Object> map = new HashMap<>();

		//身份证系统中是否已有
		TEmployeeInfo tEmployeeInfo = tEmployeeInfoService.getOne(Wrappers.<TEmployeeInfo>query().lambda()
				.eq(TEmployeeInfo::getEmpIdcard, tEmployeeProject.getEmpIdcard())
				.eq(TEmployeeInfo::getDeleteFlag, CommonConstants.STATUS_NORMAL));

		//项目下已有身份证信息
		TEmployeeProject employeeProject = this.getOne(Wrappers.<TEmployeeProject>query().lambda()
				.eq(TEmployeeProject::getEmpIdcard,tEmployeeProject.getEmpIdcard())
				.eq(TEmployeeProject::getDeptNo,tEmployeeProject.getDeptNo())
				.eq(TEmployeeProject::getDeleteFlag,CommonConstants.STATUS_NORMAL));
		if (!Common.isEmpty(employeeProject)) {

			//身份证实名认证
			if (!tEmployeeProject.getEmpName().equals(tEmployeeInfo.getEmpName())) {
				return R.failed("姓名和身份证不一致，请确认后再次新增");
			}
			//项目状态为已减项
			if (CommonConstants.dingleDigitIntArray[1] == employeeProject.getProjectStatus()) {
				if (!Common.isEmpty(tEmployeeInfo)) {
					map.put("empProId",tEmployeeProject.getId());
					if (CommonConstants.STATUS_NORMAL.equals(tEmployeeInfo.getFileStatus())) {
						return R.ok(map, "该人员已减项，是否“复项”");
					} else {
						map.put("employeeId", tEmployeeInfo.getId());
						return R.ok(map, "该人员已减档减项，是否“复档复项”");
					}
				}
			}
			//状态为草稿、已审核
			if (CommonConstants.dingleDigitIntArray[0] == employeeProject.getStatus() ||
					CommonConstants.dingleDigitIntArray[1] == employeeProject.getStatus()) {
				return R.failed("人员已有该项目信息，禁止重复添加！");
			}
		} else {
			if (Common.isEmpty(tEmployeeInfo)) {
				return R.failed("该人员暂无档案信息，禁止关联项目，如要添加，请先至“在档人员”处建档");
			}
			//判断是否已经减档
			if (CommonConstants.ONE_STRING.equals(tEmployeeInfo.getFileStatus())) {
				map.put("employeeId", tEmployeeInfo.getId());
				return R.ok(null,"该人员在离职库已存在，是否“恢复档案”");
			}
		}
		return R.ok();
	}

	@Override
	public R reEmpInfo(JSONObject jsonObject) {
		if (Common.isNotNull(jsonObject)) {
			String empId = jsonObject.getString("empId");
			String empProId = jsonObject.getString("empProId");
			if (!Common.isEmpty(empId) && Common.isEmpty(empProId)) {
				//复档
				TEmployeeInfo tEmployeeInfo = tEmployeeInfoService.getById(empId);
				tEmployeeInfo.setFileStatus(CommonConstants.dingleDigitIntArray[0]);
				tEmployeeInfoService.updateById(tEmployeeInfo);
				addEmpLog(CommonConstants.dingleDigitIntArray[0],empId,"",content1);
			} else if (Common.isEmpty(empId) && !Common.isEmpty(empProId)) {
				//复项
				TEmployeeProject tEmployeeProject = this.getById(empProId);
				tEmployeeProject.setProjectStatus(CommonConstants.dingleDigitIntArray[0]);
				this.updateById(tEmployeeProject);
				addEmpLog(CommonConstants.dingleDigitIntArray[1],"",empProId,content2);
			} else {
				//复档复项
				TEmployeeInfo tEmployeeInfo = tEmployeeInfoService.getById(empId);
				TEmployeeProject tEmployeeProject = this.getById(empProId);
				tEmployeeInfo.setFileStatus(CommonConstants.dingleDigitIntArray[0]);
				tEmployeeInfoService.updateById(tEmployeeInfo);
				tEmployeeProject.setProjectStatus(CommonConstants.dingleDigitIntArray[0]);
				this.updateById(tEmployeeProject);
				addEmpLog(CommonConstants.dingleDigitIntArray[2],empId,empProId,content3);
			}
		} else {
			return R.failed("传参为空！");
		}
		return R.ok();
	}

	@Override
	public R deleteEmpPro(TEmployeeProject tEmployeeProject) {
		if (Common.isNotNull(tEmployeeProject)) {
			TEmployeeInfo tEmployeeInfo = tEmployeeInfoService.getById(tEmployeeProject.getEmpId());
			if (tEmployeeProject.getIsLeaveEmployee()== CommonConstants.dingleDigitIntArray[1]) {
				Long count = this.count(Wrappers.<TEmployeeProject>query().lambda()
						.eq(TEmployeeProject::getEmpId,tEmployeeProject.getEmpId())
						.eq(TEmployeeProject::getProjectStatus,CommonConstants.dingleDigitIntArray[0]));
				if (count.compareTo(CommonConstants.DEPT_TREE_ROOT_ID) >0) {
					return R.failed("该人员存在其他进行中的项目，禁止同步减档！");
				}
				tEmployeeInfo.setFileStatus(CommonConstants.dingleDigitIntArray[1]);
				tEmployeeInfoService.updateById(tEmployeeInfo);
			}
			tEmployeeProject.setProjectStatus(CommonConstants.dingleDigitIntArray[1]);
			this.updateById(tEmployeeProject);
			addEmpLog(CommonConstants.dingleDigitIntArray[2],tEmployeeInfo.getId(),tEmployeeProject.getId(),content4);
			return R.ok();
		}
		return R.failed("传参为空！");
	}

	/**
	 * 新增档案操作记录
	 * @param type
	 * @param empId
	 * @param empProId
	 * @param content
	 * @author huyc
	 * @date 2022-06-20 17:19:40
	 */
	public void addEmpLog(int type, String empId, String empProId, String content) {
		TEmployeeLog tEmployeeLog = new TEmployeeLog();
		tEmployeeLog.setEmpId(empId);
		tEmployeeLog.setType(type);
		tEmployeeLog.setProjectId(empProId);
		tEmployeeLog.setContent(content);
		tEmployeeLogService.save(tEmployeeLog);
	}

	/**
	 * 新增档案操作记录
	 * @param excelVOList
	 * @param bindingResult
	 * @param
	 * @author huyc
	 * @date 2022-06-20 18:52:16
	 */
	@Override
	public R<List<ErrorMessage>> importListAdd(List<EmployeeProjectVO> excelVOList, BindingResult bindingResult, String idAdd) {
		// 通用校验获取失败的数据
		List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();

		// 个性化校验逻辑
		List<TEmployeeProject> list = this.list();

		// 执行数据插入操作 组装 PostDto
		for (int i = 0; i < excelVOList.size(); i++) {
			EmployeeProjectVO excel = excelVOList.get(i);
			Set<String> errorMsg = new HashSet<>();


			errorMessageList.add(new ErrorMessage((long) (i + 2), errorMsg));
		}
		if (CollUtil.isNotEmpty(errorMessageList)) {
			return R.failed(errorMessageList);
		}
		return R.ok();
	}

	/**
	 * 插入excel Post
	 */
	private void insertExcelPost(EmployeeProjectVO excel) {
		TEmployeeProject insert = new TEmployeeProject();
		BeanUtil.copyProperties(excel, insert);
		this.save(insert);
	}
}
