package com.ruoyi.warehouseBusiness.service.impl;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.finance.excel.FleetExcel;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsfees;
import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsfeesMapper;
import com.ruoyi.warehouseBusiness.service.ITWarehousebillsfeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 仓库费用明细Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-11
 */
@Service
public class TWarehousebillsfeesServiceImpl implements ITWarehousebillsfeesService {
    @Autowired
    private TWarehousebillsfeesMapper tWarehousebillsfeesMapper;

    /**
     * 查询仓库费用明细
     *
     * @param fId 仓库费用明细ID
     * @return 仓库费用明细
     */
    @Override
    public TWarehousebillsfees selectTWarehousebillsfeesById(Long fId) {
        return tWarehousebillsfeesMapper.selectTWarehousebillsfeesById(fId);
    }

    /**
     * 查询仓库费用明细列表
     *
     * @param tWarehousebillsfees 仓库费用明细
     * @return 仓库费用明细
     */
    @Override
    public List<TWarehousebillsfees> selectTWarehousebillsfeesList(TWarehousebillsfees tWarehousebillsfees) {
        return tWarehousebillsfeesMapper.selectTWarehousebillsfeesList(tWarehousebillsfees);
    }

    /**
     * 新增仓库费用明细
     *
     * @param tWarehousebillsfees 仓库费用明细
     * @return 结果
     */
    @Override
    public int insertTWarehousebillsfees(TWarehousebillsfees tWarehousebillsfees) {
        tWarehousebillsfees.setCreateTime(DateUtils.getNowDate());
        return tWarehousebillsfeesMapper.insertTWarehousebillsfees(tWarehousebillsfees);
    }

    /**
     * 修改仓库费用明细
     *
     * @param tWarehousebillsfees 仓库费用明细
     * @return 结果
     */
    @Override
    public int updateTWarehousebillsfees(TWarehousebillsfees tWarehousebillsfees) {
        tWarehousebillsfees.setUpdateTime(DateUtils.getNowDate());
        return tWarehousebillsfeesMapper.updateTWarehousebillsfees(tWarehousebillsfees);
    }

    /**
     * 批量删除仓库费用明细
     *
     * @param fIds 需要删除的仓库费用明细ID
     * @return 结果
     */
    @Override
    public int deleteTWarehousebillsfeesByIds(Long[] fIds) {
        return tWarehousebillsfeesMapper.deleteTWarehousebillsfeesByIds(fIds);
    }

    /**
     * 删除仓库费用明细信息
     *
     * @param fId 仓库费用明细ID
     * @return 结果
     */
    @Override
    public int deleteTWarehousebillsfeesById(Long fId) {
        return tWarehousebillsfeesMapper.deleteTWarehousebillsfeesById(fId);
    }

    /**
     *  查询报表
     * @param tWarehousebillsfees
     * @return
     */
    @Override
    public List<FleetExcel> selectFleetExcelList(TWarehousebillsfees tWarehousebillsfees) {
        List<FleetExcel> fleetExcelList = new ArrayList<>();
        // 开始月份
        int  startMonth = 1;
        // 结束月份
        int  endMonth = 1;
        if (StringUtils.isNotEmpty(tWarehousebillsfees.getfYears())) {
            // 年份
            List<String> month = tWarehousebillsfees.getfMonth();
            if (StringUtils.isNotEmpty(month.get(0))) {
                startMonth = Integer.parseInt(month.get(0));
            }
            if (StringUtils.isNotEmpty(month.get(1))) {
                endMonth = Integer.parseInt(month.get(1));
            }
            if (endMonth < startMonth) {
                return null;
            }
            for (int i = startMonth; i <= endMonth; i++) {
                String firstDay = DateUtils.getFirstDay(Integer.parseInt(tWarehousebillsfees.getfYears()), i);
                String lastDay = DateUtils.getLastDay(Integer.parseInt(tWarehousebillsfees.getfYears()), i);
                month.set(0, firstDay);
                month.set(1, lastDay);
                tWarehousebillsfees.setfMonth(month);
                List<FleetExcel> fleetExcels = tWarehousebillsfeesMapper.selectFleetExcelList(tWarehousebillsfees);
                if (StringUtils.isNotEmpty(fleetExcels)) {
                    for (FleetExcel e : fleetExcels) {
                        fleetExcelList.add(e);
                    }
                    fleetExcelList.add(tWarehousebillsfeesMapper.selectFleetExcelMonthList(tWarehousebillsfees));
                }
            }
        }
        return fleetExcelList;
    }

    /**
     *  查询报表合计
     * @param fleetExcelsList
     * @return
     */
    @Override
    public List<String> selectFleetExcel(List<FleetExcel> fleetExcelsList) {
        List<String> stringList = new ArrayList<>();
        if (StringUtils.isEmpty(fleetExcelsList)) {
            stringList.add("0");
            stringList.add("0");
            stringList.add("0");
            return stringList;
        }
        BigDecimal ccf = new BigDecimal(0);
        BigDecimal fAmount = new BigDecimal(0);
        BigDecimal totalAmount = new BigDecimal(0);
        for (FleetExcel f : fleetExcelsList) {
            if (StringUtils.isEmpty(f.getUserName())) {
                if (StringUtils.isNotEmpty(f.getCcf())) {
                    ccf = ccf.add(new BigDecimal(f.getCcf()));
                } else {
                    ccf = ccf.add(new BigDecimal(0));
                }
                if (StringUtils.isNotEmpty(f.getfAmount())) {
                    fAmount = fAmount.add(new BigDecimal(f.getfAmount()));
                } else {
                    fAmount = fAmount.add(new BigDecimal(0));
                }
                if (StringUtils.isNotEmpty(f.getTotalAmount())) {
                    totalAmount = totalAmount.add(new BigDecimal(f.getTotalAmount()));
                } else {
                    totalAmount = totalAmount.add(new BigDecimal(0));
                }
            }
        }
        stringList.add(ccf + "");
        stringList.add(fAmount + "");
        stringList.add(totalAmount + "");
        return stringList;
    }

    @Override
    public List<Map<String, Object>> accountsDue() {
        return tWarehousebillsfeesMapper.accountsDue();
    }
}
