package com.cf.imes.module.system.controller.admin.oauth2;

import co.elastic.clients.elasticsearch.core.IndexResponse;
import com.cf.imes.framework.es.core.service.ESDocumentService;
import com.cf.imes.framework.es.core.service.ESDocumentServiceImpl;
import com.cf.imes.framework.mybatis.core.generator.SnowFlake;
import com.cf.imes.framework.test.core.ut.BaseMockitoUnitTest;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EsTest extends BaseMockitoUnitTest {

    @Resource
    private ESDocumentService esDocumentService;

    private SnowFlake snowFlake = new SnowFlake(1,1);

    @Test
    public void t1() throws IOException {
        HashMap test2 = esDocumentService.getById("test2", "145116052710363136", HashMap.class);
        System.out.println(test2);
    }

    @Test
    public void c1() throws Exception {
        String json = """
                      {
                          "ID": 0,
                          "MachineID": 5511,
                          "CompanyID": 1678,
                          "Name": "通用电子锯-备份",
                          "Type": 5,
                          "Setting": {
                            "templateName": "通用电子锯表格",
                            "encodingName": "",
                            "fileA": "",
                            "fileB": "",
                            "fileCreateType": "",
                            "codeContent": "// @changelog 2022-05-09 xzh 添加可配置压缩包名称n//参数可配置通用电子锯nnif(order == null || helper == null) return getArgList();nnfunction getArgList()n{ntlet args= { list:[],add(name,value,remark){ this.list.push({name,value,remark});return this;}};ntreturn argsn                .add('zipFileName','cnc-{0}-{1}-{5}-{3}.zip', '导出zip文件名(0:日期时间,1:排单号,2:排单备注,3:地址列表,4:订单号列表,5:客户名称列表)')ntt.add('filename','{0}_{1}_{2}_{3}.csv','文件名(按板材导出时 0:板材名 1:材质 2:颜色 3:厚度 4:品牌rn按订单导出时 0:订单号 1:自定义单号 2:经销商 3:客户 4:地址)')ntt.add('gb2312',true,'中文编码方式(false:UTF-8  true:GB-2312)')ntt.add('groupByPM',true,'文件导出方式(fasle:按订单导出  true:按板材导出)')ntt.add('showTitle',true,'是否显示标题')       n        .add('fn_Filter','return false;','板件过滤函数(b板件)@b') ntt.add('fn_row',let texts = [];nlet isTitle = i ==0;nnaddV('订单号',b.OrderNo);naddV('柜号',b.BoxName);naddV('板件编号',b.BlockNo);nlet ztm = (b.FrontHoleCount+b.FrontModelCount>0 || b.BackHoleCount+b.BackModelCount==0 && b.HoleCount_Side>0 || b.IsUnRegular) ? b.BlockNo+'A' : '';nlet ftm = b.BackHoleCount+b.BackModelCount>0 ? b.BlockNo+'B' : '';naddV('正面条码',ztm);naddV('反面条码',ftm);naddV('产品名称',b.BlockName);naddV('成品名称',b.BlockName);naddV('材质名称',b.Thickness+'mm'+b.MetrialName+b.Color);nlet fL = getNum(b.Wave==2 ? b.Width : b.Length,1);nlet fW = getNum(b.Wave==2 ? b.Length : b.Width,1);naddV('成品长度',fL);naddV('成品宽度',fW);naddV('成品厚度',b.Thickness);nlet cL = getNum(b.Wave==2 ? b.CuttingWidth : b.CuttingLength,1);nlet cW = getNum(b.Wave==2 ? b.CuttingLength : b.CuttingWidth,1);naddV('开料长度',cL);naddV('开料宽度',cW);naddV('开料厚度',b.Thickness);naddV('纹路方向',b.Wave==2 ? '横纹' : '竖纹');naddV('需切数量',1);nlet fc1 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderUpper : b.BorderLeft,1);nlet fc2 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderUnder : b.BorderRight,1);nlet fk1 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderLeft : b.BorderUpper,1);nlet fk2 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderRight : b.BorderUnder,1);naddV('封长1',fc1);naddV('封宽1',fk1);naddV('封长2',fc2);naddV('封宽2',fk2);naddV('订单类型',b.RoomName);naddV('客户信息',b.ConsigneeAddress);naddV('加盟店',b.CustomerName);naddV('异形',b.IsUnRegular ? '异形' : '');naddV('旋转','');naddV('正面槽',b.FrontModelCount);naddV('是否开槽',b.FrontModelCount+b.BackModelCount>0 ? '槽' : '');naddV('反面槽',b.BackModelCount);naddV('正面孔',b.FrontHoleCount);naddV('是否打孔',b.FrontHoleCount+b.BackHoleCount>0 ? '孔' : '');naddV('反面孔',b.BackHoleCount);naddV('拉槽标识',b.FrontModelCount+b.BackModelCount>0 ? '槽' : '');naddV('排钻标识',b.FrontHoleCount+b.BackHoleCount+b.HoleCount_Side>0 ? '钻' : '');naddV('钻孔',b.FrontHoleCount>0 && b.BackHoleCount>0 ? '双' : (b.FrontHoleCount+b.HoleCount_Side>0 || b.BackHoleCount+b.HoleCount_Side>0 ? '单' : ''));naddV('异形ID','');nnreturn texts.join(',');nnfunction addV(title,v)n{n    if(v == undefined) v = '';n    texts.push(isTitle ? title : v.toString());n}nnfunction getNum(v,b)n{n    return v.toFixed(b);n},'数据行(helper帮助类,i行号,pm板材,b板)@helper,i,pm,b')ntt.list;n}nnlet filename = helper.getArg('filename','{0}_{1}_{2}_{3}.xls');nlet groupByPM = helper.getArg('groupByPM',true);nlet isGb2312 = helper.getArg('gb2312',true);nlet showTitle = helper.getArg('showTitle',true);nlet fn_Filter = helper.newFn('板件过滤函数','fn_Filter','return false;','b');  //板件过滤函数nlet fn_row = helper.newFn('数据行','fn_row',let texts = [];nlet isTitle = i ==0;nnaddV('订单号',b.OrderNo);naddV('柜号',b.BoxName);naddV('板件编号',b.BlockNo);nlet ztm = (b.FrontHoleCount+b.FrontModelCount>0 || b.BackHoleCount+b.BackModelCount==0 && b.HoleCount_Side>0 || b.IsUnRegular) ? b.BlockNo+'A' : '';nlet ftm = b.BackHoleCount+b.BackModelCount>0 ? b.BlockNo+'B' : '';naddV('正面条码',ztm);naddV('反面条码',ftm);naddV('产品名称',b.BlockName);naddV('成品名称',b.BlockName);naddV('材质名称',b.Thickness+'mm'+b.MetrialName+b.Color);nlet fL = getNum(b.Wave==2 ? b.Width : b.Length,1);nlet fW = getNum(b.Wave==2 ? b.Length : b.Width,1);naddV('成品长度',fL);naddV('成品宽度',fW);naddV('成品厚度',b.Thickness);nlet cL = getNum(b.Wave==2 ? b.CuttingWidth : b.CuttingLength,1);nlet cW = getNum(b.Wave==2 ? b.CuttingLength : b.CuttingWidth,1);naddV('开料长度',cL);naddV('开料宽度',cW);naddV('开料厚度',b.Thickness);naddV('纹路方向',b.Wave==2 ? '横纹' : '竖纹');naddV('需切数量',1);nlet fc1 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderUpper : b.BorderLeft,1);nlet fc2 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderUnder : b.BorderRight,1);nlet fk1 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderLeft : b.BorderUpper,1);nlet fk2 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderRight : b.BorderUnder,1);naddV('封长1',fc1);naddV('封宽1',fk1);naddV('封长2',fc2);naddV('封宽2',fk2);naddV('订单类型',b.RoomName);naddV('客户信息',b.ConsigneeAddress);naddV('加盟店',b.CustomerName);naddV('异形',b.IsUnRegular ? '异形' : '');naddV('旋转','');naddV('正面槽',b.FrontModelCount);naddV('是否开槽',b.FrontModelCount+b.BackModelCount>0 ? '槽' : '');naddV('反面槽',b.BackModelCount);naddV('正面孔',b.FrontHoleCount);naddV('是否打孔',b.FrontHoleCount+b.BackHoleCount>0 ? '孔' : '');naddV('反面孔',b.BackHoleCount);naddV('拉槽标识',b.FrontModelCount+b.BackModelCount>0 ? '槽' : '');naddV('排钻标识',b.FrontHoleCount+b.BackHoleCount+b.HoleCount_Side>0 ? '钻' : '');naddV('钻孔',b.FrontHoleCount>0 && b.BackHoleCount>0 ? '双' : (b.FrontHoleCount+b.HoleCount_Side>0 || b.BackHoleCount+b.HoleCount_Side>0 ? '单' : ''));naddV('异形ID','');nnreturn texts.join(',');nnfunction addV(title,v)n{n    if(v == undefined) v = '';n    texts.push(isTitle ? title : v.toString());n}nnfunction getNum(v,b)n{n    return v.toFixed(b);n},'helper','i','pm','b');nnlet hasTitle = false;nlet cache = [];nlet orderFileName='';nlet rowID = 0;nnfor(let pm of order.MetrialList)  //按板材  0:板材名 1:材质 2:颜色 3:厚度 4:品牌n{ntif(groupByPM)nt{nttcache = [];nttrowID = 0;nt}ntfor(let i = 1; i <= pm.BlockList.length; i ++)nt{nttlet str_line = '';nttlet block = pm.BlockList[i-1];nttrowID = rowID + 1;nn        let isValid = helper.exec(fn_Filter,block);  //板件过滤函数nttif(isValid == true) continue;  //过滤板件nntt//标题nttif(showTitle && !hasTitle)ntt{ntttlet str_title = helper.exec(fn_row,helper,0,pm,block);ntttcache.push(str_title);nttthasTitle = true;ntt}nntttryntt{ntttstr_line = helper.exec(fn_row,helper,rowID,pm,block);ntt}nttcatch (error)ntt{ntttstr_line = '执行失败';ntt}nttcache.push(str_line); nnttif(!groupByPM && orderFileName=='')  //按订单生成  0:订单号 1:自定义单号 2:经销商 3:客户 4:地址ntt{ntttorderFileName = helper.format(filename,block.OrderNo,block.CustomOrderNo,block.CustomerName,block.Consignee,block.ConsigneeAddress);ntt}nt}nntif(groupByPM)nt{nttlet fname = helper.format(filename,pm.GoodsName,pm.Metrial,pm.Color,pm.Thickness,pm.Brank);nttisGb2312 ? helper.pushFile_gb2312(fname,cache.join('rn')) : helper.pushFile(fname,cache.join('rn'));nt}n}nnif(!groupByPM)n{ntisGb2312 ? helper.pushFile_gb2312(orderFileName,cache.join('rn')) : helper.pushFile(orderFileName,cache.join('rn'));n}nn//参数可配置通用电子锯",
                            "Remark": "",
                            "cncConfig": {
                              "zipFileName": "{3}-电子锯文件.zip",
                              "filename": "{0}_{1}_{2}_{3}.csv",
                              "gb2312": true,
                              "groupByPM": true,
                              "showTitle": true,
                              "fn_Filter": "return false;",
                              "fn_row": "let texts = [];nlet isTitle = i ==0;nnaddV('订单号',b.util);naddV('订单号',b.OrderNo);naddV('柜号',b.BoxName);naddV('板件编号',b.BlockNo);nlet ztm = (b.FrontHoleCount+b.FrontModelCount>0 || b.BackHoleCount+b.BackModelCount==0 && b.HoleCount_Side>0 || b.IsUnRegular) ? b.BlockNo+'A' : '';nlet ftm = b.BackHoleCount+b.BackModelCount>0 ? b.BlockNo+'B' : '';naddV('正面条码',ztm);naddV('反面条码',ftm);naddV('产品名称',b.BlockName);naddV('成品名称',b.BlockName);naddV('材质名称',b.Thickness+'mm'+b.MetrialName+b.Color);nlet fL = getNum(b.Wave==2 ? b.Width : b.Length,1);nlet fW = getNum(b.Wave==2 ? b.Length : b.Width,1);naddV('成品长度',fL);naddV('成品宽度',fW);naddV('成品厚度',b.Thickness);nlet cL = getNum(b.Wave==2 ? b.CuttingWidth : b.CuttingLength,1);nlet cW = getNum(b.Wave==2 ? b.CuttingLength : b.CuttingWidth,1);naddV('开料长度',cL);naddV('开料宽度',cW);naddV('开料厚度',b.Thickness);naddV('纹路方向',b.Wave==2 ? '横纹' : '竖纹');naddV('需切数量',1);nlet fc1 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderUpper : b.BorderLeft,1);nlet fc2 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderUnder : b.BorderRight,1);nlet fk1 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderLeft : b.BorderUpper,1);nlet fk2 = b.IsUnRegular ? '-' : getNum(b.Wave==2 ? b.BorderRight : b.BorderUnder,1);naddV('封长1',fc1);naddV('封宽1',fk1);naddV('封长2',fc2);naddV('封宽2',fk2);naddV('订单类型',b.RoomName);naddV('客户信息',b.ConsigneeAddress);naddV('加盟店',b.CustomerName);naddV('异形',b.IsUnRegular ? '异形' : '');naddV('旋转','');naddV('正面槽',b.FrontModelCount);naddV('是否开槽',b.FrontModelCount+b.BackModelCount>0 ? '槽' : '');naddV('反面槽',b.BackModelCount);naddV('正面孔',b.FrontHoleCount);naddV('是否打孔',b.FrontHoleCount+b.BackHoleCount>0 ? '孔' : '');naddV('反面孔',b.BackHoleCount);naddV('拉槽标识',b.FrontModelCount+b.BackModelCount>0 ? '槽' : '');naddV('排钻标识',b.FrontHoleCount+b.BackHoleCount+b.HoleCount_Side>0 ? '钻' : '');naddV('钻孔',b.FrontHoleCount>0 && b.BackHoleCount>0 ? '双' : (b.FrontHoleCount+b.HoleCount_Side>0 || b.BackHoleCount+b.HoleCount_Side>0 ? '单' : ''));naddV('异形ID','');nnreturn texts.join(',');nnfunction addV(title,v)n{n    if(v == undefined) v = '';n    texts.push(isTitle ? title : v.toString());n}nnfunction getNum(v,b)n{n    return v.toFixed(b);n}}"
                            }
                          },
                          "Remark": "品牌:通用 备注:2020.05.09"
                        }
                """;
        json = """
                {"name": "a", "age":2}
                """;
        IndexResponse indexResponse = esDocumentService.createByJson("t1", String.valueOf(snowFlake.nextId()), json);
        System.out.println(indexResponse);
    }

    @Test
    public void u1() throws IOException {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("organId",new int[]{4, 5});
        esDocumentService.updateById("t3", "145455371992043520", HashMap.class, objectObjectHashMap);
    }

    @Test
    public void d1() throws IOException {
        esDocumentService.deleteById("t1", "145125592260546560");
    }


}
