package cn.newfeifan.mall.sale.controller.admin.shopstatus;

import cn.newfeifan.mall.sale.controller.admin.shopstatus.vo.ShopStatusRespVO;
import cn.newfeifan.mall.sale.dal.dataobject.shopstatus.ShopStatusDO;
import cn.newfeifan.mall.sale.service.shopstatus.ShopStatusService;
import cn.newfeifan.mall.sale.controller.admin.shopstatus.vo.ShopStatusPageReqVO;
import cn.newfeifan.mall.sale.controller.admin.shopstatus.vo.ShopStatusSaveReqVO;
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 cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;

import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 店铺状态")
@RestController
@RequestMapping("/sale/shop-status")
@Validated
public class ShopStatusController {

    @Resource
    private ShopStatusService shopStatusService;

    @PostMapping("/create")
    @Operation(summary = "创建店铺状态")
    @PreAuthorize("@ss.hasPermission('sale:shop-status:create')")
    public CommonResult<Long> createShopStatus(@Valid @RequestBody ShopStatusSaveReqVO createReqVO) {
        return success(shopStatusService.createShopStatus(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店铺状态")
    @PreAuthorize("@ss.hasPermission('sale:shop-status:update')")
    public CommonResult<Boolean> updateShopStatus(@Valid @RequestBody ShopStatusSaveReqVO updateReqVO) {
        shopStatusService.updateShopStatus(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店铺状态")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sale:shop-status:delete')")
    public CommonResult<Boolean> deleteShopStatus(@RequestParam("id") Long id) {
        shopStatusService.deleteShopStatus(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺状态")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sale:shop-status:query')")
    public CommonResult<ShopStatusRespVO> getShopStatus(@RequestParam("id") Long id) {
        ShopStatusDO shopStatus = shopStatusService.getShopStatus(id);
        return success(BeanUtils.toBean(shopStatus, ShopStatusRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺状态分页")
    @PreAuthorize("@ss.hasPermission('sale:shop-status:query')")
    public CommonResult<PageResult<ShopStatusRespVO>> getShopStatusPage(@Valid ShopStatusPageReqVO pageReqVO) {
        PageResult<ShopStatusDO> pageResult = shopStatusService.getShopStatusPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShopStatusRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店铺状态 Excel")
    @PreAuthorize("@ss.hasPermission('sale:shop-status:export')")
    @OperateLog(type = EXPORT)
    public void exportShopStatusExcel(@Valid ShopStatusPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopStatusDO> list = shopStatusService.getShopStatusPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店铺状态.xls", "数据", ShopStatusRespVO.class,
                        BeanUtils.toBean(list, ShopStatusRespVO.class));
    }

}