package com.malk.kuaikeli;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.malk.kuaikeli.service.KKLService;
import com.malk.kuaikeli.service.SupplierMaterialService;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.aliwork.YDSearch;
import com.malk.service.aliwork.YDClient;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class DdTest {

    @Autowired
    private SupplierMaterialService supplierMaterialService;
    @Autowired
    private YDClient ydClient;

    @Test
    public void create() {
        supplierMaterialService.sync();
//        supplierMaterialService.syncPrice();
    }

    @Test
    public void kk(){
        String fid="FINST-W9D66Q61MLZKG5LAEDWCBBS61E1D3FQFYS1WLJWL";
        Map formData=(Map) ydClient.queryData(YDParam.builder().formInstId(fid).build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
        Map addMap=new HashMap();
        String data=String.valueOf(formData.get("associationFormField_lln477nb_id"));
        System.out.println(data.substring(1,data.length()-1).replaceAll("\\\\",""));
        addMap.put("associationFormField_llndtdwg",JSONObject.parseArray(data.substring(1,data.length()-1).replaceAll("\\\\","")));// 项目点
        ydClient.operateData(YDParam.builder().formUuid("FORM-RK966E71T0LDSTW39WFHE9OQ5GU23FKRRDNLL2").formDataJson(JSONObject.toJSONString(addMap)).build(), YDConf.FORM_OPERATION.create);
    }

    String data="{\"name\":\"XM-20240429-015\",\"area\":\"早餐\",\"list\":[{\"textField_lnwktyzy\":\"已启用\",\"rowId\":\"H7D667C1X7YLJR02AX55E7EX76SB23XVXFEXL9J5\",\"textField_ln1ez5li\":\"20240615\",\"textField_llndm599\":\"五指毛桃\",\"numberField_lmqclyxa\":\"87.64\",\"textField_lptgvips\":\"KG\",\"dateField_ln1ez5lg\":1718380800000,\"numberField_lomyf89l\":\"1\",\"numberField_lns0dkzv\":\"0\",\"dateField_lqg8oid5\":1718467200000,\"numberField_lnrzzg03\":\"\",\"textField_lln3lmn2\":\"KG\",\"textField_lnrzzg04\":\"\",\"numberField_lmym5628\":\"0.45\",\"textField_lmk97m1v\":\"CS550\",\"textField_lnwxz9ji\":\"\",\"radioField_ln1ez5le\":\"否\",\"numberField_lmsvfftq\":\"79.67\",\"numberField_lln477o2\":\"450\",\"numberField_lqg8oid6\":\"0\",\"selectField_lmqclyx9\":\"蔬菜类\",\"textField_lm4m83rv\":\"块\",\"numberField_lmsvfftr\":\"10\"},{\"textField_lnwktyzy\":\"已启用\",\"rowId\":\"H7D667C1X7YLJR02AX55E7EX76SB23XVXFEXLAJ5\",\"textField_ln1ez5li\":\"20240615\",\"textField_llndm599\":\"水洗胡萝卜\",\"numberField_lmqclyxa\":\"2.42\",\"textField_lptgvips\":\"KG\",\"dateField_ln1ez5lg\":1718380800000,\"numberField_lomyf89l\":\"1\",\"numberField_lns0dkzv\":\"0\",\"dateField_lqg8oid5\":1718467200000,\"numberField_lnrzzg03\":\"\",\"textField_lln3lmn2\":\"KG\",\"textField_lnrzzg04\":\"\",\"numberField_lmym5628\":\"0.34\",\"textField_lmk97m1v\":\"CS00107\",\"textField_lnwxz9ji\":\"\",\"radioField_ln1ez5le\":\"否\",\"numberField_lmsvfftq\":\"2.2\",\"numberField_lln477o2\":\"340\",\"numberField_lqg8oid6\":\"0\",\"selectField_lmqclyx9\":\"蔬菜类\",\"textField_lm4m83rv\":\"无\",\"numberField_lmsvfftr\":\"10\"},{\"textField_lnwktyzy\":\"已启用\",\"rowId\":\"H7D667C1X7YLJR02AX55E7EX76SB23XVXFEXLBJ5\",\"textField_ln1ez5li\":\"20240615\",\"textField_llndm599\":\"红心红薯\",\"numberField_lmqclyxa\":\"7.25\",\"textField_lptgvips\":\"KG\",\"dateField_ln1ez5lg\":1718380800000,\"numberField_lomyf89l\":\"3\",\"numberField_lns0dkzv\":\"0\",\"dateField_lqg8oid5\":1718467200000,\"numberField_lnrzzg03\":\"\",\"textField_lln3lmn2\":\"KG\",\"textField_lnrzzg04\":\"\",\"numberField_lmym5628\":\"3\",\"textField_lmk97m1v\":\"CS00137\",\"textField_lnwxz9ji\":\"\",\"radioField_ln1ez5le\":\"否\",\"numberField_lmsvfftq\":\"6.59\",\"numberField_lln477o2\":\"3000\",\"numberField_lqg8oid6\":\"0\",\"selectField_lmqclyx9\":\"蔬菜类\",\"textField_lm4m83rv\":\"块\",\"numberField_lmsvfftr\":\"10\"}],\"approveFlag\":false,\"form\":{},\"user\":\"011467460262746600\",\"poid\":\"FINST-ENC66381W4YLM1DG73V7C7L7H3DJ2071T1EXLC3H\"}";

    @Autowired
    private KKLService kklService;

    @Test
    public void test(){
        kklService.validateApprove(JSONObject.parseObject(data));

//        kklService.planApproval(JSONObject.parseObject(data));
//        List<Map> table=null;
//        String formUuid="FORM-XHA66881J8KDA6V3FM6LZBUP9PDE37VESDNLL6";
//        Map formData=(Map) ydClient.queryData(YDParam.builder().formInstId(fid).build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
//        List<Map> list=UtilMap.getList(formData,"tableField_lln477nw");
//        saveData(list,formData,formUuid,false);// -
//        saveData(table,formData,formUuid,true);// +
    }

    private void saveData(List<Map> list,Map formData,String formUuid,boolean isAdd){
        for(Map map:list){
            Map queryMap=new HashMap();
            queryMap.put("dateField_llndtdwl", Arrays.asList(map.get("dateField_ln1ez5lg"),map.get("dateField_ln1ez5lg")));// 日期
            queryMap.put("textField_llq9x6ov",formData.get("textField_lln477nh"));// 项目编号
            queryMap.put("textField_lmk97m1v",formData.get("textField_lmk97m1v"));// 档口编号
            Map upMap=new HashMap();
            upMap.put("numberField_llojyfy3",map.get("numberField_lmsvfftq"));// 成本单价
            upMap.put("numberField_llojyfy4",map.get("numberField_lmsvfftr"));// 服务费率
            upMap.put("dateField_llndtdwl",map.get("dateField_ln1ez5lg"));// 计划日期
            upMap.put("textField_llndm599",map.get("textField_llndm599"));// 原材料名称
            upMap.put("textField_lluj4hu1",map.get("textField_lln3lmn2"));// 单位
            upMap.put("textField_llq9x6ov",formData.get("textField_lln477nh"));// 项目编号
            upMap.put("textField_lm5z7kvo",map.get("textField_ln1ez5li"));// 计划日期（文本）
            upMap.put("textField_lm5z4ioi",formData.get("textField_lmsvfftp"));// 项目名称
            upMap.put("textField_lmk97m1v",map.get("textField_lmk97m1v"));// 原材料编号
            upMap.put("selectField_lmqclyx9",map.get("selectField_lmqclyx9"));// 第一分类
            upMap.put("numberField_lmqclyxa",map.get("numberField_lmqclyxa"));// 供货价
            upMap.put("textField_lnsmha2j",map.get("textField_lnwxz9ji"));// 品牌
            upMap.put("textField_lpjet322",formData.get("textField_lpjet322"));// 档口名称
            upMap.put("textField_lpjet321",formData.get("textField_lpjet321"));// 档口编号
            List<Map> list2=(List<Map>) ydClient.queryData(YDParam.builder().formUuid(formUuid).searchFieldJson(JSONObject.toJSONString(queryMap)).build(), YDConf.FORM_QUERY.retrieve_list).getData();
            if(list2==null||list2.size()<1){
                if(!isAdd){
                    log.info("未找到对应的计划采购单,退出");
                    continue;
                }
                upMap.put("numberField_lln477o2", map.get("numberField_lmym5628"));// 初始采购数量（kg）
                upMap.put("numberField_lm9008fq", map.get("numberField_lmym5628"));// 实际采购数量（kg）
                upMap.put("numberField_lns0dkzv", map.get("numberField_lns0dkzv"));// 投料数量（kg）
                ydClient.operateData(YDParam.builder().formUuid(formUuid).formDataJson(JSONObject.toJSONString(upMap)).build(), YDConf.FORM_OPERATION.create);
            }else{
                Map oldMap=UtilMap.getMap(list2.get(0),"formData");
                if(isAdd){
                    upMap.put("numberField_lln477o2", NumberUtil.add(UtilMap.getString(oldMap,"numberField_lln477o2"),UtilMap.getString(map,"numberField_lmym5628")));// 初始采购数量（kg）
                    upMap.put("numberField_lm9008fq", NumberUtil.add(UtilMap.getString(oldMap,("numberField_lm9008fq")),UtilMap.getString(map,("numberField_lmym5628"))));// 实际采购数量（kg）
                    upMap.put("numberField_lns0dkzv", NumberUtil.add(UtilMap.getString(oldMap,("numberField_lns0dkzv")),UtilMap.getString(map,("numberField_lns0dkzv"))));// 投料数量
                }else {
                    upMap.put("numberField_lln477o2", NumberUtil.sub(UtilMap.getString(oldMap, "numberField_lln477o2"), UtilMap.getString(map, "numberField_lmym5628")));// 初始采购数量（kg）
                    upMap.put("numberField_lm9008fq", NumberUtil.sub(UtilMap.getString(oldMap, ("numberField_lm9008fq")), UtilMap.getString(map, ("numberField_lmym5628"))));// 实际采购数量（kg）
                    upMap.put("numberField_lns0dkzv", NumberUtil.sub(UtilMap.getString(oldMap, ("numberField_lns0dkzv")), UtilMap.getString(map, ("numberField_lns0dkzv"))));// 投料数量
                }
                ydClient.operateData(YDParam.builder().formInstanceId(UtilMap.getString(list2.get(0),"formInstanceId")).updateFormDataJson(JSONObject.toJSONString(upMap)).build(), YDConf.FORM_OPERATION.update);
            }
        }
    }


    @Test
    public void getYdData(){
        YDSearch ydSearch=new YDSearch("textField_llkb7kd2","XKL184","原材料编号", YDSearch.Type.TEXT_FIELD,YDSearch.Operator.EQ);
        List<Map> list2=(List<Map>) ydClient.queryData(YDParam.builder().formUuid("FORM-E6766M811CKD2PIXEAKOM9S2DKBM2PBQU1MLL5").searchCondition(JSONObject.toJSONString(
                Arrays.asList(ydSearch)
        )).build(), YDConf.FORM_QUERY.retrieve_list).getData();
        log.info("list2:{}",list2);
    }
}
