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

import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.product.mapper.ProdMapper;
import com.tashow.cloud.product.service.ProdService;
import com.tashow.cloud.product.service.SkuService;
import com.tashow.cloud.productapi.api.product.dto.ProdDO;
import com.tashow.cloud.productapi.api.product.vo.prod.*;
import com.tashow.cloud.productapi.enums.BaseEnum;
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.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static com.tashow.cloud.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 商品")
@RestController
@RequestMapping("/product/prod")
@Validated
public class ProdController {

    @Resource
    private ProdService prodService;
    @Resource
    private SkuService skuService;
    @Resource
    private ProdMapper prodMapper;
    @PostMapping("/create")
    @Operation(summary = "创建商品")
    @PermitAll
    public CommonResult<Long> createProd(@Valid @RequestBody ProdSaveReqVO createReqVO) {
        return success(prodService.createProd(createReqVO));
    }



    @PostMapping("/createProdService")
    @Operation(summary = "创建商品服务配置")
    @PermitAll
    public CommonResult<Boolean> createProdService(@Valid @RequestBody ProdServiceVO prodServiceVO) {
        prodService.createProdService(prodServiceVO);
        return success(true);
    }

    @PostMapping("/uptateProdService")
    @Operation(summary = "修改商品服务配置")
    @PermitAll
    public CommonResult<Boolean> uptateProdService(@Valid @RequestBody ProdServiceInfoVO prodServiceInfoVO) {
        prodService.uptateProdService(prodServiceInfoVO);
        return success(true);
    }

    @GetMapping("/getProdService")
    @Operation(summary = "获得商品服务信息")
    @Parameter(name = "prodId", description = "商品id", required = true, example = "1024")
    @PermitAll
    public CommonResult<ProdServiceVO> getProd(@RequestParam("prodId") Long prodId) {
        ProdServiceVO prodServiceVO = prodService.getProdService(prodId);
        return success(prodServiceVO);
    }


   @PutMapping("/update")
    @Operation(summary = "更新商品")
   @PermitAll
    public CommonResult<Boolean> updateProd(@RequestBody ProdSaveReqVO updateReqVO) {
        prodService.updateProd(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品")
    @PermitAll
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteProd(@RequestParam("id") Long id) {
        prodService.deleteProd(id);
        return success(true);
    }




    @DeleteMapping("/deleteSkuList")
    @Operation(summary = "批量删除商品")
    @Parameter(name = "ids", description = "商品id", required = true)
    @PermitAll
    public CommonResult<Boolean> deleteSkuList(@RequestParam("ids") List<Long> ids) {
        for(Long id:ids){
            ProdDO prod = new ProdDO();
            prod.setProdId(id);
            prod.setDeleted(BaseEnum.YES_ONE.getKey());
            prod.setDeleteTime(new Date());
            // 删除
            prodMapper.deleteById(prod);
        }
        return success(true);
    }

    @DeleteMapping("/updateSkuShelfList")
    @Operation(summary = "批量上下架")
    @Parameter(name = "status", description = "默认是1，正常状态（出售中）, 0：下架（仓库中） 2：待审核", required = true)
    @PermitAll
    public CommonResult<Boolean> updateSkuShelfList(@RequestParam("ids") List<Long> ids,@RequestParam("status") Integer status) {
        for(Long id:ids){
            ProdDO prod = new ProdDO();
            prod.setProdId(id);
            prod.setStatus(status);
            prodMapper.updateById(prod);
        }
        return success(true);
    }



    @GetMapping("/getProdInfo")
    @Operation(summary = "获得商品详情a")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<ProdDO> getProdInfo(@RequestParam("id") Long id) {
        ProdDO prod = prodService.getProd(id);
        return success(prod);
    }

    @PermitAll
    @GetMapping("/page")
    @Operation(summary = "获得商品分页")
    public CommonResult<PageResult<ProdListVO>> getProdPage(ProdPageReqVO pageReqVO) {
        PageResult<ProdListVO> pageResult = prodService.getProdPage(pageReqVO);
        return success(pageResult);
    }

    @PermitAll
    @GetMapping("/getProdRecycleBinPageList")
    @Operation(summary = "获得商品回收站分页列表")
    public CommonResult<PageResult<ProdRestoreListVO>> getProdRecycleBinPageList(ProdRecycleBinVO prodRecycleBinVO) {
        PageResult<ProdRestoreListVO> pageResult = prodService.getProdRecycleBinPageList(prodRecycleBinVO);
        return success(pageResult);
    }


    @PostMapping("/restoreProdList")
    @Operation(summary = "恢复商品")
    @Parameter(name = "ids", description = "商品id集合", required = true)
    @PermitAll
    public CommonResult<Boolean> restoreProdList(@RequestParam("ids") List<Long> ids) {
        prodService.restoreProdList(ids);
        return success(true);
    }


    @PostMapping("/reduceStocks")
    @Operation(summary = "扣减库存")
    @PermitAll
    public CommonResult<Boolean> reduceStocks(@RequestParam("skuid") Long skuId,
                                              @RequestParam("stocksNum") Integer stocksNum) {
        skuService.reduceStocks(skuId, stocksNum);
        return success(true);
    }

    @PostMapping("/increaseStocks")
    @Operation(summary = "增加库存")
    @PermitAll
    public CommonResult<Boolean> increaseStocks(@RequestParam("skuid") Long skuId,
                                                @RequestParam("stocksNum") Integer stocksNum) {
        skuService.increaseStocks(skuId, stocksNum);
        return success(true);
    }


}