package com.pqs9900.serviceImpl.linedata;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.njcn.enums.DicDataTypeEnum;
import com.njcn.mapper.configuration.NodeInformationMapper;
import com.njcn.pojo.configuration.DicData;
import com.njcn.pojo.pointInfo.LineLedger;
import com.njcn.service.configuration.DeviceService;
import com.njcn.utils.UserUtil;

import com.pqs9900.mapper.linedata.LineMarkWeightMapper;
import com.pqs9900.pojo.linedata.LineMarkWeight;
import com.pqs9900.pojo.linedata.LineMarkWeightVo;
import com.pqs9900.pojo.linedata.MarkWeightExcel;
import com.pqs9900.service.linedata.LineMarkService;
import com.pqs9900.service.linedata.LineMarkWeightService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * system
 * 监测点打分
 *
 * @author cdf
 * @date 2021/10/27
 */
@Service
public class LineMarkWeightServiceImpl implements LineMarkWeightService {

    @Resource
    private LineMarkWeightMapper lineMarkWeightMapper;

    @Resource
    private DeviceService deviceService;

    @Resource
    private NodeInformationMapper nodeInformationMapper;



    @Resource
    private UserUtil userUtil;

    /**
     * 获取监测点台账
     *
     * @author cdf
     * @date 2021/10/27
     */
    @Override
    public List<LineMarkWeightVo> getLineMarkWeight(Integer devFlag, Integer lineGrade) {
        return  lineMarkWeightMapper.getLineMarkWeight(devFlag,lineGrade);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer singleOperate(LineMarkWeight lineMarkWeight) {
        lineMarkWeightMapper.deleteByPrimaryKey(lineMarkWeight.getLineIndex());
        return lineMarkWeightMapper.singleOperate(lineMarkWeight);
    }

    @Override
    public LineLedger getLineGrade(Integer lineIndex) {
        return lineMarkWeightMapper.getLineGrade(lineIndex);
    }

    @Override
    public MarkWeightExcel getLineMarkData(String gdName, String subName, String devName, String lineName) {
        return lineMarkWeightMapper.getLineMarkData(gdName, subName, devName, lineName);
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public String dealData(List<MarkWeightExcel> list) {
        Integer flag = 2;
        for (MarkWeightExcel item : list) {
            String errMsg = "序号 " + item.getIndex();


            MarkWeightExcel m = lineMarkWeightMapper.getLineMarkData(item.getGdName().trim(), item.getSubName().trim(), item.getDevName().trim(), item.getLineName().trim());
            if (Objects.isNull(m)) {
                return errMsg + " 未匹配到监测点对象，请仔细检查!";
            } else {
                if (Objects.isNull(m.getLineIndex()) || Objects.isNull(m.getDevIndex())) {
                    return errMsg + "获取监测点ID异常,请联系管理员!";
                }
                item.setLineIndex(m.getLineIndex());
                item.setDevIndex(m.getDevIndex());
            }
            flag++;
        }
        List<Integer> lineIds = list.stream().map(MarkWeightExcel::getLineIndex).collect(Collectors.toList());

        lineMarkWeightMapper.batchDelLineWeight(lineIds);

        //进行批量操作E
        lineMarkWeightMapper.batchAddLineWeight(list);
        return "操作成功";
    }






}
