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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.commonlyused.vo.CommonlyUsedPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.commonlyused.vo.CommonlyUsedRespVO;
import cn.iocoder.yudao.module.system.controller.admin.commonlyused.vo.CommonlyUsedSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.commonlyused.vo.CommonlyUsedVO;
import cn.iocoder.yudao.module.system.dal.dataobject.commonlyused.CommonlyUsedDO;
import cn.iocoder.yudao.module.system.service.commonlyused.CommonlyUsedService;
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.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 用户常用应用配置")
@RestController
@RequestMapping("/system/commonly-used")
@Validated
public class CommonlyUsedController {

    @Resource
    private CommonlyUsedService commonlyUsedService;

    @PostMapping("/create")
    @Operation(summary = "创建用户常用应用配置")
    public CommonResult<Long> createCommonlyUsed(@Valid @RequestBody CommonlyUsedSaveReqVO createReqVO) {
        return success(commonlyUsedService.createCommonlyUsed(createReqVO));
    }

    @PostMapping("/batchCreateOrEditOrDel")
    @Operation(summary = "批量创建编辑删除用户常用应用配置")
    public CommonResult<?> batchCreateOrEditOrDel(@Valid @RequestBody List<CommonlyUsedSaveReqVO> list) {
        commonlyUsedService.batchCreateOrEditOrDel(list);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户常用应用配置")
    public CommonResult<Boolean> updateCommonlyUsed(@Valid @RequestBody CommonlyUsedSaveReqVO updateReqVO) {
        commonlyUsedService.updateCommonlyUsed(updateReqVO);
        return success(true);
    }

    @PutMapping("/batchUpdate")
    @Operation(summary = "批量更新用户常用应用配置")
    public CommonResult<Boolean> batchUpdate(@Valid @RequestBody List<CommonlyUsedSaveReqVO> list) {
        commonlyUsedService.batchUpdate(list);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户常用应用配置")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCommonlyUsed(@RequestParam("id") Long id) {
        commonlyUsedService.deleteCommonlyUsed(id);
        return success(true);
    }

    @DeleteMapping("/batchDelete")
    @Operation(summary = "批量删除用户常用应用配置")
    public CommonResult<Boolean> batchDelete(@RequestBody List<Long> ids) {
        commonlyUsedService.batchDelete(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户常用应用配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CommonlyUsedRespVO> getCommonlyUsed(@RequestParam("id") Long id) {
        CommonlyUsedDO commonlyUsed = commonlyUsedService.getCommonlyUsed(id);
        return success(BeanUtils.toBean(commonlyUsed, CommonlyUsedRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户常用应用配置列表")
    public CommonResult<CommonlyUsedVO> getCommonlyUsedList() {
        CommonlyUsedVO vo = commonlyUsedService.getCommonlyUsedList();
        return success(vo);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户常用应用配置 Excel")
    @OperateLog(type = EXPORT)
    public void exportCommonlyUsedExcel(@Valid CommonlyUsedPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CommonlyUsedDO> list = commonlyUsedService.getCommonlyUsedPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户常用应用配置.xls", "数据", CommonlyUsedRespVO.class,
                BeanUtils.toBean(list, CommonlyUsedRespVO.class));
    }

}
