package com.xjrsoft.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.organization.entity.Role;
import com.xjrsoft.module.system.dto.AddXjrMenuGroupDto;
import com.xjrsoft.module.system.dto.AddXjrMenuGroupSetDto;
import com.xjrsoft.module.system.dto.UpdateXjrMenuGroupSetDto;
import com.xjrsoft.module.system.dto.XjrMenuGroupMenuListDto;
import com.xjrsoft.module.system.dto.XjrMenuGroupSetPageDto;
import com.xjrsoft.module.system.entity.XjrMenuGroupSet;
import com.xjrsoft.module.system.service.IXjrMenuGroupSetService;
import com.xjrsoft.module.system.vo.XjrMenuGroupMenuListVo;
import com.xjrsoft.module.system.vo.XjrMenuGroupSetPageVo;
import com.xjrsoft.module.system.vo.XjrMenuGroupSetVo;
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 dzx
* @Date: 2024-08-09
* @Version 1.0
*/
@RestController
@RequestMapping("/system" + "/xjrMenuGroupSet")
@Api(value = "/system"  + "/xjrMenuGroupSet",tags = "功能分类设置代码")
@AllArgsConstructor
public class XjrMenuGroupSetController {


    private final IXjrMenuGroupSetService xjrMenuGroupSetService;

    @GetMapping(value = "/page")
    @ApiOperation(value="功能分类设置列表(分页)")
    @SaCheckPermission("xjrmenugroupset:detail")
    public RT<PageOutput<XjrMenuGroupSetPageVo>> page(@Valid XjrMenuGroupSetPageDto dto){

        MPJLambdaWrapper<XjrMenuGroupSet> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .leftJoin(Role.class, Role::getId, XjrMenuGroupSet::getRoleId)
                .select(XjrMenuGroupSet::getId)
                .selectAs(Role::getName, XjrMenuGroupSetPageVo::getRoleName)
                .select(XjrMenuGroupSet.class,x -> VoToColumnUtil.fieldsToColumns(XjrMenuGroupSetPageVo.class).contains(x.getProperty()))
                .orderByDesc(XjrMenuGroupSet::getCreateDate)
                .disableSubLogicDel();
        IPage<XjrMenuGroupSetPageVo> page = xjrMenuGroupSetService.selectJoinListPage(ConventPage.getPage(dto), XjrMenuGroupSetPageVo.class, queryWrapper);
        PageOutput<XjrMenuGroupSetPageVo> pageOutput = ConventPage.getPageOutput(page, XjrMenuGroupSetPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询功能分类设置信息")
    @SaCheckPermission("xjrmenugroupset:detail")
    public RT<XjrMenuGroupSetVo> info(@RequestParam Long id){
        XjrMenuGroupSet xjrMenuGroupSet = xjrMenuGroupSetService.getByIdDeep(id);
        if (xjrMenuGroupSet == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(xjrMenuGroupSet, XjrMenuGroupSetVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增功能分类设置")
    @SaCheckPermission("xjrmenugroupset:add")
    public RT<Boolean> add(@Valid @RequestBody AddXjrMenuGroupSetDto dto){
        XjrMenuGroupSet xjrMenuGroupSet = BeanUtil.toBean(dto, XjrMenuGroupSet.class);
        boolean isSuccess = xjrMenuGroupSetService.add(xjrMenuGroupSet);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改功能分类设置")
    @SaCheckPermission("xjrmenugroupset:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateXjrMenuGroupSetDto dto){
        XjrMenuGroupSet xjrMenuGroupSet = BeanUtil.toBean(dto, XjrMenuGroupSet.class);
        return RT.ok(xjrMenuGroupSetService.update(xjrMenuGroupSet));
    }

    @DeleteMapping
    @ApiOperation(value = "删除功能分类设置")
    @SaCheckPermission("xjrmenugroupset:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(xjrMenuGroupSetService.delete(ids));
    }

    @GetMapping(value = "/menu-list")
    @ApiOperation(value = "查询学生菜单")
    @SaCheckPermission("xjrmenugroupset:detail")
    public RT<List<XjrMenuGroupMenuListVo>> menuList(@Valid XjrMenuGroupMenuListDto dto){
        XjrMenuGroupSet groupSet = xjrMenuGroupSetService.getById(dto.getXjrMenuGroupSetId());
        dto.setRoleId(groupSet.getRoleId());
        return RT.ok(xjrMenuGroupSetService.getMenuList(dto));
    }

    @PostMapping(value = "/set-menu")
    @ApiOperation(value = "配置功能")
    @SaCheckPermission("xjrmenugroupset:add")
    public RT<Boolean> setMenu(@Valid @RequestBody AddXjrMenuGroupDto dto){
        boolean isSuccess = xjrMenuGroupSetService.setMenu(dto);
        return RT.ok(isSuccess);
    }

}