package cn.ysk.cashier.controller;

import cn.ysk.cashier.dto.QueryReceiveDto;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.entity.TbShopCoupon;
import cn.ysk.cashier.mybatis.service.TbShopCouponService;
import cn.ysk.cashier.mybatis.vo.TbShopCouponVo;
import cn.ysk.cashier.utils.SecurityUtils;
import io.swagger.annotations.Api;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import cn.ysk.cashier.dto.TbShopCouponQueryCriteria;

import javax.annotation.Resource;

/**
 * 优惠券(TbShopCoupon)表控制层
 *
 * @author ww
 * @since 2024-10-22 15:43:24
 */
@Api(tags = "优惠券管理")
@RestController
@RequestMapping("/api/tbShopCoupon")
public class TbShopCouponController {
    /**
     * 服务对象
     */
    @Resource
    private TbShopCouponService tbShopCouponService;

    @GetMapping
    @ApiOperation("分页查询")
    public ResponseEntity<Object> selectAll(TbShopCouponQueryCriteria criteria) {
        return new ResponseEntity<>(tbShopCouponService.queryAll(criteria), HttpStatus.OK);
    }

    @GetMapping("{id}")
    @ApiOperation("通过Id查询详情")
    public TbShopCoupon selectOne(@PathVariable Integer id) {
        return tbShopCouponService.findById(id);
    }

    @GetMapping("/product/{couponId}")
    @ApiOperation("查询赠送的商品")
    public ResponseEntity<Object> queryActivatePros(@PathVariable("couponId") Integer couponId){
        return new ResponseEntity<>(tbShopCouponService.findActivatePros(couponId),HttpStatus.OK);
    }

    @PostMapping
    @ApiOperation("新增")
    public ResponseEntity<Object> insert(@RequestBody TbShopCouponVo tbShopCoupon) {
        if (tbShopCoupon.getType().equals(2) && CollectionUtils.isEmpty(tbShopCoupon.getProducts())) {
            throw new BadRequestException("赠送商品不可为空");
        }
        tbShopCoupon.setEditor(SecurityUtils.getCurrentUserNickName());
        return new ResponseEntity<>(tbShopCouponService.update(tbShopCoupon), HttpStatus.CREATED);
    }

    @PutMapping
    @ApiOperation("通过id修改")
    public ResponseEntity<Object> update(@RequestBody TbShopCouponVo tbShopCoupon) {
        if (tbShopCoupon.getType().equals(2) && CollectionUtils.isEmpty(tbShopCoupon.getProducts()))
            throw new BadRequestException("赠送商品不可为空");
        tbShopCouponService.update(tbShopCoupon);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @ApiOperation("删除优惠券")
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        tbShopCouponService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @PostMapping("/queryReceive")
    @ApiOperation("领取详情")
    public ResponseEntity<Object> queryReceive(@Validated @RequestBody QueryReceiveDto param) {
        return new ResponseEntity<>(tbShopCouponService.queryReceive(param), HttpStatus.OK);
    }

    @DeleteMapping("receive")
    @ApiOperation("删除用户的券")
    public ResponseEntity<Object> deleteReceive(@RequestBody Integer[] ids) {
        tbShopCouponService.deleteReceive(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("activateByOrderId")
    @ApiOperation("根据订单id获取可用优惠券信息")
    public ResponseEntity<Object> activateByOrderIds(@RequestParam Integer shopId, @RequestParam Integer orderId, @RequestParam(required = false) Integer memberId) {
        return ResponseEntity.ok(tbShopCouponService.getActivateByOrderIds(shopId, orderId, memberId));
    }
}

