package com.iformall.schedule;

import com.alibaba.fastjson.JSONObject;
import com.iformall.domain.po.msg.WxMsgRecord;
import com.iformall.enums.*;
import com.iformall.mapper.WxBillAllMapper;
import com.iformall.mq.MqBaseProducer;
import com.iformall.service.WxBillAllService;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

/**
 * @author gongbiao
 */
@Component
public class BillNotificationSchedule {


    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxBillAllMapper wxBillAllMapper;

    @Autowired
    private MqBaseProducer mqBaseProducer;

    @Autowired
    WxBillAllService wxBillAllService;

    /**
     * 每天9点执行
     */
    @Scheduled(cron = "0 0 14 * * ?")
    public void sendMsgFromBill() {
        logger.info("账单通知任务启动");
        sendWaitPayBill();
        wxBillAllService.sendWaitPayBillEmail();
        sendOweBill();
        wxBillAllService.sendOweBillForEmail();
        logger.info("账单通知任务结束");
    }

    private void sendOweBill() {
        logger.info("查询欠缴账单数据");
        List<Map<String, Object>> oweBillList = wxBillAllMapper.getOweBill();
        for (Map<String, Object> bill : oweBillList) {
            String tenantId = bill.get("tenantId").toString();
            logger.info("欠缴账单：" + JSONObject.toJSONString(bill));
            BigDecimal owe = (BigDecimal) bill.get("owe");
            BigDecimal price = owe.divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_EVEN);
            String phone = bill.get("linkPhone").toString();
            String appname = bill.get("appname").toString();
            String merchantName = bill.get("merchantName").toString();
            logger.info("欠缴账单检验短信配置信息");

            WxMsgRecord wxMsgRecord = new WxMsgRecord();
            wxMsgRecord.setMsgType(EnumMsgRecordType.SMS.getCode());
            wxMsgRecord.setModelType(EnumMsgModel.BILL_OWE.getCode());
            wxMsgRecord.setReceiver(phone);
            wxMsgRecord.setTenantId(tenantId);
            Map<String, String> map = new HashedMap();
            map.put("price", price.toString());
            map.put("app", appname);
            map.put("merName", merchantName);
            wxMsgRecord.setDynamicContentMap(map);
            mqBaseProducer.sendMessage(wxMsgRecord, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
        }
    }

    private void sendWaitPayBill() {
        logger.info("查询待缴账单数据");
        List<Map<String, Object>> waitBillList = wxBillAllMapper.getWaitPayBill();
        for (Map<String, Object> bill : waitBillList) {
            String tenantId = bill.get("tenantId").toString();
            logger.info("待缴账单账单：" + JSONObject.toJSONString(bill));

            String receiveDate = bill.get("receiveDate").toString();
            BigDecimal needPay = (BigDecimal) bill.get("needPay");
            BigDecimal price = needPay.divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_EVEN);
            String phone = bill.get("linkPhone").toString();
            String appname = bill.get("appname").toString();
            String merchantName = bill.get("merchantName").toString();
            logger.info("待缴账单检验短信配置信息");

            WxMsgRecord wxMsgRecord = new WxMsgRecord();
            wxMsgRecord.setMsgType(EnumMsgRecordType.SMS.getCode());
            wxMsgRecord.setModelType(EnumMsgModel.BILL_WAIT_PAY.getCode());
            wxMsgRecord.setReceiver(phone);
            wxMsgRecord.setTenantId(tenantId);
            Map<String, String> map = new HashedMap();
            map.put("price", price.toString());
            map.put("app", appname);
            map.put("date", receiveDate);
            map.put("merName", merchantName);
            wxMsgRecord.setDynamicContentMap(map);
            mqBaseProducer.sendMessage(wxMsgRecord, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
        }
    }



}