package com.weaver.seconddev.meishi.esb.util;

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.common.form.dto.data.FormDataDetailDto;
import com.weaver.common.form.dto.data.FormDataDto;
import com.weaver.common.hrm.util.HrmCommonUtil;
import com.weaver.framework.rpc.annotation.RpcReference;
import com.weaver.seconddev.meishi.esb.dao.CreatePerformanceDao;
import com.weaver.teams.domain.user.SimpleEmployee;
import com.weaver.workflow.core.api.rest.flow.entity.operate.WfcRequestOperateParamDataEntity;
import com.weaver.workflow.core.api.rest.flow.entity.operate.WfcRequestOperationResultDto;
import com.weaver.workflow.core.api.rest.publicapi.WfcRequestOperateRest;
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.Component;

import java.text.SimpleDateFormat;
import java.util.*;


@Component
public class CreatePerformanceUtil {

    private final static Logger log = LoggerFactory.getLogger(CreatePerformanceUtil.class);

    @RpcReference
    WfcRequestOperateRest wfcRequestOperateRest;

    @Autowired
    CreatePerformanceDao createPerformanceDao;

    @Autowired
    private HrmCommonUtil hrmCommonUtil;

    public Map<String,Object>  doCreateWorkflowByUserData(String tenant_key,Long form_id,Map<String,Object> mainData,Map<String,Object> createrData,String form_data_id,Long workflowid,Long userid){
        Map<String, Object> reslutMap = new HashMap<String, Object>();

        try{
            Map<String,Object> mainFieldMap = createPerformanceDao.queryMainTableField(tenant_key,form_id);

            // t.id,t.xm,t.bm,k.name as deptname,k.parent as sjbm,t2.bmfzr as sjbmfzr,t3.bmcj

            Long tjr  = Long.valueOf(String.valueOf(createrData.get("xm")));
            String tjrbm = String.valueOf(createrData.get("bm"));
            String tjrbmmc = String.valueOf(createrData.get("deptname"));
            String tjrxm = String.valueOf(createrData.get("username"));

            String bmcj = String.valueOf(createrData.get("bmcj"));
            String bmfzr = String.valueOf(createrData.get("bmfzr"));

            String sjbm = String.valueOf(createrData.get("sjbm"));
            String sjbmfzr = String.valueOf(createrData.get("sjbmfzr"));

            String location = String.valueOf(createrData.get("location"));

            String zq = String.valueOf(mainData.get("zq"));
            String tbnf = String.valueOf(mainData.get("rq_urvy"));

            log.error("tjr:"+tjr);
            log.error("tjrbm:"+tjrbm);
            log.error("tjrxm:"+tjrxm);

            String zc = createPerformanceDao.queryDepartmentLevel(tenant_key,tjr,tjrbm);
            log.error("zc:"+zc);
            String bmcjmc = "";

            String sfbmfzr = "";
            if(StringUtils.isNotBlank(zc)){
                sfbmfzr = "0";
                if("0".equals(zc)){
                    bmcjmc = "总裁";
                }else if("1".equals(zc)){
                    bmcjmc = "本部长";
                }else if("2".equals(zc)){
                    bmcjmc = "部长";
                }else if("3".equals(zc)){
                    bmcjmc = "高级经理";
                }else if("4".equals(zc)){
                    bmcjmc = "经理";
                }
            }else{
                sfbmfzr = "1";
            }

            List<FormDataDetailDto> formDataDetailDtos = getFormDataDetailDtosList(mainFieldMap,tbnf,zq,bmcj,String.valueOf(tjr),tjrbm,sjbm,sjbmfzr,form_data_id,zc,sfbmfzr,bmfzr,location);
            log.error("doCreateWorkflowByTopLeader:formDataDetailDtos:" + formDataDetailDtos.size());

            SimpleEmployee tjrEmployee = hrmCommonUtil.getSimpleEmployee(tjr);
            reslutMap = createWorkflow(tenant_key,form_id, workflowid,tjrEmployee,bmcjmc,tjrbmmc,formDataDetailDtos,tjrxm);

        }catch (Exception e){
            log.error("doCreateWorkflowByTopLeader:e:"+e);
        }
        return reslutMap;
    }

    public Map<String,Object> createWorkflow(String tenant_key,Long formId, Long workflowId, SimpleEmployee tjrEmployee,String bmcjmc,String tjrbmmc, List<FormDataDetailDto> formDataDetailDtos,String tjrxm){

        Map<String,Object> requestMap = new HashMap<String,Object>();
        try{

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String currentDate = sdf.format(new Date());

//            目标分解+部门名称+职称+日期年月日 ，非部门负责人流程标题为：目标分解+部门名称+日期年月日
            String requestName = "目标分解-"+tjrbmmc+"-"+currentDate;
            if(StringUtils.isNotBlank(bmcjmc)){
                requestName = "目标分解-"+tjrbmmc+"-"+bmcjmc+"-"+currentDate ;
            }

            log.error("requestName:"+requestName);

            WfcRequestOperateParamDataEntity wfcRequestOperateParamDataEntity = new WfcRequestOperateParamDataEntity();
            wfcRequestOperateParamDataEntity.setWorkflowId(workflowId);         //审批流id  必填
            wfcRequestOperateParamDataEntity.setIsNextFlow(1);
            wfcRequestOperateParamDataEntity.setRequestName(requestName);
            // 表单设计器页开发者工具 通过此接口获取字段id   /api/workflow/core/form/builder/getFormLayout

            //表单数据
            FormDataDto formDataDto = new FormDataDto();
            formDataDto.setFormId(formId);   //表单id 必填
            formDataDto.setDataDetails(formDataDetailDtos);

            log.error("tjrEmployee:"+tjrEmployee.getEmployeeId());
            log.error("tjrEmployee2:"+tjrEmployee.getUserId());

            WeaResult<WfcRequestOperationResultDto> flow = wfcRequestOperateRest.createFlow(wfcRequestOperateParamDataEntity, tjrEmployee, formDataDto);
            int code = flow.getCode();
            String msg = flow.getMsg();

            log.error("getExceptionFlowLog:"+flow.getData().getExceptionFlowLog());
            log.error("getResultMessage:"+flow.getData().getResultMessage());
            log.error("createWorkflow:code:"+code);
            log.error("createWorkflow:msg:"+msg);

            log.error("getOutErrorCode:"+flow.getOutErrorCode().toString());

            Long requestid = flow.getData().getRequestId();
            log.error("createWorkflow:requestid:"+requestid);


            if(requestid > 0){
                requestMap.put("code",code);
                requestMap.put("msg",msg);
                requestMap.put("requestid",requestid);

            }else{
                requestMap.put("code",code);
                requestMap.put("msg","requestid小于等于0");
            }

        }catch (Exception e){
            requestMap.put("code",500);
            requestMap.put("msg","创建流程接口异常");
            requestMap.put("requestid",-1);
            e.printStackTrace();
            log.error("e:"+e);
        }
        return requestMap;
    }


    public List<FormDataDetailDto> getFormDataDetailDtosList(Map<String,Object> fieldMap,String tbnf,String zq,String bmlx,String tjr,String tjrbm,String sjbm,String sjbmfzr,String form_data_id,String zc,String sfbmfzr,String bmfzr,String location){
        List<FormDataDetailDto> formDataDetailDtos = new ArrayList<FormDataDetailDto>();

        //'tjr','bm','yjtbnf','zq','bmlx','sjbm','sjbmfzr','zlc'

        String tjrFieldid = String.valueOf(fieldMap.get("tjr"));
        log.error("getFormDataDetailDtosList:tjrFieldid:"+tjrFieldid);
        log.error("getFormDataDetailDtosList:tjr:"+tjr);
        if(StringUtils.isNotBlank(tjrFieldid)){
            Long tjr_FieldId = Long.valueOf(tjrFieldid);
            if(tjr_FieldId > 0 && StringUtils.isNotBlank(String.valueOf(tjr))){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(tjr_FieldId);
                filed.setContent(String.valueOf(tjr));
                formDataDetailDtos.add(filed);
            }
        }

        String tjrbmFieldid = String.valueOf(fieldMap.get("bm"));

        log.error("getFormDataDetailDtosList:tjrbmFieldid:"+tjrbmFieldid);
        log.error("getFormDataDetailDtosList:tjrbm:"+tjrbm);

        if(StringUtils.isNotBlank(tjrbmFieldid)){
            Long tjrbm_FieldId = Long.valueOf(tjrbmFieldid);
            if(tjrbm_FieldId > 0 && StringUtils.isNotBlank(tjrbm)){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(tjrbm_FieldId);
                filed.setContent(tjrbm);
                formDataDetailDtos.add(filed);
            }
        }

        String tbnfFieldid = String.valueOf(fieldMap.get("yjtbnf"));

        log.error("getFormDataDetailDtosList:tbnfFieldid:"+tbnfFieldid);
        log.error("getFormDataDetailDtosList:tbnf:"+tbnf);

        if(StringUtils.isNotBlank(tbnfFieldid)){
            Long tbnf_FieldId = Long.valueOf(tbnfFieldid);
            if(tbnf_FieldId > 0 && StringUtils.isNotBlank(tbnf)){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(tbnf_FieldId);
                filed.setContent(tbnf);
                formDataDetailDtos.add(filed);
            }
        }


        String zqFieldid = String.valueOf(fieldMap.get("zq"));

        log.error("getFormDataDetailDtosList:zqFieldid:"+zqFieldid);
        log.error("getFormDataDetailDtosList:zq:"+zq);

        if(StringUtils.isNotBlank(zqFieldid)){
            Long zq_FieldId = Long.valueOf(zqFieldid);
            if(zq_FieldId > 0 && StringUtils.isNotBlank(zq)){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(zq_FieldId);
                filed.setContent(zq);
                formDataDetailDtos.add(filed);
            }
        }


        String bmlxFieldid = String.valueOf(fieldMap.get("bmlx"));
        log.error("getFormDataDetailDtosList:bmlxFieldid:"+bmlxFieldid);
        log.error("getFormDataDetailDtosList:bmlx:"+bmlx);
        if(StringUtils.isNotBlank(bmlxFieldid)){
            Long bmlx_FieldId = Long.valueOf(bmlxFieldid);
            if(bmlx_FieldId > 0 && StringUtils.isNotBlank(bmlx)){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(bmlx_FieldId);
                filed.setContent(bmlx);
                formDataDetailDtos.add(filed);
            }
        }

        String sjbmFieldid = String.valueOf(fieldMap.get("sjbm"));
        log.error("getFormDataDetailDtosList:sjbmFieldid:"+sjbmFieldid);
        log.error("getFormDataDetailDtosList:sjbm:"+sjbm);
        if(StringUtils.isNotBlank(sjbmFieldid)){
            Long sjbm_FieldId = Long.valueOf(sjbmFieldid);
            if(sjbm_FieldId > 0 && StringUtils.isNotBlank(sjbm)){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(sjbm_FieldId);
                filed.setContent(sjbm);
                formDataDetailDtos.add(filed);
            }
        }

        String sjbmfzrFieldid = String.valueOf(fieldMap.get("sjbmfzr"));
        log.error("getFormDataDetailDtosList:sjbmfzrFieldid:"+sjbmfzrFieldid);
        log.error("getFormDataDetailDtosList:sjbmfzr:"+sjbmfzr);
        if(StringUtils.isNotBlank(sjbmfzrFieldid)){
            Long sjbmfzr_FieldId = Long.valueOf(sjbmfzrFieldid);
            if(sjbmfzr_FieldId > 0 && StringUtils.isNotBlank(sjbmfzr)){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(sjbmfzr_FieldId);
                filed.setContent(sjbmfzr);
                formDataDetailDtos.add(filed);
            }
        }

        String zlcFieldid = String.valueOf(fieldMap.get("zlc"));
        log.error("getFormDataDetailDtosList:zlcFieldid:"+zlcFieldid);
        log.error("getFormDataDetailDtosList:zlc:"+form_data_id);
        if(StringUtils.isNotBlank(zlcFieldid)){
            Long zlc_FieldId = Long.valueOf(zlcFieldid);
            if(zlc_FieldId > 0 && StringUtils.isNotBlank(form_data_id)){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(zlc_FieldId);
                filed.setContent(form_data_id);
                formDataDetailDtos.add(filed);
            }
        }

        String zcFieldid = String.valueOf(fieldMap.get("zc"));
        log.error("getFormDataDetailDtosList:zcFieldid:"+zcFieldid);
        log.error("getFormDataDetailDtosList:zc:"+zc);
        if(StringUtils.isNotBlank(zcFieldid)){
            Long zc_FieldId = Long.valueOf(zcFieldid);
            if(zc_FieldId > 0 && StringUtils.isNotBlank(zc)){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(zc_FieldId);
                filed.setContent(zc);
                formDataDetailDtos.add(filed);
            }
        }


        String sfbmfzrFieldid = String.valueOf(fieldMap.get("sfbmfzr"));
        log.error("getFormDataDetailDtosList:sfbmfzrFieldid:"+sfbmfzrFieldid);
        log.error("getFormDataDetailDtosList:sfbmfzr:"+sfbmfzr);
        if(StringUtils.isNotBlank(sfbmfzrFieldid)){
            Long sfbmfzr_FieldId = Long.valueOf(sfbmfzrFieldid);
            if(sfbmfzr_FieldId > 0 && StringUtils.isNotBlank(sfbmfzr)){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(sfbmfzr_FieldId);
                filed.setContent(sfbmfzr);
                formDataDetailDtos.add(filed);
            }
        }


        String bmfzrFieldid = String.valueOf(fieldMap.get("ryxz_3ezm"));
        log.error("getFormDataDetailDtosList:bmfzrFieldid:"+bmfzrFieldid);
        log.error("getFormDataDetailDtosList:bmfzr:"+bmfzr);
        if(StringUtils.isNotBlank(bmfzrFieldid)){
            Long bmfzr_FieldId = Long.valueOf(bmfzrFieldid);
            if(bmfzr_FieldId > 0 && StringUtils.isNotBlank(bmfzr)){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(bmfzr_FieldId);
                filed.setContent(bmfzr);
                formDataDetailDtos.add(filed);
            }
        }


        String bgddFieldid = String.valueOf(fieldMap.get("bgdd"));
        log.error("getFormDataDetailDtosList:bgddFieldid:"+bgddFieldid);
        log.error("getFormDataDetailDtosList:location:"+location);
        if(StringUtils.isNotBlank(bgddFieldid)){
            Long bgdd_FieldId = Long.valueOf(bgddFieldid);
            if(bgdd_FieldId > 0 && StringUtils.isNotBlank(location)){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(bgdd_FieldId);
                filed.setContent(location);
                formDataDetailDtos.add(filed);
            }
        }

        return formDataDetailDtos;
    }
}
