package com.weaver.seconddev.meishi.service.impl;

import com.weaver.seconddev.meishi.constant.Constants;
import com.weaver.seconddev.meishi.esb.dao.PerformanceSubmitDao;
import com.weaver.seconddev.meishi.esb.util.PerformanceSubmitUtil;
import com.weaver.seconddev.meishi.service.PerformanceSubmitService;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PerformanceSubmitServiceImpl implements PerformanceSubmitService {

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

    @Autowired
    private PerformanceSubmitUtil performanceSubmitUtil;

    @Autowired
    PerformanceSubmitDao performanceSubmitDao;

    @Override
    public Map<String, Object> createWorkflowV1(String tenant_key,Map<String,Object> params) {
        Map<String, Object> dataMap = new HashMap<String,Object>();

        String dt2_form_id = String.valueOf(params.get("dt2_form_id"));
        Long form_id = Long.valueOf(String.valueOf(params.get("form_id")));
        Long workflowid = Long.valueOf(String.valueOf(params.get("workflowid")));
        String form_data_id = String.valueOf(params.get("form_data_id"));
        Long userid = Long.valueOf(String.valueOf(params.get("userid")));

        log.error("createWorkflowV1:form_id:"+form_id);
        log.error("createWorkflowV1:workflowid:"+workflowid);
        log.error("createWorkflowV1:form_data_id:"+form_data_id);
        log.error("createWorkflowV1:userid:"+userid);

        String requestids = "";
        int errcount = 0;
        try {
            String sfzdcf = "0";

            List<Map<String, Object>> reslutList = new ArrayList<Map<String, Object>>();
            Map<String,Object> mainData = performanceSubmitDao.queryMainDataByFormDataId(Constants.TENANT_KEY,form_data_id);
            String bmlx = String.valueOf(mainData.get("bmlx"));
            log.error("createWorkflowV1:bmlx:"+bmlx);
            if("0".equals(bmlx)){ //董事长
                String bmcj = "1";
                reslutList = performanceSubmitUtil.doCreateWorkflowByTopLeader(Constants.TENANT_KEY,form_id,mainData,form_data_id,workflowid,dt2_form_id,userid,bmcj,sfzdcf);
                log.error("reslutList1:"+reslutList.size());
            }else{
                String bmcj = "";
                if("1".equals(bmlx)){ // 本部长
                    bmcj = "2";
                }else if("2".equals(bmlx)){ // 部长
                    bmcj = "3";
                }else if("3".equals(bmlx)){ // 科
                    bmcj = "4";
                }

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

                if(StringUtils.isNotBlank(bmcj)){
                    reslutList = performanceSubmitUtil.doCreateWorkflowByMiddleLeader(Constants.TENANT_KEY, form_id,mainData,form_data_id,workflowid,dt2_form_id,userid,bmcj,sfzdcf);
                }

                log.error("reslutList2:"+reslutList.size());
            }

            for (int i = 0; i < reslutList.size(); i++) {
                Map<String, Object> reslutMap = reslutList.get(i);
                if (reslutMap.containsKey("code")) {
                    String code = String.valueOf(reslutMap.get("code"));
                    log.error("code:"+code);
                    if ("200".equals(code)) {
                        String requestid = String.valueOf(reslutMap.get("requestid"));
                        requestids += StringUtils.isBlank(requestids) ? requestid : "," + requestid;
                    } else {
                        errcount++;
                    }
                }
            }

//            if (errcount == 0) {
//                performanceSubmitDao.updateRequestid2FormTable(tenant_key, requestids, dt2_form_id + "");
//            } else {
//                performanceSubmitDao.updateRequestid2FormTable(tenant_key, requestids, dt2_form_id + "");
//            }
        }catch (Exception e){
            log.error("createWorkflowV1:"+e);
        }
        dataMap.put("errcount",errcount);
        dataMap.put("requestids",requestids);
        return dataMap;
    }

}
