package com.template.controller;


import com.template.api.SystemMenuControllerAPI;
import com.template.model.pojo.SystemMenu;
import com.template.model.result.CommonResult;
import com.template.model.vo.SystemMenuTreeVo;
import com.template.model.vo.SystemMenuVo;
import com.template.services.SystemMenuService;
import com.template.annotation.PassToken;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-03-30
 */
@RestController
public class SystemMenuController implements SystemMenuControllerAPI {

    @Autowired
    private SystemMenuService systemMenuService;

    @Override
    @PassToken
    public CommonResult<Integer> InsertPlaybackRecord(@RequestBody SystemMenu pb){
        CommonResult<Integer> result = systemMenuService.insertPlaybackRecord(pb); // 帮我们自动生成id
        return result;
    }

    @Override
    @PassToken
    public CommonResult<List<SystemMenu>> QueryPlaybackRecords(HttpServletRequest request) {
        CommonResult<List<SystemMenu>> result = systemMenuService.getPlaybackRecords();
        return result;
    }

    @Override
    @PassToken
    public CommonResult<List<SystemMenuVo>> QueryPlaybackRecordBySql() {
        CommonResult<List<SystemMenuVo>> result = systemMenuService.getPlaybackRecordBySql();
        return result;
    }

    @Override
    @PassToken
    public CommonResult<List<SystemMenu>> QueryPlaybackRecords() {
        return systemMenuService.getSystemMenus();
    }

    @Override
    @PassToken
    public CommonResult InsertSystemMenu(SystemMenu systemMenu) {
        return systemMenuService.insertSystemMenus(systemMenu);
    }

    @Override
    @PassToken
    public CommonResult DeleteSystemMenu(Integer userID) {
        return systemMenuService.deleteSystemMenu(userID);
    }

    @Override
    @PassToken
    public CommonResult<List<SystemMenuTreeVo>> QueryMenuTreeRecords() {
        List<SystemMenuTreeVo> newTrees = new ArrayList<>();

        newTrees = QueryMenuTreeRecords(0L);

        return CommonResult.ok().setResult(newTrees);
    }

    /*
    根据父类ID获取树形菜单数据
     */
    private List<SystemMenuTreeVo> QueryMenuTreeRecords(Long parentID){
        List<SystemMenuTreeVo> newTrees = new ArrayList<>();

        List<SystemMenu> datas = systemMenuService.getSystemMenusByParentId(parentID).getData();

        for (SystemMenu data : datas) {
            SystemMenuTreeVo item = SystemMenuTreeVo.builder()
                    .id(data.getId())
                    .parentId(data.getMenuParentId())
                    .menuName(data.getMenuName())
                    .build();
            List<SystemMenuTreeVo> news = QueryMenuTreeRecords(item.getId());
            if (news == null || news.size() == 0) {
                newTrees.add(item);
                continue;
            }else {
                item.setChildren(news);
                newTrees.add(item);
            }
        }

        return  newTrees;
    }
}

