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

import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.service.pojo.bo.NoticeMessageBO;
import com.gic.haoban.manage.service.pojo.bo.content.PotentialCustomerStaticsBO;
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.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

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

/**
 * @Author MUSI
 * @Date 2023/3/30 10:55 AM
 * @Description
 * @Version
 **/
@Slf4j
@Data
@Builder
public class PotentialCustomerNotifyContext {

    /**
     * 开始时间
     */
    private Date startTime;

    /**
     * 结束时间
     */
    private Date endTime;

    /**
     * 企业id
     */
    private String enterpriseId;

    /**
     * 企业下的导购id
     */
    private List<String> clerkIds;

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

    /**
     * 导购新销售线索统计
     */
    List<PotentialCustomerStaticsBO> customerStaticsBos;

    /**
     * 构建小程序消息通知
     * @return
     */
    public List<NoticeMessageBO> buildNoticeMessage() {
        if (CollectionUtils.isEmpty(this.customerStaticsBos)) {
            return Collections.emptyList();
        }
        if (CollectionUtils.isEmpty(this.staffClerkRelations)) {
            return Collections.emptyList();
        }
        Map<String, Integer> customerStaticsMap = this.customerStaticsBos.stream()
                .collect(Collectors.toMap(PotentialCustomerStaticsBO::getClerkId,
                        item -> Optional.ofNullable(item.getNum()).orElse(0),
                        (v1, v2) -> v1));
        return this.staffClerkRelations
                .stream()
                .filter(item -> customerStaticsMap.get(item.getClerkId()) > 0)
                .map(item -> {
                    NoticeMessageBO noticeMessageBO = new NoticeMessageBO();
                    noticeMessageBO.setCategoryType(POTENTIAL_CUSTOMER_NOTIFY.getCategory());
                    noticeMessageBO.setMessageType(POTENTIAL_CUSTOMER_NOTIFY.getType());
                    noticeMessageBO.setClerkId(item.getClerkId());
                    noticeMessageBO.setStoreId(StringUtils.isNotBlank(item.getStoreId()) ? item.getStoreId() : "-1");
                    noticeMessageBO.setTitle("销售线索通知");
                    noticeMessageBO.setTemplateCode(POTENTIAL_CUSTOMER_NOTIFY.getTemplateCode());
                    noticeMessageBO.setDescription(String.format("新增%s个客户的销售线索，请及时跟进", customerStaticsMap.get(item.getClerkId())));
                    noticeMessageBO.setEnterpriseId(this.enterpriseId);
                    noticeMessageBO.setRelationId(item.getClerkId());
                    noticeMessageBO.setMessageContent("");
                    return noticeMessageBO;
                })
                .collect(Collectors.toList());
    }
}
