package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SysIndustryBo;
import org.dromara.system.domain.vo.SysIndustryVo;
import org.dromara.system.domain.SysIndustry;
import org.dromara.system.mapper.SysIndustryMapper;
import org.dromara.system.service.ISysIndustryService;

import javax.annotation.Resource;
import java.util.*;

/**
 * 行业字典信息Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-18
 */
@RequiredArgsConstructor
@Service
public class SysIndustryServiceImpl implements ISysIndustryService {

    private final SysIndustryMapper baseMapper;

    @Override
    public List<Map> selectIndustryList() {
        List<Map> list = new ArrayList();
        SysIndustryBo bo = new SysIndustryBo();
        bo.setIsUse(1);
        List<SysIndustryVo> voList = this.baseMapper.selectVoList(this.buildQueryWrapper(bo));
        voList.forEach(vo -> {
            if(vo.getParentId().equals(0L) && vo.getLevel().equals(1)){
                Map map = new TreeMap();
                map.put("value",vo.getId());
                map.put("label",vo.getName());
                list.add(map);
            }else{
                Map map = getSubMap(vo.getParentId(),list);

                List subList = (List)map.get("children");
                if(subList == null){
                    subList = new ArrayList();
                    map.put("children",subList);
                }

                Map submap = new HashMap();
                submap.put("value",vo.getId());
                submap.put("label",vo.getName());
                subList.add(submap);
            }
        });
        return list;
    }

    private Map getSubMap(Long id, List<Map> list) {
        for(Map m : list){
            Object v = m.get("value");
            if(v != null && id.equals(v)){
                return m;
            }
        }
        return null;
    }

    /**
     * 查询行业字典信息
     */
    @Override
    public SysIndustryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询行业字典信息列表
     */
    @Override
    public TableDataInfo<SysIndustryVo> queryPageList(SysIndustryBo bo, PageQuery pageQuery) {
        QueryWrapper<SysIndustry> lqw = buildQueryWrapper(bo);
        Page<SysIndustryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询行业字典信息列表
     */
    @Override
    public List<SysIndustryVo> queryList(SysIndustryBo bo) {
        QueryWrapper<SysIndustry> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<SysIndustry> buildQueryWrapper(SysIndustryBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<SysIndustry> lqw = new QueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(bo.getName()), "name", bo.getName());

        lqw.eq(bo.getParentId() != null, "parent_id", bo.getParentId());
        lqw.eq(bo.getIsUse() != null, "is_use", bo.getIsUse());
        lqw.eq(bo.getSort() != null, "i_sort", bo.getSort());
        lqw.eq(bo.getLevel() != null, "i_level", bo.getLevel());
        lqw.orderByAsc("parent_id","sort");
        return lqw;
    }

    /**
     * 新增行业字典信息
     */
    @Override
    public Boolean insertByBo(SysIndustryBo bo) {
        SysIndustry add = MapstructUtils.convert(bo, SysIndustry.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改行业字典信息
     */
    @Override
    public Boolean updateByBo(SysIndustryBo bo) {
        SysIndustry update = MapstructUtils.convert(bo, SysIndustry.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysIndustry entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除行业字典信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
