package com.iformall.controller.msg;

import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.msg.*;
import com.iformall.enums.*;
import com.iformall.mapper.WxMsgMapper;
import com.iformall.mq.MqBaseProducer;
import com.iformall.service.WxMsgRecordService;
import com.iformall.service.msg.impl.SendCallBackSmsServiceImpl;
import com.iformall.utils.JsonUtil;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@Api(description = "消息记录")
@RequestMapping(value = "msgrecord")
public class WxMsgRecordController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMsgRecordService wxMsgRecordService;
    @Autowired
    private MqBaseProducer mqBaseProducer;
    @Autowired
    private SendCallBackSmsServiceImpl msgSendService;


    @GetMapping(value = "/test")
    public ResultData test(Long id) throws Exception{
//        String systemTime = DateUtils.getSystemTime("yyyy-MM-dd HH:00:00");
//        WxMsg wxMsg = new WxMsg();
//        wxMsg.setIsright(0);
//        wxMsg.setSendtime(systemTime);
//        wxMsg.setId(id);
//        List<WxMsg> list = wxMsgMapper.findList(wxMsg);
//        logger.info("将要发送的短信列表:" + JSONArray.toJSONString(list));
//        for (WxMsg msg : list) {
//            //sendmsg(msg);
//            msg.setMsgType(EnumMsgRecordType.SMS_CALLBACK.getCode());
//            msg.setReceiver(msg.getPhones());
//            mqBaseProducer.sendMessage(msg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(),null);
//        }

        String s = "{\"msg\": \"新手妈妈不要怕，金宝贝帮您带孩子，音乐美术还有游戏，孩子玩着玩着就长大了\", \"way\": 2, \"name\": \"亲子活动\", \"uuid\": \"0a4ae632-9522-4a8f-bb0d-b22620ac79c3\", \"label\": \"\", \"domain\": 0, \"phones\": \"18601973448\", \"status\": 1, \"isright\": 1, \"modelId\": 204500584231862272, \"msgType\": 2, \"receiver\": \"18601973448\", \"sendtime\": \"2019-04-17 18:29:08\", \"tenantId\": \"456\", \"signature\": \"富茂科技\", \"errorNumber\": 0, \"wxMsgConfig\": {\"id\": 1, \"bid\": \"465565\", \"appid\": \"wx9ff823abeef23b94\", \"total\": 100000, \"secret\": \"7305150347587283553aa8898e7dbf20\", \"account\": \"15626593768\", \"remains\": 62667, \"recharge\": 0, \"tenantId\": \"456\", \"notifyurl\": \"http://202.165.179.86:9000/wxMsgCallback/receivemsg/456\", \"publickey\": \"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAvh8j/zagfxQdnSh5OIic\\r\\nMzN+MuRuWQJPjgu4Gza4+gX3j5Ln2xNDBOTjpwyuLBjh/JcBd1cGO3lAaKCwcaix\\r\\nsmhTq56wVXXUMgDiAChu4ud8FSvRc8G8tdZAirKVAIi3NW+/pYgpWBs/0wnF8hz4\\r\\n8no4pyJHl9Jc1LH3VNIMz8vqzKUPc4ack4pFUXlcNj6C+sBlaurmI4/vwLqNxBGs\\r\\n7/zyM7dv6oy3DSU/Y1qBArM1YPjfL2dNun8rmtPgJvlPwXqA7uoHPwQ2Ym3aUn59\\r\\npkS7QI6IE8uuqNkfSte8BXLd2nIqPLFxLYLDmdll7eoyRblHcHqAYSj8stK6StC7\\r\\nDNryNKEjTEwbgf9trUI0uvF1pfgTy2gpclnY69FtD/m0+FvLyorMq+nmBqYMjka5\\r\\nK0txDQJPOa7gsi//uXd/cJW2SAXY9MSO1AfMi8Xq/YKRQzN9FW5iapskXFHca7uX\\r\\ng5NhH7flr6DW+QInFlpoN6WIEAuDF1aj4O49Ikm3WxwhTqnvEkdSCfivpYQkp9Sh\\r\\n4kQ/SQdxuT7VX+Nz6k+uMx2z4cySk33bHi0KoHbA9QFGg/54Qd0+eU4qZnd4mrgh\\r\\nhH7/QQhL7Z9eF1U5UPrsHq2Vq3rEnN+tYQ26AuKeU8vzTxBrC/SxC6C/SMFt3f/Y\\r\\nnuFh1UnNJZleZwyQt+ZdGO0CAwEAAQ==\", \"modelnotifyurl\": \"http://202.165.179.86:9000/wxMsgCallback/receivemodel/456\", \"reminderstatus\": 0, \"verifynotifyurl\": \"http://202.165.179.86:9000/wxMsgCallback/receiveverifymodel/456\"}, \"couponInject\": {\"id\": 278440211602472960, \"name\": \"END\", \"tags\": \"[{\\\"name\\\":\\\"一天以上+普通会员+男\\\",\\\"tagList\\\":[{\\\"id\\\":1011,\\\"typeId\\\":50},{\\\"id\\\":0,\\\"typeId\\\":51},{\\\"id\\\":1}]},{\\\"name\\\":\\\"一月以上+男\\\",\\\"tagList\\\":[{\\\"id\\\":1009,\\\"typeId\\\":50},{\\\"id\\\":1}]}]\", \"status\": 2, \"mUserId\": 204398756307664896, \"modelId\": 204500584231862272, \"muserId\": 204398756307664896, \"couponId\": 275856462247362560, \"sendTime\": \"2019-04-17 18:28:49\", \"sendType\": 0, \"tenantId\": \"456\", \"couponName\": \"礼品券大礼5\", \"filterList\": [{\"name\": \"一天以上+普通会员+男\", \"tagList\": [{\"id\": 1011, \"typeId\": 50}, {\"id\": 0, \"typeId\": 51}, {\"id\": 1}]}, {\"name\": \"一月以上+男\", \"tagList\": [{\"id\": 1009, \"typeId\": 50}, {\"id\": 1}]}], \"sendAmount\": 40}, \"successNumber\": 0, \"delayTimeLevel\": 0, \"expectSendNumber\": 40}";
        WxMsg wxMsg = (WxMsg) JsonUtil.readValue(s,WxMsg.class);
        wxMsg.setMsgType(EnumMsgRecordType.SMS_CALLBACK.getCode());
        wxMsg.setReceiver("18601973448");
        wxMsg.setTenantId("456");
        msgSendService.send(wxMsg);
        return new ResultData();
    }

    /**
     * 重新发送失败的消息
     * @param x
     * @return
     */
    @GetMapping(value = "/resendMsg")
    public ResultData resendMsg(String x, Integer msgType) {
        logger.debug("[" + getIpAddr() + "] WxMsgRecordController::resendMsg");
        if("xll1dxx1314".equals(x)){
            WxMsgRecord msgRecord = new WxMsgRecord();
            msgRecord.setMsgType(msgType);
            msgRecord.setTenantId(getTenantId());
            msgRecord.setMsgStatus(EnumMsgRecordStatus.CONSUME_FAIL.getCode());
            List<WxMsgRecord> recordList = wxMsgRecordService.findList(msgRecord);
            for (WxMsgRecord wxMsgRecord:recordList) {
                if(EnumMsgRecordType.SMS.getCode().equals(wxMsgRecord.getMsgType())){
                    //短信
                    mqBaseProducer.sendMessage(wxMsgRecord, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }else if(EnumMsgRecordType.SMS_CALLBACK.getCode().equals(wxMsgRecord.getMsgType())){
                    //业务短信
                    WxMsg wxMsg = (WxMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),WxMsg.class);
                    wxMsg.setMsgType(wxMsgRecord.getMsgType());
                    wxMsg.setReceiver(wxMsgRecord.getReceiver());
                    wxMsg.setTenantId(wxMsgRecord.getTenantId());
                    mqBaseProducer.sendMessage(wxMsg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }else if(EnumMsgRecordType.EMAIL.getCode().equals(wxMsgRecord.getMsgType())){
                    //邮件
                    MailMsg wxMsg = (MailMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),MailMsg.class);
                    wxMsg.setMsgType(wxMsgRecord.getMsgType());
                    wxMsg.setReceiver(wxMsgRecord.getReceiver());
                    wxMsg.setTenantId(wxMsgRecord.getTenantId());
                    mqBaseProducer.sendMessage(wxMsg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }else if(EnumMsgRecordType.SMART_APP.getCode().equals(wxMsgRecord.getMsgType())){
                    //微信小程序
                    SmartAppMsg wxMsg = (SmartAppMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),SmartAppMsg.class);
                    wxMsg.setMsgType(wxMsgRecord.getMsgType());
                    wxMsg.setReceiver(wxMsgRecord.getReceiver());
                    wxMsg.setTenantId(wxMsgRecord.getTenantId());
                    mqBaseProducer.sendMessage(wxMsg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }else if(EnumMsgRecordType.INSIDE_ORDER_SUCCESS.getCode().equals(wxMsgRecord.getMsgType())){
                    //内部消息 - 下订单成功
                    FmInsideOrderSuccessMsg msg = (FmInsideOrderSuccessMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),FmInsideOrderSuccessMsg.class);
                    msg.setMsgType(wxMsgRecord.getMsgType());
                    msg.setReceiver(wxMsgRecord.getReceiver());
                    msg.setTenantId(wxMsgRecord.getTenantId());
                    mqBaseProducer.sendMessage(msg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }
            }
            return new ResultData();
        }else{
            return new ResultData("fail");
        }
    }





}