package contact;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.haoban.manage.api.dto.contact.ContactConfigDTO;
import com.gic.haoban.manage.api.dto.contact.ContactFollowDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactFollowQDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactFollowSearchQDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactLogQDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactOrderQDTO;
import com.gic.haoban.manage.api.service.contact.ContactOrderApiService;
import com.gic.haoban.manage.service.pojo.bo.content.InteractRecordBO;
import com.gic.haoban.manage.service.pojo.bo.potential.MemberLastInteractBO;
import com.gic.haoban.manage.service.service.contact.ContactConfigService;
import com.gic.haoban.manage.service.service.contact.ContactFollowService;
import com.gic.haoban.manage.service.service.contact.ContactLogService;
import com.gic.haoban.manage.service.service.contact.ContactOrderService;
import com.gic.haoban.manage.service.service.content.InteractRecordService;
import com.gic.order.api.dto.resp.OrderInfoResp;
import com.google.common.collect.Lists;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-conf.xml"})
public class ContactTest {

    @Autowired
    private ContactFollowService contactFollowService;
    @Autowired
    private ContactLogService contactLogService;
    @Autowired
    private ContactConfigService contactConfigService;
    @Autowired
    private ContactOrderApiService contactOrderApiService;
    @Autowired
    private InteractRecordService interactRecordService;

    String eid = "ff8080815dacd3a2015dacd3ef5c0000";
    String clerkId = "3c01cef9159f4599abf600db1f9583ef";
    String memberId = "ff8080818215878101821949a845003c";
    String storeId = "ff8080816a11a716016a23d2f5a5000d";

    @Test
    public void saveBatchFollow() throws Exception {
        List<ContactFollowQDTO> list = new ArrayList<>();
        ContactFollowQDTO followQDTO = new ContactFollowQDTO();
        followQDTO.setMemberId(memberId);
        followQDTO.setClerkId(clerkId);
        followQDTO.setFollowRemark("大逼斗向客户发送了2款喜好推荐商品");
        followQDTO.setGoodsInfoList(Lists.newArrayList("8a80908c92dc2d690192dc408b6c0026","8a80908c943bcebb01944e1699c20810"));
        followQDTO.setFollowTime(new Date());
        followQDTO.setEnterpriseId(eid);
        list.add(followQDTO);
        followQDTO = new ContactFollowQDTO();
        followQDTO.setMemberId(memberId);
        followQDTO.setClerkId(clerkId);
        followQDTO.setFollowRemark("大逼斗标记了建联。备注:啦啦啦");
        followQDTO.setFollowMaterialList(Lists.newArrayList("https://jhdm-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-57dff778cdae48618d44b809b6ddbb45.jpg"));
        followQDTO.setFollowTime(new Date());
        followQDTO.setEnterpriseId(eid);
        list.add(followQDTO);
        contactFollowService.saveBatchFollow(list);
    }

    @Test
    public void pageFollow() throws Exception {
        ContactFollowSearchQDTO searchQDTO = new ContactFollowSearchQDTO();
        searchQDTO.setMemberId(memberId);
        searchQDTO.setClerkId(clerkId);
        Page<ContactFollowDTO> page = contactFollowService.pageFollow(eid, searchQDTO, new BasePageInfo());
        System.out.println(JSON.toJSONString(page));
    }

    @Test
    public void mapMemberLastFollow() throws Exception {
        List<String> memberIds = Lists.newArrayList(memberId);
        Map<String, ContactFollowDTO> map = contactFollowService.mapMemberLastFollow(eid, clerkId, memberIds);
        System.out.println(JSON.toJSONString(map));
    }

    @Test
    public void saveContactLog() throws Exception {
        ContactLogQDTO contactLogQDTO = new ContactLogQDTO();
        contactLogQDTO.setMemberId(memberId);
        contactLogQDTO.setClerkId(clerkId);
        contactLogQDTO.setStoreId(storeId);
        contactLogQDTO.setEnterpriseId(eid);
        contactLogService.saveContactLog(contactLogQDTO);
    }

    @Test
    public void clearContactLog() throws Exception {
        contactLogService.clearContactLog(memberId, 1,"");
    }

    @Test
    public void mapInteractRecord() throws Exception {
        List<MemberLastInteractBO> list = new ArrayList<>();
        MemberLastInteractBO memberLastInteractBO = new MemberLastInteractBO();
        memberLastInteractBO.setMemberId(memberId);
        memberLastInteractBO.setLastAccessTime(new Date("2024/01/01 00:00:00"));
        list.add(memberLastInteractBO);
        List<InteractRecordBO> res = interactRecordService.lastInteractRecord(eid, "26efaa8144e14f60b2f4d48ffe9f708c", list);
        System.out.println(JSON.toJSONString(res));
    }

    @Test
    public void queryMemberIdsByTime() throws Exception {
        List<MemberLastInteractBO> list = interactRecordService.queryMemberIdsByTime(
                "26efaa8144e14f60b2f4d48ffe9f708c",
                DateUtil.date().offset(DateField.DAY_OF_MONTH, -14));
        System.out.println(JSON.toJSONString(list));
    }

    @Test
    public void saveContactOrder() throws Exception {
        ContactOrderQDTO orderQDTO = new ContactOrderQDTO();
        orderQDTO.setMemberId("ff80808194fdf81b0195035d9d6d006d");
        orderQDTO.setEnterpriseId("ff8080815dacd3a2015dacd3ef5c0000");
        orderQDTO.setOrderId("ff80808195db691a0195fa33dc0401a8");
        orderQDTO.setOnlineOrder(true);
        contactOrderApiService.saveContactOrder(orderQDTO);
    }

    @Test
    public void saveContactConfig() throws Exception {
        ContactConfigDTO contactConfigDTO = new ContactConfigDTO();
        contactConfigDTO.setRemarkFlag(1);
        contactConfigDTO.setEnterpriseId(eid);
        contactConfigDTO.setContactAutoList(Lists.newArrayList(10,20,30));
        contactConfigDTO.setRemarkContentList(Lists.newArrayList("微信邀约了客户","电话邀约了客户"));
        contactConfigService.saveContactConfig(contactConfigDTO);

        ContactConfigDTO contactConfig = contactConfigService.getContactConfig(eid);
        System.out.println(JSON.toJSONString(contactConfig));
    }
}
