package com.weaver.seconddev.chapanda.beisen.esb;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.common.hrm.util.HrmCommonUtil;
import com.weaver.ebuilder.form.client.entity.data.EBDataChangeResult;
import com.weaver.ebuilder.form.client.entity.data.EBDataReqDetailDto;
import com.weaver.ebuilder.form.client.entity.data.EBDataReqDto;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.beisen.constant.Constants;
import com.weaver.seconddev.chapanda.beisen.dao.HrmFormFieldDao;
import com.weaver.seconddev.chapanda.beisen.dao.HrmAssessment360Dao;
import com.weaver.seconddev.chapanda.beisen.util.EbuilderOperateUtils;
import com.weaver.seconddev.chapanda.beisen.util.Esb2BeiSenAssessment360Util;
import com.weaver.teams.domain.user.SimpleEmployee;
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("Esb2BeisenAssessment360Action")
public class Esb2BeisenAssessment360Action implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2BeisenAssessment360Action.class);

    @Autowired
    HrmFormFieldDao hrmFormFieldDao;

    @Autowired
    EbuilderOperateUtils ebuilderOperateUtils;

    @Autowired
    Esb2BeiSenAssessment360Util esb2BeiSenAssessment360Util;

    @Autowired
    private HrmCommonUtil hrmCommonUtil;

    @Autowired
    HrmAssessment360Dao hrmAssessment360Dao;

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

        List<String> ebList = new ArrayList<String>();

        List<String> fieldList = new ArrayList<String>();
        fieldList.add("ryid");
        fieldList.add("gsyx");
        fieldList.add("gh");
        fieldList.add("hdid");
        fieldList.add("hdmc");
        fieldList.add("wjid");
        fieldList.add("wjmc");
        fieldList.add("zzjg");
        fieldList.add("cjsj");
        fieldList.add("wcsj");

        log.error("Esb2BeisenAssessment360Action");

        String employeeid = String.valueOf(params.get("employeeid"));
        log.error("employeeid:{}",employeeid);
        if(StringUtils.isNotBlank(employeeid)){
            Long employeeId = Long.valueOf(employeeid);
            log.error("employeeId:{}",employeeId);
            SimpleEmployee createByEmployee = hrmCommonUtil.getSimpleEmployee(employeeId);
            String email = createByEmployee.getEmail();
            log.error("email:{}",email);
            JSONArray assessmentArray = esb2BeiSenAssessment360Util.convertAssessmentData(email);
            log.error("assessmentArray:{}",assessmentArray.size());

            Map<String, Object> fieldMap = hrmFormFieldDao.queryFromTableField(Constants.assessment360Table,fieldList);
            log.error("fieldMap:{}",fieldMap.size());

            List<EBDataReqDto> addDatas = new ArrayList<EBDataReqDto>();
            for(int i=0;i<assessmentArray.size();i++){
                JSONObject dataJson = assessmentArray.getJSONObject(i);
                log.error("dataJson:{}",dataJson.toJSONString());

                if(dataJson.containsKey("gh") && dataJson.containsKey("hdid") && dataJson.containsKey("wjid")){
                    String gh = dataJson.getString("gh");
                    String hdid = dataJson.getString("hdid");
                    String wjid = dataJson.getString("wjid");

                    log.error("gh:{}",gh);
                    log.error("hdid:{}",hdid);
                    log.error("wjid:{}",wjid);

                    String ryid = "";
                    if(StringUtils.isNotBlank(gh)){
                        ryid = hrmAssessment360Dao.queryEmployeeJobNumData(gh);
                    }
                    log.error("ryid:{}",ryid);

                    dataJson.put("ryid",ryid);

                    String ufId = hrmAssessment360Dao.getAssessment360Data(gh,hdid,wjid);
                    log.error("ufId:{}",ufId);

                    if(StringUtils.isBlank(ufId)){
                        log.error("新增");
                        EBDataReqDto ebDataReqDto = new EBDataReqDto();
                        List<EBDataReqDetailDto> mainData = new ArrayList<EBDataReqDetailDto>();
                        for(int n=0;n<fieldList.size();n++){
                            log.error("fieldid:{}",fieldList.get(n));
                            log.error("fieldid2:{}",dataJson.containsKey(fieldList.get(n)));
                            log.error("fieldid3:{}",fieldMap.containsKey(fieldList.get(n)));

                            if(dataJson.containsKey(fieldList.get(n)) && fieldMap.containsKey(fieldList.get(n))){
                                String value = dataJson.getString(fieldList.get(n));
                                String fieldId = fieldMap.get(fieldList.get(n)).toString();

                                log.error("fieldId:{}",fieldId);
                                log.error("value:{}",value);
                                mainData.add(new EBDataReqDetailDto(fieldId, value));
                            }
                        }
                        ebDataReqDto.setMainDatas(mainData);
                        addDatas.add(ebDataReqDto);
                    }
                }
            }
            log.error("addDatas:{}",addDatas.size());

            Map<String,Object> recordMap = hrmFormFieldDao.queryTableFormId(Constants.assessment360Table);
            String formId = recordMap.get("id").toString();
            String appId = recordMap.get("app_id").toString();
            log.error("formId:{}",formId);
            log.error("appId:{}",appId);
            if(StringUtils.isNotBlank(formId) && StringUtils.isNotBlank(appId)){
                if(addDatas.size() > 0){
                    EBDataChangeResult addEbDataChangeResult = ebuilderOperateUtils.bacthInsertDbForm(addDatas,formId,appId);
                    boolean isTrue = addEbDataChangeResult.getStatus();
                    log.error("message:{}",addEbDataChangeResult.getMessage());
                    log.error("isTrue1:{}",isTrue);
                    if(isTrue){
                        ebList.addAll(addEbDataChangeResult.getDataIds());
                    }
                }
            }
        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","同步成功");
        actionMap.put("ebList",ebList.size());
        return WeaResult.success(actionMap);
    }

}
