package com.purui.prims;

import com.alibaba.fastjson.JSON;
import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.common.pojo.JoinCondition;
import com.purui.prims.common.pojo.enums.AuthType;
import com.purui.prims.common.pojo.enums.FilterOperatorEnum;
import com.purui.prims.headless.api.pojo.ModelDetail;
import com.purui.prims.headless.api.pojo.TableJoinRelation;
import com.purui.prims.headless.api.pojo.request.ModelReq;
import com.purui.prims.headless.api.pojo.response.ModelResp;
import com.purui.prims.headless.server.service.ModelService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 模型测试
 *
 * @author charlie
 * @date 2024/7/4
 */
public class ModelTest extends BaseTest {

    @Resource
    ModelService modelService;

    @Test
    void testCreateModel() throws Exception {

        ModelReq modelReq = new ModelReq();

        ModelDetail modelDetail = new ModelDetail();
        modelReq.setModelDetail(modelDetail);

        List<TableJoinRelation> tableJoinRelations = new ArrayList<>();

        // 2张表m->n(单一join)
        TableJoinRelation tableJoinRelation = new TableJoinRelation();
        tableJoinRelation.setLeft("ods_prhis_adm");
        tableJoinRelation.setRight("ods_prhis_adm_order");
        List<JoinCondition> conditions = new ArrayList<>(2);
        tableJoinRelation.setConditions(conditions);
        JoinCondition joinCondition = new JoinCondition();
        joinCondition.setLeftField("id");
        joinCondition.setRightField("adm_id");
        joinCondition.setOperator(FilterOperatorEnum.EQUALS);
        conditions.add(joinCondition);

        JoinCondition joinCondition1 = new JoinCondition();
        joinCondition1.setLeftField("patient_id");
        joinCondition1.setRightField("patient_id");
        joinCondition1.setOperator(FilterOperatorEnum.EQUALS);
        conditions.add(joinCondition1);
        tableJoinRelation.setLeftFields(Arrays.asList("id","adm_date"));
        tableJoinRelations.add(tableJoinRelation);

        // m->n->x(多层join)
        TableJoinRelation tableJoinRelation1 = new TableJoinRelation();
        tableJoinRelation1.setLeft("ods_prhis_adm_order");
        tableJoinRelation1.setRight("ods_prhis_adm_order_detail");
        List<JoinCondition> conditions1 = new ArrayList<>(1);
        tableJoinRelation1.setConditions(conditions1);
        JoinCondition joinCondition2 = new JoinCondition();
        joinCondition2.setLeftField("id");
        joinCondition2.setRightField("adm_order_id");
        joinCondition2.setOperator(FilterOperatorEnum.EQUALS);
        conditions1.add(joinCondition2);
        tableJoinRelation1.setLeftFields(Arrays.asList("adm_id","order_date","qty"));
        tableJoinRelations.add(tableJoinRelation1);

        // m->n,m->x (一对多)
        TableJoinRelation tableJoinRelation2 = new TableJoinRelation();
        tableJoinRelation2.setLeft("ods_prhis_adm");
        tableJoinRelation2.setRight("ods_prhis_user");
        List<JoinCondition> conditions2 = new ArrayList<>(1);
        tableJoinRelation2.setConditions(conditions2);
        JoinCondition joinCondition3 = new JoinCondition();
        joinCondition3.setLeftField("user_id");
        joinCondition3.setRightField("id");
        joinCondition3.setOperator(FilterOperatorEnum.EQUALS);
        conditions2.add(joinCondition3);
        tableJoinRelation2.setLeftFields(Arrays.asList("price","unit","discount_price"));
        tableJoinRelations.add(tableJoinRelation2);

        modelReq.setDomainId(1L);
        modelReq.setDatabaseId(3L);
        modelReq.getModelDetail().setTableQuery("ods_prhis_adm");
        modelReq.getModelDetail().setJoinRelations(tableJoinRelations);
        // create
        ModelResp modelResp = modelService.createModel(modelReq, User.getFakeUser());

        Assert.assertNotNull(modelResp);

    }


    @Test
    void modelDetailTest()
    {
        String json = "{\"databaseId\":3,\"domainId\":10,\"sourceType\":\"快速创建\",\"name\":\"账单\",\"bizName\":\"账单\",\"typeEnum\":\"MODEL\",\"sensitiveLevel\":0,\"modelDetail\":{\"queryType\":\"quick\",\"tableQuery\":false,\"joinRelations\":[{\"left\":\"ods_prhis_payment_bill\",\"leftFields\":[\"Bill_Dr\"],\"right\":\"ods_prhis_payment\",\"rightFields\":[\"ID\"],\"type\":\"LeftJoin\",\"conditions\":{\"left\":\"Bill_Dr\",\"right\":\"ID\",\"operator\":\"IN\"}}],\"identifiers\":[{\"name\":\"ID\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_bill_ID\",\"isCreateDimension\":1},{\"name\":\"Bill_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_bill_Bill_Dr\",\"isCreateDimension\":1},{\"name\":\"AdmLoc_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_bill_AdmLoc_Dr\",\"isCreateDimension\":1},{\"name\":\"Loc_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_bill_Loc_Dr\",\"isCreateDimension\":1},{\"name\":\"Doctor_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_bill_Doctor_Dr\",\"isCreateDimension\":1},{\"name\":\"OrderDetail_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_bill_OrderDetail_Dr\",\"isCreateDimension\":1},{\"name\":\"MedItem_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_bill_MedItem_Dr\",\"isCreateDimension\":1},{\"name\":\"OrderExec_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_bill_OrderExec_Dr\",\"isCreateDimension\":1},{\"name\":\"Unit_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_bill_Unit_Dr\",\"isCreateDimension\":1},{\"name\":\"CreateUser_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_bill_CreateUser_Dr\",\"isCreateDimension\":1},{\"name\":\"UpdateUser_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_bill_UpdateUser_Dr\",\"isCreateDimension\":1},{\"name\":\"ID\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_ID\",\"isCreateDimension\":1},{\"name\":\"Hospital_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_Hospital_Dr\",\"isCreateDimension\":1},{\"name\":\"PayType_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_PayType_Dr\",\"isCreateDimension\":1},{\"name\":\"AdmType_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_AdmType_Dr\",\"isCreateDimension\":1},{\"name\":\"AdmReason_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_AdmReason_Dr\",\"isCreateDimension\":1},{\"name\":\"Adm_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_Adm_Dr\",\"isCreateDimension\":1},{\"name\":\"PatBill_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_PatBill_Dr\",\"isCreateDimension\":1},{\"name\":\"User_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_User_Dr\",\"isCreateDimension\":1},{\"name\":\"Insu_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_Insu_Dr\",\"isCreateDimension\":1},{\"name\":\"Status_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_Status_Dr\",\"isCreateDimension\":1},{\"name\":\"Primitive_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_Primitive_Dr\",\"isCreateDimension\":1},{\"name\":\"Report_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_Report_Dr\",\"isCreateDimension\":1},{\"name\":\"ReportUser_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_ReportUser_Dr\",\"isCreateDimension\":1},{\"name\":\"PrintPINV_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_PrintPINV_Dr\",\"isCreateDimension\":1},{\"name\":\"RefAuthUser_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_RefAuthUser_Dr\",\"isCreateDimension\":1},{\"name\":\"RefAuthLoc_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_RefAuthLoc_Dr\",\"isCreateDimension\":1},{\"name\":\"RefReason_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_RefReason_Dr\",\"isCreateDimension\":1},{\"name\":\"Group_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_Group_Dr\",\"isCreateDimension\":1},{\"name\":\"Loc_Dr\",\"type\":\"identify\",\"bizName\":\"ods_prhis_payment_Loc_Dr\",\"isCreateDimension\":1}],\"dimensions\":[{\"name\":\"OrderDate\",\"type\":\"time\",\"bizName\":\"ods_prhis_payment_bill_OrderDate\",\"isCreateDimension\":1,\"description\":\"下医嘱日期\",\"typeParams\":{\"isPrimary\":true,\"timeGranularity\":\"day\"}},{\"name\":\"UpdateDate\",\"type\":\"time\",\"bizName\":\"ods_prhis_payment_bill_UpdateDate\",\"isCreateDimension\":1,\"description\":\"更新日期\",\"typeParams\":{\"isPrimary\":true,\"timeGranularity\":\"day\"}},{\"name\":\"CreateDate\",\"type\":\"time\",\"bizName\":\"ods_prhis_payment_bill_CreateDate\",\"isCreateDimension\":1,\"description\":\"创建日期\",\"typeParams\":{\"isPrimary\":true,\"timeGranularity\":\"day\"}},{\"name\":\"PayDate\",\"type\":\"time\",\"bizName\":\"ods_prhis_payment_PayDate\",\"isCreateDimension\":1,\"description\":\"收款日期\",\"typeParams\":{\"isPrimary\":true,\"timeGranularity\":\"day\"}},{\"name\":\"PrintDate\",\"type\":\"time\",\"bizName\":\"ods_prhis_payment_PrintDate\",\"isCreateDimension\":1,\"description\":\"打印日期\",\"typeParams\":{\"isPrimary\":true,\"timeGranularity\":\"day\"}},{\"name\":\"ReportDate\",\"type\":\"time\",\"bizName\":\"ods_prhis_payment_ReportDate\",\"isCreateDimension\":1,\"description\":\"结账日期\",\"typeParams\":{\"isPrimary\":true,\"timeGranularity\":\"day\"}},{\"name\":\"RefAuthDate\",\"type\":\"time\",\"bizName\":\"ods_prhis_payment_RefAuthDate\",\"isCreateDimension\":1,\"description\":\"退费审核日期\",\"typeParams\":{\"isPrimary\":true,\"timeGranularity\":\"day\"}},{\"name\":\"CreateUserName\",\"type\":\"categorical\",\"bizName\":\"ods_prhis_payment_bill_CreateUserName\",\"isCreateDimension\":1,\"description\":\"开单人\"},{\"name\":\"HospitalName\",\"type\":\"categorical\",\"bizName\":\"ods_prhis_payment_HospitalName\",\"isCreateDimension\":1,\"description\":\"医院名称\"},{\"name\":\"HISTradeNum\",\"type\":\"categorical\",\"bizName\":\"ods_prhis_payment_HISTradeNum\",\"isCreateDimension\":1,\"description\":\"票据号\"},{\"name\":\"UserName\",\"type\":\"categorical\",\"bizName\":\"ods_prhis_payment_UserName\",\"isCreateDimension\":1,\"description\":\"收款人\"},{\"name\":\"PrintFlag\",\"type\":\"categorical\",\"bizName\":\"ods_prhis_payment_PrintFlag\",\"isCreateDimension\":1,\"description\":\"打印标记\"},{\"name\":\"ReportFlag\",\"type\":\"categorical\",\"bizName\":\"ods_prhis_payment_ReportFlag\",\"isCreateDimension\":1,\"description\":\"结账状态\"},{\"name\":\"RefAuthFlag\",\"type\":\"categorical\",\"bizName\":\"ods_prhis_payment_RefAuthFlag\",\"isCreateDimension\":1,\"description\":\"退费审核标记\"},{\"name\":\"IP\",\"type\":\"categorical\",\"bizName\":\"ods_prhis_payment_IP\",\"isCreateDimension\":1,\"description\":\"IP\"},{\"name\":\"EmergencyStay\",\"type\":\"categorical\",\"bizName\":\"ods_prhis_payment_EmergencyStay\",\"isCreateDimension\":1,\"description\":\"急诊留观标志\"}],\"measures\":[{\"name\":\"UnitPrice\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_bill_UnitPrice\",\"isCreateDimension\":1,\"alias\":\"单价\",\"constraint\":\"约束条件\"},{\"name\":\"Qty\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_bill_Qty\",\"isCreateDimension\":1,\"alias\":\"数量\",\"constraint\":\"约束条件\"},{\"name\":\"TotalAmt\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_bill_TotalAmt\",\"isCreateDimension\":1,\"alias\":\"总金额\",\"constraint\":\"约束条件\"},{\"name\":\"DiscountAmt\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_bill_DiscountAmt\",\"isCreateDimension\":1,\"alias\":\"折扣金额\",\"constraint\":\"约束条件\"},{\"name\":\"ShareFreeAmt\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_bill_ShareFreeAmt\",\"isCreateDimension\":1,\"alias\":\"记账金额\",\"constraint\":\"约束条件\"},{\"name\":\"PatAmt\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_bill_PatAmt\",\"isCreateDimension\":1,\"alias\":\"实付金额\",\"constraint\":\"约束条件\"},{\"name\":\"TotalAmt\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_TotalAmt\",\"isCreateDimension\":1,\"alias\":\"总金额\",\"constraint\":\"约束条件\"},{\"name\":\"DiscountAmt\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_DiscountAmt\",\"isCreateDimension\":1,\"alias\":\"折扣金额\",\"constraint\":\"约束条件\"},{\"name\":\"ShareFreeAmt\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_ShareFreeAmt\",\"isCreateDimension\":1,\"alias\":\"记账金额\",\"constraint\":\"约束条件\"},{\"name\":\"PatAmt\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_PatAmt\",\"isCreateDimension\":1,\"alias\":\"实付金额\",\"constraint\":\"约束条件\"},{\"name\":\"InsuAmt\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_InsuAmt\",\"isCreateDimension\":1,\"alias\":\"医保结算金额\",\"constraint\":\"约束条件\"},{\"name\":\"RoundAmt\",\"agg\":\"聚合函数\",\"expr\":\"表达式\",\"bizName\":\"ods_prhis_payment_RoundAmt\",\"isCreateDimension\":1,\"alias\":\"舍入金额\",\"constraint\":\"约束条件\"}]}}";

        ModelReq modelReq = JSON.parseObject(json, ModelReq.class);

        Assert.assertNotNull(modelReq);

    }

    @Test
    void queryModelTest() {
        List<ModelResp> modelResps = modelService.getModelListWithAuth(User.getFakeUser(), 10L, AuthType.ADMIN);
        Assert.assertTrue(modelResps.size()>0);

        modelService.calcMetricCount(modelResps);
        Map<String,Long> metricsCount = modelResps.get(0).getMetricsCount();
        Assert.assertNotNull(metricsCount);

    }
}
