import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ebo.module.vo.SysMESComplaintFormVo;
import com.ebo.system.entity.SysOrgEntity;
import com.ebo.system.entity.SysPostEntity;
import com.ebo.system.entity.SysUserEntity;
import com.ebo.system.entity.SysUserPostEntity;
import com.ebo.system.service.SysOrgService;
import com.ebo.system.service.SysPostService;
import com.ebo.system.service.SysUserPostService;
import com.ebo.system.service.SysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class test {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysOrgService sysOrgService;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private SysPostService sysPostService;
    @Resource
    private SysUserPostService sysUserPostService;
    private static String appId = "2753387689773494150";

    private static String secret = "zdrhodfkm2itmwy5ns00ndrllwixzjktodmxndiwywi1zgey";

    private static String tokenUrl = "https://gzehr.greatpower.net:7051/GCYDHR/restApi/token/genToken";

    private String urls="https://gzehr.greatpower.net:7051/GCYDHR/sys/webreq/sysWebReqDef/eai.do?";

    private String eaiKey = "eaiKey=apiHQYGDA";


    @Test
    public void getTekesss(){
        String url = "http://10.0.100.20:8080/mes/third/thirdPartyAPI!hnMachineRepairdata.action";
        /*JSONObject headers = new JSONObject();
        headers.put("DATE_MODIFIED_FROM","2023-07-12");
        headers.put("DATE_MODIFIED_TO","2023-07-13");*/
        String responseBody = HttpUtil.get(url);
        System.out.println("11111"+responseBody);

        List<SysMESComplaintFormVo> list = new ArrayList<>();
        JSONObject resultData = JSON.parseObject(responseBody);
        if(resultData.getBoolean("status")){
            JSONArray resultArray = resultData.getJSONArray("result");
            for (int i = 0; i < resultArray.size(); i++) {
                JSONObject result = resultArray.getJSONObject(i);
                SysMESComplaintFormVo sysMESComplaintFormVo = JSON.parseObject(result.toJSONString(),SysMESComplaintFormVo.class);
                list.add(sysMESComplaintFormVo);
            }
        }
    }




    @Test
    public void getTeke(){
        String url = "http://192.168.18.37:18080/els/openApi/getToken?key="+"7d45fa41"+"&secret="+"77777c3cafda450cacfc515229d0a902";

        String result = HttpUtil.get(url);
        log.info("aaa"+result);
        String token = "";
        JSONObject jsonObject = JSONObject.parseObject(result);
        if(jsonObject.getInteger("code") == 200){
            JSONObject resultData = jsonObject.getJSONObject("result");
             token = resultData.getString("token");
            log.info("token::::"+token);
        }

        JSONObject headers = new JSONObject();
        headers.put("bus_account","520027");
        headers.put("interface_code","JK20221228000005");


        JSONObject body = new JSONObject();
        JSONObject data = new JSONObject();
        JSONArray headList = new JSONArray();
        JSONObject heads = new JSONObject();
        heads.put("materialNumber","FH24080100005");
        heads.put("materialName","极耳切刀");
      //  heads.put("cateCode","A10007");
        heads.put("cateName","消耗品");
        //heads.put("materialSpec","cessee");
        heads.put("purchaseType",2);
        heads.put("orgCode","001");
        heads.put("deleted",0);
        heads.put("baseUnit","桶");
        heads.put("sourceType","JWL");
    /*    heads.put("fbk1","2");
        heads.put("fbk2","A");
        heads.put("remark","测试");

        heads.put("materialDesc","111zz");*/
        headList.add(heads);
        data.put("headList",headList);

        body.put("data",data);
        body.put("busAccount","520027");
        body.put("simpleFlag","0");
        body.put("interfaceCode","createMaterialHead");
        headers.put("body",body);
        Map<String,String> header = new HashMap<>();
        header.put("token",token);
        String headersUrl = "http://192.168.18.37:18080/els/openApi/invoke";
        log.info("headersUrl::::"+headersUrl);
        log.info("headers::::"+headers);
        String responseBody = HttpUtil.createRequest(Method.POST, headersUrl).addHeaders(header).body(JSON.toJSONString(headers)).execute().body();
        log.info("responseBody::::"+responseBody);
    }


    @Test
    public void sds() {
        Map<String,Object> params = new HashMap<>();

        params.put("appId",appId);
        params.put("secret",secret);
        String result = HttpUtil.post(tokenUrl,params);

        JSONObject lt = JSONObject.parseObject(result);
        if(lt.getString("data") != null){
            Map<String,Object> paramsSysWebReqDef = new HashMap<>();
            paramsSysWebReqDef.put("empId","");
            paramsSysWebReqDef.put("empName","");
            paramsSysWebReqDef.put("depart","");
            paramsSysWebReqDef.put("company","");
            paramsSysWebReqDef.put("superior","");
            paramsSysWebReqDef.put("PageNum",1);
            paramsSysWebReqDef.put("PageSize",5);
            String hrUrl = urls+eaiKey+"&token="+lt.getString("data");

            String responseBody = HttpUtil.createRequest(Method.POST, hrUrl).body(JSON.toJSONString(paramsSysWebReqDef)).execute().body();
            log.info("responseBody:{}", JSON.toJSONString(responseBody));
            JSONObject request = JSONObject.parseObject(responseBody);
            SysUserEntity sysUserEntity = null;
            if(request.getBoolean("success")){
                JSONArray datas = request.getJSONArray("data");
                if(datas != null){
                    for (int i = 0; i < datas.size(); i++) {
                        JSONObject jsonObject = datas.getJSONObject(i);
                        sysUserEntity = sysUserService.getOne(new LambdaQueryWrapper<SysUserEntity>()
                                .eq(SysUserEntity::getUsername,jsonObject.getString("F_GH"))
                        );
                        if(sysUserEntity != null){
                            return;
                        }
                        String F_BM1_NAME = jsonObject.getString("F_BM1_NAME");
                        SysOrgEntity sysOrgEntity = null;
                        String fbmPid1 = "";//一级id
                        String fbmPid2 = "";//二级id
                        String fbmPid3 = "";//三级id
                        String fbmPid4 = "";//四级id
                        if(StringUtils.isNotEmpty(F_BM1_NAME)){
                            sysOrgEntity = sysOrgService.getOne(new LambdaQueryWrapper<SysOrgEntity>()
                                    .eq(SysOrgEntity::getName,F_BM1_NAME));
                            if(sysOrgEntity == null){
                                sysOrgEntity = new SysOrgEntity();
                                sysOrgEntity.setName(F_BM1_NAME);
                                sysOrgEntity.setCreator("HR");
                                sysOrgEntity.setTenantCode("A01");
                                sysOrgEntity.setUpdater("HR");
                                sysOrgService.save(sysOrgEntity);
                            }
                            fbmPid1 = sysOrgEntity.getId();
                        }
                        String F_BM2_NAME = jsonObject.getString("F_BM2_NAME");
                        if(StringUtils.isNotEmpty(F_BM2_NAME)){
                            sysOrgEntity = sysOrgService.getOne(new LambdaQueryWrapper<SysOrgEntity>()
                                    .eq(SysOrgEntity::getName,F_BM2_NAME));
                            if(sysOrgEntity == null){
                                sysOrgEntity = new SysOrgEntity();
                                sysOrgEntity.setPid(fbmPid1);
                                sysOrgEntity.setName(F_BM2_NAME);
                                sysOrgEntity.setCreator("HR");
                                sysOrgEntity.setTenantCode("A01");
                                sysOrgEntity.setUpdater("HR");
                                sysOrgService.save(sysOrgEntity);
                            }
                            fbmPid2 = sysOrgEntity.getId();
                        }
                        String F_BM3_NAME = jsonObject.getString("F_BM3_NAME");
                        if(StringUtils.isNotEmpty(F_BM3_NAME)){
                            sysOrgEntity = sysOrgService.getOne(new LambdaQueryWrapper<SysOrgEntity>()
                                    .eq(SysOrgEntity::getName,F_BM3_NAME));
                            if(sysOrgEntity == null){
                                sysOrgEntity = new SysOrgEntity();
                                sysOrgEntity.setPid(fbmPid2);
                                sysOrgEntity.setName(F_BM3_NAME);
                                sysOrgEntity.setCreator("HR");
                                sysOrgEntity.setTenantCode("A01");
                                sysOrgEntity.setUpdater("HR");
                                sysOrgService.save(sysOrgEntity);
                            }
                            fbmPid3 = sysOrgEntity.getId();
                        }

                        String F_BM4_NAME = jsonObject.getString("F_BM4_NAME");
                        if(StringUtils.isNotEmpty(F_BM4_NAME)){
                            sysOrgEntity = sysOrgService.getOne(new LambdaQueryWrapper<SysOrgEntity>()
                                    .eq(SysOrgEntity::getName,F_BM4_NAME));
                            if(sysOrgEntity == null){
                                sysOrgEntity = new SysOrgEntity();
                                sysOrgEntity.setPid(fbmPid3);
                                sysOrgEntity.setName(F_BM4_NAME);
                                sysOrgEntity.setCreator("HR");
                                sysOrgEntity.setTenantCode("A01");
                                sysOrgEntity.setUpdater("HR");
                                sysOrgService.save(sysOrgEntity);
                            }
                            fbmPid4 = sysOrgEntity.getId();
                        }

                        sysUserEntity = new SysUserEntity();
                        sysUserEntity.setCreator("HR");
                        sysUserEntity.setUpdater("HR");
                        sysUserEntity.setTenantCode("A01");
                        sysUserEntity.setMobile(jsonObject.getString("F_SJHM"));
                        sysUserEntity.setUsername(jsonObject.getString("F_GH"));
                        sysUserEntity.setRealName(jsonObject.getString("F_XM"));
                        if(jsonObject.getString("F_XB_NAME").equals("男")){
                            sysUserEntity.setGender(0);
                        }else {
                            sysUserEntity.setGender(1);
                        }
                        sysUserEntity.setStatus(1);
                        sysUserEntity.setPassword(passwordEncoder.encode("abcd1234"));
                        if(StringUtils.isNotEmpty(fbmPid4)){
                            sysUserEntity.setOrgId(fbmPid4);
                        }else {
                            if(StringUtils.isNotEmpty(fbmPid3)){
                                sysUserEntity.setOrgId(fbmPid3);
                            }else {
                                if(StringUtils.isNotEmpty(fbmPid2)){
                                    sysUserEntity.setOrgId(fbmPid2);
                                }else {
                                    sysUserEntity.setOrgId(fbmPid1);
                                }
                            }
                        }
                        sysUserService.save(sysUserEntity);

                        if(StringUtils.isNotEmpty(jsonObject.getString("F_GWMC_NAME"))){
                            SysPostEntity sysPostEntity = null;
                            sysPostEntity = sysPostService.getOne(new LambdaQueryWrapper<SysPostEntity>()
                                    .eq(SysPostEntity::getPostName,jsonObject.getString("F_GWMC_NAME")));
                            if(sysPostEntity == null){
                                sysPostEntity = new SysPostEntity();
                                sysPostEntity.setPostCode(jsonObject.getString("F_GWMC"));
                                sysPostEntity.setPostName(jsonObject.getString("F_GWMC_NAME"));
                                sysPostEntity.setTenantCode("A01");
                                sysPostEntity.setStatus(1);
                                sysPostEntity.setCreator("HR");
                                sysPostEntity.setUpdater("HR");
                                sysPostService.save(sysPostEntity);
                            }
                            SysUserPostEntity sysUserPostEntity = new SysUserPostEntity();
                            sysUserPostEntity.setPostId(sysPostEntity.getId());
                            sysUserPostEntity.setUserId(sysUserEntity.getId());
                            sysUserPostEntity.setTenantCode("A01");
                            sysUserPostEntity.setCreator("HR");
                            sysUserPostEntity.setUpdater("HR");
                            sysUserPostService.save(sysUserPostEntity);
                        }

                    }
                }
            }
        }

    }
}
