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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.sap.entity.OkHttpDto;
import com.weaver.seconddev.chapanda.sap.util.Esb2SapIncorporatedCompanyUtil;
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.Map;

@Service("Esb2SapIncorporatedCompanyCron")
public class Esb2SapIncorporatedCompanyCron implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2SapIncorporatedCompanyCron.class);

    @Autowired
    Esb2SapIncorporatedCompanyUtil esb2SapIncorporatedCompanyUtil;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        log.error("Esb2QunjieTestAttachmentCron");
        OkHttpDto okHttpDto = esb2SapIncorporatedCompanyUtil.queryIF311List();
        log.error("getResponseCode:{}",okHttpDto.getResponseCode());
        if(okHttpDto.getResponseCode() == 200){
            JSONObject dataJson = okHttpDto.getDataJson();
            JSONArray dataList = new JSONArray();
            if(dataJson.containsKey("data")){
                dataList = dataJson.getJSONArray("data");
            }

            log.error("dataList:{}",dataList.size());

            if(dataList.size()>0){
                /***
                 * 更新公司法人
                 */
                esb2SapIncorporatedCompanyUtil.updateIncorporatedCompany(dataList);
                /**
                 * 更新成本中心
                 */
                esb2SapIncorporatedCompanyUtil.updateCostCenter(dataList);
                /**
                 * 更新部门成本中心、人员成本中心
                 */
                esb2SapIncorporatedCompanyUtil.updateDeptEmployeeCostCenter(dataList);
            }
            else{
                return WeaResult.fail(500,"SAP接口获取为空");
            }
        }else{
            return WeaResult.fail(500,okHttpDto.getDataMsg());
        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","同步成功");

        return WeaResult.success(actionMap);
    }

    public JSONObject convertData(){
        log.error("convertData");
        JSONArray bodyArray = new JSONArray();
        String reposponseBody = "{\"RETURN\":[{\"P_ID\":\"28C626567F2311F09C870000005DF48A\",\"BUMEN\":836567,\"KOSTL\":\"10EAE15030\",\"KOSTL_T\":\"部门测试1\",\"BUKRS\":1000,\"BUKRS_T\":\"四川百茶百道实业股份有限公司\"},{\"P_ID\":\"28C626567F2311F09C870000005DF48A\",\"BUMEN\":2025659,\"KOSTL\":\"10EAE15041\",\"KOSTL_T\":\"部门测试2\",\"BUKRS\":1000,\"BUKRS_T\":\"四川百茶百道实业股份有限公司\"},{\"P_ID\":\"28C626567F2311F09C870000005DF48A\",\"BUMEN\":2335391,\"KOSTL\":\"10EAE15031\",\"KOSTL_T\":\"部门测试2\",\"BUKRS\":1000,\"BUKRS_T\":\"四川百茶百道实业股份有限公司\"},{\"P_ID\":\"28C626567F2311F09C870000005DF48A\",\"BUMEN\":2025659,\"KOSTL\":\"10EAE15032\",\"KOSTL_T\":\"部门测试3\",\"BUKRS\":1000,\"BUKRS_T\":\"四川百茶百道实业股份有限公司\"},{\"P_ID\":\"28C626567F2311F09C870000005DF48A\",\"BUMEN\":2335391,\"KOSTL\":\"10EAE15033\",\"KOSTL_T\":\"部门测试4\",\"BUKRS\":1000,\"BUKRS_T\":\"四川百茶百道实业股份有限公司\"},{\"P_ID\":\"28C626567F2311F09C870000005DF48A\",\"BUMEN\":836567,\"KOSTL\":\"10EAE15033\",\"KOSTL_T\":\"部门测试4\",\"BUKRS\":1000,\"BUKRS_T\":\"四川百茶百道实业股份有限公司\"},{\"P_ID\":\"28C626567F2311F09C870000005DF48A\",\"BUMEN\":836567,\"KOSTL\":\"10EAE15040\",\"KOSTL_T\":\"部门测试1\",\"BUKRS\":1000,\"BUKRS_T\":\"四川百茶百道实业股份有限公司\"},{\"P_ID\":\"28C626567F2311F09C870000005DF48A\",\"BUMEN\":836567,\"KOSTL\":\"10EAE15041\",\"KOSTL_T\":\"部门测试2\",\"BUKRS\":1000,\"BUKRS_T\":\"四川百茶百道实业股份有限公司\"},{\"P_ID\":\"28C626567F2311F09C870000005DF48A\",\"BUMEN\":2025659,\"KOSTL\":\"10EAE15033\",\"KOSTL_T\":\"部门测试4\",\"BUKRS\":1000,\"BUKRS_T\":\"四川百茶百道实业股份有限公司\"}]}";
        if(StringUtils.isNotBlank(reposponseBody)){
            JSONObject msgJson = JSONObject.parseObject(reposponseBody);
            log.error("msgJson:{}",msgJson.toJSONString());
            if(msgJson.containsKey("RETURN")){
                String returnStr = msgJson.getString("RETURN");
                log.error("returnStr:{}",returnStr);
                if(!"[]".equals(returnStr)){
                    JSONArray returnArray = msgJson.getJSONArray("RETURN");
                    log.error("returnArray:{}",returnArray.size());
                    for (int i = 0; i < returnArray.size(); i++) {
                        JSONObject dataJson = returnArray.getJSONObject(i);

                        String P_ID = dataJson.getString("P_ID");
                        String BUMEN = dataJson.getString("BUMEN");
                        String KOSTL = dataJson.getString("KOSTL");
                        String BUKRS = dataJson.getString("BUKRS");
                        String KOSTL_T = dataJson.getString("KOSTL_T");
                        String BUKRS_T = dataJson.getString("BUKRS_T");

                        log.error("KOSTL:{}",KOSTL);
                        log.error("BUKRS:{}",BUKRS);
                        log.error("BUMEN:{}",BUMEN);
                        log.error("BUKRS_T:{}",BUKRS_T);

                        JSONObject dataMap = new JSONObject();
                        dataMap.put("P_ID",P_ID);
                        dataMap.put("BUMEN",BUMEN);
                        dataMap.put("KOSTL",KOSTL);
                        dataMap.put("BUKRS",BUKRS);
                        dataMap.put("KOSTL_T",KOSTL_T);
                        dataMap.put("BUKRS_T",BUKRS_T);
                        bodyArray.add(dataMap);
                    }
                }
            }
        }

        log.error("bodyArray:{}",bodyArray.size());
        JSONObject responseDataJson = new JSONObject();
        responseDataJson.put("data",bodyArray);
        return responseDataJson;
    }



}
