package com.persagy.dmp.rwd.delivery.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.persagy.dmp.common.constant.ValidEnum;
import com.persagy.dmp.common.model.entity.BaseEntity;
import com.persagy.dmp.define.entity.GraphDefine;
import com.persagy.dmp.define.entity.ObjectTypeDefine;
import com.persagy.dmp.define.entity.RelationDefine;
import com.persagy.dmp.delivery.dto.RelationDefineCollectDTO;
import com.persagy.dmp.delivery.entity.RelationDefineCollect;
import com.persagy.dmp.rwd.define.service.IGraphDefineService;
import com.persagy.dmp.rwd.define.service.IRelationDefineService;
import com.persagy.dmp.rwd.delivery.dao.RelationDefineCollectMapper;
import com.persagy.dmp.rwd.delivery.service.IRelationDefineCollectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 项目，现场交付范围管理-关系
 * @author:linhuili
 * @date:2021/10/13
 */
@Service
public class RelationDefineCollectServiceImpl extends ServiceImpl<RelationDefineCollectMapper, RelationDefineCollect> implements IRelationDefineCollectService {

    @Autowired
    private RelationDefineCollectMapper relationDefineCollectMapper;

    @Autowired
    private IRelationDefineService relationDefineService;

    @Autowired
    private IGraphDefineService graphDefineService;

    /**
     * 数量统计
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public Integer queryCount(Wrapper<RelationDefineCollect> queryWrapper) {
        return relationDefineCollectMapper.selectCount(queryWrapper);
    }

    /**
     * 查询信息点交付范围
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public List<RelationDefineCollect> queryByCondition(Wrapper<RelationDefineCollect> queryWrapper) {
        return relationDefineCollectMapper.selectList(queryWrapper);
    }

    /**
     * 分页询信息点交付范围
     * @param page 分页条件
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public Page queryByCondition(Page page, Wrapper<RelationDefineCollect> queryWrapper) {
        Page selectPage = relationDefineCollectMapper.selectPage(page, queryWrapper);
        List<RelationDefineCollect> records = (List<RelationDefineCollect>)selectPage.getRecords();
        if(CollUtil.isEmpty(records)){
            return selectPage;
        }
        List<RelationDefineCollectDTO> content = new ArrayList<>();
        //获取图类型定义
        Map<String, GraphDefine> graphDefineMap = queryAllGraphDefine();
        //获取边类型定义
        Map<String, RelationDefine> relationDefineMap = queryAllRelDefine();
        records.forEach(item->{
            RelationDefineCollectDTO collectDTO = new RelationDefineCollectDTO();
            //设置图类型名称
            String graphCode = item.getGraphCode();
            if(StringUtils.isNotEmpty(graphCode)) {
                GraphDefine graphDefine = graphDefineMap.get(graphCode);
                if (graphDefine != null) {
                    collectDTO.setGraphName(graphDefine.getName());
                }
            }
            //设置边类型定义
            String relCode = item.getRelCode();
            if(StringUtils.isNotEmpty(relCode)){
                RelationDefine relationDefine = relationDefineMap.get(relCode);
                if(relationDefine != null){
                    BeanUtils.copyProperties(relationDefine,collectDTO);
                }
            }
            BeanUtils.copyProperties(item,collectDTO);
            content.add(collectDTO);
        });
        selectPage.setRecords(content);
        return selectPage;

    }

    /**
     * 获取图类型定义
     * @return
     */
    private Map<String, GraphDefine> queryAllGraphDefine(){
        List<GraphDefine> graphDefines = graphDefineService.queryByCondition(new QueryWrapper<>());
        if(CollUtil.isEmpty(graphDefines)){
            return new HashMap<>();
        }
        return graphDefines.stream().collect(Collectors.toMap(GraphDefine::getCode, item->item, (k1, k2) -> k1));
    }

    /**
     * 获取边类型编码
     * @return
     */
    private Map<String, RelationDefine> queryAllRelDefine(){
        List<RelationDefine> relationDefines = relationDefineService.queryByCondition(new QueryWrapper<>());
        if(CollUtil.isEmpty(relationDefines)){
            return new HashMap<>();
        }
        return relationDefines.stream().collect(Collectors.toMap(RelationDefine::getCode,item->item,(k1,k2) -> k1));
    }

    /**
     * 新增信息点交付范围
     * @param voList 待新增数据
     * @return
     */
    @Override
    public List<RelationDefineCollect> insert(List<RelationDefineCollect> voList) throws Exception{
        //新增采集信息点
        saveOrUpdateBatch(voList);
        return voList;
    }

    /**
     * 修改信息点交付范围
     * @param voList 待修改数据
     * @return
     */
    @Override
    public List<RelationDefineCollect> update(List<RelationDefineCollect> voList) throws Exception {
        //修改采集信息点
        saveOrUpdateBatch(voList);
        return voList;
    }

    /**
     * 删除信息点交付范围
     * @param idList 主键
     */
    @Override
    public void delete(List<String> idList) {
        if(CollUtil.isEmpty(idList)) {
            return;
        }
        List<RelationDefineCollect> dbList = relationDefineCollectMapper.selectBatchIds(idList);
        Map<String, RelationDefineCollect> dbMap = CollectionUtil.fieldValueMap(dbList, BaseEntity.PROP_ID);
        idList.forEach(id -> {
            RelationDefineCollect dbVO = MapUtil.get(dbMap, id, RelationDefineCollect.class);
            // 容错处理
            if(dbVO == null) {
                return;
            }
            // 删除
            RelationDefineCollect vo = new RelationDefineCollect();
            vo.setId(id);
            vo.setValid(ValidEnum.FALSE.getType());
            relationDefineCollectMapper.updateById(vo);
        });
    }
}
