package com.czg.controller.admin;

import com.czg.annotation.SaAdminCheckPermission;
import com.czg.market.dto.GbWareDTO;
import com.czg.market.dto.GbWareQueryParamDTO;
import com.czg.market.entity.GbWare;
import com.czg.market.service.GbWareService;
import com.czg.order.dto.GbOrderQueryParam;
import com.czg.order.service.GbOrderService;
import com.czg.order.vo.GbOrderCountVO;
import com.czg.order.vo.GbWareVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 拼团商品
 *
 * @author ww
 */
@Slf4j
@RestController
@RequestMapping("/admin/ware")
public class GbWareController {

    @Resource
    private GbWareService wareService;
    @Resource
    private GbOrderService gbOrderService;

    @PostMapping("/upShopConfig")
    @SaAdminCheckPermission(parentName = "拼团商品", value = "ware:info:up", name = "拼团商品-修改")
    public CzgResult<Boolean> upShopConfig(@RequestBody GbWareDTO param) {
        AssertUtil.isNull(param.getOnlineStatus(), "操作失败,请选择状态");
        return CzgResult.success(gbOrderService.upShopConfig(param.getOnlineStatus(), StpKit.USER.getShopId()));
    }


    /**
     * 拼团商品统计
     */
    @GetMapping("/ware/count")
    @SaAdminCheckPermission(parentName = "拼团商品", value = "ware:info:list", name = "拼团商品-列表")
    public CzgResult<GbOrderCountVO> getWareCount(GbOrderQueryParam param) {
        if (param.getShopId() == null) {
            param.setShopId(StpKit.USER.getShopId());
        }
        return CzgResult.success(gbOrderService.countOrder(param));
    }

    @GetMapping("/getGbWarePage")
    @SaAdminCheckPermission(parentName = "拼团商品", value = "ware:info:list", name = "拼团商品-列表")
    public CzgResult<Page<GbWare>> getGbWarePage(GbWareQueryParamDTO param) {
        if (param.getShopId() == null) {
            param.setShopId(StpKit.USER.getShopId());
        }
        return CzgResult.success(wareService.getGbWarePage(param, param.getShopId(), true));
    }

    @PostMapping("/addGbWare")
    @SaAdminCheckPermission(parentName = "拼团商品", value = "ware:info:add", name = "拼团商品-新增")
    public CzgResult<Boolean> addGbWare(@RequestBody @Validated GbWareDTO param) {
        return CzgResult.success(wareService.addGbWare(param));
    }

    @PostMapping("/editOnlineStatus")
    @SaAdminCheckPermission(parentName = "拼团商品", value = "ware:info:up", name = "拼团商品-修改")
    public CzgResult<Boolean> editGbWareOnlineStatus(@RequestBody GbWareDTO param) {
        AssertUtil.isNull(param.getId(), "操作失败,请选择商品");
        AssertUtil.isNull(param.getOnlineStatus(), "操作失败,请选择商品状态");
        return CzgResult.success(gbOrderService.editGbWareOnlineStatus(param.getId(), param.getOnlineStatus()));
    }

    @PostMapping("/updateGbWareById")
    @SaAdminCheckPermission(parentName = "拼团商品", value = "ware:info:up", name = "拼团商品-修改")
    public CzgResult<Boolean> updateGbWareById(@RequestBody @Validated GbWareDTO param) {
        return CzgResult.success(wareService.updateGbWareById(param));
    }

    @DeleteMapping("/deleteGbWare/{id}")
    @SaAdminCheckPermission(parentName = "拼团商品", value = "ware:info:del", name = "拼团商品-删除")
    public CzgResult<Boolean> deleteGbWare(@PathVariable("id") Long id) {
        AssertUtil.isNull(id, "操作失败,请选择商品");
        return CzgResult.success(wareService.deleteGbWare(id));
    }

    /**
     * 拼团商品详情
     */
    @GetMapping("/ware/detail")
    public CzgResult<GbWareVO> getWareDetail(@RequestParam Long shopId, @RequestParam Long wareId) {
        return CzgResult.success(gbOrderService.getWareDetail(shopId, wareId, null));
    }
}
