package com.yc.ship.module.resource.controller.admin.site;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.resource.controller.admin.site.vo.ScenicSitePageReqVO;
import com.yc.ship.module.resource.controller.admin.site.vo.ScenicSiteRespVO;
import com.yc.ship.module.resource.controller.admin.site.vo.ScenicSiteSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.site.ScenicSiteDO;
import com.yc.ship.module.resource.service.site.ScenicSiteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
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 com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 站点")
@RestController
@RequestMapping("/resource/scenic-site")
@Validated
public class ScenicSiteController {

    @Resource
    private ScenicSiteService scenicSiteService;

    @PostMapping("/create")
    @Operation(summary = "创建站点")
    @PreAuthorize("@ss.hasPermission('product:scenic-site:create')")
    public CommonResult<Long> createScenicSite(@Valid @RequestBody ScenicSiteSaveReqVO createReqVO) {
        return success(scenicSiteService.createScenicSite(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新站点")
    @PreAuthorize("@ss.hasPermission('product:scenic-site:update')")
    public CommonResult<Boolean> updateScenicSite(@Valid @RequestBody ScenicSiteSaveReqVO updateReqVO) {
        scenicSiteService.updateScenicSite(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除站点")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:scenic-site:delete')")
    public CommonResult<Boolean> deleteScenicSite(@RequestParam("id") Long id) {
        scenicSiteService.deleteScenicSite(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得站点")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:scenic-site:query')")
    public CommonResult<ScenicSiteRespVO> getScenicSite(@RequestParam("id") Long id) {
        ScenicSiteDO scenicSite = scenicSiteService.getScenicSite(id);
        return success(BeanUtils.toBean(scenicSite, ScenicSiteRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得站点分页")
    @PreAuthorize("@ss.hasPermission('product:scenic-site:query')")
    public CommonResult<PageResult<ScenicSiteRespVO>> getScenicSitePage(@Valid ScenicSitePageReqVO pageReqVO) {
        PageResult<ScenicSiteDO> pageResult = scenicSiteService.getScenicSitePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ScenicSiteRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出站点 Excel")
    @PreAuthorize("@ss.hasPermission('product:scenic-site:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportScenicSiteExcel(@Valid ScenicSitePageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ScenicSiteDO> list = scenicSiteService.getScenicSitePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "站点数据.xls", "数据", ScenicSiteRespVO.class,
                BeanUtils.toBean(list, ScenicSiteRespVO.class));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取站点精简信息列表", description = "只包含被启用的站点，主要用于前端的下拉选项")
    public CommonResult<List<ScenicSiteRespVO>> getSimpleSiteList(@RequestParam("siteType") String siteTypeStr) {
        Integer siteType = StringUtils.isBlank(siteTypeStr) ? null : Integer.parseInt(siteTypeStr);
        List<ScenicSiteDO> list = scenicSiteService.getSiteListByStatus(siteType);
        return success(BeanUtils.toBean(list, ScenicSiteRespVO.class));
    }

}
