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

import com.alibaba.fastjson.JSON;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.meishi.constant.Constants;
import com.weaver.seconddev.meishi.esb.dao.UpdatePerformanceDao;
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("Lamex_UpdatePerformanceSupDepartmentAction")
public class UpdatePerformanceSupDepartmentAction implements EsbServerlessRpcRemoteInterface {

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

    @Autowired
    UpdatePerformanceDao updatePerformanceDao;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {

        Map<String, Object> actionMap = new HashMap<>();
        String form_data_id = String.valueOf(params.get("form_data_id"));
        Long userid = Long.valueOf(String.valueOf(params.get("userid")));
        log.error("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"));

            boolean isDeptManager = updatePerformanceDao.compareDepartmentManagerByUser(Constants.TENANT_KEY,userid,bm);
            log.error("isDeptManager:"+isDeptManager);
            if(isDeptManager){
                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("dt1List:"+dt1List.size());
                log.error("dt2List:"+dt2List.size());
                log.error("dt3List:"+dt3List.size());

                String dxwb_p8r4 = String.valueOf(mainData.get("dxwb_p8r4"));
                if(dxwb_p8r4.contains("'")){
                    dxwb_p8r4 = dxwb_p8r4.replace("'","''");
                    log.error("dxwb_p8r4:"+dxwb_p8r4);
                }
                String txktbt = String.valueOf(mainData.get("txktbt"));
                if(txktbt.contains("'")){
                    txktbt = txktbt.replace("'","''");
                    log.error("txktbt:"+txktbt);
                }
                String qz = String.valueOf(mainData.get("qz"));
                if(qz.contains("'")){
                    qz = qz.replace("'","''");
                    log.error("qz:"+qz);
                }

                String dxwb_3m7o = String.valueOf(mainData.get("dxwb_3m7o"));
                if(dxwb_3m7o.contains("'")){
                    dxwb_3m7o = dxwb_3m7o.replace("'","''");
                    log.error("dxwb_3m7o:"+dxwb_3m7o);
                }

                String txkt2bt = String.valueOf(mainData.get("txkt2bt"));
                if(txkt2bt.contains("'")){
                    txkt2bt = txkt2bt.replace("'","''");
                    log.error("txkt2bt:"+txkt2bt);
                }

                String qz2 = String.valueOf(mainData.get("qz2"));
                if(qz2.contains("'")){
                    qz2 = qz2.replace("'","''");
                    log.error("qz2:"+qz2);
                }

                String kts = String.valueOf(mainData.get("kts"));
                if(kts.contains("'")){
                    kts = kts.replace("'","''");
                    log.error("kts:"+kts);
                }

                String txkt3bt = String.valueOf(mainData.get("txkt3bt"));
                if(txkt3bt.contains("'")){
                    txkt3bt = txkt3bt.replace("'","''");
                    log.error("txkt3bt:"+txkt3bt);
                }

                String qz3 = String.valueOf(mainData.get("qz3"));
                if(qz3.contains("'")){
                    qz3 = qz3.replace("'","''");
                }

                String upSet = "sfact=0,kty='"+dxwb_p8r4+"',ktbt11='"+txktbt+"',qz11='"+qz+"',kte='"+dxwb_3m7o+"',ktbt22='"+txkt2bt+"',qz22='"+qz2+"',kts33='"+kts+"',ktbt33='"+txkt3bt+"',qz33='"+qz3+"'";
                String upSetNull = "sfact=null,kty=null,ktbt11=null,qz11=null,kte=null,ktbt22=null,qz22=null,kts33=null,ktbt33=null,qz33=null";
                for(int i=0;i<dt1List.size();i++){
                    Map<String,Object> dt1Data = dt1List.get(i);

                    String dxwb_ckts = String.valueOf(dt1Data.get("dxwb_ckts"));
                    if(dxwb_ckts.contains("'")){
                        dxwb_ckts = dxwb_ckts.replace("'","''");
                        log.error("dxwb_ckts:"+dxwb_ckts);
                    }

                    String dxwb_07dy = String.valueOf(dt1Data.get("dxwb_07dy"));
                    if(dxwb_07dy.contains("'")){
                        dxwb_07dy = dxwb_07dy.replace("'","''");
                        log.error("dxwb_07dy:"+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+"'" ;

                    upSetNull += StringUtils.isBlank(upSetNull) ? "fjmb"+(i+1)+"=null,kpi1"+(i+1)+"=null" : ",fjmb"+(i+1)+"=null,kpi1"+(i+1)+"=null" ;
                }

                for(int i=0;i<dt2List.size();i++){
                    Map<String,Object>  dt2Data = dt2List.get(i);
                    String dxwb_ckts = String.valueOf(dt2Data.get("dxwb_ckts"));
                    if(dxwb_ckts.contains("'")){
                        dxwb_ckts = dxwb_ckts.replace("'","''");
                        log.error("dxwb_ckts:"+dxwb_ckts);
                    }

                    String dxwb_07dy = String.valueOf(dt2Data.get("dxwb_07dy"));
                    if(dxwb_07dy.contains("'")){
                        dxwb_07dy = dxwb_07dy.replace("'","''");
                        log.error("dxwb_07dy:"+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+"'" ;

                    upSetNull += StringUtils.isBlank(upSetNull) ? "fjmb"+(i+1)+"2=null,kpi2"+(i+1)+"=null" : ",fjmb"+(i+1)+"2=null,kpi2"+(i+1)+"=null" ;
                }

                for(int i=0;i<dt3List.size();i++){
                    Map<String,Object> dt3Data = dt3List.get(i);

                    String dxwb_ckts = String.valueOf(dt3Data.get("dxwb_ckts"));
                    if(dxwb_ckts.contains("'")){
                        dxwb_ckts = dxwb_ckts.replace("'","''");
                        log.error("dxwb_ckts:"+dxwb_ckts);
                    }

                    String dxwb_07dy = String.valueOf(dt3Data.get("dxwb_07dy"));
                    if(dxwb_07dy.contains("'")){
                        dxwb_07dy = dxwb_07dy.replace("'","''");
                        log.error("dxwb_07dy:"+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+"'" ;

                    upSetNull+= StringUtils.isBlank(upSetNull) ? "fjmb"+(i+1)+"3=null,kpi3"+(i+1)+"=null" : ",fjmb"+(i+1)+"3=null,kpi3"+(i+1)+"=null" ;

                }

                int deptcount = 0;
                String errorDeptName = "";
                List<Map<String, Object>> deptList = updatePerformanceDao.queryDepartmentManager(Constants.TENANT_KEY,bm);
                log.error("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("deptid:" + deptid);
                        log.error("bmfzr:" + bmfzr);

                        Map<String, Object> result = updatePerformanceDao.updateFormDataByFormdataid(Constants.TENANT_KEY, upSetNull, deptid, yjtbnf, zq, bmfzr);
                        if (result.containsKey("code")) {
                            String code = String.valueOf(result.get("code"));
                            if ("200".equals(code)) {
                                result = updatePerformanceDao.updateFormDataByFormdataid(Constants.TENANT_KEY, upSet, deptid, yjtbnf, zq, bmfzr);
                                log.error("UpdatePerformanceSupDepartmentAction-result:" + JSON.toJSONString(result));
                                if (result.containsKey("code")) {
                                    code = String.valueOf(result.get("code"));
                                    log.error("code:" + code);
                                    if (!"200".equals(code)) {
                                        deptcount++;
                                        errorDeptName += StringUtils.isBlank(errorDeptName) ? name : "," + name;
                                    }
                                } else {
                                    deptcount++;
                                    errorDeptName += StringUtils.isBlank(errorDeptName) ? name : "," + name;
                                }
                            }else{
                                deptcount++;
                            }
                        }else{
                            deptcount++;
                        }
                    }
                }
                log.error("deptcount:"+deptcount);
                if(deptcount == 0){

                    Map<String, Object> result2 = updatePerformanceDao.updateFormDataByDeptManager(Constants.TENANT_KEY,upSetNull,bm,yjtbnf,zq,userid);
                    if(result2.containsKey("code")) {
                        String code2 = String.valueOf(result2.get("code"));
                        if ("200".equals(code2)) {
                            result2 = updatePerformanceDao.updateFormDataByDeptManager(Constants.TENANT_KEY,upSet,bm,yjtbnf,zq,userid);
                            if(result2.containsKey("code")) {
                                code2 = String.valueOf(result2.get("code"));
                                if ("200".equals(code2)) {
                                    actionMap.put("code",200);
                                    actionMap.put("msg","更新数据成功");
                                }else{
                                    return WeaResult.fail(500,"更新部门非部门负责人的目标填写流程失败");
                                }
                            }else{
                                return WeaResult.fail(500,"更新部门非部门负责人的目标填写流程报错");
                            }
                        }else{
                            return WeaResult.fail(500,"更新部门非部门负责人的目标填写流程失败");
                        }
                    }else{
                        return WeaResult.fail(500,"更新部门非部门负责人的目标填写流程报错");
                    }
                }else{
                    return WeaResult.fail(500,"更新下级部门["+errorDeptName+"]的目标填写流程报错");
                }
            }else{
                actionMap.put("code",200);
                actionMap.put("msg","非部门负责人，无需更新数据");
            }
        }catch (Exception e){
            return WeaResult.fail(500,"更新下级部门部门负责人及本级部门的其他人员目标填写流程异常");
        }
        return WeaResult.success(actionMap);
    }

}
