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.constant.Constants;
import com.weaver.seconddev.meishi.esb.dao.PerformanceSubmitDao;
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 PerformanceSubmitUtil {

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

    @RpcReference
    WfcRequestOperateRest wfcRequestOperateRest;

    @Autowired
    PerformanceSubmitDao performanceSubmitDao;

    @Autowired
    private HrmCommonUtil hrmCommonUtil;


    public List<Map<String,Object>>  doCreateWorkflowByTopLeader(String tenant_key,Long form_id,Map<String,Object> mainData,String form_data_id,Long workflowid,String dt2_form_id,Long userid,String bmcj,String sfzdcf){
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

        try{
            Map<String,Object> mainFieldMap = performanceSubmitDao.queryMainTableField(tenant_key,form_id);
            Map<String,Object> dt2FieldMap = performanceSubmitDao.queryDetailTableField(tenant_key,form_id,dt2_form_id);
            String sqrbm = String.valueOf(mainData.get("bm"));
            List<Map<String, Object>> deptLeaderList = performanceSubmitDao.queryCreateUserByFormDataId(tenant_key,bmcj,sqrbm);
            log.error("doCreateWorkflowByTopLeader:deptLeaderList:"+deptLeaderList.size());
            for(int i=0;i<deptLeaderList.size();i++) {
                Map<String, Object> deptLeaderMap = deptLeaderList.get(i);
                Long tjr  = Long.valueOf(String.valueOf(deptLeaderMap.get("id")));
                String tjrbm = String.valueOf(deptLeaderMap.get("department"));

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

                List<FormDataDetailDto> formDataDetailDtos = getFormDataDetailDtosList(mainFieldMap, mainData, bmcj, sfzdcf,String.valueOf(tjr),tjrbm,sjbm,sjbmfzr);
                log.error("doCreateWorkflowByTopLeader:formDataDetailDtos:" + formDataDetailDtos.size());

                List<Map<String, Object>> ktList = performanceSubmitDao.queryFormtableDt2ByTopLeader(Constants.TENANT_KEY, form_data_id);
                log.error("doCreateWorkflowByTopLeader:ktList:" + ktList.size());

                for (int n = 0; n < ktList.size(); n++) {
                    Map<String, Object> ktMap = ktList.get(n);
                    String kt = String.valueOf(ktMap.get("kt"));
                    String ktFieldid = String.valueOf(dt2FieldMap.get("kt"));
                    log.error("doCreateWorkflowByTopLeader:ktFieldid:" + ktFieldid);
                    if (StringUtils.isNotBlank(ktFieldid)) {
                        Long kt_FieldId = Long.valueOf(ktFieldid);
                        if (kt_FieldId > 0) {
                            FormDataDetailDto filed = new FormDataDetailDto();
                            filed.setFieldId(kt_FieldId);
                            filed.setContent(kt);
                            filed.setDataIndex(Long.valueOf(n + 1));
                            formDataDetailDtos.add(filed);
                        }
                    }

                    String qz = String.valueOf(ktMap.get("qz"));
                    String qzFieldid = String.valueOf(dt2FieldMap.get("qz"));

                    log.error("doCreateWorkflowByTopLeader:qzFieldid:" + qzFieldid);
                    if (StringUtils.isNotBlank(qzFieldid)) {
                        Long qz_FieldId = Long.valueOf(qzFieldid);
                        if (qz_FieldId > 0) {
                            FormDataDetailDto filed = new FormDataDetailDto();
                            filed.setFieldId(qz_FieldId);
                            filed.setContent(qz);
                            filed.setDataIndex(Long.valueOf(n + 1));
                            formDataDetailDtos.add(filed);
                        }
                    }
                }
                SimpleEmployee cjrEmploye = hrmCommonUtil.getSimpleEmployee(tjr);
                Map<String, Object> reslutMap = createWorkflow(form_id, workflowid, cjrEmploye, formDataDetailDtos);
                list.add(reslutMap);
            }
        }catch (Exception e){
            log.error("doCreateWorkflowByTopLeader:e:"+e);
        }
        return list;
    }

    /**
     *
     * @param tenant_key
     * @param workflowid
     * @param userid
     * @param form_data_id
     * @param form_id
     * @param dt2_form_id
     * @return
     */
    public List<Map<String,Object>> doCreateWorkflowByMiddleLeader(String tenant_key,Long form_id,Map<String,Object> mainData,String form_data_id,Long workflowid,String dt2_form_id,Long userid,String bmcj,String sfzdcf){

        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

        try{
            Map<String,Object> mainFieldMap = performanceSubmitDao.queryMainTableField(tenant_key,form_id);
            Map<String,Object> dt2FieldMap = performanceSubmitDao.queryDetailTableField(tenant_key,form_id,dt2_form_id);;

            Map<String,List<String>> workflowCreatorMap = performanceSubmitDao.queryFormtableDt2(tenant_key,form_data_id);
            log.error("doCreateWorkflowByMiddleLeader:workflowCreatorMap:"+workflowCreatorMap.size());

            for(Map.Entry<String, List<String>> entry : workflowCreatorMap.entrySet()){
                String tjrbm = entry.getKey();
                List<String> ktList = entry.getValue();

                log.error("doCreateWorkflowByMiddleLeader:bmid:"+tjrbm);
                log.error("doCreateWorkflowByMiddleLeader:ktList:"+ktList.size());

                Map<String,Object> tjrData = performanceSubmitDao.queryDeptManagerByDeptid(tenant_key,tjrbm);

//                bmfzr,k.parent sjbm,t2.bmfzr sjbmfzr

                String tjr = String.valueOf(tjrData.get("bmfzr"));
                String sjbm = String.valueOf(tjrData.get("sjbm"));
                String sjbmfzr = String.valueOf(tjrData.get("sjbmfzr"));

                List<FormDataDetailDto> formDataDetailDtos = getFormDataDetailDtosList(mainFieldMap,mainData,bmcj,sfzdcf,tjr,tjrbm,sjbm,sjbmfzr);
                log.error("doCreateWorkflowByMiddleLeader:formDataDetailDtos:"+formDataDetailDtos.size());

                for(int n = 0;n<ktList.size();n++){
                    String kt = ktList.get(n);
                    String ktFieldid = String.valueOf(dt2FieldMap.get("kt"));
                    if(StringUtils.isNotBlank(ktFieldid)){
                        Long kt_FieldId = Long.valueOf(ktFieldid);
                        if(kt_FieldId > 0){
                            FormDataDetailDto filed = new FormDataDetailDto();
                            filed.setFieldId(kt_FieldId);                //姓名 (mx)
                            filed.setContent(kt);
                            filed.setDataIndex(Long.valueOf(n+1));
                            formDataDetailDtos.add(filed);
                        }
                    }
                }
                SimpleEmployee cjrEmploye = hrmCommonUtil.getSimpleEmployee(Long.valueOf(tjr));
                Map<String,Object> reslutMap = createWorkflow(form_id,workflowid,cjrEmploye,formDataDetailDtos);
                list.add(reslutMap);
            }
        }catch (Exception e){
            log.error("doCreateWorkflowByMiddleLeader:e:"+e);
        }

        return list;
    }


    public Map<String,Object> createWorkflow(Long formId, Long workflowId, SimpleEmployee cjrEmployee, List<FormDataDetailDto> formDataDetailDtos){

        Map<String,Object> requestMap = new HashMap<String,Object>();
        try{
            String username = cjrEmployee.getUsername();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String currentDate = sdf.format(new Date());

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

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

            //无法创建带有明细表的审批流
            WeaResult<WfcRequestOperationResultDto> flow = wfcRequestOperateRest.createFlow(wfcRequestOperateParamDataEntity, cjrEmployee, formDataDto);
            int code = flow.getCode();
            String msg = flow.getMsg();
            Long reuestid = flow.getData().getRequestId();

            requestMap.put("code",code);
            requestMap.put("msg",msg);
            requestMap.put("requestid",reuestid);

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


    public List<FormDataDetailDto>  getFormDataDetailDtosList(Map<String,Object> fieldMap,Map<String,Object> mainData,String bmlx,String sfzdcf,String tjr,String tjrbm,String sjbm,String sjbmfzr){
        List<FormDataDetailDto> formDataDetailDtos = new ArrayList<FormDataDetailDto>();
        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){
                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){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(tjrbm_FieldId);
                filed.setContent(tjrbm);
                formDataDetailDtos.add(filed);
            }
        }

        String tbnfFieldid = String.valueOf(fieldMap.get("yjtbnf"));
        String tbnf = String.valueOf(mainData.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){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(tbnf_FieldId);
                filed.setContent(tbnf);
                formDataDetailDtos.add(filed);
            }
        }


        String zqFieldid = String.valueOf(fieldMap.get("zq"));
        String zq = String.valueOf(mainData.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){
                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){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(bmlx_FieldId);
                filed.setContent(bmlx);
                formDataDetailDtos.add(filed);
            }
        }


        String sfzdcfFieldid = String.valueOf(fieldMap.get("sfzdcf"));
        log.error("getFormDataDetailDtosList:sfzdcfFieldid:"+sfzdcfFieldid);
        log.error("getFormDataDetailDtosList:sfzdcf:"+sfzdcf);

        if(StringUtils.isNotBlank(sfzdcfFieldid)){
            Long sfzdcf_FieldId = Long.valueOf(sfzdcfFieldid);
            if(sfzdcf_FieldId > 0){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(sfzdcf_FieldId);
                filed.setContent(sfzdcf);
                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){
                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){
                FormDataDetailDto filed = new FormDataDetailDto();
                filed.setFieldId(sjbmfzr_FieldId);
                filed.setContent(sjbmfzr);
                formDataDetailDtos.add(filed);
            }
        }

        return formDataDetailDtos;
    }
}
