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

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.resource.vo.*;
import com.yc.ship.module.resource.dal.dataobject.resource.ResourceDO;
import com.yc.ship.module.resource.service.resource.ResourceService;
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 com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 产品资源")
@RestController
@RequestMapping("/resource/resource")
@Validated
public class ResourceController {

    @Resource
    private ResourceService resourceService;

    @PostMapping("/create")
    @Operation(summary = "创建产品资源")
    @PreAuthorize("@ss.hasPermission('product:resource:create')")
    public CommonResult<Long> createResource(@Valid @RequestBody ResourceSaveReqVO createReqVO) {
        return success(resourceService.createResource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品资源")
    @PreAuthorize("@ss.hasPermission('product:resource:update')")
    public CommonResult<Boolean> updateResource(@Valid @RequestBody ResourceSaveReqVO updateReqVO) {
        resourceService.updateResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品资源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:resource:delete')")
    public CommonResult<Boolean> deleteResource(@RequestParam("id") Long id) {
        resourceService.deleteResource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品资源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:resource:query')")
    public CommonResult<ResourceRespVO> getResource(@RequestParam("id") Long id) {
        ResourceDO resource = resourceService.getResource(id);
        return success(BeanUtils.toBean(resource, ResourceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品资源分页")
    @PreAuthorize("@ss.hasPermission('product:resource:query')")
    public CommonResult<PageResult<ResourceRespVO>> getResourcePage(@Valid ResourcePageReqVO pageReqVO) {
        PageResult<ResourceDO> pageResult = resourceService.getResourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResourceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品资源 Excel")
    @PreAuthorize("@ss.hasPermission('product:resource:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportResourceExcel(@Valid ResourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceDO> list = resourceService.getResourcePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品资源.xls", "数据", ResourceRespVO.class,
                        BeanUtils.toBean(list, ResourceRespVO.class));
    }

    @GetMapping({"/list-all-simple", "simple-list"})
    @Operation(summary = "获取资源精简信息列表")
    public CommonResult<List<ResourceSimpleRespVO>> getSimpleResourceList() {
        List<ResourceDO> list = resourceService.getSimpleResourceList();
        return success(BeanUtils.toBean(list, ResourceSimpleRespVO.class));
    }

    @GetMapping({"/getProductListByAccess"})
    @Operation(summary = "获取第三方直连资源列表")
    public CommonResult<List<ResourceAccessRespVO>> getProductListByAccess(String supplierId) {
        List<ResourceAccessRespVO> list = resourceService.getProductListByAccessList(supplierId);
        return success(list);
    }

    @GetMapping("/list-by-category")
    @Operation(summary = "获取资源精简信息列表")
    public CommonResult<List<ResourceSimpleRespVO>> getResourceList(Long categoryId) {
        List<ResourceDO> list = resourceService.getResourceByCategoryId(categoryId);
        return success(BeanUtils.toBean(list, ResourceSimpleRespVO.class));
    }

}