package cn.pluss.platform.device.impl;

import cn.pluss.platform.device.DeviceBsjService;
import cn.pluss.platform.deviceStock.DeviceStockService;
import cn.pluss.platform.dict.DictService;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.mapper.DeviceBsjMapper;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.notice.NoticeService;
import cn.pluss.platform.notice.template.NoticeTemplateService;
import cn.pluss.platform.row.DeviceBsjRowGetter;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.excel.ExcelImportUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2022-05-09
 */
@Service
public class DeviceBsjServiceImpl extends ServiceImpl<DeviceBsjMapper, DeviceBsj> implements DeviceBsjService {

    @Autowired
    private DeviceStockService deviceStockService;

    @Autowired
    private MerchantBaseInfoService mbiService;

    @Autowired
    private UserAppService uaService;

    @Autowired
    private DictService dictService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private NoticeTemplateService templateService;

    @Override
    public void importExpiredTime(MultipartFile file) throws IOException {
        final List<DeviceBsj> dataList = ExcelImportUtil.readExcel(file, new DeviceBsjRowGetter(), 1);

        if (dataList.isEmpty()) {
            return;
        }

        LambdaQueryWrapper<DeviceBsj> qWrapper = Wrappers.lambdaQuery();

        for (DeviceBsj item : dataList) {
            qWrapper.clear();
            qWrapper.eq(DeviceBsj::getIccid, item.getIccid());
            DeviceBsj existData = getOne(qWrapper);
            if (existData == null) {
                continue;
            }

            existData.setSim(item.getSim());
            existData.setSimExpireTime(item.getSimExpireTime());

            LambdaUpdateWrapper<DeviceBsj> uWrapper = Wrappers.lambdaUpdate();
            uWrapper.eq(DeviceBsj::getIccid, existData.getIccid());
            update(existData, uWrapper);
        }
    }

    @Override
    public void sendBatchExpiredNotice() {
        List<Map<String, Object>> deviceInfoList = baseMapper.selectLastMonthExpiredData();
        final NoticeTemplate noticeTemplate = templateService.getNoticeTemplateByType("33");

        Dict dict = new Dict();
        dict.setValue("33");
        dict = dictService.getByCode("notice_template_type", dict);

        String url = "https://www.shouyinbei.net/wap/merchantDevice/simChargeIndex";

        // 根据模版发送通知
        for (Map<String, Object> deviceInfo : deviceInfoList) {
            noticeService.sendTempNotice(dict.getName(), noticeTemplate.getContent(), noticeTemplate.getContent(), url,
                    deviceInfo.get("userId").toString(), deviceInfo.get("alias"), deviceInfo.get("snNo"));
        }
    }
}
