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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.service.CosTprStationService;
import com.tofly.xrtygis.custom.util.CommonMethod;
import com.tofly.xrtygis.entity.Costcalcconfig;
import com.tofly.xrtygis.entity.Costprestation;
import com.tofly.xrtygis.entity.Scadastation;
import com.tofly.xrtygis.service.CostcalcconfigService;
import com.tofly.xrtygis.service.CostprestationService;
import com.tofly.xrtygis.service.ScadastationService;
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 CosTprStationServiceImpl implements CosTprStationService {

    private final CostprestationService costprestationService;
    private final CosTprStationMapper cosTprStationMapper;
    private final ScadastationService scadastationService;
    private final CostcalcconfigService costcalcconfigService;

    /**
     * 添加加压站信息
     *
     * @param station 加压站指标填报信息
     * @return true of false
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertInfo(Costprestation station) {

        if (null == station.getMark()) {
            throw new ToflyDeniedException("选择填报方式：财务/非财务");
        }

        if ("1".equals(station.getMark())) {
            Long finance = cosTprStationMapper.isFinance(DateUtil.getDateFormat(station.getUpDate(), "yyyy-MM"), "TF_YWPN_COSTPRESTATION_W", station.getPreType());
            if (null != finance) {
                throw new ToflyDeniedException("该月该站点已进行财务填报");
            }
            station.setYgcb(CommonMethod.multiply(CommonMethod.divide(station.getSjje(),station.getGsl()),1000D));
            station.setSjdh(CommonMethod.multiply(CommonMethod.divide(station.getSjdl(), station.getGsl()), 10D));
        } else {
            Long aLong = cosTprStationMapper.notFinance(DateUtil.getDateFormat(station.getUpDate(), "yyyy-MM-dd"), station.getPreType());
            if (null != aLong) {
                throw new ToflyDeniedException("该测站已进行填报");
            }
        }
        return costprestationService.save(station);
    }

    /**
     * 删除填报信息
     *
     * @param ids ids
     * @return true of false
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteInfo(String ids) {
        return costprestationService.removeByIds(Arrays.asList(ids.split(",")));
    }

    /**
     * 修改加压站信息
     *
     * @param station 加压站信息
     * @return true of false
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateInfo(Costprestation station) {
        if ("1".equals(station.getMark())) {
            station.setYgcb(CommonMethod.multiply(CommonMethod.divide(station.getSjje(),station.getGsl()),1000D));
            station.setSjdh(CommonMethod.divide(CommonMethod.divide(station.getSjdl(), station.getGsl()), 10D));
        }
        return costprestationService.updateById(station);
    }

    /**
     * 查看加压站信息
     *
     * @param month 月份
     * @return map
     */
    @Override
    public Map<String, Object> viewInfo(Long preId, String month) {
        Map<String, Object> result = new HashMap<>(3);
        Costcalcconfig config = costcalcconfigService.getById(1);
        String start = config.getStartDate();
        String end = config.getEndDate();
        //财务合计值
        result.put("finance", cosTprStationMapper.getRealSum2(month, "1", preId, start, end));
//        if (preId != null && month != null) {
//
//        } else {
//            result.put("finance", cosTprStationMapper.getRealSum("1", start, end));
//        }

        //运行合计值
        result.put("realSum", cosTprStationMapper.getRealSum3(month, "0", preId, start, end));
//        if (preId != null && month != null) {
//
//        } else {
//            result.put("realSum", cosTprStationMapper.getRealSum("0",start,end));
//
//        }

        result.put("realList", cosTprStationMapper.getStationInfo(month, preId, start, end));

        return result;
    }


    /**
     * 获取所有测站名称
     *
     * @return list
     */
    @Override
    public List getPreName() {
        return scadastationService.list(Wrappers.<Scadastation>lambdaQuery()
                .eq(Scadastation::getIscraft, "1")
                .eq(Scadastation::getState, "1")
                .select(Scadastation::getId, Scadastation::getStnm));

    }

    /**
     * 获取测站指标的各项值
     *
     * @param month 月份
     * @return list
     */
    @Override
    public List<Map<String, Object>> getPreValue(String month, Long pcid) {
        return cosTprStationMapper.getPreValue(month, pcid);
    }
}
