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

import com.alibaba.fastjson.JSON;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
import com.gic.haoban.manage.api.enums.content.MaterialReportType;
import com.gic.haoban.manage.service.config.Config;
import com.gic.haoban.manage.service.entity.TabHaobanClerkMainStoreRelated;
import com.gic.haoban.manage.service.pojo.bo.content.context.MaterialReportContext;
import com.gic.haoban.manage.service.service.ClerkMainStoreRelatedService;
import com.gic.haoban.manage.service.service.StaffClerkRelationService;
import com.gic.haoban.manage.service.util.DrawImageUtils;
import com.gic.wechat.api.dto.qywx.QywxNewsArticleMessageDTO;
import com.gic.wechat.api.dto.qywx.QywxNewsSendMessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @Author MUSI
 * @Date 2023/3/24 4:11 PM
 * @Description
 * @Version 素材周报/月报组装
 **/

@Slf4j
@Component
public class MaterialReportBuilder {
    @Autowired
    private StaffClerkRelationService staffClerkRelationService;
    @Autowired
    private ClerkMainStoreRelatedService clerkMainStoreRelatedService;
    @Autowired
    private Config config;

    /**
     * 周报模板
     */
    private static final String week_template_report = "template/week_bkg.png";
    /**
     * 月报模板
     */
    private static final String month_template_report = "template/month_bkg.png";

    /**
     * 柱状成员绑定的导购信息
     *
     * @param context
     */
    public void buildStaffRelationClerk(MaterialReportContext context) {
        if (StringUtils.isAnyBlank(context.getStaffId(), context.getEnterpriseId())) {
            log.info("参数异常 staffId:{}, enterpriseId:{}", context.getStaffId(), context.getEnterpriseId());
            return;
        }

        List<StaffClerkRelationDTO> staffClerkRelations =
                staffClerkRelationService.listBindCodeByStaffId(Collections.singletonList(context.getEnterpriseId()), context.getStaffId());
        if (CollectionUtils.isEmpty(staffClerkRelations)) {
            log.info("成员未关联导购 staffId:{}, enterpriseId:{}", context.getStaffId(), context.getEnterpriseId());
            return;
        }
        context.setStaffClerkRelations(staffClerkRelations);
    }

    /**
     * 填充该成员的主门店
     * @param context
     */
    public void fillMainStore(MaterialReportContext context) {
        if (CollectionUtils.isEmpty(context.getStaffClerkRelations())) {
            return;
        }
        StaffClerkRelationDTO staffClerkRelationDTO = context.getStaffClerkRelations().get(0);
        String wxEnterpriseId = staffClerkRelationDTO.getWxEnterpriseId();
        context.setClerkId(staffClerkRelationDTO.getClerkId());
        TabHaobanClerkMainStoreRelated mainStoreInfo = clerkMainStoreRelatedService.getMainStoreByStaffId(context.getStaffId(), wxEnterpriseId);
        if (mainStoreInfo != null) {
            context.setMainStoreId(mainStoreInfo.getStoreId());
            Optional<StaffClerkRelationDTO> mainClerkInfo = context.getStaffClerkRelations()
                    .stream()
                    .filter(item -> StringUtils.equals(item.getStoreId(), mainStoreInfo.getStoreId()))
                    .findFirst();
            if (!mainClerkInfo.isPresent()){
                log.info("成员{}与主门店{}不存在导购关联关系", context.getStaffId(), context.getMainStoreId());
                return;
            }
            context.setClerkId(mainClerkInfo.get().getClerkId());
        }
    }

    /**
     * 周报/月报 数据
     * @param context
     */
    public void buildMaterialReportData(MaterialReportContext context) {
        // 获取该导购 在门店 的周报/月报数据
        String templatePath = "";
        if (MaterialReportType.WEEK.getCode().equals(context.getReportType())) {
            context.setUseMaterialNum(103);
            context.setConvertAmount("3.1万");
            templatePath = week_template_report;
        }else {
            context.setUseMaterialNum(1043);
            context.setConvertAmount("12.3万");
            templatePath = month_template_report;
        }

        // 生成图片地址
        String url = DrawImageUtils.drawImage(context.getFactoryCode(), context.getUseMaterialNum() + "", context.getConvertAmount(), templatePath);
        context.setMaterialReportUrl(url);
    }

    /***
     * 构建企业微信消息
     *
     * @param context
     */
    public QywxNewsSendMessageDTO buildQywxNewsMessage(MaterialReportContext context, WxEnterpriseQwDTO wxEnterpriseQwDTO) {
        if (context.getStaffInfo() == null) {
            log.info("构建企业微信对象, 成员信息不存在 {}", JSON.toJSONString(context));
            return null;
        }
        String wxUserId = context.getStaffInfo().getWxUserId();
        if (wxEnterpriseQwDTO.needOpenUserId3th()) {
            wxUserId = context.getStaffInfo().getWxOpenUseId();
        }
        QywxNewsSendMessageDTO newsSendMessage = new QywxNewsSendMessageDTO();
        newsSendMessage.setAgentId(Integer.parseInt(wxEnterpriseQwDTO.getAgentId()));
        newsSendMessage.setUserIds(Collections.singletonList(wxUserId));
        QywxNewsArticleMessageDTO articleInfo = new QywxNewsArticleMessageDTO();
        articleInfo.setAppid(config.getAppid());
        articleInfo.setTitle(context.getMessageTitle());
        articleInfo.setDescription(context.getMessageDesc());
        // pagePath 拼接参数?
        articleInfo.setPagepath("/monthList");
        articleInfo.setPicurl(context.getMaterialReportUrl());
        newsSendMessage.setArticleMessages(Collections.singletonList(articleInfo));
        return newsSendMessage;
    }
}
