package com.xjrsoft.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.system.dto.AddXjrMenuQuickDto;
import com.xjrsoft.module.system.dto.SaveRoleXjrMenuQuickDto;
import com.xjrsoft.module.system.dto.SaveXjrMenuQuickDto;
import com.xjrsoft.module.system.dto.UpdateXjrMenuQuickDto;
import com.xjrsoft.module.system.dto.XjrMenuQuickListDto;
import com.xjrsoft.module.system.entity.XjrMenuQuick;
import com.xjrsoft.module.system.service.IXjrMenuQuickService;
import com.xjrsoft.module.system.vo.XjrMenuQuickListVo;
import com.xjrsoft.module.system.vo.XjrMenuQuickVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @title: 快捷菜单
 * @Author fanxp
 * @Date: 2023-11-11
 * @Version 1.0
 */
@RestController
@RequestMapping("/system" + "/xjrmenuquick")
@Api(value = "/system" + "/xjrmenuquick", tags = "快捷菜单代码")
@AllArgsConstructor
public class XjrMenuQuickController {


    private final IXjrMenuQuickService xjrMenuQuickService;

    @GetMapping(value = "/list")
    @ApiOperation(value = "快捷菜单列表(不分页)")
    @SaCheckPermission("xjrmenuquick:detail")
    public RT<List<XjrMenuQuickListVo>> list(@Valid XjrMenuQuickListDto dto) {

        LambdaQueryWrapper<XjrMenuQuick> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(XjrMenuQuick::getId)
                .select(XjrMenuQuick.class, x -> VoToColumnUtil.fieldsToColumns(XjrMenuQuickListVo.class).contains(x.getProperty()));

        List<XjrMenuQuick> list = xjrMenuQuickService.list(queryWrapper);
        List<XjrMenuQuickListVo> listVos = BeanUtil.copyToList(list, XjrMenuQuickListVo.class);
        return RT.ok(listVos);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询快捷菜单信息")
    @SaCheckPermission("xjrmenuquick:detail")
    public RT<XjrMenuQuickVo> info(@RequestParam Long id) {
        XjrMenuQuick xjrMenuQuick = xjrMenuQuickService.getById(id);
        if (xjrMenuQuick == null) {
            return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(xjrMenuQuick, XjrMenuQuickVo.class));
    }


    @PostMapping(value = "/role-save")
    @ApiOperation(value = "保存角色快捷菜单")
    @SaCheckPermission("xjrmenuquick:add")
    public RT<Boolean> roleSave(@Valid @RequestBody SaveRoleXjrMenuQuickDto dto) {
        SaveXjrMenuQuickDto params = new SaveXjrMenuQuickDto() {{
            setRoleId(dto.getRoleId());
            setDataType(2);
            setMenus(dto.getMenus());
        }};
        boolean isSuccess = xjrMenuQuickService.save(params);
        return RT.ok(isSuccess);
    }

    @PostMapping(value = "/user-save")
    @ApiOperation(value = "保存用户快捷菜单")
    @SaCheckPermission("xjrmenuquick:add")
    public RT<Boolean> userSave(@Valid @RequestBody List<AddXjrMenuQuickDto> dto) {
        SaveXjrMenuQuickDto params = new SaveXjrMenuQuickDto() {{
            setUserId(StpUtil.getLoginIdAsLong());
            setDataType(1);
            setMenus(dto);
        }};
        boolean isSuccess = xjrMenuQuickService.save(params);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改快捷菜单")
    @SaCheckPermission("xjrmenuquick:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateXjrMenuQuickDto dto) {

        XjrMenuQuick xjrMenuQuick = BeanUtil.toBean(dto, XjrMenuQuick.class);
        return RT.ok(xjrMenuQuickService.updateById(xjrMenuQuick));

    }

    @DeleteMapping
    @ApiOperation(value = "删除快捷菜单")
    @SaCheckPermission("xjrmenuquick:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids) {
        return RT.ok(xjrMenuQuickService.removeBatchByIds(ids));

    }

}