package com.hzs.system.sys.controller.manage;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.sys.SysMenuColumn;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.sys.service.ISysMenuColumnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 菜单列维护控制器
 * @Author: jiang chao
 * @Time: 2023/6/7 11:35
 * @Classname: SysMenuColumnController
 * @PackageName: com.hzs.system.sys.controller.manage
 */
@RestController
@RequestMapping("/manage/menu-column")
public class SysMenuColumnController {

    @Autowired
    private ISysMenuColumnService iSysMenuColumnService;

    /**
     * 菜单列列表
     *
     * @param menuId 菜单ID
     * @return
     */
    @GetMapping("/list/{menuId}")
    public AjaxResult list(@PathVariable("menuId") Long menuId) {
        QueryWrapper<SysMenuColumn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_MENU", menuId);
        queryWrapper.orderByAsc("FIELD_SORT");
        return AjaxResult.success(iSysMenuColumnService.list(queryWrapper));
    }

    /**
     * 保存菜单列
     *
     * @param menuId    菜单ID
     * @param paramList 列参数列表
     * @return
     */
    @PostMapping("/{menuId}")
    public AjaxResult save(@PathVariable("menuId") Long menuId, @RequestBody List<SysMenuColumn> paramList) {
        if (CollectionUtil.isEmpty(paramList)) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        for (SysMenuColumn sysMenuColumn : paramList) {
            if (StringUtils.isAnyEmpty(sysMenuColumn.getFieldName(), sysMenuColumn.getFieldPerms()) || null == sysMenuColumn.getFieldSort()) {
                // 缺少参数
                return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
            }
        }

        iSysMenuColumnService.saveData(menuId, paramList, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

}
