package com.ruoyi.task.billEleRecord;

import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.billEleRecord.domain.BillEleRecord;
import com.ruoyi.ss.billEleRecord.service.BillEleRecordConverter;
import com.ruoyi.ss.billEleRecord.service.BillEleRecordService;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.suit.domain.enums.SuitFeeMode;
import com.ruoyi.ss.suit.domain.enums.SuitFeeType;
import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillType;
import com.ruoyi.ss.transactionBill.domain.vo.TransactionBillVO;
import com.ruoyi.ss.transactionBill.service.TransactionBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author wjh
 * 2025/1/4
 */
@Component
@Slf4j
public class BillEleRecordTask {

    @Autowired
    private TransactionBillService transactionBillService;

    @Autowired
    private BillEleRecordService billEleRecordService;

    @Autowired
    private BillEleRecordConverter billEleRecordConverter;

    @Autowired
    private DeviceService deviceService;

    // 记录分时段订单电量
    public void record() {

        // 查询未结束的分时段电量订单
        TransactionBillQuery query = new TransactionBillQuery();
        query.setType(TransactionBillType.RECHARGE.getType());
        query.setIsFinished(false);
        query.setSuitFeeMode(SuitFeeMode.SMART.getMode());
        query.setSuitFeeType(SuitFeeType.TIMING_COUNT.getType());
        List<TransactionBillVO> billList = transactionBillService.selectSmTransactionBillList(query);
        if (CollectionUtils.isEmpty(billList)) {
            log.info("记录分时段订单电量：暂无未结束的分时段电量订单");
            return;
        }
        // 抄表时间
        LocalDateTime time = LocalDateTime.now();
        // 计费时间
        LocalDateTime chargeTime = time.plusHours(-1);

        // 查询设备列表
        List<DeviceVO> deviceList = deviceService.selectByIds(CollectionUtils.map(billList, TransactionBillVO::getDeviceId));
        deviceService.pullDeviceInfoList(deviceList, null);

        // 待插入的记录列表
        List<BillEleRecord> recordList = new ArrayList<>();

        // 记录电量
        for (TransactionBillVO bill : billList) {
            try {
                DeviceVO device = deviceList.stream().filter(item -> Objects.equals(bill.getDeviceId(), item.getDeviceId())).findFirst().orElse(null);
                if (device == null) {
                    continue;
                }
                BillEleRecord record = billEleRecordConverter.toPo(bill, device, time, chargeTime);
                if (record != null) {
                    recordList.add(record);
                }

            } catch (Exception e) {
                log.error("记录分时段订单电量失败，billNo:{}, {}", bill.getBillNo(), e.getMessage());
            }
        }

        int insert = billEleRecordService.batchInsert(recordList);
        log.info("记录分时段订单电量：共{}条记录", insert);
    }
}
