package cn.newfeifan.mall.module.controller.admin.shop;

import cn.newfeifan.mall.module.controller.admin.shop.vo.ShopRespVO;
import cn.newfeifan.mall.module.controller.admin.shop.vo.ShopPageReqVO;
import cn.newfeifan.mall.module.controller.admin.shop.vo.ShopSaveReqVO;
import cn.newfeifan.mall.module.dal.dataobject.shop.ShopDO;
import cn.newfeifan.mall.module.service.shop.ShopService;
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")
@Validated
public class ShopController {

    @Resource
    private ShopService shopService;

    @PostMapping("/create")
    @Operation(summary = "创建店铺")
    @PreAuthorize("@ss.hasPermission('sale:shop:create')")
    public CommonResult<Long> createShop(@Valid @RequestBody ShopSaveReqVO createReqVO) {
        return success(shopService.createShop(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店铺")
    @PreAuthorize("@ss.hasPermission('sale:shop:update')")
    public CommonResult<Boolean> updateShop(@Valid @RequestBody ShopSaveReqVO updateReqVO) {
        shopService.updateShop(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得店铺")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sale:shop:query')")
    public CommonResult<ShopRespVO> getShop(@RequestParam("id") Long id) {
        ShopDO shop = shopService.getShop(id);
        return success(BeanUtils.toBean(shop, ShopRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺分页")
    @PreAuthorize("@ss.hasPermission('sale:shop:query')")
    public CommonResult<PageResult<ShopRespVO>> getShopPage(@Valid ShopPageReqVO pageReqVO) {
        PageResult<ShopDO> pageResult = shopService.getShopPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShopRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店铺 Excel")
    @PreAuthorize("@ss.hasPermission('sale:shop:export')")
    @OperateLog(type = EXPORT)
    public void exportShopExcel(@Valid ShopPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopDO> list = shopService.getShopPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店铺.xls", "数据", ShopRespVO.class,
                        BeanUtils.toBean(list, ShopRespVO.class));
    }

}