package cn.iocoder.yudao.module.member.controller.admin.appup;

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.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.appup.vo.AppUpPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.appup.vo.AppUpRespVO;
import cn.iocoder.yudao.module.member.controller.admin.appup.vo.AppUpSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.appup.AppUpDO;
import cn.iocoder.yudao.module.member.service.appup.AppUpService;
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.validation.annotation.Validated;
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.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 = "管理后台 - app更新")
@RestController
@RequestMapping("/member/app-up")
@Validated
public class AppUpController {

    @Resource
    private AppUpService appUpService;

    @PostMapping("/create")
    @Operation(summary = "创建app更新")
    //@PreAuthorize("@ss.hasPermission('member:app-up:create')")
    public CommonResult<Long> createAppUp(@Valid @RequestBody AppUpSaveReqVO createReqVO) {
        return success(appUpService.createAppUp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新app更新")
    //@PreAuthorize("@ss.hasPermission('member:app-up:update')")
    public CommonResult<Boolean> updateAppUp(@Valid @RequestBody AppUpSaveReqVO updateReqVO) {
        appUpService.updateAppUp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除app更新")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('member:app-up:delete')")
    public CommonResult<Boolean> deleteAppUp(@RequestParam("id") Long id) {
        appUpService.deleteAppUp(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得app更新")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('member:app-up:query')")
    public CommonResult<AppUpRespVO> getAppUp(@RequestParam("id") Long id) {
        AppUpDO appUp = appUpService.getAppUp(id);
        return success(BeanUtils.toBean(appUp, AppUpRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得app更新分页")
    //@PreAuthorize("@ss.hasPermission('member:app-up:query')")
    public CommonResult<PageResult<AppUpRespVO>> getAppUpPage(@Valid AppUpPageReqVO pageReqVO) {
        PageResult<AppUpDO> pageResult = appUpService.getAppUpPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppUpRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出app更新 Excel")
    //@PreAuthorize("@ss.hasPermission('member:app-up:export')")
    @OperateLog(type = EXPORT)
    public void exportAppUpExcel(@Valid AppUpPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppUpDO> list = appUpService.getAppUpPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "app更新.xls", "数据", AppUpRespVO.class,
                        BeanUtils.toBean(list, AppUpRespVO.class));
    }

}