package cn.iocoder.yudao.module.member.controller.admin.storesalegoods;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.storesalegoods.vo.StoreSaleGoodsPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storesalegoods.vo.StoreSaleGoodsRespVO;
import cn.iocoder.yudao.module.member.controller.admin.storesalegoods.vo.StoreSaleGoodsSaveBatchVO;
import cn.iocoder.yudao.module.member.controller.admin.storesalegoods.vo.StoreSaleGoodsSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storesalegoods.StoreSaleGoodsDO;
import cn.iocoder.yudao.module.member.service.async.MemberAsyncService;
import cn.iocoder.yudao.module.member.service.storesalegoods.StoreSaleGoodsService;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 售卖商品")
@RestController
@RequestMapping("/member/store-sale-goods")
@Validated
public class StoreSaleGoodsController {

    @Resource
    private StoreSaleGoodsService storeSaleGoodsService;
    @Resource
    private MemberAsyncService memberAsyncService;

    @PostMapping("/create")
    @Operation(summary = "创建售卖商品(上架)")
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:create')")
    public CommonResult<Integer> createStoreSaleGoods(@Valid @RequestBody StoreSaleGoodsSaveBatchVO createReqVO) {
        Integer storeSaleGoods = storeSaleGoodsService.createStoreSaleGoods(createReqVO);
        return success(storeSaleGoods);
    }

    @PutMapping("/update")
    @Operation(summary = "更新售卖商品")
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:update')")
    public CommonResult<Boolean> updateStoreSaleGoods(@Valid @RequestBody StoreSaleGoodsSaveReqVO updateReqVO) {
        storeSaleGoodsService.updateStoreSaleGoods(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除售卖商品(下架)")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:delete')")
    public CommonResult<Boolean> deleteStoreSaleGoods(@RequestParam("id") String id) {
        storeSaleGoodsService.deleteStoreSaleGoods(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得售卖商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:query')")
    public CommonResult<StoreSaleGoodsRespVO> getStoreSaleGoods(@RequestParam("id") Long id) {
        StoreSaleGoodsDO storeSaleGoods = storeSaleGoodsService.getStoreSaleGoods(id);
        return success(BeanUtils.toBean(storeSaleGoods, StoreSaleGoodsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得售卖商品分页")
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:query')")
    public CommonResult<PageResult<StoreSaleGoodsRespVO>> getStoreSaleGoodsPage(@Valid StoreSaleGoodsPageReqVO pageReqVO) {
        PageResult<StoreSaleGoodsDO> pageResult = storeSaleGoodsService.getStoreSaleGoodsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreSaleGoodsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出售卖商品 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreSaleGoodsExcel(@Valid StoreSaleGoodsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreSaleGoodsDO> list = storeSaleGoodsService.getStoreSaleGoodsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "售卖商品.xls", "数据", StoreSaleGoodsRespVO.class,
                        BeanUtils.toBean(list, StoreSaleGoodsRespVO.class));
    }


}