package com.gic.haoban.manage.service.service.content.impl;

import com.alibaba.fastjson.JSON;
import com.gic.enterprise.api.dto.EnterpriseDTO;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
import com.gic.haoban.manage.service.config.Config;
import com.gic.haoban.manage.service.entity.TabHaobanStaff;
import com.gic.haoban.manage.service.pojo.bo.NoticeMessageBO;
import com.gic.haoban.manage.service.pojo.bo.content.context.MaterialReportContext;
import com.gic.haoban.manage.service.service.StaffService;
import com.gic.haoban.manage.service.service.WxEnterpriseService;
import com.gic.haoban.manage.service.service.content.MaterialReportService;
import com.gic.haoban.manage.service.service.content.adaptor.MaterialReportBuilder;
import com.gic.haoban.manage.service.service.notify.NoticeMessageService;
import com.gic.wechat.api.dto.qywx.QywxNewsSendMessageDTO;
import com.gic.wechat.api.service.qywx.QywxSuiteApiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author MUSI
 * @Date 2023/3/24 3:59 PM
 * @Description
 * @Version
 **/

@Slf4j
@Component
public class MaterialReportServiceImpl implements MaterialReportService {

    @Autowired
    private NoticeMessageService noticeMessageService;
    @Autowired
    private MaterialReportBuilder materialReportBuilder;
    @Autowired
    private QywxSuiteApiService qywxSuiteApiService;
    @Autowired
    private WxEnterpriseService wxEnterpriseService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private Config config;


    /**
     * 素材周报/月报
     *
     * @param enterpriseId
     */
    @Override
    public void handlerMaterialReport(String enterpriseId, String staffId, Integer reportType) {

        EnterpriseDTO enterpriseDTO = enterpriseService.getEnterpriseById(enterpriseId);
        if (enterpriseDTO == null) {
            log.info("企业信息不存在 enterpriseId:{}", enterpriseId);
            return;
        }
        TabHaobanStaff staffInfo = staffService.selectById(staffId);
        if (staffInfo == null) {
            log.info("成员不存在 {}", staffId);
            return;
        }
        MaterialReportContext context = MaterialReportContext.builder()
                .enterpriseId(enterpriseId)
                .factoryCode(enterpriseDTO.getFactoryCode())
                .staffId(staffId)
                .staffInfo(staffInfo)
                .reportType(reportType)
                .build();
        // 获取成员关联的导购
        materialReportBuilder.buildStaffRelationClerk(context);
        // 判断是区经还是 店长/导购 填充主门店
        materialReportBuilder.fillMainStore(context);
        // 获取素材数据
        materialReportBuilder.buildMaterialReportData(context);
        // 保存小程序消息
        List<NoticeMessageBO> noticeMessageBos = context.buildNotifyMessageBos();
        if (CollectionUtils.isNotEmpty(noticeMessageBos)) {
            noticeMessageService.addNoticeMessageBatch(noticeMessageBos);
        }
        // 发送企微消息
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(staffInfo.getWxEnterpriseId());
        QywxNewsSendMessageDTO newsSendMessageDTO = materialReportBuilder.buildQywxNewsMessage(context, qwDTO);
        if (newsSendMessageDTO == null) {
            return;
        }
        log.info("发送企业图文消息参数{}", JSON.toJSONString(newsSendMessageDTO));
        boolean result = qywxSuiteApiService.sendMessage(qwDTO.getThirdCorpid(), config.getWxSuiteid(), newsSendMessageDTO);
        log.info("成员{}发送素材企微使用报告结果 {}", staffId, result);
    }
}
