package com.gic.haoban.manage.service.service.commission;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.api.enums.NoticeMessageTypeEnum;
import com.gic.haoban.manage.api.util.notify.NoticeMessageUtil;
import com.gic.haoban.manage.api.dto.qdto.content.ClerkCashListQDTO;
import com.gic.haoban.manage.api.dto.qdto.content.ClerkCashNotifyQDTO;
import com.gic.haoban.manage.service.pojo.bo.commission.ClerkCommissionStaticsBO;
import com.gic.haoban.manage.service.service.StaffClerkRelationService;
import com.gic.haoban.manage.service.service.message.NoticeMessageHandler;
import com.gic.message.center.api.subscribe.model.NoticeMessageForm;
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.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author MUSI
 * @Date 2023/6/7 2:31 PM
 * @Description
 * @Version
 * 佣金服务内部服务
 **/
@Slf4j
@Component
public class CommissionNotifyInnerService {

    @Autowired
    private StaffClerkRelationService staffClerkRelationService;
    @Autowired
    private NoticeMessageHandler noticeMessageHandler;

    /**
     * 处理导购佣金
     * @param commissionStaticsBo
     */
    public void handlerCommissionNotify(ClerkCommissionStaticsBO commissionStaticsBo) {
        log.info("handlerCommissionNotify params:{}", JSONObject.toJSONString(commissionStaticsBo));
        String clerkId = commissionStaticsBo.getClerkId();
        StaffClerkRelationDTO staffClerkRelationDTO = staffClerkRelationService.getByClerkId(clerkId);
        if (staffClerkRelationDTO == null) {
            log.info("handlerCommissionNotify 处理导购佣金消息,导购不存在 {}", clerkId);
            return;
        }
        if (staffClerkRelationDTO.getClerkType() != null && staffClerkRelationDTO.getClerkType() == 2) {
            log.info("导购为区经角色 {}, 不发送消息", JSON.toJSONString(staffClerkRelationDTO));
            return;
        }
        String staffId = staffClerkRelationDTO.getStaffId();
        String uniqueKey = UUID.randomUUID().toString().replaceAll("-", "");
        Map<String, String> params = new HashMap<>();
        params.put("currentTab", "0");
        params.put("commissAmount", commissionStaticsBo.getCommissionAmount().toPlainString());
        params.put("memberNum", commissionStaticsBo.getMemberCount() + "");
        JSONObject innerParams = new JSONObject();
        innerParams.put("currentTab", "0");
        String pageUrl = NoticeMessageUtil.buildMiniAppUrl(NoticeMessageTypeEnum.CLERK_COMMISSION_NOTIFY, innerParams.toJSONString());
        params.put("page", pageUrl);
        params.put("pageParams", innerParams.toJSONString());
        NoticeMessageForm noticeMessageForm = NoticeMessageForm
                .builder()
                .businessId(clerkId)
                .messageCode(NoticeMessageTypeEnum.CLERK_COMMISSION_NOTIFY.getMessageCode())
                .mqRouterCode(NoticeMessageTypeEnum.CLERK_COMMISSION_NOTIFY.getRouterCode())
                .enterpriseId(commissionStaticsBo.getEnterpriseId())
                .uniqueKey(uniqueKey)
                .createTime(new Date())
                .userIdList(Collections.singletonList(clerkId))
                .variableMap(params)
                .build();
        noticeMessageForm.setStaffIdList(Collections.singletonList(staffId));
        noticeMessageHandler.sendMessage(noticeMessageForm);

    }

    /**
     * 处理提现消息
     * @param clerkCashNotifyQDTO
     */
    public void handlerCashNotify(ClerkCashNotifyQDTO clerkCashNotifyQDTO) {
        log.info("handlerCashNotify params:{}", JSONObject.toJSONString(clerkCashNotifyQDTO));
        List<ClerkCashListQDTO> cashList = clerkCashNotifyQDTO.getCashList();
        if (CollectionUtils.isEmpty(cashList)) {
            log.info("导购提现消息列表为空");
            return;
        }
        List<String> clerkIdList = cashList.stream().map(ClerkCashListQDTO::getClerkId).collect(Collectors.toList());
        List<StaffClerkRelationDTO> list = staffClerkRelationService.listByClerkIds(clerkIdList);
        if (CollectionUtils.isEmpty(list)) {
            log.info("handlerCashNotify 处理导购提现消息,导购不存在 {}", JSON.toJSONString(clerkIdList));
            return;
        }
        Map<String, BigDecimal> cashMap = cashList.stream().collect(Collectors.toMap(ClerkCashListQDTO::getClerkId, ClerkCashListQDTO::getApplyAmount));
        for (StaffClerkRelationDTO staffClerkRelationDTO : list) {
            String staffId = staffClerkRelationDTO.getStaffId();
            String clerkId = staffClerkRelationDTO.getClerkId();
            String uniqueKey = UUID.randomUUID().toString().replaceAll("-", "");
            Map<String, String> params = new HashMap<>();
            params.put("currentTab", "1");
            BigDecimal applyAmount = cashMap.get(clerkId);
            params.put("applyAmount", applyAmount.toPlainString());
            JSONObject innerParams = new JSONObject();
            innerParams.put("currentTab", "1");
            NoticeMessageTypeEnum cashTypeEnum = clerkCashNotifyQDTO.getCashTypeEnum();
            String pageUrl = NoticeMessageUtil.buildMiniAppUrl(cashTypeEnum, innerParams.toJSONString());
            params.put("page", pageUrl);
            params.put("pageParams", innerParams.toJSONString());
            NoticeMessageForm noticeMessageForm = NoticeMessageForm
                    .builder()
                    .businessId(clerkId)
                    .messageCode(cashTypeEnum.getMessageCode())
                    .mqRouterCode(cashTypeEnum.getRouterCode())
                    .enterpriseId(clerkCashNotifyQDTO.getEnterpriseId())
                    .uniqueKey(uniqueKey)
                    .createTime(new Date())
                    .userIdList(Collections.singletonList(clerkId))
                    .variableMap(params)
                    .build();
            noticeMessageForm.setStaffIdList(Collections.singletonList(staffId));
            noticeMessageHandler.sendMessage(noticeMessageForm,true);
        }
    }
}
