/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.comm.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.DateUtil;
import com.tofly.gpsboot.mapper.comm.RxsmsmessageMapper;
import com.tofly.gpsboot.service.comm.RxsmsmessageService;
import com.tofly.gpsboot.util.SMSUtils;
import com.tofly.pojo.comm.Rxsmsmessage;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 消息发送记录服务
 *
 * @author admin
 * @date Thu Mar 03 00:00:00 CST 2022
 */
@Service("rxsmsmessageService")
public class RxsmsmessageServiceImpl extends ServiceImpl<RxsmsmessageMapper, Rxsmsmessage> implements RxsmsmessageService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean sendNote(String phone, String content) {
        //解析多个电话号码
        List<String> mobileList = Arrays.asList(phone.split(","));
        List<Long> idList = new ArrayList<>();
        Integer size = mobileList.size();
        for (int i = 0; i < size; i++) {
            Rxsmsmessage message = new Rxsmsmessage();
            message.setMobile(mobileList.get(i));
            message.setFszt("0");
            message.setLsbh(DateUtil.parseDateToStr("yyyyMMddHHmmssSSS",Date8Util.getCurrentDate())+i);
            message.setTxnr(content);
            message.setIslock("0");
            message.setTxlx("");
            this.save(message);
            idList.add(message.getId());
        }
        //专门有个方法发送，专门发送短信和更改短信发送标志
        action(idList);
        return true;
    }

    /**
     * 专门发送短信及更改数据库短信信息
     * 根据ID范围确定短信信息
     *
     * @param idList
     */
    @Async
    public void action(List<Long> idList) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                //用ID的范围查询需要发送的短信；并且只能是未发送的短信信息
                List<Rxsmsmessage> rxsmsmessageList = list(Wrappers.<Rxsmsmessage>lambdaQuery().in(Rxsmsmessage::getId, idList)
                        .eq(Rxsmsmessage::getIslock, "0").eq(Rxsmsmessage::getFszt, "0")
                        .select(Rxsmsmessage::getMobile, Rxsmsmessage::getTxnr, Rxsmsmessage::getId));

                //轮训发送短信，并修改短信字段信息
                rxsmsmessageList.forEach(k -> {
                    try {
                        //发送短信
                        SMSUtils smsUtils = new SMSUtils();
                        String result = smsUtils.sendMsg(k.getMobile(), k.getTxnr());
                        //发送成功
                        if (result.contains("发送成功")) {
                            k.setFszt("1");
                            k.setIslock("1");
                        }//发送失败
                        else {
                            k.setFszt("2");
                            k.setIslock("0");
                        }
                        k.setBz(result);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                //修改数据库短信信息数据
                updateBatchById(rxsmsmessageList);
            }
        });
    }

}
