/*
 *    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.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.mapper.TAttaInfoMapper;
import com.yifu.cloud.plus.v1.yifu.archives.mapper.TEmpDisabilityInfoMapper;
import com.yifu.cloud.plus.v1.yifu.archives.mapper.TEmployeeInfoMapper;
import com.yifu.cloud.plus.v1.yifu.archives.service.TEmpDisabilityInfoService;
import com.yifu.cloud.plus.v1.yifu.archives.vo.DisabilitySearchVo;
import com.yifu.cloud.plus.v1.yifu.archives.vo.EmpDisabilityExcelVO;
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.ErrorMessage;
import com.yifu.cloud.plus.v1.yifu.common.core.util.MsgUtils;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.mybatis.base.BaseEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 员工伤残信息表
 *
 * @author fxj
 * @date 2022-06-18 09:39:46
 */
@RequiredArgsConstructor
@Service
public class TEmpDisabilityInfoServiceImpl extends ServiceImpl<TEmpDisabilityInfoMapper, TEmpDisabilityInfo> implements TEmpDisabilityInfoService {

	private final TAttaInfoMapper attaInfoMapper;

	private final TEmployeeInfoMapper employeeInfoMapper;
	/**
	 * @Author fxj
	 * @Description  员工伤残信息导入
	 * @Date 10:41 2022/6/18
	 * @Param
	 * @return
	**/
	@Override
	public void importTmpDisability(List<EmpDisabilityExcelVO> excelVOList, List<ErrorMessage> errorMessageList) {
		// 个性化校验逻辑
		List<TEmpDisabilityInfo> list = this.list();
		TEmployeeInfo emp;
		TEmpDisabilityInfo  exist;
		ErrorMessage errorMsg;
		// 执行数据插入操作 组装
		for (int i = 0; i < excelVOList.size(); i++) {
			// 已有验证报错直接下一个
			EmpDisabilityExcelVO excel = excelVOList.get(i);
			emp = employeeInfoMapper.selectOne(Wrappers.<TEmployeeInfo>query().lambda()
					.eq(TEmployeeInfo::getEmpIdcard,excel.getEmpIdcard())
					.eq(TEmployeeInfo::getDeleteFlag,CommonConstants.ZERO_STRING)
					.last(CommonConstants.LAST_ONE_SQL));
			errorMsg = checkRes(list, emp, excel);
			if (Common.isNotNull(errorMsg)){
				errorMessageList.add(errorMsg);
				continue;
			}
			exist = baseMapper.selectOne(Wrappers.<TEmpDisabilityInfo>query().lambda()
					.eq(TEmpDisabilityInfo::getEmpIdcard,excel.getEmpIdcard())
					.eq(TEmpDisabilityInfo::getDisabilityName,excel.getDisabilityName())
					.eq(TEmpDisabilityInfo::getDeleteFlag,CommonConstants.ZERO_STRING)
					.last(CommonConstants.LAST_ONE_SQL));
			if (Common.isNotNull(exist)){
				errorMessageList.add(new ErrorMessage(excel.getRowIndex(),MsgUtils.getMessage(ErrorCodes.ARCHIVES_EMP_DISABILITY_NAME_EXISTING)));
			}
			// 数据合法情况
			// 数据合法情况
			insertExcel(excel,emp);
			errorMessageList.add(new ErrorMessage(excel.getRowIndex(),CommonConstants.SAVE_SUCCESS));
		}
	}
	private ErrorMessage checkRes(List<TEmpDisabilityInfo> list, TEmployeeInfo emp,EmpDisabilityExcelVO excel) {
		if (Common.isEmpty(emp)){
			return new ErrorMessage(excel.getRowIndex(),MsgUtils.getMessage(ErrorCodes.ARCHIVES_EMP_NOT_EXIST));
		}
		if (CommonConstants.ONE_STRING.equals(emp.getFileStatus())){
			return new ErrorMessage(excel.getRowIndex(),MsgUtils.getMessage(ErrorCodes.ARCHIVES_EMP_REDUCED));
		}
		// 检查添加是否已经存在
		boolean match = list.stream().anyMatch(info -> excel.getDisabilityName().equals(info.getDisabilityName())
				&& excel.getEmpIdcard().equals(info.getEmpIdcard()));

		if (match) {
			return new ErrorMessage(excel.getRowIndex(),MsgUtils.getMessage(ErrorCodes.ARCHIVES_EMP_DISABILITY_NAME_EXISTING));
		}
		return null;
	}
	@Override
	public R<Boolean> saveDiy(TEmpDisabilityInfo tEmpDisabilityInfo) {
		tEmpDisabilityInfo.setDeleteFlag(CommonConstants.ZERO_STRING);
		int res  = baseMapper.insert(tEmpDisabilityInfo);
		if (res > CommonConstants.ZERO_INT){
			//更新附件信息
			List<String> ids;
			if (Common.isNotNull(tEmpDisabilityInfo.getAttaIds())){
				ids = Common.initStrToList(tEmpDisabilityInfo.getAttaIds(), CommonConstants.COMMA_STRING);
				attaInfoMapper.updateDomainId(tEmpDisabilityInfo.getId(),ids);
			}
			return R.ok();
		}else {
			return R.failed();
		}
	}

	@Override
	public IPage<TEmpDisabilityInfo> pageDiy(Page page, DisabilitySearchVo searchVo) {
		LambdaQueryWrapper<TEmpDisabilityInfo> wrapper = buildQueryWrapper(searchVo);
		wrapper.orderByDesc(BaseEntity::getCreateTime);
		return baseMapper.selectPage(page,wrapper);
	}

	@Override
	public List<TEmpDisabilityInfo> getTEmpDisabilityInfoNoPage(DisabilitySearchVo searchVo) {
		LambdaQueryWrapper<TEmpDisabilityInfo> wrapper = buildQueryWrapper(searchVo);
		List<String> idList = Common.getList(searchVo.getIds());
		if (Common.isNotNull(idList)){
			wrapper.in(TEmpDisabilityInfo::getId,idList);
		}
		return baseMapper.selectList(wrapper);
	}

	private LambdaQueryWrapper buildQueryWrapper(DisabilitySearchVo entity){
		LambdaQueryWrapper<TEmpDisabilityInfo> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(TEmpDisabilityInfo::getDeleteFlag,CommonConstants.ZERO_STRING);
		if (ArrayUtil.isNotEmpty(entity.getCreateTimes())) {
			wrapper.ge(TEmpDisabilityInfo::getCreateTime, entity.getCreateTimes()[0])
					.le(TEmpDisabilityInfo::getCreateTime,
							entity.getCreateTimes()[1]);
		}
		if (Common.isNotNull(entity.getEmpName())){
			wrapper.likeRight(TEmpDisabilityInfo::getEmpName,entity.getEmpName());
		}
		if (Common.isNotNull(entity.getEmpIdcard())){
			wrapper.likeRight(TEmpDisabilityInfo::getEmpIdcard,entity.getEmpIdcard());
		}
		if (Common.isNotNull(entity.getCreateName())){
			wrapper.eq(TEmpDisabilityInfo::getCreateName,entity.getCreateName());
		}
		return wrapper;
	}

	/**
	 * 插入excel
	 */
	private void insertExcel(EmpDisabilityExcelVO excel, TEmployeeInfo emp) {
		TEmpDisabilityInfo insert = new TEmpDisabilityInfo();
		BeanUtil.copyProperties(excel, insert);
		insert.setDeleteFlag(CommonConstants.ZERO_STRING);
		insert.setEmpCode(emp.getEmpCode());
		insert.setEmpId(emp.getId());
		this.save(insert);
	}
}
