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.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.toolkit.MPJWrappers;
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.system.dto.AddSystemMenuCommonlyUsedDto;
import com.xjrsoft.module.system.dto.SystemMenuCommonlyUsedPageDto;
import com.xjrsoft.module.system.dto.UpdateSystemMenuCommonlyUsedDto;
import com.xjrsoft.module.system.entity.SystemMenuCommonlyUsed;
import com.xjrsoft.module.system.service.ISystemMenuCommonlyUsedService;
import com.xjrsoft.module.system.vo.SystemMenuCommonlyUsedPageVo;
import com.xjrsoft.module.system.vo.SystemMenuCommonlyUsedVo;
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 brealinxx
* @Date: 2024-04-18
* @Version 1.0
*/
@RestController
@RequestMapping("/system" + "/systemMenuCommonlyUsed")
@Api(value = "/system"  + "/systemMenuCommonlyUsed",tags = "常用功能设置代码")
@AllArgsConstructor
public class SystemMenuCommonlyUsedController {


    private final ISystemMenuCommonlyUsedService systemMenuCommonlyUsedService;

    @GetMapping(value = "/page")
    @ApiOperation(value="常用功能设置列表(分页)")
    @SaCheckPermission("systemmenucommonlyused:detail")
    public RT<PageOutput<SystemMenuCommonlyUsedPageVo>> page(@Valid SystemMenuCommonlyUsedPageDto dto){

        LambdaQueryWrapper<SystemMenuCommonlyUsed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(SystemMenuCommonlyUsed::getId)
                .eq(SystemMenuCommonlyUsed::getCreateUserId, StpUtil.getLoginIdAsLong())
                .select(SystemMenuCommonlyUsed.class,x -> VoToColumnUtil.fieldsToColumns(SystemMenuCommonlyUsedPageVo.class).contains(x.getProperty()));
        IPage<SystemMenuCommonlyUsed> page = systemMenuCommonlyUsedService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<SystemMenuCommonlyUsedPageVo> pageOutput = ConventPage.getPageOutput(page, SystemMenuCommonlyUsedPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/nopage")
    @ApiOperation(value = "常用功能设置列表(不分页)")
    @SaCheckPermission("systemmenucommonlyused:detail")
    public RT<List<SystemMenuCommonlyUsedPageVo>> noPage(@Valid SystemMenuCommonlyUsedPageDto dto) {

        MPJLambdaWrapper<SystemMenuCommonlyUsed> queryWrapper = MPJWrappers.<SystemMenuCommonlyUsed>lambdaJoin()
                .distinct()
                .orderByDesc(SystemMenuCommonlyUsed::getId)
                .select(SystemMenuCommonlyUsed.class, x -> VoToColumnUtil.fieldsToColumns(SystemMenuCommonlyUsedPageVo.class).contains(x.getProperty()))
                .select("t.id",SystemMenuCommonlyUsedPageVo::getId)
                .select("t1.icon_url",SystemMenuCommonlyUsedPageVo::getIconUrl)
                .select("t1.path",SystemMenuCommonlyUsedPageVo::getPath)
                .select("t1.remark",SystemMenuCommonlyUsedPageVo::getRemark)
                .select("t1.title",SystemMenuCommonlyUsedPageVo::getTitle)
                .select("t1.system_id",SystemMenuCommonlyUsedPageVo::getSystemId)
                .eq(SystemMenuCommonlyUsed::getCreateUserId, StpUtil.getLoginIdAsLong())
                .leftJoin("xjr_menu t1 on t1.id = t.menu_id");



        List<SystemMenuCommonlyUsedPageVo> dataList = systemMenuCommonlyUsedService.selectJoinList(SystemMenuCommonlyUsedPageVo.class,queryWrapper);
        // List<SystemMenuCommonlyUsedPageVo> treeVoList = TreeUtil.build(dataList);

        return RT.ok(dataList);
    }

    @DeleteMapping
    @ApiOperation(value = "删除常用功能设置")
    @SaCheckPermission("systemmenucommonlyused:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(systemMenuCommonlyUsedService.removeByMenuId(ids));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询常用功能设置信息")
    @SaCheckPermission("systemmenucommonlyused:detail")
    public RT<SystemMenuCommonlyUsedVo> info(@RequestParam Long id){
        SystemMenuCommonlyUsed systemMenuCommonlyUsed = systemMenuCommonlyUsedService.getById(id);
        if (systemMenuCommonlyUsed == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(systemMenuCommonlyUsed, SystemMenuCommonlyUsedVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增常用功能设置")
    @SaCheckPermission("systemmenucommonlyused:add")
    public RT<Boolean> add(@Valid @RequestBody AddSystemMenuCommonlyUsedDto dto){
        if(dto.getMenuId() == null){
            return RT.error("菜单id缺失");
        }
        SystemMenuCommonlyUsed systemMenuCommonlyUsed = BeanUtil.toBean(dto, SystemMenuCommonlyUsed.class);
        boolean isSuccess = systemMenuCommonlyUsedService.save(systemMenuCommonlyUsed);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改常用功能设置")
    @SaCheckPermission("systemmenucommonlyused:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateSystemMenuCommonlyUsedDto dto){

        SystemMenuCommonlyUsed systemMenuCommonlyUsed = BeanUtil.toBean(dto, SystemMenuCommonlyUsed.class);
        return RT.ok(systemMenuCommonlyUsedService.updateById(systemMenuCommonlyUsed));

    }



}