package com.kym.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.kym.common.utils.CommUtil;
import com.kym.entity.admin.StationStatMonth;
import com.kym.entity.admin.queryParams.StatementsQueryParam;
import com.kym.entity.common.PageBean;
import com.kym.mapper.admin.StationStatMonthMapper;
import com.kym.service.admin.StationStatMonthService;
import com.kym.service.cache.KymCache;
import com.kym.service.mybatisplus.MyBaseServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 站点统计表-月 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2023-12-04
 */
@Service
public class StationStatMonthServiceImpl extends MyBaseServiceImpl<StationStatMonthMapper, StationStatMonth> implements StationStatMonthService {

    @Override
    public PageBean<StationStatMonth> listStatMonth(StatementsQueryParam params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        var res = lambdaQuery()
                .eq(!CommUtil.isEmptyOrNull(params.getStationId()), StationStatMonth::getStationId, params.getStationId())
                .eq(!CommUtil.isEmptyOrNull(params.getStatMonth()), StationStatMonth::getStatMonth, params.getStatMonth())
                .orderByDesc(StationStatMonth::getStatMonth)
                .list();
        var page = new PageBean<>(res);
        page.setList(res.stream().peek(item -> item.setStationName(KymCache.INSTANCE.getStationNameById(item.getStationId()))).toList());
        return page;
    }

    @Override
    public void modifyStationStatMonth(StationStatMonth stationStatMonth) {
        lambdaUpdate().eq(StationStatMonth::getId, stationStatMonth.getId())
                .set(CommUtil.isNotEmptyAndNull(stationStatMonth.getActualPower()), StationStatMonth::getActualPower, stationStatMonth.getActualPower())
                .set(CommUtil.isNotEmptyAndNull(stationStatMonth.getActualElecMoney()), StationStatMonth::getActualElecMoney, stationStatMonth.getActualElecMoney())
                .update();
    }
}
