package com.persagy.dmp.rwd.define.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.LambdaQueryWrapper;
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.ObjectInfoCollect;
import com.persagy.dmp.rwd.define.dao.ObjectInfoCollectMapper;
import com.persagy.dmp.rwd.define.service.IObjectInfoCollectService;
import com.persagy.dmp.rwd.define.service.IObjectInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 平台、项目级信息点采集
 * @author:linhuili
 * @date:2021/10/13
 */
@Service
public class ObjectInfoCollectServiceImpl extends ServiceImpl<ObjectInfoCollectMapper,ObjectInfoCollect> implements IObjectInfoCollectService {

    @Autowired
    private ObjectInfoCollectMapper objectInfoCollectMapper;

    @Autowired
    private IObjectInfoService objectInfoService;

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

    /**
     * 查询采集的信息点
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public List<ObjectInfoCollect> queryByCondition(Wrapper<ObjectInfoCollect> queryWrapper) {
        return objectInfoCollectMapper.selectList(queryWrapper);
    }

    /**
     * 分页询采集的信息点
     * @param page 分页条件
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public Page queryByCondition(Page page, Wrapper<ObjectInfoCollect> queryWrapper) {
        return objectInfoCollectMapper.selectPage(page, queryWrapper);
    }

    /**
     * 新增采集的信息点
     * @param voList 待新增数据
     * @return
     */
    @Override
    public List<ObjectInfoCollect> insert(List<ObjectInfoCollect> voList) throws Exception{
        //填充信息点采集ID
        fillObjectInfoCollectId(voList);
        //新增采集信息点
        saveOrUpdateBatch(voList);
        return voList;
    }

    /**
     * 修改采集的信息点
     * @param voList 待修改数据
     * @return
     */
    @Override
    public List<ObjectInfoCollect> update(List<ObjectInfoCollect> voList) throws Exception {
        //填充信息点采集ID
        fillObjectInfoCollectId(voList);
        //修改采集信息点
        saveOrUpdateBatch(voList);
        return voList;
    }

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

    /**
     * 校验信息点是否已被采集，若已被采集，填充相应的采集ID
     * @param voList
     */
    private void fillObjectInfoCollectId(List<ObjectInfoCollect> voList){
        if(CollUtil.isEmpty(voList)){
            return;
        }
        for (ObjectInfoCollect objectInfoCollect : voList) {
            if(StringUtils.isNotEmpty(objectInfoCollect.getId()) || StringUtils.isEmpty(objectInfoCollect.getDefineInfoId())){
                continue;
            }
            //查询采集信息点
            LambdaQueryWrapper<ObjectInfoCollect> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ObjectInfoCollect::getGroupCode,objectInfoCollect.getGroupCode());
            wrapper.eq(ObjectInfoCollect::getProjectId,objectInfoCollect.getProjectId());
            wrapper.eq(ObjectInfoCollect::getDefineInfoId,objectInfoCollect.getDefineInfoId());
            wrapper.eq(ObjectInfoCollect::getValid,true);
            List<ObjectInfoCollect> objectInfoCollects = queryByCondition(wrapper);
            if(CollUtil.isEmpty(objectInfoCollects)){
                //信息点未被采集
                continue;
            }else{
                //信息点已采集
                objectInfoCollect.setId(objectInfoCollects.get(0).getId());
            }
        }
    }
}
