
package com.jeelowcode.core.framework.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeelowcode.core.framework.entity.GroupDbFormEntity;
import com.jeelowcode.core.framework.entity.GroupDesFormEntity;
import com.jeelowcode.core.framework.entity.GroupReportEntity;
import com.jeelowcode.core.framework.entity.GroupTabEntity;
import com.jeelowcode.core.framework.service.IGroupDbFormService;
import com.jeelowcode.core.framework.service.IGroupDesFormService;
import com.jeelowcode.core.framework.service.IGroupReportService;
import com.jeelowcode.core.framework.service.IGroupTabService;
import com.jeelowcode.framework.global.JeeLowCodeBaseConstant;
import com.jeelowcode.framework.utils.model.global.BaseWebResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "低代码框架-表单开发分组")
@RestController
@AllArgsConstructor
@RequestMapping(JeeLowCodeBaseConstant.REQUEST_URL_START+"/group")
public class GroupController extends BaseController {

    private final IGroupDbFormService groupDbFormService;

    private final IGroupDesFormService groupDesFormService;

    private final IGroupReportService groupReportService;

    private final IGroupTabService groupTabService;

    //---------------------- 表单开发 -------------------------------
    @GetMapping("/dbform/detail")
    @ApiOperationSupport(order = 1)
    @Operation(tags = "表单开发分组",summary = "详情")
    public BaseWebResult<GroupDbFormEntity> dbformDetail(@RequestParam Long id){
        return BaseWebResult.success(groupDbFormService.getById(id));
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:dbform:create')")
    @PostMapping("/dbform/save")
    @ApiOperationSupport(order = 2)
    @Operation(tags = "表单开发分组",summary = "新增")
    public BaseWebResult dbformSave(@RequestBody GroupDbFormEntity model) {
        model.setId(IdWorker.getId());
        groupDbFormService.save(model);
        return BaseWebResult.success(model.getId());
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:dbform:update')")
    @PutMapping("/dbform/update")
    @ApiOperationSupport(order = 3)
    @Operation(tags = "表单开发分组",summary = "修改")
    public BaseWebResult dbformUpdate(@RequestBody GroupDbFormEntity model) {
        groupDbFormService.updateById(model);
        return BaseWebResult.success("成功");
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:dbform:delete')")
    @DeleteMapping("/dbform/delete")
    @ApiOperationSupport(order = 4)
    @Operation(tags = "表单开发分组",summary = "删除(逻辑删除)")
    public BaseWebResult dbformDel(@RequestBody List<Long> ids) {
        groupDbFormService.removeByIds(ids);
        return BaseWebResult.success("成功");
    }

    @GetMapping("/dbform/list")
    @ApiOperationSupport(order = 5)
    @Operation(tags = "表单开发分组",summary = "获取列表")
    public BaseWebResult dbformList(GroupDbFormEntity entity) {
        LambdaQueryWrapper<GroupDbFormEntity> wrapper=new LambdaQueryWrapper<>();
        wrapper.setEntity(entity);
        wrapper.orderByAsc(GroupDbFormEntity::getId);
        List<GroupDbFormEntity> dataList = groupDbFormService.list(wrapper);
        return BaseWebResult.success(dataList);
    }

    //---------------------- 表单设计 -------------------------------
    @GetMapping("/desform/detail")
    @ApiOperationSupport(order = 1)
    @Operation(tags = "自定义表单分组",summary = "详情")
    public BaseWebResult<GroupDesFormEntity> desformDetail(@RequestParam Long id){
        return BaseWebResult.success(groupDesFormService.getById(id));
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:desform:create')")
    @PostMapping("/desform/save")
    @ApiOperationSupport(order = 2)
    @Operation(tags = "自定义表单分组",summary = "新增")
    public BaseWebResult desformSave(@RequestBody GroupDesFormEntity model) {
        model.setId(IdWorker.getId());
        groupDesFormService.save(model);
        return BaseWebResult.success(model.getId());
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:desform:update')")
    @PutMapping("/update")
    @ApiOperationSupport(order = 3)
    @Operation(tags = "自定义表单分组",summary = "修改")
    public BaseWebResult desformUpdate(@RequestBody GroupDesFormEntity model) {
        groupDesFormService.updateById(model);
        return BaseWebResult.success("成功");
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:desform:delete')")
    @DeleteMapping("/desform/delete")
    @ApiOperationSupport(order = 4)
    @Operation(tags = "自定义表单分组",summary = "删除(逻辑删除)")
    public BaseWebResult desformDel(@RequestBody List<Long> ids) {
        groupDesFormService.removeByIds(ids);
        return BaseWebResult.success("成功");
    }

    @GetMapping("/desform/list")
    @ApiOperationSupport(order = 5)
    @Operation(tags = "自定义表单分组",summary = "获取列表")
    public BaseWebResult desformList(GroupDesFormEntity entity) {
        LambdaQueryWrapper<GroupDesFormEntity> wrapper=new LambdaQueryWrapper<>();
        wrapper.setEntity(entity);
        wrapper.orderByAsc(GroupDesFormEntity::getId);
        List<GroupDesFormEntity> dataList = groupDesFormService.list(wrapper);
        return BaseWebResult.success(dataList);
    }

    //---------------------- 报表 ---------------------------------------
    @GetMapping("/report/detail")
    @ApiOperationSupport(order = 1)
    @Operation(tags = "报表分组",summary = "详情")
    public BaseWebResult<GroupReportEntity> reportDetail(@RequestParam Long id){
        return BaseWebResult.success(groupReportService.getById(id));
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:report:create')")
    @PostMapping("/report/save")
    @ApiOperationSupport(order = 2)
    @Operation(tags = "报表分组",summary = "新增")
    public BaseWebResult reportSave(@RequestBody GroupReportEntity model) {
        model.setId(IdWorker.getId());
        groupReportService.save(model);
        return BaseWebResult.success(model.getId());
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:report:update')")
    @PutMapping("/report/update")
    @ApiOperationSupport(order = 3)
    @Operation(tags = "报表分组",summary = "修改")
    public BaseWebResult reportUpdate(@RequestBody GroupReportEntity model) {
        groupReportService.updateById(model);
        return BaseWebResult.success("成功");
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:report:delete')")
    @DeleteMapping("/report/delete")
    @ApiOperationSupport(order = 4)
    @Operation(tags = "报表分组",summary = "删除(逻辑删除)")
    public BaseWebResult reportDel(@RequestBody List<Long> ids) {
        groupReportService.removeByIds(ids);
        return BaseWebResult.success("成功");
    }


    @GetMapping("/report/list")
    @ApiOperationSupport(order = 5)
    @Operation(tags = "报表分组",summary = "获取列表")
    public BaseWebResult reportList(GroupReportEntity entity) {
        LambdaQueryWrapper<GroupReportEntity> wrapper=new LambdaQueryWrapper<>();
        wrapper.setEntity(entity);
        wrapper.orderByAsc(GroupReportEntity::getId);
        List<GroupReportEntity> dataList = groupReportService.list(wrapper);
        return BaseWebResult.success(dataList);
    }


    //---------------------- 报表 ---------------------------------------

    @GetMapping("/tab/detail")
    @ApiOperationSupport(order = 1)
    @Operation(tags = "报表分组",summary = "详情")
    public BaseWebResult<GroupTabEntity> tabDetail(@RequestParam Long id){
        return BaseWebResult.success(groupTabService.getById(id));
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:report:create')")
    @PostMapping("/tab/save")
    @ApiOperationSupport(order = 2)
    @Operation(tags = "报表分组",summary = "新增")
    public BaseWebResult tabSave(@RequestBody GroupTabEntity model) {
        model.setId(IdWorker.getId());
        groupTabService.save(model);
        return BaseWebResult.success(model.getId());
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:report:update')")
    @PutMapping("/tab/update")
    @ApiOperationSupport(order = 3)
    @Operation(tags = "报表分组",summary = "修改")
    public BaseWebResult tabUpdate(@RequestBody GroupTabEntity model) {
        groupTabService.updateById(model);
        return BaseWebResult.success("成功");
    }

    @PreAuthorize("@ss.hasPermission('jeelowcode:report:delete')")
    @DeleteMapping("/tab/delete")
    @ApiOperationSupport(order = 4)
    @Operation(tags = "报表分组",summary = "删除(逻辑删除)")
    public BaseWebResult tabDel(@RequestBody List<Long> ids) {
        groupTabService.removeByIds(ids);
        return BaseWebResult.success("成功");
    }


    @GetMapping("/tab/list")
    @ApiOperationSupport(order = 5)
    @Operation(tags = "报表分组",summary = "获取列表")
    public BaseWebResult tabList(GroupTabEntity entity) {
        LambdaQueryWrapper<GroupTabEntity> wrapper=new LambdaQueryWrapper<>();
        wrapper.setEntity(entity);
        wrapper.orderByAsc(GroupTabEntity::getId);
        List<GroupTabEntity> dataList = groupTabService.list(wrapper);
        return BaseWebResult.success(dataList);
    }


}
