package com.ruoyi.ems.service.impl;

import com.huashe.common.utils.DateUtils;
import com.ruoyi.ems.domain.ElecPriceStrategy;
import com.ruoyi.ems.domain.ElecPriceStrategyHour;
import com.ruoyi.ems.mapper.ElecPriceStrategyMapper;
import com.ruoyi.ems.service.IElecPriceStrategyHourService;
import com.ruoyi.ems.service.IElecPriceStrategyService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电价策略Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-23
 */
@Service
public class ElecPriceStrategyServiceImpl implements IElecPriceStrategyService {
    @Autowired
    private ElecPriceStrategyMapper elecPriceStrategyMapper;

    @Autowired
    private IElecPriceStrategyHourService hourService;

    /**
     * 查询电价策略
     *
     * @param id 电价策略主键
     * @return 电价策略
     */
    @Override
    public ElecPriceStrategy selectById(Long id) {
        ElecPriceStrategy strategy = elecPriceStrategyMapper.selectById(id);

        if (null != strategy) {
            List<ElecPriceStrategyHour> hourList = hourService.selectListByStrategyCode(strategy.getStrategyCode());
            strategy.setHourList(hourList);
        }

        return strategy;
    }

    /**
     * 查询电价策略列表
     *
     * @param elecPriceStrategy 电价策略
     * @return 电价策略
     */
    @Override
    public List<ElecPriceStrategy> selectList(ElecPriceStrategy elecPriceStrategy) {
        List<ElecPriceStrategy> list = elecPriceStrategyMapper.selectList(elecPriceStrategy);

        if (CollectionUtils.isNotEmpty(list)) {
            for (ElecPriceStrategy strategy : list) {
                List<ElecPriceStrategyHour> hourList = hourService.selectListByStrategyCode(strategy.getStrategyCode());
                strategy.setHourList(hourList);
            }
        }

        return list;
    }

    /**
     * 新增电价策略
     *
     * @param elecPriceStrategy 电价策略
     * @return 结果
     */
    @Override
    public int insert(ElecPriceStrategy elecPriceStrategy) {
        elecPriceStrategy.setCreateTime(DateUtils.getNowDate());

        if (CollectionUtils.isNotEmpty(elecPriceStrategy.getHourList())) {
            hourService.deleteByStrategyCode(elecPriceStrategy.getStrategyCode());
            hourService.insertBatch(elecPriceStrategy.getHourList());
        }

        return elecPriceStrategyMapper.insert(elecPriceStrategy);
    }

    /**
     * 修改电价策略
     *
     * @param elecPriceStrategy 电价策略
     * @return 结果
     */
    @Override
    public int update(ElecPriceStrategy elecPriceStrategy) {
        elecPriceStrategy.setUpdateTime(DateUtils.getNowDate());

        if (CollectionUtils.isNotEmpty(elecPriceStrategy.getHourList())) {
            hourService.deleteByStrategyCode(elecPriceStrategy.getStrategyCode());
            hourService.insertBatch(elecPriceStrategy.getHourList());
        }
        else {
            hourService.deleteByStrategyCode(elecPriceStrategy.getStrategyCode());
        }

        return elecPriceStrategyMapper.update(elecPriceStrategy);
    }

    /**
     * 批量删除电价策略
     *
     * @param ids 需要删除的电价策略主键
     * @return 结果
     */
    @Override
    public int deleteByIds(Long[] ids) {
        for (Long id : ids) {
            ElecPriceStrategy elecPriceStrategy = elecPriceStrategyMapper.selectById(id);

            if (null != elecPriceStrategy) {
                hourService.deleteByStrategyCode(elecPriceStrategy.getStrategyCode());
            }
        }

        return elecPriceStrategyMapper.deleteByIds(ids);
    }

    /**
     * 删除电价策略信息
     *
     * @param id 电价策略主键
     * @return 结果
     */
    @Override
    public int deleteById(Long id) {
        ElecPriceStrategy elecPriceStrategy = elecPriceStrategyMapper.selectById(id);

        if (null != elecPriceStrategy) {
            hourService.deleteByStrategyCode(elecPriceStrategy.getStrategyCode());
        }

        return elecPriceStrategyMapper.deleteById(id);
    }
}
