package com.persagy.dmp.codetable.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.persagy.dmp.codetable.dao.CodeTableDataMapper;
import com.persagy.dmp.codetable.entity.CodeTableData;
import com.persagy.dmp.codetable.entity.CodeTableType;
import com.persagy.dmp.codetable.service.ICodeTableDataService;
import com.persagy.dmp.codetable.service.ICodeTableTypeService;
import com.persagy.dmp.common.constant.ValidEnum;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.entity.BaseEntity;
import com.persagy.dmp.mybatis.utils.ConditionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 码表数据 实现类
 * @author Charlie Yu
 * @date 2021-07-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CodeTableDataServiceImpl implements ICodeTableDataService {

    @Resource
    private CodeTableDataMapper dao;
    @Autowired
    private ICodeTableTypeService typeService;

    @Override
    public CodeTableData insert(CodeTableData vo) {
        dao.insert(vo);
        return vo;
    }

    @Override
    public CodeTableData update(CodeTableData vo) {
        dao.updateById(vo);
        return vo;
    }

    @Override
    public void delete(String id) {
        CodeTableData vo = load(id);
        if(vo == null) {
            throw new BusinessException("找不到对应的数据！");
        }
        vo.setValid(ValidEnum.FALSE.getType());
        dao.updateById(vo);
    }

    @Override
    public CodeTableData load(String id) {
        return dao.selectById(id);
    }

    @Override
    public List<CodeTableData> queryByCondition(Wrapper<CodeTableData> queryWrapper) {
        return dao.selectList(queryWrapper);
    }

    @Override
    public List<CodeTableData> queryAllByTypeCode(String projectId, String typeCode) {
        CodeTableType codeType = typeService.loadByCode(projectId, typeCode);
        if(codeType == null) {
            return null;
        }
        LambdaQueryWrapper<CodeTableData> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(CodeTableData::getId, CodeTableData::getParentId, CodeTableData::getCode, CodeTableData::getName, CodeTableData::getExtras);
        wrapper.eq(CodeTableData::getTypeId, codeType.getId());
        wrapper.eq(CodeTableData::getValid, ValidEnum.TRUE.getType());
        ConditionUtil.ensureProjectCriteria(wrapper, CodeTableData::getProjectId, projectId);
        wrapper.orderByAsc(CodeTableData::getShowOrder);
        List<CodeTableData> dataList = dao.selectList(wrapper);
        if(CollUtil.isEmpty(dataList)) {
            return null;
        } else if(!codeType.isTree()) {
            // 列表结构直接返回即可
            return dataList;
        }
        // 转换为树型
        Map<String, CodeTableData> dataMap = CollUtil.fieldValueMap(dataList, BaseEntity.PROP_ID);
        List<CodeTableData> rsList = new ArrayList<>();
        for(CodeTableData data:dataList) {
            String parentId = data.getParentId();
            // 如果找不到上级节点，直接添加到结果列表中
            if(StrUtil.isBlank(parentId) || dataMap.get(parentId) == null) {
                rsList.add(data);
                continue;
            }
            // 能找到上级节点，则放入上级节点的子节点列表中
            CodeTableData parent = dataMap.get(parentId);
            if(parent.getContent() == null) {
                parent.setContent(new ArrayList<>());
            }
            parent.getContent().add(data);
        }
        return rsList;
    }
}
