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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.DateUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.custom.mapper.CosTprStationMapper;
import com.tofly.xrtygis.custom.mapper.FixedMapper;
import com.tofly.xrtygis.custom.mapper.WaterCostMapper;
import com.tofly.xrtygis.custom.service.CustomFixedService;
import com.tofly.xrtygis.custom.service.CustomWaterService;
import com.tofly.xrtygis.custom.util.CommonMethod;
import com.tofly.xrtygis.entity.Costrlfixed;
import com.tofly.xrtygis.entity.Costrlprice;
import com.tofly.xrtygis.entity.Costrlwater;
import com.tofly.xrtygis.service.CostrlfixedService;
import com.tofly.xrtygis.service.CostrlpriceService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 20126
 */
@Service
@AllArgsConstructor
public class CustomFixedServiceImpl implements CustomFixedService {

    private final static String TABLE_NAME = "TF_YWPN_COSTRLFIXED_W";

    private final FixedMapper fixedMapper;
    private final WaterCostMapper waterCostMapper;
    private final CosTprStationMapper cosTprStationMapper;
    private final CostrlfixedService costrlfixedService;
    private final CostrlpriceService costrlpriceService;
    private final CustomWaterService customWaterService;



    /**
     * 填报制水单变
     *
     * @param fixed 实际制水单变填报信息
     * @return true of false
     */
    @Override
    public boolean insertFixed(Costrlfixed fixed) {

        if(null == fixed){
            throw new ToflyDeniedException("制水单变填报信息不允许为空");
        }
        if ("1".equals(fixed.getMark())){
            Long finance = cosTprStationMapper.isFinance(DateUtil.getDateFormat(fixed.getUpDate(), "yyyy-MM"),TABLE_NAME,null);

            if (null != finance){
                throw new ToflyDeniedException("本月已进行财务填报");
            }
        }else {
            Long infoByDay = waterCostMapper.getInfoByDay(TABLE_NAME, DateUtil.getDateFormat(fixed.getUpDate(), "yyyy-MM-dd"),"0");
            if (null != infoByDay){
                throw new ToflyDeniedException("该天已进行非财务填报");
            }
        }

        Costrlwater water = fixedMapper.selectWaterInfo(DateUtil.getDateFormat(fixed.getUpDate(), "yyyy-MM-dd"), fixed.getMark());

        Costrlprice price = costrlpriceService.getOne(Wrappers.<Costrlprice>lambdaQuery()
                .eq(Costrlprice::getYear, Date8Util.getYear())
                .eq(Costrlprice::getIsnew,"1"));

        return costrlfixedService.save(CommonMethod.countFixed(fixed,price,water));
    }

    /**
     * 删除制水单变
     *
     * @param ids id
     * @return true of false
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFixed(String ids) {
        List<String> split = Arrays.asList(ids.split(","));
        for (String id : split) {
            Costrlfixed fixed = costrlfixedService.getById(id);
            if (fixed != null){
                Integer waterId = fixedMapper.getWaterInfoId(fixed.getMark(), fixed.getUpDate());
                if (waterId != null){
                    customWaterService.deleteWater(waterId.toString());
                }
            }
        }
        return costrlfixedService.removeByIds(split);
    }

    /**
     * 修改制水单变
     *
     * @param fixed 实际制水单变填报信息
     * @return true of false
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateFixed(Costrlfixed fixed) {
        Costrlfixed fixedById = costrlfixedService.getById(fixed.getId());

        Costrlwater water = fixedMapper.selectWaterInfo(DateUtil.getDateFormat(fixedById.getCreateTime(), "yyyy-MM-dd"), fixedById.getMark());
        if (null == water){
            throw new ToflyDeniedException("未查询到制水指标对应信息");
        }

        Costrlprice price = costrlpriceService.getOne(Wrappers.<Costrlprice>lambdaQuery()
                .eq(Costrlprice::getYear, DateUtil.getDateFormat(fixedById.getCreateTime(),"yyyy"))
                .eq(Costrlprice::getIsnew,"1"));
        if (null == price){
            throw new ToflyDeniedException("未查询到单价相关信息");
        }

        return costrlfixedService.updateById(CommonMethod.countFixed(fixed,price,water));
    }

    /**
     * 查看制水单变
     *
     * @param month 月份
     * @return map
     */
    @Override
    public Map<String, Object> selectFixed(String month) {
        Map<String, Object> map = new HashMap<>(4);
        //查询预算值
        map.put("fixedPlan",fixedMapper.viewFixedPlan(month));
        //查询财务填报
        map.put("finance",fixedMapper.viewFinance(month));

        map.put("sumFixed",fixedMapper.viewSumFixed(month));

        map.put("fixed",fixedMapper.viewFixed(month));

        return map;
    }


}
