package com.njcn.device.pms.service.ledgerManger.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.device.pms.mapper.DeptLineMapper;
import com.njcn.device.pq.pojo.po.DeptLine;
import com.njcn.device.pms.service.ledgerManger.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.HashMap;
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 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);
        Map<String, List<String>> collect = deptLines.stream ( ).collect (Collectors.groupingBy (DeptLine::getId, Collectors.mapping (DeptLine::getLineId,Collectors.toList ())));

        return collect;
    }


}
