package com.pwt.web.service.impl;

import cn.hutool.core.util.StrUtil;
import com.pwt.common.utils.DateUtils;
import com.pwt.netty.handler.ChargerServerHandler;
import com.pwt.web.domain.PwtChargeRecordDto;
import com.pwt.web.domain.PwtChargeRecordVo;
import com.pwt.web.domain.StatisticsRecord;
import com.pwt.web.mapper.PwtChargeRecordMapper;
import com.pwt.web.service.IPwtChargeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 充电记录Service业务层处理
 *
 * @author linyun
 * @date 2022-11-17
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PwtChargeRecordServiceImpl implements IPwtChargeRecordService {
    @Autowired
    private PwtChargeRecordMapper pwtChargeRecordMapper;

    /**
     * 查询充电记录
     *
     * @param recordId 充电记录ID
     * @return 充电记录
     */
    @Override
    public PwtChargeRecordVo selectPwtChargeRecordById(Long recordId) {
        return pwtChargeRecordMapper.selectPwtChargeRecordById(recordId);
    }

    /**
     * 查询充电记录列表
     *
     * @param pwtChargeRecordVo 充电记录
     * @return 充电记录
     */
    @Override
    public List<PwtChargeRecordVo> selectPwtChargeRecordList(PwtChargeRecordVo pwtChargeRecordVo) {
        return pwtChargeRecordMapper.selectPwtChargeRecordList(pwtChargeRecordVo);
    }

    /**
     * 新增充电记录
     *
     * @return 结果
     */
    @Override
    public int insertPwtChargeRecord(PwtChargeRecordDto dto) {
        dto.setCreateTime(DateUtils.getNowDate());
        return pwtChargeRecordMapper.insertPwtChargeRecord(dto);
    }

    /**
     * 修改充电记录
     *
     * @param pwtChargeRecordVo 充电记录
     * @return 结果
     */
    @Override
    public int updatePwtChargeRecord(PwtChargeRecordVo pwtChargeRecordVo) {
        return pwtChargeRecordMapper.updatePwtChargeRecord(pwtChargeRecordVo);
    }

    /**
     * 批量删除充电记录
     *
     * @param recordIds 需要删除的充电记录ID
     * @return 结果
     */
    @Override
    public int deletePwtChargeRecordByIds(Long[] recordIds) {
        return pwtChargeRecordMapper.deletePwtChargeRecordByIds(recordIds);
    }

    /**
     * 删除充电记录信息
     *
     * @param recordId 充电记录ID
     * @return 结果
     */
    @Override
    public int deletePwtChargeRecordById(Long recordId) {
        return pwtChargeRecordMapper.deletePwtChargeRecordById(recordId);
    }

    /**
     * 统计记录
     *
     * @param statisticsRecord
     * @return
     */
    @Override
    public List<StatisticsRecord> getStatisticsRecord(StatisticsRecord statisticsRecord) {
        if (Objects.nonNull(statisticsRecord.getHolderNo())) {
            String chargerNo = ChargerServerHandler.HOLDER_AND_GUN_MAP.get(statisticsRecord.getHolderNo());
            statisticsRecord.setChargerNo(chargerNo);
        }
        return pwtChargeRecordMapper.getStatisticsRecord(statisticsRecord);
    }

    /**
     * 查询充电机编号字典
     *
     * @return
     */
    @Override
    public List<String> getChargerNoDict() {
        return pwtChargeRecordMapper.getChargerNoDict();
    }
}
