package com.tashow.cloud.product.controller.admin;

import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageParam;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import com.tashow.cloud.excel.excel.core.util.ExcelUtils;
import com.tashow.cloud.productapi.api.product.dto.ShopDetailDO;
import com.tashow.cloud.product.service.ShopDetailService;
import com.tashow.cloud.productapi.api.product.vo.shopdetail
.ShopDetailPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.shopdetail
.ShopDetailRespVO;
import com.tashow.cloud.productapi.api.product.vo.shopdetail
.ShopDetailSaveReqVO;
import com.tashow.cloud.web.apilog.core.annotation.ApiAccessLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.tashow.cloud.common.pojo.CommonResult.success;
import static com.tashow.cloud.web.apilog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 店铺信息")
@RestController
@RequestMapping("/shop")
@Validated
public class ShopDetailController {

    @Resource
    private ShopDetailService shopDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建店铺信息")
    @PreAuthorize("@ss.hasPermission('tz:shop-detail:create')")
    public CommonResult<Long> createShopDetail(@Valid @RequestBody ShopDetailSaveReqVO createReqVO) {
        return success(shopDetailService.createShopDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店铺信息")
    @PreAuthorize("@ss.hasPermission('tz:shop-detail:update')")
    public CommonResult<Boolean> updateShopDetail(@Valid @RequestBody ShopDetailSaveReqVO updateReqVO) {
        shopDetailService.updateShopDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店铺信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:shop-detail:delete')")
    public CommonResult<Boolean> deleteShopDetail(@RequestParam("id") Long id) {
        shopDetailService.deleteShopDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:shop-detail:query')")
    public CommonResult<ShopDetailRespVO> getShopDetail(@RequestParam("id") Long id) {
        ShopDetailDO shopDetail = shopDetailService.getShopDetail(id);
        return success(BeanUtils.toBean(shopDetail, ShopDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺信息分页")
    @PreAuthorize("@ss.hasPermission('tz:shop-detail:query')")
    public CommonResult<PageResult<ShopDetailRespVO>> getShopDetailPage(@Valid ShopDetailPageReqVO pageReqVO) {
        PageResult<ShopDetailDO> pageResult = shopDetailService.getShopDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShopDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店铺信息 Excel")
    @PreAuthorize("@ss.hasPermission('tz:shop-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShopDetailExcel(@Valid ShopDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopDetailDO> list = shopDetailService.getShopDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店铺信息.xls", "数据", ShopDetailRespVO.class,
                        BeanUtils.toBean(list, ShopDetailRespVO.class));
    }

}