package cn.timer.api.utils.router.business;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.timer.api.bean.zzgl.ZzglBmgwM;
import cn.timer.api.config.exception.CustomException;
import cn.timer.api.controller.zzgl.service.ZzglBmgwMService;
import cn.timer.api.dto.spmk.FromData;
import cn.timer.api.dto.zzgl.UpEmpDeptDto;
import lombok.Builder;

/**
 * 调岗-业务
 * @author Tang
 *
 */

@Service
public class TransferPositionBusiness extends SpmkAssoBusiness {

	@Autowired
	ZzglBmgwMService zzglBmgwMService;
	
	@Value("${config-8timer.environmental-science}")
	public String environmental_science;
	
	public void Logoutput(String science) {
		
		if(!("pro").equals(environmental_science)) {
			
			System.out.println(science);
		}else {
			System.out.println("");
		}
		
	}
	
	@Override
	public void handleApprove(JSONObject jsonObj) {
		// TODO Auto-generated method stub
		// 发起人企业id
		Integer orgCode = ObjectUtil.isNull(jsonObj.get("orgCode")) ? null : Convert.toInt(jsonObj.get("orgCode"));
		// 发起人id
		Integer id = jsonObj.getInt("id");
		
		// 申请人
		String applicant = ObjectUtil.isNull(jsonObj.get("__applicant",FromData.class)) ? null : jsonObj.get("__applicant",FromData.class).getValue();
		// 申请原因
		String ReasonForApplication = ObjectUtil.isNull(jsonObj.get("__ReasonForApplication",FromData.class)) ? null : jsonObj.get("__ReasonForApplication",FromData.class).getValue();
		// 调入部门
		Integer TransferInDepartment = ObjectUtil.isNull(jsonObj.get("__TransferInDepartment",FromData.class)) ? null : Convert.toInt(jsonObj.get("__TransferInDepartment",FromData.class).getValue());
		
		/* __TransferInPosition */
		if(!("").equals(jsonObj.get("__TransferInDepartment",FromData.class).getValue())) {
			// 调入岗位
			Integer TransferInPosition = ObjectUtil.isNull(jsonObj.get("__TransferInDepartment",FromData.class)) ? null : Integer.parseInt(jsonObj.get("__TransferInDepartment",FromData.class).getValue());
		}
		
		if (TransferInDepartment == null) {
			throw new CustomException("部门或岗位 未填写,审批失败");
		}
		// 生效日期
		String effectiveDate = ObjectUtil.isNull(jsonObj.get("__effectiveDate",FromData.class)) ? null : jsonObj.get("__effectiveDate",FromData.class).getValue();
		// 备注
		String Remarks = ObjectUtil.isNull(jsonObj.get("Remarks",FromData.class)) ? null : jsonObj.get("Remarks",FromData.class).getValue();
		// 上传文件
		String UploadAttachment = ObjectUtil.isNull(jsonObj.get("UploadAttachment",FromData.class)) ? null : jsonObj.get("UploadAttachment",FromData.class).getValue();
		
		UpEmpDeptDto upEmpDeptDto = UpEmpDeptDto.builder()
				.dpetId(TransferInDepartment)
				.isdg(1)
				.build();
		
		Logoutput("调岗:"+upEmpDeptDto);
		
		ZzglBmgwM bmg = ZzglBmgwM.builder().build().selectOne(new QueryWrapper<ZzglBmgwM>().lambda().eq(ZzglBmgwM::getOrgCode, orgCode).eq(ZzglBmgwM::getLeader, id));
		if(bmg != null) {
			ZzglBmgwM.builder().id(bmg.getId()).leader(0).build().updateById();
		}
		
		// 调 员工管理 业务
		zzglBmgwMService.applicationTransferPosition(id, orgCode, upEmpDeptDto);
	}

}