package cn.iocoder.yudao.module.system.controller.admin.biz;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.GroupsPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.GroupsRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.GroupsSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.GroupsDO;
import cn.iocoder.yudao.module.system.service.biz.GroupsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @Author: jinyd
 * @CreateTime: 2024/9/6  14:56
 * @Description:
 * @Version: 4.0
 */


@Tag(name = "管理后台 - 集团")
@RestController
@RequestMapping("/system/groups")
@Validated
public class GroupsController {

    @Resource
    private GroupsService groupsService;

    @PostMapping("/create")
    @Operation(summary = "创建集团")
    @PreAuthorize("@ss.hasPermission('system:groups:create')")
    public CommonResult<Long> createGroups(@Valid @RequestBody GroupsSaveReqVO createReqVO) {
        return success(groupsService.createGroups(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新集团")
    @PreAuthorize("@ss.hasPermission('system:groups:update')")
    public CommonResult<Boolean> updateGroups(@Valid @RequestBody GroupsSaveReqVO updateReqVO) {
        groupsService.updateGroups(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除集团")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:groups:delete')")
    public CommonResult<Boolean> deleteGroups(@RequestParam("id") Long id) {
        groupsService.deleteGroups(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得集团")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:groups:query')")
    public CommonResult<GroupsRespVO> getGroups(@RequestParam("id") Long id) {
        GroupsDO groups = groupsService.getGroups(id);
        return success(BeanUtils.toBean(groups, GroupsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得集团分页")
    @PreAuthorize("@ss.hasPermission('system:groups:query')")
    public CommonResult<PageResult<GroupsRespVO>> getGroupsPage(@Valid GroupsPageReqVO pageReqVO) {
        PageResult<GroupsDO> pageResult = groupsService.getGroupsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GroupsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出集团 Excel")
    @PreAuthorize("@ss.hasPermission('system:groups:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGroupsExcel(@Valid GroupsPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GroupsDO> list = groupsService.getGroupsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "集团.xls", "数据", GroupsRespVO.class,
                BeanUtils.toBean(list, GroupsRespVO.class));
    }

}
