package com.njcn.csdevice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.csdevice.mapper.CsDevCapacityPOMapper;
import com.njcn.csdevice.pojo.po.CsDevCapacityPO;
import com.njcn.csdevice.pojo.po.CsLinePO;
import com.njcn.csdevice.service.CsDevCapacityPOService;
import com.njcn.csdevice.service.CsLinePOService;
import com.njcn.system.api.DicDataFeignClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Objects;

/**
 *
 * Description:
 * Date: 2023/9/27  10:29【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CsDevCapacityPOServiceImpl extends ServiceImpl<CsDevCapacityPOMapper, CsDevCapacityPO> implements CsDevCapacityPOService{

    private final CsLinePOService csLinePOService;
    private final DicDataFeignClient dicDataFeignClient;
    private final DecimalFormat df = new DecimalFormat("#0.000");

    @Override
    public void addList(List<CsDevCapacityPO> list) {
        String line = list.get(0).getLineId();
        LambdaQueryWrapper<CsDevCapacityPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CsDevCapacityPO::getLineId,line);
        this.remove(lambdaQueryWrapper);
        this.saveBatch(list);
    }

    @Override
    public Double getDevCapacity(String id) {
        List<CsLinePO> pos = csLinePOService.queryByDevId(id);
        /*治理侧监测点*/
        CsLinePO po = pos.stream().filter(item -> Objects.equals(item.getClDid(),0)).findFirst().orElse(null);
        if (po == null) {
            return 0.0;
        }
        CsDevCapacityPO one = this.lambdaQuery().eq(CsDevCapacityPO::getLineId, po.getLineId()).eq(CsDevCapacityPO::getCldid, 0).one();
        if(Objects.isNull(one)){
            return 0.0;
        }
        return Double.valueOf(df.format(one.getCapacity()));
    }
}
