package cn.iocoder.yudao.module.pms.controller.admin.app;

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.pms.controller.admin.app.vo.IotAppPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.app.vo.IotAppRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.app.vo.IotAppSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.app.vo.IotWgtRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.app.IotAppDO;
import cn.iocoder.yudao.module.pms.service.app.IotAppService;
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.annotation.security.PermitAll;
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;


@Tag(name = "管理后台 - app版本更新")
@RestController
@RequestMapping("/rq/iot-app")
@Validated
public class IotAppController {

    @Resource
    private IotAppService iotAppService;

    @PostMapping("/create")
    @Operation(summary = "创建app版本更新")
    @PreAuthorize("@ss.hasPermission('rq:iot-app:create')")
    public CommonResult<Long> createIotApp(@Valid @RequestBody IotAppSaveReqVO createReqVO) {
        return success(iotAppService.createIotApp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新app版本更新")
    @PreAuthorize("@ss.hasPermission('rq:iot-app:update')")
    public CommonResult<Boolean> updateIotApp(@Valid @RequestBody IotAppSaveReqVO updateReqVO) {
        iotAppService.updateIotApp(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得app版本更新")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-app:query')")
    public CommonResult<IotAppRespVO> getIotApp(@RequestParam("id") Long id) {
        IotAppDO iotApp = iotAppService.getIotApp(id);
        return success(BeanUtils.toBean(iotApp, IotAppRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得app版本更新分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-app:query')")
    public CommonResult<PageResult<IotAppRespVO>> getIotAppPage(@Valid IotAppPageReqVO pageReqVO) {
        PageResult<IotAppDO> pageResult = iotAppService.getIotAppPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotAppRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出app版本更新 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-app:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotAppExcel(@Valid IotAppPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotAppDO> list = iotAppService.getIotAppPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "app版本更新.xls", "数据", IotAppRespVO.class,
                        BeanUtils.toBean(list, IotAppRespVO.class));
    }

    @GetMapping("/new")
    @Operation(summary = "获取最新app版本信息")
    @PermitAll
    public CommonResult<IotAppRespVO> getNewIotAppRespVO() {
        IotAppDO aNew = iotAppService.getNew();
        return success(BeanUtils.toBean(aNew, IotAppRespVO.class));
    }

    @GetMapping("/newWgt")
    @Operation(summary = "获取最新app版本信息 wgt app内更新")
    @PermitAll
    public CommonResult<IotWgtRespVO> getNewIotWgtRespVO() {
        IotAppDO aNew = iotAppService.getNew();
        IotWgtRespVO wgt = new IotWgtRespVO();
        wgt.setWgtUrl(aNew.getUrl());
        wgt.setVersion(aNew.getAppVersion());
        wgt.setForce(true);
        return success(wgt);
    }
}
