package com.ruoyi.web.controller.buyerapi;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.buyerapi.domain.BuyerQuickMenu;
import com.ruoyi.buyerapi.service.IBuyerMenuService;
import com.ruoyi.buyerapi.service.IBuyerQuickMenuService;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 快捷菜单Controller
 *
 * @author ruoyi
 * @date 2022-10-06
 */
@Api(tags = "快捷菜单")
@RestController
@RequestMapping("/quick/menu")
public class BuyerQuickMenuController extends BaseController {
    @Autowired
    private IBuyerQuickMenuService buyerQuickMenuService;

    @Autowired
    private IBuyerMenuService buyerMenuService;

    /**
     * 查询快捷菜单列表
     */
    @ApiOperation(value = "查询快捷菜单列表")
    @GetMapping("/list")
    public TableDataInfo list(BuyerQuickMenu buyerQuickMenu) {
        startPage();
        List<BuyerQuickMenu> list = buyerQuickMenuService.selectBuyerQuickMenuList(buyerQuickMenu);
        return getDataTable(list);
    }

    /**
     * 获取用户快捷菜单信息
     */
    @ApiOperation(value = "获取用户快捷菜单信息")
    @GetMapping("/info")
    public AjaxResult info() {
        AjaxResult ajaxResult = new AjaxResult();
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> sysMenuList = new ArrayList<>();
        List<SysMenu> oldSysMenuList = buyerMenuService.selectMenusByUserId(userId);
        List<BuyerQuickMenu> haveList = buyerQuickMenuService.selectBuyerQuickMenuInfo();
        for (int i = 0; i < oldSysMenuList.size(); i++) {
            boolean isAdd = true;
            for (BuyerQuickMenu buyerQuickMenu : haveList) {
                if (buyerQuickMenu.getMenuId().equals(oldSysMenuList.get(i).getMenuId())) {
                    isAdd = false;
                    break;
                }
            }
            if(isAdd){
                sysMenuList.add(oldSysMenuList.get(i));
            }
        }
        // 添加上级
        for (BuyerQuickMenu buyerQuickMenu : haveList) {
            SysMenu sysMenu = buyerMenuService.selectMenuById(buyerQuickMenu.getParentId());
            if (sysMenu != null) {
                buyerQuickMenu.setParentPath(sysMenu.getPath());
            }
        }
        ajaxResult.put("code", 200);
        ajaxResult.put("msg", "操作成功");
        ajaxResult.put("haveList", haveList);
        ajaxResult.put("sysMenuList", sysMenuList);
        return ajaxResult;
    }

    /**
     * 导出快捷菜单列表
     */
    @ApiOperation(value = "导出快捷菜单列表")
    @Log(title = "快捷菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyerQuickMenu buyerQuickMenu) {
        List<BuyerQuickMenu> list = buyerQuickMenuService.selectBuyerQuickMenuList(buyerQuickMenu);
        ExcelUtil<BuyerQuickMenu> util = new ExcelUtil<BuyerQuickMenu>(BuyerQuickMenu.class);
        util.exportExcel(response, list, "快捷菜单数据");
    }

    /**
     * 获取快捷菜单详细信息
     */
    @ApiOperation(value = "获取快捷菜单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(buyerQuickMenuService.selectBuyerQuickMenuById(id));
    }

    /**
     * 快捷菜单调整接口
     */
    @ApiOperation(value = "快捷菜单调整接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuIds", value = "菜单数组", required = true, paramType = "query"),
    })
    @GetMapping(value = "/change")
    public AjaxResult change(Long[] menuIds) {
        Long userId = SecurityUtils.getUserId();
        List<Long> menuIdList = Arrays.asList(menuIds);
        List<SysMenu> sysMenuList = buyerMenuService.selectMenusByUserId(userId);
        for (int i = 0; i < sysMenuList.size(); i++) {
            for (Long menuId : menuIdList) {
                if (menuId.equals(sysMenuList.get(i).getMenuId())) {
                    sysMenuList.remove(i);
                }
            }
        }
        return AjaxResult.success(sysMenuList);
    }

    /**
     * 新增快捷菜单
     */
    @ApiOperation(value = "新增快捷菜单")
    @Log(title = "快捷菜单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyerQuickMenu buyerQuickMenu) {
        buyerQuickMenu.setCreateBy(getUsername());
        return toAjax(buyerQuickMenuService.insertBuyerQuickMenu(buyerQuickMenu));
    }

    /**
     * 批量新增快捷菜单
     */
    @ApiOperation(value = "批量新增快捷菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buyerQuickMenus", value = "快捷菜单实体数组", required = true, paramType = "query"),
    })
    @Log(title = "快捷菜单", businessType = BusinessType.INSERT)
    @PostMapping(value = "/batch")
    public AjaxResult batch(@RequestBody List<BuyerQuickMenu> buyerQuickMenus) {
        return toAjax(buyerQuickMenuService.batchInsertBuyerQuickMenu(buyerQuickMenus));
    }

    /**
     * 修改快捷菜单
     */
    @ApiOperation(value = "修改快捷菜单")
    @Log(title = "快捷菜单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyerQuickMenu buyerQuickMenu) {
        buyerQuickMenu.setUpdateBy(getUsername());
        return toAjax(buyerQuickMenuService.updateBuyerQuickMenu(buyerQuickMenu));
    }

    /**
     * 删除快捷菜单
     */
    @ApiOperation(value = "删除快捷菜单")
    @Log(title = "快捷菜单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(buyerQuickMenuService.deleteBuyerQuickMenuByIds(ids));
    }
}
