//package com.gree.mall.manager.logic.marketing;
//
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
//import org.apache.commons.lang3.StringUtils;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.google.gson.JsonArray;
//import com.google.gson.JsonObject;
//import com.gree.mall.manager.bean.admin.AdminUserCom;
//import com.gree.mall.manager.bean.marketing.DynamicMaterialsDetailsBean;
//import com.gree.mall.manager.bean.marketing.DynamicMaterialsPageBean;
//import com.gree.mall.manager.bean.marketing.DynamicMaterialsVisitStatisticsBean;
//import com.gree.mall.manager.commonmapper.MarketingMaterialsVisitCustomMapper;
//import com.gree.mall.manager.config.wx.WxConfiguration;
//import com.gree.mall.manager.enums.MarketingMaterialsTypeEnum;
//import com.gree.mall.manager.exception.RemoteServiceException;
//import com.gree.mall.manager.logic.common.CommonLogic;
//import com.gree.mall.manager.logic.common.WorkWechatLogic;
//import com.gree.mall.manager.plus.entity.AdminCompanyWechat;
//import com.gree.mall.manager.plus.entity.MarketingMaterials;
//import com.gree.mall.manager.plus.entity.MarketingMaterialsVisit;
//import com.gree.mall.manager.plus.entity.Tag;
//import com.gree.mall.manager.plus.service.MarketingMaterialsService;
//import com.gree.mall.manager.plus.service.MarketingMaterialsVisitService;
//import com.gree.mall.manager.utils.WXPushUtils;
//import lombok.extern.slf4j.Slf4j;
//import me.chanjar.weixin.common.error.WxErrorException;
//import me.chanjar.weixin.cp.api.WxCpService;
//import me.chanjar.weixin.cp.bean.message.WxCpMessage;
//import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.servlet.http.HttpServletRequest;
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
///**
// * 动态素材
// */
//@Service
//@Slf4j
//public class DynamicMaterialsLogic {
//
//    @Autowired
//    private CommonLogic commonLogic;
//    @Autowired
//    private MarketingMaterialsService marketingMaterialsService;
//    @Autowired
//    private MarketingMaterialsVisitCustomMapper marketingMaterialsVisitCustomMapper;
//    @Autowired
//    private MarketingMaterialsVisitService marketingMaterialsVisitService;
//    @Autowired
//    private WorkWechatLogic workWechatLogic;
//
//    /**
//     * 分页查询动态素材
//     */
//    public IPage<DynamicMaterialsPageBean> page(HttpServletRequest request, String name, Integer pageNum, Integer pageSize) {
//
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        List<String> companyWechatIds = adminUser.getCompanyWechatIds();
//
//        IPage<MarketingMaterials> page = marketingMaterialsService.lambdaQuery()
//                .like(StringUtils.isNotBlank(name), MarketingMaterials::getTitle, name)
//                .eq(MarketingMaterials::getDynamic, true)
//                .eq(MarketingMaterials::getType, MarketingMaterialsTypeEnum.WEB.getCode())  //限制为网页
//                .in(CollectionUtils.isNotEmpty(companyWechatIds), MarketingMaterials::getCompanyWechatId, companyWechatIds)
//                .orderByDesc(MarketingMaterials::getCreateTime)
//                .page(new Page<>(pageNum, pageSize));
//
//        List<DynamicMaterialsPageBean> respList = new ArrayList<>();
//        for (MarketingMaterials marketingMaterials : page.getRecords()
//        ) {
//            DynamicMaterialsPageBean respBean = new DynamicMaterialsPageBean();
//            BeanUtils.copyProperties(marketingMaterials, respBean);
//            respList.add(respBean);
//
//            List<Tag> materialsTag = marketingMaterialsVisitCustomMapper.getMaterialsTag(respBean.getMarketingMaterialsId());
//            respBean.setTags(materialsTag);
//        }
//
//        Page<DynamicMaterialsPageBean> respPage = new Page<>();
//        BeanUtils.copyProperties(page, respPage);
//        respPage.setRecords(respList);
//        return respPage;
//    }
//
//
//    /**
//     * 动态素材详情
//     */
//    public DynamicMaterialsDetailsBean details(HttpServletRequest request, String materialsId, String name, String beginTime, String endTime) {
//        MarketingMaterials marketingMaterials = marketingMaterialsService.lambdaQuery()
//                .eq(MarketingMaterials::getMarketingMaterialsId, materialsId)
//                .eq(MarketingMaterials::getDynamic, true)
//                .one();
//        if (marketingMaterials == null) {
//            throw new RemoteServiceException("未找到该动态素材");
//        }
//
//        DynamicMaterialsDetailsBean respBean = new DynamicMaterialsDetailsBean();
//        BeanUtils.copyProperties(marketingMaterials, respBean);
//
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date now = new Date();
//        String nowStart = simpleDateFormat.format(now) + " 00:00:00";
//        String nowEnd = simpleDateFormat.format(now) + " 23:59:59";
//
//        //点击数
//        Integer totalCount = marketingMaterialsVisitService.lambdaQuery()
//                .eq(MarketingMaterialsVisit::getMarketingMaterialsId, materialsId)
//                .count();
//        Integer todayCount = marketingMaterialsVisitService.lambdaQuery()
//                .eq(MarketingMaterialsVisit::getMarketingMaterialsId, materialsId)
//                .between(MarketingMaterialsVisit::getCreateTime, nowStart, nowEnd)
//                .count();
//        respBean.setTotalCount(totalCount);
//        respBean.setTodayCount(todayCount);
//
//        //点击人数
//        Integer totalUserCount = marketingMaterialsVisitService.lambdaQuery()
//                .eq(MarketingMaterialsVisit::getMarketingMaterialsId, materialsId)
//                .groupBy(MarketingMaterialsVisit::getUserId)
//                .count();
//        Integer todayUserCount = marketingMaterialsVisitService.lambdaQuery()
//                .eq(MarketingMaterialsVisit::getMarketingMaterialsId, materialsId)
//                .between(MarketingMaterialsVisit::getCreateTime, nowStart, nowEnd)
//                .groupBy(MarketingMaterialsVisit::getUserId)
//                .count();
//        respBean.setTotalUserCount(totalUserCount);
//        respBean.setTodayUserCount(todayUserCount);
//
//        //用户点击统计
//        List<DynamicMaterialsVisitStatisticsBean> visitList
//                = marketingMaterialsVisitCustomMapper.getDynamicMaterialsVisitStatistics(materialsId, name, beginTime, endTime);
//
//        respBean.setVisitList(visitList);
//        return respBean;
//    }
//
//    public void sendToEnterpriseWechatWorker(HttpServletRequest request, String wechatUserId, String content) {
//        AdminCompanyWechat adminCompanyWechat = commonLogic.getAdminUser(request).getAdminCompanyWechat();
//        WxCpMessage wxCpMessage = new WxCpMessage();
//        wxCpMessage.setToUser(wechatUserId);
//        wxCpMessage.setMsgType("text");
//        wxCpMessage.setContent(content);
//        wxCpMessage.setAgentId(adminCompanyWechat.getAgentId1());
//        WxCpService wxCpService = workWechatLogic.miniApp(adminCompanyWechat);
//        WXPushUtils.sendToEnterpriseWechatWorker(wxCpService,wxCpMessage);
//    }
//
//
//}
