package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxAppinfo;
import com.iformall.domain.po.WxCouponPassword;
import com.iformall.domain.po.WxCouponPresent;
import com.iformall.domain.po.msg.WxMsgRecord;
import com.iformall.enums.*;
import com.iformall.mapper.WxCouponPasswordMapper;
import com.iformall.mapper.WxCouponPresentMapper;
import com.iformall.mq.MqBaseProducer;
import com.iformall.service.WxAppinfoService;
import com.iformall.service.WxCouponPresentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author gongbiao
 */
@Service
public class WxCouponPresentServiceImpl implements WxCouponPresentService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxCouponPresentMapper wxCouponPresentMapper;

    @Autowired
    WxCouponPasswordMapper wxCouponPasswordMapper;

    @Autowired
    WxAppinfoService wxAppinfoService;

    @Autowired
    private MqBaseProducer mqBaseProducer;

    @Override
    public PageInfo<WxCouponPresent> listAsPage(WxCouponPresent record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxCouponPresentMapper.findList(record));
    }

    @Override
    public WxCouponPresent findById(Long id) {
        return wxCouponPresentMapper.selectById(id);
    }

    @Async
    @Override
    public ResultData add(WxCouponPresent record, List<WxCouponPassword> list, Set<String> phoneSet) {
        String tenantId = record.getTenantId();

        //添加记录
        final IdWorker idWorker = IdWorker.get();
        long id = idWorker.nextId();
        record.setId(id);
        record.setSendAmount(phoneSet.size());
        wxCouponPresentMapper.insert(record);
        //小程序
        WxAppinfo appInfo = wxAppinfoService.getCAppInfo(tenantId);
        String title = record.getCouponName();
        String name = appInfo.getName();
        List<WxCouponPassword> tempList = new ArrayList<>();
        //循环发送
        int i = 0;
        for (String phone : phoneSet) {
            WxCouponPassword couponPassword = list.get(i);
            //发送
            sendMsg(tenantId, phone, title, name, couponPassword.getPassword(), couponPassword.getId().toString());
            //记录发送数据便于更新状态
            couponPassword.setPresentId(id);
            couponPassword.setSendedPhone(phone);
            tempList.add(couponPassword);
            i++;
        }
        //修改状态
        wxCouponPasswordMapper.updateStatus(tempList);
        return new ResultData();
    }

    private void sendMsg(String tenantId, String phone, String title, String appName, String password, String id) {
        logger.info("》》》》》》》》》》》");
        logger.info("系统发卡发送短信开始");
        logger.info("》》》》》》》》》》》");
        Map<String, String> msgReplaceMap = new HashMap();
        msgReplaceMap.put("id", id);
        msgReplaceMap.put("app", appName);
        msgReplaceMap.put("title", title);
        msgReplaceMap.put("pw", password);
        WxMsgRecord wxMsgRecord = new WxMsgRecord();
        wxMsgRecord.setMsgType(EnumMsgRecordType.SMS.getCode());
        wxMsgRecord.setModelType(EnumMsgModel.COUPON_CARD_SEND.getCode());
        wxMsgRecord.setReceiver(phone);
        wxMsgRecord.setTenantId(tenantId);
        wxMsgRecord.setDynamicContentMap(msgReplaceMap);
        mqBaseProducer.sendMessage(wxMsgRecord, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
        logger.info("》》》》》》》》》》》");
        logger.info("系统发卡发送短信结束");
        logger.info("》》》》》》》》》》》");
    }
}
