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

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

@Service
public class UpdatePerformanceServiceImpl implements UpdatePerformanceService {

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

    @Autowired
    UpdatePerformanceDao updatePerformanceDao;

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

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

        log.error("createWorkflowV1:form_data_id:"+form_data_id);

        try{
            Map<String,Object> mainData = updatePerformanceDao.queryMainDataByFormDataId(Constants.TENANT_KEY,form_data_id);
            String bm = String.valueOf(mainData.get("bm"));
            String yjtbnf = String.valueOf(mainData.get("yjtbnf"));
            String zq = String.valueOf(mainData.get("zq"));

            List<Map<String,Object>> dt1List = updatePerformanceDao.queryDt1DataByFormDataId(Constants.TENANT_KEY,form_data_id);
            List<Map<String,Object>> dt2List = updatePerformanceDao.queryDt2DataByFormDataId(Constants.TENANT_KEY,form_data_id);
            List<Map<String,Object>> dt3List = updatePerformanceDao.queryDt3DataByFormDataId(Constants.TENANT_KEY,form_data_id);

            log.error("createWorkflowV1:dt1List:"+dt1List.size());
            log.error("createWorkflowV1:dt2List:"+dt2List.size());
            log.error("createWorkflowV1:dt3List:"+dt3List.size());

            String dxwb_p8r4 = String.valueOf(mainData.get("dxwb_p8r4"));
            String txktbt = String.valueOf(mainData.get("txktbt"));
            String qz = String.valueOf(mainData.get("qz"));
            String dxwb_3m7o = String.valueOf(mainData.get("dxwb_3m7o"));
            String txkt2bt = String.valueOf(mainData.get("txkt2bt"));
            String qz2 = String.valueOf(mainData.get("qz2"));
            String kts = String.valueOf(mainData.get("kts"));
            String txkt3bt = String.valueOf(mainData.get("txkt3bt"));
            String qz3 = String.valueOf(mainData.get("qz3"));

            String upSet = "kty='"+dxwb_p8r4+"',ktbt11='"+txktbt+"',qz11='"+qz+"',kte='"+dxwb_3m7o+"',ktbt22='"+txkt2bt+"',qz22='"+qz2+"',kts33='"+kts+"',ktbt33='"+txkt3bt+"',qz33='"+qz3+"'";
            for(int i=0;i<dt1List.size();i++){
                Map<String,Object> dt1Data = dt1List.get(i);
                String dxwb_ckts = String.valueOf(dt1Data.get("dxwb_ckts"));
                String dxwb_07dy = String.valueOf(dt1Data.get("dxwb_07dy"));
                upSet+= StringUtils.isBlank(upSet) ? "fjmb"+(i+1)+"='"+dxwb_ckts+"',kpi1"+(i+1)+"='"+dxwb_07dy+"'" : ",fjmb"+(i+1)+"='"+dxwb_ckts+"',kpi1"+(i+1)+"='"+dxwb_07dy+"'" ;
            }

            for(int i=0;i<dt2List.size();i++){
                Map<String,Object>  dt2Data = dt2List.get(i);
                String dxwb_ckts = String.valueOf(dt2Data.get("dxwb_ckts"));
                String dxwb_07dy = String.valueOf(dt2Data.get("dxwb_07dy"));
                upSet+= StringUtils.isBlank(upSet) ? "fjmb"+(i+1)+"2='"+dxwb_ckts+"',kpi2"+(i+1)+"='"+dxwb_07dy+"'" : ",fjmb"+(i+1)+"2='"+dxwb_ckts+"',kpi2"+(i+1)+"='"+dxwb_07dy+"'" ;
            }

            for(int i=0;i<dt3List.size();i++){
                Map<String,Object> dt3Data = dt3List.get(i);
                String dxwb_ckts = String.valueOf(dt3Data.get("dxwb_ckts"));
                String dxwb_07dy = String.valueOf(dt3Data.get("dxwb_07dy"));
                upSet+= StringUtils.isBlank(upSet) ? "fjmb"+(i+1)+"3='"+dxwb_ckts+"',kpi3"+(i+1)+"='"+dxwb_07dy+"'" : ",fjmb"+(i+1)+"3='"+dxwb_ckts+"',kpi3"+(i+1)+"='"+dxwb_07dy+"'" ;
            }

            int deptcount = 0;
            String errorDeptName = "";
            List<Map<String, Object>> deptList = updatePerformanceDao.queryDepartmentManager(Constants.TENANT_KEY,bm);
            log.error("createWorkflowV1:deptList:"+deptList.size());
            if(deptList.size()>0){
                for(int i=0;i<deptList.size();i++){
                    Map<String,Object> deptMap = deptList.get(i);
                    String deptid = String.valueOf(deptMap.get("id"));
                    String bmfzr = String.valueOf(deptMap.get("bmfzr"));
                    String name = String.valueOf(deptMap.get("name"));
                    log.error("createWorkflowV1:deptid:"+deptid);
                    log.error("createWorkflowV1:bmfzr:"+bmfzr);
                    Map<String, Object> result = updatePerformanceDao.updateFormDataByFormdataid(Constants.TENANT_KEY,upSet,deptid,yjtbnf,zq,bmfzr);
                    if(result.containsKey("code")) {
                        String code = String.valueOf(result.get("code"));
                        log.error("createWorkflowV1:code:"+code);
                        if (!"200".equals(code)) {
                            deptcount++;
                            errorDeptName += StringUtils.isBlank(errorDeptName) ? name : ","+name ;
                        }
                    }else{
                        deptcount++;
                        errorDeptName += StringUtils.isBlank(errorDeptName) ? name : ","+name ;
                    }
                }
            }

            if(deptcount == 0){
                Map<String, Object> result2 = updatePerformanceDao.updateFormDataByDeptManager(Constants.TENANT_KEY,upSet,bm,yjtbnf,zq,userid);
                dataMap.putAll(result2);
            }

            dataMap.put("updatesql",upSet);
            dataMap.put("deptcount",deptcount);
            dataMap.put("errorDeptName",errorDeptName);

        }catch (Exception e){
            log.error("UpdatePerformanceServiceImpl:e:"+e);
        }

        return dataMap;
    }

}
