package co.yixiang.yshop.module.shop.controller.admin.canvas;

import co.yixiang.yshop.module.shop.controller.admin.canvasdetail.vo.CanvasDetailRespVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;

import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.shop.controller.admin.canvas.vo.*;
import co.yixiang.yshop.module.shop.dal.dataobject.canvas.CanvasDO;
import co.yixiang.yshop.module.shop.convert.canvas.CanvasConvert;
import co.yixiang.yshop.module.shop.service.canvas.CanvasService;

@Tag(name = "管理后台 - 画布信息")
@RestController
@RequestMapping("/shop/canvas")
@Validated
public class CanvasController {

    @Resource
    private CanvasService canvasService;

    @PostMapping("/create")
    @Operation(summary = "创建画布信息")
    @PreAuthorize("@ss.hasPermission('shop:canvas:create')")
    public CommonResult<Long> createCanvas(@Valid @RequestBody CanvasCreateReqVO createReqVO) {
        return success(canvasService.createCanvas(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新画布信息")
    @PreAuthorize("@ss.hasPermission('shop:canvas:update')")
    public CommonResult<Boolean> updateCanvas(@Valid @RequestBody CanvasUpdateReqVO updateReqVO) {
        canvasService.updateCanvas(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-json")
    @Operation(summary = "更新画布json")
    @PreAuthorize("@ss.hasPermission('shop:canvas:update-json')")
    public CommonResult<Boolean> updateCanvasJson(@Valid @RequestBody CanvasJsonUpdateReqVO jsonUpdateReqVO) {
        canvasService.updateCanvasJson(jsonUpdateReqVO);
        return success(true);
    }

    @GetMapping("/use")
    @Operation(summary = "应用画布")
    @PreAuthorize("@ss.hasPermission('shop:canvas:use')")
    public CommonResult<Boolean> useCanvas(@RequestParam("id") Long id) {
        canvasService.useCanvas(id);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除画布信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:canvas:delete')")
    public CommonResult<Boolean> deleteCanvas(@RequestParam("id") Long id) {
        canvasService.deleteCanvas(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得画布信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:canvas:query')")
    public CommonResult<CanvasRespVO> getCanvas(@RequestParam("id") Long id) {
        return success(canvasService.getCanvas(id));
    }

    @GetMapping("/getDetail")
    @Operation(summary = "获得画布明细信息")
    @PreAuthorize("@ss.hasPermission('shop:canvas:query-detail')")
    public CommonResult<CanvasDetailRespVO> getCanvasDetail(@Valid CanvasDetailReqVO reqVO) {
        return success(canvasService.getCanvasDetail(reqVO));
    }

    @GetMapping("/list")
    @Operation(summary = "获得画布信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:canvas:query')")
    public CommonResult<List<CanvasRespVO>> getCanvasList(@RequestParam("ids") Collection<Long> ids) {
        List<CanvasDO> list = canvasService.getCanvasList(ids);
        return success(CanvasConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得画布信息分页")
    @PreAuthorize("@ss.hasPermission('shop:canvas:query')")
    public CommonResult<PageResult<CanvasPageRespVO>> getCanvasPage(@Valid CanvasPageReqVO pageVO) {
        PageResult<CanvasDO> pageResult = canvasService.getCanvasPage(pageVO);
        return success(CanvasConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出画布信息 Excel")
    @PreAuthorize("@ss.hasPermission('shop:canvas:export')")
    @OperateLog(type = EXPORT)
    public void exportCanvasExcel(@Valid CanvasExportReqVO exportReqVO,
                                  HttpServletResponse response) throws IOException {
        List<CanvasDO> list = canvasService.getCanvasList(exportReqVO);
        // 导出 Excel
        List<CanvasExcelVO> datas = CanvasConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "画布信息.xls", "数据", CanvasExcelVO.class, datas);
    }

}
