package com.xjrsoft.module.app.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.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.app.dto.AddBaseAppFunctionDto;
import com.xjrsoft.module.app.dto.BaseAppFunctionListDto;
import com.xjrsoft.module.app.dto.BaseAppFunctionPageDto;
import com.xjrsoft.module.app.dto.UpdateBaseAppFunctionDto;
import com.xjrsoft.module.app.entity.BaseAppFunction;
import com.xjrsoft.module.app.service.IBaseAppFunctionService;
import com.xjrsoft.module.app.vo.BaseAppFunctionListVo;
import com.xjrsoft.module.app.vo.BaseAppFunctionPageVo;
import com.xjrsoft.module.app.vo.BaseAppFunctionVo;
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: APP端功能模块管理
* @Author fanxp
* @Date: 2023-11-07
* @Version 1.0
*/
@RestController
@RequestMapping("/app" + "/baseappfunction")
@Api(value = "/app"  + "/baseappfunction",tags = "APP端功能模块管理代码")
@AllArgsConstructor
public class BaseAppFunctionController {


    private final IBaseAppFunctionService baseAppFunctionService;

    @GetMapping(value = "/page")
    @ApiOperation(value="APP端功能模块管理列表(分页)")
    @SaCheckPermission("baseappfunction:detail")
    public RT<PageOutput<BaseAppFunctionPageVo>> page(@Valid BaseAppFunctionPageDto dto){

        LambdaQueryWrapper<BaseAppFunction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(BaseAppFunction::getId)
                .select(BaseAppFunction.class,x -> VoToColumnUtil.fieldsToColumns(BaseAppFunctionPageVo.class).contains(x.getProperty()));
        IPage<BaseAppFunction> page = baseAppFunctionService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<BaseAppFunctionPageVo> pageOutput = ConventPage.getPageOutput(page, BaseAppFunctionPageVo.class);
        return RT.ok(pageOutput);
    }
    @GetMapping(value = "/list")
    @ApiOperation(value="根据Type返回模块")
    @SaCheckPermission("baseappfunction:detail")
    public RT<List<BaseAppFunctionListVo>> list(@Valid BaseAppFunctionListDto dto) {

        LambdaQueryWrapper<BaseAppFunction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByAsc(BaseAppFunction::getSortCode)
                .eq(BaseAppFunction::getType, dto.getType())
                .select(BaseAppFunction.class, x -> VoToColumnUtil.fieldsToColumns(BaseAppFunctionListVo.class).contains(x.getProperty()));

        List<BaseAppFunction> list = baseAppFunctionService.list(queryWrapper);
        List<BaseAppFunctionListVo> listVos = BeanUtil.copyToList(list, BaseAppFunctionListVo.class);
        return RT.ok(listVos);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询APP端功能模块管理信息")
    @SaCheckPermission("baseappfunction:detail")
    public RT<BaseAppFunctionVo> info(@RequestParam Long id){
        BaseAppFunction baseAppFunction = baseAppFunctionService.getById(id);
        if (baseAppFunction == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseAppFunction, BaseAppFunctionVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增APP端功能模块管理")
    @SaCheckPermission("baseappfunction:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseAppFunctionDto dto){
        BaseAppFunction baseAppFunction = BeanUtil.toBean(dto, BaseAppFunction.class);
        boolean isSuccess = baseAppFunctionService.save(baseAppFunction);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改APP端功能模块管理")
    @SaCheckPermission("baseappfunction:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseAppFunctionDto dto){

        BaseAppFunction baseAppFunction = BeanUtil.toBean(dto, BaseAppFunction.class);
        return RT.ok(baseAppFunctionService.updateById(baseAppFunction));

    }

    @DeleteMapping
    @ApiOperation(value = "删除APP端功能模块管理")
    @SaCheckPermission("baseappfunction:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(baseAppFunctionService.removeBatchByIds(ids));

    }

}