package com.gic.haoban.manage.service.pojo.bo.content.context;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.api.enums.content.MaterialReportType;
import com.gic.haoban.manage.service.entity.TabHaobanStaff;
import com.gic.haoban.manage.service.pojo.bo.NoticeMessageBO;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.gic.haoban.manage.api.enums.NoticeMessageTypeEnum.MATERIAL_REPORT_NOTIFY;

/**
 * @Author MUSI
 * @Date 2023/3/24 4:08 PM
 * @Description
 * @Version 好办 周报/月报 上下文
 **/
@Slf4j
@Data
@Builder
public class MaterialReportContext {

    private static final String week_title = "素材使用周报";
    private static final String month_title = "素材使用月报";

    private static final String week_desc = "%s～%s的素材使用周报已生成,请查收";
    private static final String month_desc = "%s～%s的素材使用月报已生成,请查收";

    private String enterpriseId;

    /**
     * 素材周期
     *
     * @see com.gic.haoban.manage.api.enums.content.MaterialReportType
     */
    private Integer reportType;

    /**
     * 成员id
     */
    private String staffId;

    /**
     * 成员对象
     */
    private TabHaobanStaff staffInfo;

    /**
     * 成员(店长或导购角色)在主门店时的导购id
     * 区经时就是导购id
     */
    private String clerkId;

    /**
     * 主门店
     */
    private String mainStoreId;

    /**
     * 使用素材数量
     */
    private Integer useMaterialNum;

    /**
     * 转化金额
     */
    private String convertAmount;

    /**
     * 企业factoryCode
     */
    private String factoryCode;

    /**
     * 导购关联表
     */
    private List<StaffClerkRelationDTO> staffClerkRelations;


    /**
     * 素材报告 图片地址
     */
    private String materialReportUrl;

    /**
     * 构建好办小程序消息
     *
     * @return
     */
    public List<NoticeMessageBO> buildNotifyMessageBos() {
        if (CollectionUtils.isEmpty(this.staffClerkRelations)) {
            log.info("成员未绑定导购, staffId: {}", this.staffId);
            return Collections.emptyList();
        }
        if (StringUtils.isBlank(this.mainStoreId)) {
            log.info("成员 {} 没有主门店数据, 区经角色不发送小程序消息通知 ", this.staffId);
            return Collections.emptyList();
        }
        return this.staffClerkRelations
                .stream()
                .map(item -> {
                    NoticeMessageBO noticeMessageBO = new NoticeMessageBO();
                    noticeMessageBO.setCategoryType(MATERIAL_REPORT_NOTIFY.getCategory());
                    noticeMessageBO.setMessageType(MATERIAL_REPORT_NOTIFY.getType());
                    noticeMessageBO.setClerkId(StringUtils.isNotBlank(item.getClerkId()) ? item.getClerkId() : "-1");
                    noticeMessageBO.setStoreId(StringUtils.isNotBlank(item.getStoreId()) ? item.getStoreId() : "-1");
                    noticeMessageBO.setTitle(this.getMessageTitle());
                    noticeMessageBO.setTemplateCode(MATERIAL_REPORT_NOTIFY.getTemplateCode());
                    noticeMessageBO.setDescription(this.getMessageDesc());
                    noticeMessageBO.setEnterpriseId(this.enterpriseId);
                    noticeMessageBO.setRelationId(this.clerkId);
                    noticeMessageBO.setMessageContent(this.extendParams(item.getStoreId()));
                    return noticeMessageBO;
                })
                .collect(Collectors.toList());
    }

    public String getMessageTitle() {
        if (MaterialReportType.WEEK.getCode().equals(this.reportType)) {
            return week_title;
        }
        return month_title;
    }

    public String getMessageDesc() {
        if (MaterialReportType.WEEK.getCode().equals(this.reportType)) {
            // 获取周报信息
            DateTime lastWeek = DateUtil.lastWeek();
            String weekBeginStr = DateUtil.beginOfWeek(lastWeek)
                    .toString("MM月dd日");
            String weekEndStr = DateUtil.endOfWeek(lastWeek)
                    .toString("MM月dd日");
            return String.format(week_desc, weekBeginStr, weekEndStr);
        }

        DateTime lastMonth = DateUtil.lastMonth();
        String monthBeginStr = DateUtil.beginOfMonth(lastMonth)
                .toString("MM月dd日");
        String monthEndStr = DateUtil.endOfMonth(lastMonth)
                .toString("MM月dd日");
        return String.format(month_desc, monthBeginStr, monthEndStr);
    }

    public String extendParams(String storeId) {
        JSONObject params = new JSONObject();
//        params.put("clerkId", this.clerkId);
        // 此处页面链接 最长128个字节
        params.put("s", storeId);
        params.put("t", this.reportType);
        return params.toJSONString();
    }


}
