import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.DateUtil;
import com.gic.haoban.manage.api.dto.ContentMaterialDTO;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
import com.gic.haoban.manage.api.dto.moment.QwMomentPlanDTO;
import com.gic.haoban.manage.api.qdto.moment.PlanClerkListQDTO;
import com.gic.haoban.manage.api.qdto.moment.PlanDataListQDTO;
import com.gic.haoban.manage.api.qdto.moment.QwMomentPlanQDTO;
import com.gic.haoban.manage.api.qdto.moment.SendResultQDTO;
import com.gic.haoban.manage.api.service.moment.QwMomentApiService;
import com.gic.haoban.manage.service.dao.mapper.StaffMapper;
import com.gic.haoban.manage.service.dao.mapper.moment.QwMomentPlanAttendMapper;
import com.gic.haoban.manage.service.dao.mapper.moment.QwMomentPlanMapper;
import com.gic.haoban.manage.service.entity.TabHaobanStaff;
import com.gic.haoban.manage.service.entity.moment.TabQwMomentPlan;
import com.gic.haoban.manage.service.entity.moment.TabQwMomentPlanAttend;
import com.gic.haoban.manage.service.service.WxEnterpriseService;
import com.gic.haoban.manage.service.service.chat.GroupChatPlanService;
import com.gic.haoban.manage.service.service.content.impl.GroupMessageServiceImpl;
import com.gic.haoban.manage.service.service.moment.QwMomentPlanService;
import com.gic.wechat.api.dto.qdto.group.QywxMomentInfoQDTO;
import com.gic.wechat.api.dto.qywx.group.QywxMomentRespDTO;
import com.gic.wechat.api.dto.qywx.moment.MomentResultDTO;
import com.gic.wechat.api.dto.qywx.response.QywxTransferCustomerDTO;
import com.gic.wechat.api.service.qywx.QywxExternalUserService;
import com.gic.wechat.api.service.qywx.QywxSuiteApiService;
import com.gic.wechat.api.service.qywx.QywxUserApiService;
import org.apache.commons.collections.CollectionUtils;
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.stream.Collectors;

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



    @Autowired
    private QwMomentApiService qwMomentApiService ;

    @Autowired
    private WxEnterpriseService wxEnterpriseService ;
    @Autowired
    private QywxExternalUserService qywxExternalUserService ;
    @Autowired
    private QwMomentPlanService qwMomentPlanService ;
    @Autowired
    private QwMomentPlanMapper qwMomentPlanMapper ;
    @Autowired
    private QwMomentPlanAttendMapper qwMomentPlanAttendMapper ;
    @Autowired
    private QywxUserApiService qywxUserApiService ;
    @Test
    public void jc() {
        String wxEnterpriseId = "d0a9e3cd351e4c26a526082a8c210f03";
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(wxEnterpriseId) ;
        String wxUserId = "JF10105";
        String takeoverUserId = "JF10884";
        List<String> list = new ArrayList<>();
        list.add("wmEalNBwAANkqRWoKYqbMhlIxwpvTV0g");
        QywxTransferCustomerDTO transferCustomerResp = qywxUserApiService.transferCustomer(qwDTO.getThirdCorpid(), qwDTO.getSelf3thSecret(), wxUserId, takeoverUserId, list,qwDTO.isSelf(),qwDTO.getUrlHost());
        System.out.println(JSONObject.toJSONString(transferCustomerResp));
    }


    @Test
    public void ok() {
        SendResultQDTO plan = new SendResultQDTO() ;
        plan.setWxEnterpriseId("6d52f722471343eeb7fbed87994cbdf8");
        plan.setClerkId("1664331543ab49769ad5ccfb6dc7986c");
        plan.setSendTime(DateUtil.strToDate("2025-05-13 18:31:00","yyyy-MM-dd HH:mm:ss"));
        plan.setStaffId("e35fe570efe44201beb3381f1e50f07e");
        plan.setPlanId(795415784309457024L);
        this.qwMomentApiService.finishTask(plan) ;
    }


    @Test
    public void sendToQw() {
        Long id = 792759787778540646L ;
        TabQwMomentPlan plan = this.qwMomentPlanMapper.getById(id) ;
     //   this.qwMomentPlanService.sendToQiwei(plan);
    }



    @Test
    public void page() {
        QwMomentPlanQDTO qdto = new QwMomentPlanQDTO();
        qdto.setEnterpriseId("123");
        qdto.setWxEnterpriseId("123");
        qdto.setExecType(1);
        qdto.setStatusFlag(1);
        qdto.setStartTime(new Date());
        qdto.setEndTime(new Date());
        qdto.setNameParams("123");
        qdto.setPageNum(1);
        qdto.setPageSize(2);
        this.qwMomentApiService.planList(qdto) ;
    }


    @Test
    public void dataPage() {
        PlanDataListQDTO qdto = new PlanDataListQDTO();
        qdto.setPlanId(1111l);
        qdto.setPageNum(1);
        qdto.setPageSize(2);
        qdto.setStaffName("111");
        qdto.setClerkParams("2222");
        qdto.setType(1);
        this.qwMomentApiService.dataList(qdto) ;
    }



    @Test
    public void clerkPage() {
        PlanClerkListQDTO qdto = new PlanClerkListQDTO();
        qdto.setPlanId(1111l);
        qdto.setPageNum(1);
        qdto.setPageSize(2);
        qdto.setStaffName("111");
        qdto.setClerkParams("2222");
        this.qwMomentApiService.attendList(qdto) ;
    }

    @Test
    public void getmomentid() {
        String wxEnterpriseId = "d4210ec4393d4be9b75b79f7e4db4287";
        String jobId = "PpJSQQbEj2idWBfdPk0s8qL4RZiXOcFryhsozrorxtf_S0iGF2a2bCN0LzSBj8er";
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(wxEnterpriseId);
        ServiceResponse<Object> resp2 = this.qywxExternalUserService.getMomentTaskResult(qwDTO.getThirdCorpid(),
                qwDTO.getSelf3thSecret(), jobId, qwDTO.isSelf(),qwDTO.getUrlHost()) ;
        System.out.println(JSONObject.toJSONString(resp2));
    }


    @Test
    public void data() {
        Long planId = 801500314782997092L ;
        //this.qwMomentPlanService.getQwPublishData(planId,null);
        this.qwMomentPlanService.getQwData(planId) ;
    }


    @Autowired
    private StaffMapper staffMapper ;
    @Autowired
    private QywxSuiteApiService qywxSuiteApiService ;
    @Test
    public void finish() {
        /*SendResultQDTO q = new SendResultQDTO() ;
        q.setClerkId("2dd543ad1f2e4feab2e5a5c767197858");
        q.setWxEnterpriseId("ca66a01b79474c40b3e7c7f93daf1a3b");
        q.setStaffId("2c0d179610a04016bf8971ac0f158442");
        q.setPlanId(772845323461615680L);
        q.setSendTime(new Date(1741760727397l));
        this.qwMomentApiService.finishTask(q) ;*/
        String staffId = "92a1edc7147e411c9b13ba9e815a6efb";
        Date sendTime = DateUtil.strToDate("2025-03-24 13:47:29","yyyy-MM-dd HH:mm:ss") ;
        Date startTime =  DateUtil.addNumForSecond(sendTime,-10) ;
        Date endTime = DateUtil.addNumForMinute(sendTime,1) ;
        TabHaobanStaff staff =  this.staffMapper.selectByPrimaryKey(staffId) ;
        String wxEnterpriseId = staff.getWxEnterpriseId() ;
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(wxEnterpriseId) ;
        String wxUserId = staff.getWxUserId() ;
        if(qwDTO.needOpenUserId3th()) {
            wxUserId = staff.getWxOpenUseId() ;
        }
        QywxMomentInfoQDTO qywxMomentInfoQDTO = new QywxMomentInfoQDTO();
        qywxMomentInfoQDTO.setCorpid(qwDTO.getThirdCorpid());
        qywxMomentInfoQDTO.setSuiteid(qwDTO.getSelf3thSecret());
        qywxMomentInfoQDTO.setStartTime(startTime.getTime() / 1000);
        qywxMomentInfoQDTO.setEndTime(endTime.getTime() / 1000);
        qywxMomentInfoQDTO.setCreatorId(wxUserId);
        qywxMomentInfoQDTO.setPageSize(1);
        ServiceResponse<QywxMomentRespDTO> serviceResponse = qywxSuiteApiService.queryQywxMomentList(qywxMomentInfoQDTO,qwDTO.isSelf(),qwDTO.getUrlHost());
        System.out.println(JSONObject.toJSONString(serviceResponse));

    }

}
