package com.njcn.device.pq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.device.biz.pojo.dto.TerminalGetBase;
import com.njcn.device.pq.mapper.DeptLineMapper;
import com.njcn.device.biz.pojo.dto.LineDevGetDTO;
import com.njcn.device.pq.pojo.po.DeptLine;
import com.njcn.device.pq.service.DeptLineService;
import com.njcn.web.pojo.param.DeptLineParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author denghuajun
 * @date 2022/1/12 17:32
 *
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeptLineServiceImpl extends ServiceImpl<DeptLineMapper, DeptLine> implements DeptLineService {

    private final DeptLineMapper deptLineMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deptBindLine(DeptLineParam deptLineParam) {
        //先解绑，再进行绑定
        QueryWrapper<DeptLine> deptLineQueryWrapper = new QueryWrapper<>();
        deptLineQueryWrapper.eq("pq_dept_line.id", deptLineParam.getId());
        this.baseMapper.delete(deptLineQueryWrapper);
        List<DeptLine> deptLines = deptLineParam.getIds().stream().map(id -> {
            DeptLine deptLine = new DeptLine();
            deptLine.setId(deptLineParam.getId());
            deptLine.setLineId(id);
            return deptLine;
        }).collect(Collectors.toList());
        this.saveBatch(deptLines);
    }

    @Override
    public void deptDeleteBindLine(DeptLineParam deptLineParam) {
        for (int i = 0; i < deptLineParam.getIds().size(); i++) {
            QueryWrapper<DeptLine> deptLineQueryWrapper = new QueryWrapper<>();
            deptLineQueryWrapper.eq("pq_dept_line.id", deptLineParam.getId());
            deptLineQueryWrapper.eq("pq_dept_line.Line_Id", deptLineParam.getIds().get(i));
            this.baseMapper.delete(deptLineQueryWrapper);
        }
    }

    @Override
    public List<DeptLine> selectDeptBindLines(List<String> ids) {
        return this.lambdaQuery().in(DeptLine::getId, ids).list();
    }

    @Override
    public List<String> getLineByDeptIds(List<String> ids,List<Integer> runFlag,List<Integer> dataType) {
       return this.baseMapper.getLineIdByDeptIds(ids,runFlag,dataType);
    }

    @Override
    public int removeBind(String id) {
        QueryWrapper<DeptLine> deptLineQueryWrapper = new QueryWrapper<>();
        deptLineQueryWrapper.eq("pq_dept_line.id", id);
        return this.baseMapper.delete(deptLineQueryWrapper);
    }

    @Override
    public List<String> getLineByDeptId(String id) {
        return this.lambdaQuery().in(DeptLine::getId, id).list().stream().map(DeptLine::getLineId).distinct().collect(Collectors.toList());
    }

    /**
     * @param devDataType
     * @Description: 获取部门和监测点的关系（分稳态暂态）
     * @Param: [devDataType]
     * @return: java.util.Map<java.lang.String, java.util.List < java.lang.String>>
     * @Author: clam
     * @Date: 2022/10/19
     */
    @Override
    public Map<String, List<String>> getLineByDeptRelation(Integer devDataType) {
        List<DeptLine> deptLines = deptLineMapper.getLineByDeptRelation(devDataType);
        return deptLines.stream ( ).collect (Collectors.groupingBy (DeptLine::getId, Collectors.mapping (DeptLine::getLineId,Collectors.toList ())));
    }

    @Override
    public Map<String, List<LineDevGetDTO>> lineDevGet(List<Integer> devDataType,Integer type) {
        List<LineDevGetDTO> deptLines = deptLineMapper.lineDevGet(devDataType,type);
        return deptLines.stream ().collect (Collectors.groupingBy (LineDevGetDTO::getUnitId));
    }

    @Override
    public Map<String, List<String>> orgSubStationGet(List<Integer> devDataType) {
        List<TerminalGetBase> deptLines = deptLineMapper.orgSubStationGet(devDataType);
        return deptLines.stream ().collect (Collectors.groupingBy (TerminalGetBase::getUnitId, Collectors.mapping (TerminalGetBase::getLedgerId,Collectors.toList ())));
    }



}
