package com.flossom.system.service.impl;

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

import com.flossom.common.core.domain.entity.WxScriptTag;
import com.flossom.common.core.domain.entity.WxScriptTemplate;
import com.flossom.common.core.domain.entity.WxUserScriptLog;
import com.flossom.common.core.domain.req.WxUserScriptReq;
import com.flossom.common.core.enums.IsScriptTemplateEnum;
import com.flossom.common.core.enums.Status;
import com.flossom.common.core.enums.TagTypeStatusEnum;
import com.flossom.common.core.enums.WxUserIntegralMessageTypeEnum;
import com.flossom.common.core.mapper.*;
import com.flossom.common.core.utils.DateUtils;
import com.flossom.common.security.utils.SecurityUtils;
import com.flossom.system.service.IWxUserMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.flossom.system.service.IWxUserScriptLogService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发送话术记录Service业务层处理
 *
 * @author flossom
 * @date 2023-12-20
 */
@Service
public class WxUserScriptLogServiceImpl implements IWxUserScriptLogService {
    @Autowired
    private WxUserScriptLogMapper wxUserScriptLogMapper;

    @Autowired
    private IWxUserMemberService wxUserMemberService;

    @Autowired
    private WxScriptTemplateMapper wxScriptTemplateMapper;

    @Autowired
    private WxScriptTagMapper wxScriptTagMapper;

    /**
     * 查询发送话术记录
     *
     * @param id 发送话术记录主键
     * @return 发送话术记录
     */
    @Override
    public WxUserScriptLog selectWxUserScriptLogById(Long id) {
        return wxUserScriptLogMapper.selectWxUserScriptLogById(id);
    }

    /**
     * 查询发送话术记录列表
     *
     * @param wxUserScriptLog 发送话术记录
     * @return 发送话术记录
     */
    @Override
    public List<WxUserScriptLog> selectWxUserScriptLogList(WxUserScriptLog wxUserScriptLog) {
        return wxUserScriptLogMapper.selectWxUserScriptLogList(wxUserScriptLog);
    }

    /**
     * 新增发送话术记录
     *
     * @param wxUserScriptLog 发送话术记录
     * @return 结果
     */
    @Override
    public int insertWxUserScriptLog(WxUserScriptLog wxUserScriptLog) {
        wxUserScriptLog.setCreateTime(DateUtils.getNowDate());
        return wxUserScriptLogMapper.insertWxUserScriptLog(wxUserScriptLog);
    }

    /**
     * 修改发送话术记录
     *
     * @param wxUserScriptLog 发送话术记录
     * @return 结果
     */
    @Override
    public int updateWxUserScriptLog(WxUserScriptLog wxUserScriptLog) {
        return wxUserScriptLogMapper.updateWxUserScriptLog(wxUserScriptLog);
    }

    /**
     * 批量删除发送话术记录
     *
     * @param ids 需要删除的发送话术记录主键
     * @return 结果
     */
    @Override
    public int deleteWxUserScriptLogByIds(Long[] ids) {
        return wxUserScriptLogMapper.deleteWxUserScriptLogByIds(ids);
    }

    /**
     * 删除发送话术记录信息
     *
     * @param id 发送话术记录主键
     * @return 结果
     */
    @Override
    public int deleteWxUserScriptLogById(Long id) {
        return wxUserScriptLogMapper.deleteWxUserScriptLogById(id);
    }

    @Override
    @Transactional
    public void sendScript(WxUserScriptReq wxUserScriptReq) {
        List<Long> userIdList = wxUserScriptReq.getUserIdList();
        // 1、话术模板
        if (IsScriptTemplateEnum.SCRIPT_TEMPLATE.getCode() == wxUserScriptReq.getIsCustom()) {
            // 1.1、保存消息
            // 获取话术模板
            Integer scriptTemplateId = wxUserScriptReq.getScriptTemplateId();
            WxScriptTemplate wxScriptTemplate = wxScriptTemplateMapper.selectWxScriptTemplateById(scriptTemplateId.longValue());
            for (Long wxUserId : userIdList) {
                WxUserScriptLog wxUserScriptLog = new WxUserScriptLog();
                BeanUtils.copyProperties(wxScriptTemplate, wxUserScriptLog);

                wxUserScriptLog.setId(null);
                wxUserScriptLog.setWxUserId(wxUserId);
                wxUserScriptLog.setMessageType(WxUserIntegralMessageTypeEnum.MEMBER_MANAGEMENT.getCode());
                wxUserScriptLog.setIsCustom(IsScriptTemplateEnum.SCRIPT_TEMPLATE.getCode());
                wxUserScriptLog.setScriptTemplateId(scriptTemplateId);

                wxUserScriptLog.setStatus(Status.OK.getCode());
                wxUserScriptLog.setCreateBy(SecurityUtils.getUsername());
                wxUserScriptLog.setCreateTime(DateUtils.getNowDate());
                wxUserScriptLog.setUpdateBy(null);
                wxUserScriptLog.setUpdateTime(null);
                wxUserScriptLog.setRemark(null);
                wxUserScriptLogMapper.insertWxUserScriptLog(wxUserScriptLog);
            }

            //1.2、 会员添加标签
            WxScriptTag wxScriptTag = new WxScriptTag();
            wxScriptTag.setScriptId(wxScriptTemplate.getId());
            List<WxScriptTag> wxScriptTagList = wxScriptTagMapper.selectWxScriptTagList(wxScriptTag);
            if (wxScriptTagList != null && wxScriptTagList.size() > 0) {
                // 小程序标签集合
                List<Integer> miniProgramTagIdList = wxScriptTagList.stream()
                        .filter(scriptTag -> TagTypeStatusEnum.MINI_PROGRAM.getCode().equals(scriptTag.getTagType()))
                        .map(WxScriptTag::getTagId)
                        .map(Long::intValue)
                        .collect(Collectors.toList());
                // 外部标签集合
                List<Integer> wecomTagIdList = wxScriptTagList.stream()
                        .filter(scriptTag -> TagTypeStatusEnum.ENTERPRISE_WECHAT.getCode().equals(scriptTag.getTagType()))
                        .map(WxScriptTag::getTagId)
                        .map(Long::intValue)
                        .collect(Collectors.toList());
                // 用户id集合
                List<Integer> collect = userIdList.stream().map(Long::intValue).collect(Collectors.toList());
                wxUserMemberService.batchAddMiniProgramTag(miniProgramTagIdList, collect);
                wxUserMemberService.batchAddWecomTag(wecomTagIdList, collect);
            }
        }

        // 2、自定义话术
        if (IsScriptTemplateEnum.CUSTOM_SCRIPT.getCode() == wxUserScriptReq.getIsCustom()) {
            //2、1 保存消息
            for (Long wxUserId : userIdList) {
                WxUserScriptLog wxUserScriptLog = new WxUserScriptLog();
                BeanUtils.copyProperties(wxUserScriptReq, wxUserScriptLog);
                if (wxUserScriptReq.getTagIds() != null && wxUserScriptReq.getTagIds().size() > 0) {
                    wxUserScriptLog.setTagIds(wxUserScriptReq.getTagIds().stream().collect(Collectors.joining(",")));
                }
                wxUserScriptLog.setMessageType(WxUserIntegralMessageTypeEnum.MEMBER_MANAGEMENT.getCode());
                wxUserScriptLog.setWxUserId(wxUserId);
                wxUserScriptLog.setCreateBy(SecurityUtils.getUsername());
                wxUserScriptLog.setCreateTime(DateUtils.getNowDate());
                wxUserScriptLogMapper.insertWxUserScriptLog(wxUserScriptLog);
            }
            //2、2 会员添加标签
            if (wxUserScriptReq.getTagIds() != null && wxUserScriptReq.getTagIds().size() > 0) {
                List<Integer> tagList = wxUserScriptReq.getTagIds().stream().map(Integer::parseInt).collect(Collectors.toList());
                List<Integer> collect = userIdList.stream().map(Long::intValue).collect(Collectors.toList());
                wxUserMemberService.batchAddMiniProgramTag(tagList, collect);
            }
        }

    }
}
