package com.sptg.permissions.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.permissions.api.dto.DictDto;
import com.sptg.permissions.api.entity.Dict;
import com.sptg.permissions.api.feigns.DictFeign;
import com.sptg.permissions.biz.service.DictServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * title: DictController
 * package: com.sptg.permissions.biz.controller
 * description: 数字字典-控制器
 *
 *
 *
 */
@RestController
@RequiredArgsConstructor
public class DictController implements DictFeign {

    private final DictServiceInterface dictServiceInterface;

    @Override
    @GetMapping(value = "/dict/getDictTreeList")
    public ResultBean<Dict> getDictTreeList() {
        //  构建查询体检
        Wrapper<Dict> wrapper = new EntityWrapper<>();
        //  获取菜单列表
        List<Dict> dictList = this.dictServiceInterface.selectList(wrapper);
        //  树形排序
        dictList = getTreeList(dictList);

        return new ResultBean<>(ResultEnum.SUCCESS,dictList);
    }

    public List<Dict> getTreeList(List<Dict> dictList){
        List<Dict> plist = new ArrayList<>();
        List<Dict> nlist = new ArrayList<>();
        dictList.forEach(dict -> {
            if (dict.getPid() == 0) {
                plist.add(dict);
            } else {
                nlist.add(dict);
            }
        });

        nlist.forEach(dict -> {
            Dict pDict = getDictInList(dict.getPid(), plist);
            if (pDict != null){
                List<Dict> subList = pDict.getSubList();
                if (subList == null){
                    subList = new ArrayList<>();
                }
                subList.add(dict);
                pDict.setSubList(subList);
            }
        });

        List<Dict> list = new ArrayList<>();
        Dict dict = new Dict();
        dict.setId(0L);
        dict.setDictValue("字典管理");
        dict.setSubList(plist);
        list.add(dict);
        return list;
    }

    public Dict getDictInList(Long pid, List<Dict> list){
        Dict pDict = null;
        for (Dict item: list) {
            if (item.getId() == pid){
                pDict = item;
                break;
            }
        }
        return pDict;
    }

    @Override
    @PostMapping(value = "/dict/insertDict")
    public ResultBean<Boolean> insertDict(@RequestBody DictDto dictDto) {
        dictDto.setCreateDate(new Date());
        dictDto.setStatus(OperationConstant.ZERO);
        return this.dictServiceInterface.insertDict(dictDto);
    }

    @Override
    public ResultBean<Boolean> updateDict(@RequestBody DictDto dictDto) {
        return this.dictServiceInterface.updateDict(dictDto);
    }

    @Override
    public ResultBean<Boolean> deleteDict(@RequestBody DictDto dictDto) {
        dictDto.setStatus(OperationConstant.MINUS_ONE);
        return new ResultBean<>(ResultEnum.SUCCESS,this.dictServiceInterface.updateById(dictDto));
    }

    @Override
    @GetMapping(value = "/dict/selectById")
    public Dict selectById(Long id) {
        return null;
    }

    @Override
    @PostMapping(value = "/dict/getByCode")
    public ResultBean<Dict> getByCode(@RequestBody String code) {
        return this.dictServiceInterface.getByCode(code);
    }


}
