package com.tofly.xrtygis.custom.service.impl;

import cn.hutool.core.date.DateTime;
import com.tofly.common.core.util.DateUtil;
import com.tofly.xrtygis.custom.mapper.UnitMapper;
import com.tofly.xrtygis.custom.service.CustomUnitService;
import com.tofly.xrtygis.entity.Costcalcconfig;
import com.tofly.xrtygis.service.CostcalcconfigService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 陈浩
 * @date 2021/4/26
 */
@Service
@AllArgsConstructor
public class CustomUnitServiceImpl implements CustomUnitService {

    private final UnitMapper unitMapper;

    private final CostcalcconfigService costcalcconfigService;

    /**
     * 查询制水单耗
     *
     * @param month 实际指标单耗填报信息
     * @return true of false
     */
    @Override
    public Map<String, Object> viewUnit(String month) {
        Map<String, Object> result = new HashMap<>(4);

        Costcalcconfig config = costcalcconfigService.getById(1);

        Date yeatMonth = DateUtil.formatDate(month, "yyyy-MM");
        DateTime dateTime = cn.hutool.core.date.DateUtil.offsetMonth(yeatMonth, -1);
        String format = cn.hutool.core.date.DateUtil.format(dateTime, "yyyy-MM");

        String start = format + "-" + config.getStartDate();
        String end = month + "-" + config.getEndDate();

        result.put("finance", unitMapper.selectFinance(month));

        result.put("real", unitMapper.selectReal(start, end));

        result.put("plan", unitMapper.selectPlan(month));

        result.put("resultSet", unitMapper.viewReals(start, end));

        return result;
    }

}
