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

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;
import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import co.yixiang.yshop.module.shop.controller.admin.shop.vo.*;
import co.yixiang.yshop.module.shop.convert.shop.ShopConvert;
import co.yixiang.yshop.module.shop.dal.dataobject.shop.ShopDO;
import co.yixiang.yshop.module.shop.service.shop.ShopService;
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.Collection;
import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 门店")
@RestController
@RequestMapping("/mall/shop")
@Validated
public class ShopController {

    @Resource
    private ShopService shopService;

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

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

    @DeleteMapping("/delete")
    @Operation(summary = "删除门店")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mall: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('mall:shop:query')")
    public CommonResult<ShopRespVO> getShop(@RequestParam("id") Long id) {
        ShopDO shop = shopService.getShop(id);
        return success(ShopConvert.INSTANCE.convert(shop));
    }

    @GetMapping("/list")
    @Operation(summary = "获得门店列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('mall:shop:query')")
    public CommonResult<List<ShopRespVO>> getShopList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopDO> list = shopService.getShopList(ids);
        return success(ShopConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/shopList")
    @Operation(summary = "获得门店列表")
    @PreAuthorize("@ss.hasPermission('mall:shop:query')")
    public CommonResult<List<ShopRespVO>> getShopList(ShopExportReqVO shopExportReqVO) {
        List<ShopDO> list = shopService.getShopList(shopExportReqVO);
        return success(ShopConvert.INSTANCE.convertList(list));
    }


    @GetMapping("/page")
    @Operation(summary = "获得门店分页")
    @PreAuthorize("@ss.hasPermission('mall:shop:query')")
    public CommonResult<PageResult<ShopRespVO>> getShopPage(@Valid ShopPageReqVO pageVO) {
        PageResult<ShopDO> pageResult = shopService.getShopPage(pageVO);
        return success(ShopConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出门店 Excel")
    @PreAuthorize("@ss.hasPermission('mall:shop:export')")
    @OperateLog(type = EXPORT)
    public void exportShopExcel(@Valid ShopExportReqVO exportReqVO,
                                HttpServletResponse response) throws IOException {
        List<ShopDO> list = shopService.getShopList(exportReqVO);
        // 导出 Excel
        List<ShopExcelVO> datas = ShopConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "门店.xls", "数据", ShopExcelVO.class, datas);
    }

}
