import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.business.order.dto.ordermanage.OrderOutInfoDTO;
import com.gic.business.order.qdto.ordermanage.OrderInfoQDTO;
import com.gic.business.order.service.ordermanage.OrderInfoOutApiService;
import com.gic.enterprise.api.constant.EnterpriseServiceEnum;
import com.gic.enterprise.api.dto.enterprise.EnterpriseUsingStatusDTO;
import com.gic.enterprise.api.enums.PlatformChannelEnum;
import com.gic.enterprise.api.service.EnterpriseUseForbidService;
import com.gic.haoban.manage.api.dto.chat.ChatOwnerTotalDTO;
import com.gic.haoban.manage.api.dto.qdto.chat.GroupChatPlanSearchQDTO;
import com.gic.haoban.manage.api.dto.qdto.content.InteractRecordQDTO;
import com.gic.haoban.manage.api.enums.content.InteractRecordInfoDTO;
import com.gic.haoban.manage.api.enums.content.MaterialInteractRecordEventType;
import com.gic.haoban.manage.api.enums.content.TriggerCustomerChannelType;
import com.gic.haoban.manage.api.service.content.InteractRecordApiService;
import com.gic.haoban.manage.service.dao.mapper.chat.GroupChatPlanOwnerLogMapper;
import com.gic.haoban.manage.service.pojo.bo.content.InteractRecordBO;
import com.gic.haoban.manage.service.pojo.bo.content.InteractRecordExtendInfoBO;
import com.gic.haoban.manage.service.pojo.bo.content.MemberAttrChangeBO;
import com.gic.haoban.manage.service.pojo.bo.content.message.InteractRecordMessageBO;
import com.gic.haoban.manage.service.pojo.qo.content.InteractRecordQO;
import com.gic.haoban.manage.service.service.content.InteractRecordService;
import com.gic.haoban.manage.service.service.content.message.InteractRecordMessageService;
import com.gic.order.api.dto.OrderDTO;
import com.gic.order.api.dto.req.AllChannelOrderSearchReq;
import com.gic.order.api.dto.req.MemberOrderOptional;
import com.gic.order.api.dto.req.OrderFindQDTO;
import com.gic.order.api.dto.resp.OrderInfoResp;
import com.gic.order.api.service.member.MemberOrderReadApiService;
import com.gic.orderecommerce.api.dto.EorderDTO;
import com.gic.orderecommerce.api.service.EcommerceOrderOutputApiService;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Author MUSI
 * @Date 2023/3/29 3:16 PM
 * @Description
 * @Version
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-conf.xml"})
public class InteractRecordTest {

    @Autowired
    private InteractRecordService interactRecordService;
    @Autowired
    private InteractRecordMessageService interactRecordMessageService;
    @Autowired
    InteractRecordApiService interactRecordApiService;
    @Autowired
    private EnterpriseUseForbidService enterpriseUseForbidService;
    @Autowired
    private OrderInfoOutApiService orderInfoOutApiService;
    @Autowired
    private EcommerceOrderOutputApiService ecommerceOrderOutputApiService;
    @Autowired
    private MemberOrderReadApiService memberOrderReadApiService;

    @Test
    public void test2222() {
        ServiceResponse<List<EnterpriseUsingStatusDTO>> code = enterpriseUseForbidService.getUseEnterpriseByCode(EnterpriseServiceEnum.CONTENT.getRightMenuCode(), eid);
        System.out.println(JSON.toJSONString(code));
    }


    String eid = "ff8080815dacd3a2015dacd3ef5c0000";
    String wxEid = "ca66a01b79474c40b3e7c7f93daf1a3b";
    String staffId = "e608b51b267e4943b87e222a343b4f25";
    String clerkId = "bcbfff6c8e2e40aaac795a99d0dde2aa";
    String memberId = "ff8080818147efc8018148d1759903c8";

    @Test
    public void queryInteractPageTest(){
        InteractRecordQDTO interactRecordQO = new InteractRecordQDTO();
        interactRecordQO.setClerkId(clerkId);
        interactRecordQO.setMemberId(memberId);
        ServiceResponse<Page<InteractRecordInfoDTO>> serviceResponse =
                interactRecordApiService.queryInteractRecordList(interactRecordQO);
        System.out.println(JSON.toJSONString(serviceResponse));
    }

    @Test
    public void dealRecordTest(){
        String businessId = "UUID.randomUUID().toString()";
        InteractRecordMessageBO message = new InteractRecordMessageBO();
        message.setClerkId(clerkId);
        message.setEnterpriseId(eid);
        message.setMemberId(memberId);
        message.setUnionId(memberId);
        message.setStoreId("ff8080818499a54801849ce85f9f003f");
        message.setMaterialId("123213");
        message.setChannelSource(TriggerCustomerChannelType.PYQ.getCode());
        message.setEventType(MaterialInteractRecordEventType.VISIT_PRODUCT.getCode());
        message.setGoodsId("fbc508e395f846ef9005852c420e1c23");
        message.setDurationTime(2000);
        message.setBusinessUUId(businessId);
        message.setLastAccessTime(new Date().getTime());
        interactRecordMessageService.dealRecord(message);
    }

    @Test
    public void test(){
        for (int i = 0; i < 10; i++) {
            System.out.println(MD5.create().digestHex("song"));
        }
    }

    @Autowired
    GroupChatPlanOwnerLogMapper groupChatPlanOwnerLogMapper;
    
    @Test
    public void caseWhenTest(){
        GroupChatPlanSearchQDTO search = new GroupChatPlanSearchQDTO();
        search.setWxEnterpriseId("f5f8c78e395e4ca1a493707316096097");
        List<ChatOwnerTotalDTO> chatOwnerTotalDTOS = groupChatPlanOwnerLogMapper.listOwnerLogPageForWxaTotal(search);
    }

    @Test
    public void mergeMember() {
        MemberAttrChangeBO memberAttrChangeBO = new MemberAttrChangeBO();
        memberAttrChangeBO.setMemberId(memberId);
        memberAttrChangeBO.setUnionId("orXl9tyHOI4qP1QxwVcA7A3sB7zg");
        memberAttrChangeBO.setOpt(MemberAttrChangeBO.MemberChangeOpt.MINI_APP_AUTHORIZED.getCode());
        interactRecordApiService.memberChangeMQ(JSON.toJSONString(memberAttrChangeBO));
    }

    @Test
    public void fixOrderChannelCode() throws Exception{
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        InteractRecordQO interactRecordQO = new InteractRecordQO();
        interactRecordQO.setEnterpriseId("ff8080817af2def7017b146da4d212c4");
        interactRecordQO.setPageNum(1);
        interactRecordQO.setPageSize(100000);
        interactRecordQO.setStartTime(new Date("2020/01/01"));
        interactRecordQO.setEndTime(new Date("2026/01/01"));
        interactRecordQO.setEventTypeList(Lists.newArrayList(MaterialInteractRecordEventType.ORDER.getCode()));
        Page<InteractRecordBO> interactRecordBOPage = interactRecordService.queryInteractRecordPage(interactRecordQO);
        List<InteractRecordBO> result = interactRecordBOPage.getResult();
        for (InteractRecordBO interactRecordBO : result) {
            executorService.execute(()->{
                InteractRecordExtendInfoBO extendInfo = interactRecordBO.getExtendInfo();
                if (extendInfo == null) {
                    return;
                }
                List<InteractRecordExtendInfoBO.ExtendOrderInfo> extendOrderInfos = extendInfo.getExtendOrderInfos();
                for (InteractRecordExtendInfoBO.ExtendOrderInfo extendOrderInfo : extendOrderInfos) {

                    OrderInfoQDTO orderInfoQDTO = new OrderInfoQDTO();
                    String enterpriseId = interactRecordBO.getEnterpriseId();
                    String orderNumber = extendOrderInfo.getOrderNumber();
                    String memberId = interactRecordBO.getMemberId();
                    orderInfoQDTO.setOrderNumber(orderNumber);
                    orderInfoQDTO.setEnterpriseId(enterpriseId);
                    String orderId = "";
                    String channelCode = "";
                    ServiceResponse<OrderOutInfoDTO> serviceResponse = orderInfoOutApiService.queryOrderInfo(orderInfoQDTO);
                    if (serviceResponse.getResult() == null) {
                        EorderDTO eorderDTO = ecommerceOrderOutputApiService.getEorderByMemberIdAndOrderNumber(memberId, PlatformChannelEnum.C_WEIMOB.getOrderChannelCodeOld(),orderNumber,enterpriseId);
                        if (eorderDTO == null) {
                            eorderDTO = ecommerceOrderOutputApiService.getEorderByMemberIdAndOrderNumber(memberId, PlatformChannelEnum.C_BRAND_MALL.getOrderChannelCodeOld(),orderNumber,enterpriseId);
                        }
                        if (eorderDTO == null) {
                            ServiceResponse<OrderDTO> ipos = memberOrderReadApiService.queryByChannelOrderId(enterpriseId, memberId, "weimob", orderNumber);
                            OrderDTO result1 = ipos.getResult();
                            if (result1 != null) {
                                orderId = result1.getOrderId();
                                channelCode = PlatformChannelEnum.C_POS.getChannelCode();
                            }
                        }else {
                            orderId = eorderDTO.getEorderId();
                            channelCode = PlatformChannelEnum.getEnumByChannelCodeOrder(eorderDTO.getChannelCode()).getChannelCode();
                        }
                    }else {
                        orderId = serviceResponse.getResult().getOrderId();
                        channelCode = PlatformChannelEnum.C_WECHAT_MINI.getChannelCode();
                    }
                    if (StringUtils.isAnyBlank(orderId, channelCode)) {
                        continue;
                    }
                    extendOrderInfo.setOrderId(orderId);
                    extendOrderInfo.setChannelCode(channelCode);
                }
                extendInfo.setExtendOrderInfos(extendOrderInfos);
                InteractRecordBO update = new InteractRecordBO();
                update.setId(interactRecordBO.getId());
                update.setExtendInfo(extendInfo);
                interactRecordService.saveOrUpdateInteractRecord(update);
            });
        }
        executorService.awaitTermination(10, TimeUnit.HOURS);
    }
}
