package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeReverseGroupBidding;
import shop.alien.entity.store.LifeReverseGroupBuying;
import shop.alien.entity.store.vo.LifeReverseGroupBuyingStoreVo;
import shop.alien.entity.store.vo.LifeReverseGroupBuyingUserVo;
import shop.alien.store.service.LifeReverseGroupBuyingService;

/**
 * 用户反向团购表 前端控制器
 *
 * @author ssk
 * @since 2025-03-05
 */
@Slf4j
@Api(tags = {"反向团购"})
@ApiSort(17)
@CrossOrigin
@RestController
@RequestMapping("/lifeReverseGroupBuying")
@RequiredArgsConstructor
public class LifeReverseGroupBuyingController {

    private final LifeReverseGroupBuyingService lifeReverseGroupBuyingService;

    @ApiOperation(value = "用户-新增或修改反向团购")
    @ApiOperationSupport(order = 1)
    @PostMapping("/saveOrUpdate")
    public R<Boolean> saveOrUpdate(@RequestBody LifeReverseGroupBuying lifeReverseGroupBuying) {
        log.info("LifeReverseGroupBuyingController.saveOrUpdate?lifeReverseGroupBuying={}", lifeReverseGroupBuying.toString());
        return R.status(lifeReverseGroupBuyingService.saveOrUpdate(lifeReverseGroupBuying));
    }

    @ApiOperation("用户-反向团购列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态, 0:待审核, 1:未通过, 2:已通过, 3:待接单, 4:未接单(已截止), 5:待使用, 6:已完成, 7:已退款", dataType = "Integer", paramType = "query")
    })
    @ApiOperationSupport(order = 2)
    @GetMapping("/getUserList")
    public R<IPage<LifeReverseGroupBuyingUserVo>> getUserList(Integer pageNum, Integer pageSize, Integer userId, Integer status) {
        log.info("LifeReverseGroupBuyingController.getUserList?pageNum={}&pageSize={}&userId={}&status={}", pageNum, pageSize, userId, status);
        return R.data(lifeReverseGroupBuyingService.getUserList(pageNum, pageSize, userId, status));
    }

    @ApiOperation("反向团购详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "反向团购主键id", dataType = "Integer", paramType = "query", required = true)})
    @ApiOperationSupport(order = 3)
    @GetMapping("/getDetail")
    public R<LifeReverseGroupBuyingUserVo> getDetail(Integer id) {
        log.info("LifeReverseGroupBuyingController.getDetail?id={}", id);
        return R.data(lifeReverseGroupBuyingService.getDetail(id));
    }

    @ApiOperation("商家-反向团购列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "distance", value = "距离, 大于等多少米以内于多少米", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "dateSort", value = "时间排序, 0:倒序, 1:正序", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "price", value = "价格, 多少钱以内", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query", required = true)
    })
    @ApiOperationSupport(order = 3)
    @GetMapping("/getStoreList")
    public R<IPage<LifeReverseGroupBuyingStoreVo>> getStoreList(Integer pageNum, Integer pageSize, Integer distance, Integer dateSort, String price, Integer storeId) {
        log.info("LifeReverseGroupBuyingController.getStoreList?pageNum={}&pageSize={}&distance={}&dateSort={}&price={}&storeId={}", pageNum, pageSize, distance, dateSort, price, storeId);
        return R.data(lifeReverseGroupBuyingService.getStoreList(pageNum, pageSize, distance, dateSort, price, storeId));
    }

    @ApiOperation("用户-接受或拒绝商家")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "反向团购主键id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态, 0:拒绝, 1:接收", dataType = "Integer", paramType = "query", required = true)
    })
    @ApiOperationSupport(order = 4)
    @GetMapping("/receiveOrRefuse")
    public R<Boolean> receiveOrRefuse(Integer id, Integer userId, Integer storeId, Integer status) {
        log.info("LifeReverseGroupBuyingController.receiveOrRefuse?id={}&userId={}&storeId={}&status={}", id, userId, storeId, status);
        if (lifeReverseGroupBuyingService.receiveOrRefuse(id, userId, storeId, status)) {
            return R.success("操作成功");
        }
        return R.fail("操作失败");
    }

    @ApiOperation("商家-参与竞价")
    @ApiOperationSupport(order = 5)
    @PostMapping("/joinBidding")
    public R<Boolean> joinBidding(@RequestBody LifeReverseGroupBidding lifeReverseGroupBidding) {
        log.info("LifeReverseGroupBuyingController.joinBidding?lifeReverseGroupBidding={}", lifeReverseGroupBidding.toString());
        if (lifeReverseGroupBuyingService.joinBidding(lifeReverseGroupBidding)) {
            return R.success("操作成功");
        }
        return R.fail("操作失败");
    }

}
