package com.weaver.seconddev.chapanda.beisen.esb;

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.common.hrm.cache.HrmDepartmentComInfo;
import com.weaver.common.hrm.manage.HrmComInfoCacheHandler;
import com.weaver.common.hrm.remote.department.HrmRemoteDepartmentService;
import com.weaver.common.hrm.service.HrmCommonDepartmentService;
import com.weaver.common.hrm.util.HrmCommonUtil;
import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.framework.rpc.annotation.RpcReference;
import com.weaver.seconddev.chapanda.beisen.constant.Constants;
import com.weaver.seconddev.chapanda.beisen.dao.HrmDepartmentDao;
import com.weaver.seconddev.chapanda.beisen.entity.BeisenDepartmentDto;
import com.weaver.seconddev.chapanda.beisen.entity.BeisenStaffDto;
import com.weaver.seconddev.chapanda.beisen.util.*;
import com.weaver.teams.domain.department.SimpleDepartment;
import com.weaver.workflow.list.api.rest.publicapi.WflRequestListRest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.weaver.teams.domain.user.SimpleEmployee;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("Esb2BeisenJobRequirementAction")
public class Esb2BeisenJobRequirementAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2BeisenJobRequirementAction.class);

    @Autowired
    JobRequirement2BeisenUtil jobRequirement2BeisenUtil;

    @Autowired
    Token2BeiSenUtil token2BeiSenUtil ;

    @Autowired
    DatabaseUtils databaseUtils;

    @Autowired
    BeisenStaffInfoByStaffCodeUtil beisenStaffInfoByStaffCodeUtil ;

    @Autowired
    private HrmCommonUtil hrmCommonUtil;

    @Autowired
    private HrmDepartmentDao hrmDepartmentDao;

    @Autowired
    BeisenQueryUseridUtil beisenQueryUseridUtil;

    @Autowired
    BeisenQueryDeptIdUtil beisenQueryDeptIdUtil;
    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        log.error("Esb2BeisenJobRequirementAction start");

        int requirementStatus = converInteger(params,"requirementStatus");
        String form_data_id = (String) params.get("form_data_id");
        int requirementType = converInteger(params,"requirementType");

        String requestname = (String) params.get("requestname");
        String createDate = (String) params.get("createDate");

        int requirementCount = converInteger(params,"requirementCount") ;
        String arivalTime = (String) params.get("arivalTime");

        int salaryType = converInteger(params,"salaryType");
        int category = converInteger(params,"category");
        String educationInfo = (String) params.get("educationInfo");
        String workExperience = (String) params.get("workExperience");
        int kind = converInteger(params,"kind");
        boolean isSecrecy = false;
        String isSecrecyStr = (String) params.get("isSecrecy");
        if("1".equals(isSecrecyStr)){
            isSecrecy = true;
        }

        String originalId = String.valueOf(params.get("originalId"));

        String nlyq = String.valueOf(params.get("nlyq"));

        log.error("requirementStatus:" + requirementStatus);
        log.error("originalId:" + originalId);
        log.error("requirementType:" + requirementType);
        log.error("requestname:" + requestname);
        log.error("createDate:" + createDate);
        log.error("requirementCount:" + requirementCount);
        log.error("arivalTime:" + arivalTime);
        log.error("salaryType:" + salaryType);
        log.error("category:" + category);
        log.error("educationInfo:" + educationInfo);
        log.error("workExperience:" + workExperience);
        log.error("kind:" + kind);
        log.error("isSecrecy:" + isSecrecy);
        log.error("form_data_id:" + form_data_id);
        log.error("nlyq:" + nlyq);

        String createBy = String.valueOf(params.get("createBy"));
        String dutyUserId = String.valueOf(params.get("dutyUserId"));
        log.error("createBy:" + createBy);
        log.error("dutyUserId:" + dutyUserId);



        String zpgw = String.valueOf(params.get("zpgw"));
        String zwmc = String.valueOf(params.get("zwmc"));
//        String createByEmail = hrmDepartmentDao.queryEmailByEmployeeId(dutyUserId);
//        log.error("createByEmail:" + createByEmail);
//        String dutyUserEmail = hrmDepartmentDao.queryEmailByEmployeeId(dutyUserId);
//        log.error("dutyUserEmail:" + dutyUserEmail);

        String originalCode = "";
        if(StringUtils.isNotBlank(originalId)){
            originalCode = hrmDepartmentDao.getDepartmentCodeById(originalId);
        }
        log.error("originalCode:" + originalCode);

        String createByEmail = "";
        int beisenCreateUserId = -1;
        if(StringUtils.isNotBlank(createBy)){
            String jobnum = "";
            SimpleEmployee createByEmployee = hrmCommonUtil.getSimpleEmployee(Long.valueOf(createBy));
            if(createByEmployee !=null && createByEmployee.getId() > 0){
                jobnum = createByEmployee.getJobNum();
                createByEmail = createByEmployee.getEmail();
            }
            log.error("jobnum:" + jobnum);
            if(StringUtils.isNotBlank(jobnum)){
                String userid = beisenQueryUseridUtil.queryBeisenUserIdByJobNum(jobnum);
                if(StringUtils.isNotBlank(userid)){
                    beisenCreateUserId = Integer.valueOf(userid);
                }
                log.error("beisenCreateUserId:" + beisenCreateUserId);
            }
        }
        log.error("createByEmail:" + createByEmail);


        String dutyUserEmail = "";
        int beisenDutyUserId = -1;
        if(StringUtils.isNotBlank(dutyUserId)){
            String jobnum = "";
            SimpleEmployee dutyEmployee = hrmCommonUtil.getSimpleEmployee(Long.valueOf(dutyUserId));
            if(dutyEmployee !=null && dutyEmployee.getId() > 0){
                jobnum = dutyEmployee.getJobNum();
                dutyUserEmail = dutyEmployee.getEmail();
            }
            log.error("jobnum:" + jobnum);
            if(StringUtils.isNotBlank(jobnum)){
                String userid = beisenQueryUseridUtil.queryBeisenUserIdByJobNum(jobnum);
                if(StringUtils.isNotBlank(userid)){
                    beisenDutyUserId = Integer.valueOf(userid);
                }
                log.error("beisenDutyUserId:" + beisenDutyUserId);
            }
        }
        log.error("dutyUserEmail:" + dutyUserEmail);

        int orgId = -1;
        if(StringUtils.isNotBlank(originalCode)){
//            BeisenDepartmentDto beisenDepartmentDto = beisenDeptInfoByOriginalIdUtil.getBeisenDeptDataByOriginalId(originalId);
//            log.error("beisenDepartmentDto:{}",beisenDepartmentDto.toString());
            String deptid = beisenQueryDeptIdUtil.queryBeisenDeptIdByCode(originalCode);
            if(StringUtils.isNotBlank(deptid)){
                orgId = Integer.valueOf(deptid);
            }
//            orgId = beisenDepartmentDto.getOrgId();
        }

        String xqmc = (String) params.get("xqmc");
        String gzzz = String.valueOf(params.get("gzzz"));
        String rzzg  = String.valueOf(params.get("rzzg "));
        String zxssyf = String.valueOf(params.get("zxssyf"));
        String jjcd = String.valueOf(params.get("jjcd"));

        log.error("orgId:" + orgId);
        String dataId = jobRequirement2BeisenUtil.sendJobRequirement(requirementStatus,requirementType,
                requestname,beisenCreateUserId,
                createDate,requirementCount,
                arivalTime,
                createByEmail,
                dutyUserEmail,
                orgId,
                salaryType,
                category,
                educationInfo,
                workExperience,
                kind,
                isSecrecy,
                originalCode,nlyq,zpgw,zwmc,beisenDutyUserId,xqmc,gzzz,rzzg,zxssyf,jjcd);

        log.error("dataId:" + dataId);
//        if(StringUtils.isNotBlank(dataId)){
//            String sourceType = "";
//            String groupId = "";
//
//            String dataSql =" update uf_zpxqgl set bszxid = ? " +
//                            " where form_data_id = ?\n" +
//                            " and tenant_key=? \n" +
//                            " and delete_type=0 \n" ;
//
//            log.error("dataSql:" + dataSql);
//            List<String> paramList = new ArrayList<>(100);
//
//            paramList.add(form_data_id);
//            paramList.add(Constants.TENANT_KEY);
//            List<SqlParamEntity> sqlParamList = databaseUtils.querySqlParamEntity(paramList);
//            result = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList);
//        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","Esb2BeisenJobRequirementAction");
        actionMap.put("dataId",dataId);

        return WeaResult.success(actionMap);
    }


    public int converInteger(Map<String,Object> params,String key){
        int value = -1;
        try{
            String requirementStatusStr = (String) params.get(key);
            value = Integer.valueOf(requirementStatusStr);
        }catch (Exception e){
            log.error("key:{},e:{}",key,e.getMessage());
        }
        return value;
    }
}
