package com.starry.admin.modules.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.modules.shop.module.entity.PlayCouponDetailsEntity;
import com.starry.admin.modules.shop.module.vo.PlayCouponDetailsQueryVo;
import com.starry.admin.modules.shop.module.vo.PlayCouponDetailsReturnVo;
import com.starry.admin.modules.shop.module.vo.PlayCouponDetailsUpdateUseStateVo;
import com.starry.admin.modules.shop.service.IPlayCouponDetailsService;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 优惠券详情Controller
 *
 * @author admin
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/shop/couponRecord")
public class PlayCouponDetailsController {

    @Resource
    private IPlayCouponDetailsService playCouponDetailsService;

    /**
     * 查询优惠券详情列表
     */
    // @PreAuthorize("@customSs.hasPermission('shop:couponDetails:list')")
    @PostMapping("/listByPage")
    public R listByPage(@Validated @RequestBody PlayCouponDetailsQueryVo vo) {
        IPage<PlayCouponDetailsReturnVo> list = playCouponDetailsService.selectByPage(vo);
        return R.ok(list);
    }

    /**
     * 获取优惠券详情详细信息
     */
    // @PreAuthorize("@customSs.hasPermission('shop:couponDetails:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") String id) {
        return R.ok(playCouponDetailsService.selectPlayCouponDetailsById(id));
    }

    /**
     * 新增优惠券详情
     */
    // @PreAuthorize("@customSs.hasPermission('shop:couponDetails:create')")
    @Log(title = "优惠券详情", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@RequestBody PlayCouponDetailsEntity playCouponDetails) {
        boolean success = playCouponDetailsService.create(playCouponDetails);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 更新优惠券使用状态
     *
     * @param vo 优惠券实体信息
     * @since 2024/7/4 17:15
     **/
    @PostMapping("/updateUseState")
    public R update(PlayCouponDetailsUpdateUseStateVo vo) {
        for (String couponId : vo.getCouponIds()) {
            playCouponDetailsService.selectPlayCouponDetailsById(couponId);
            PlayCouponDetailsEntity entity = new PlayCouponDetailsEntity();
            entity.setId(couponId);
            entity.setUseState(vo.getUseState());
            playCouponDetailsService.update(entity);
        }
        return R.ok();
    }

    /**
     * 修改优惠券详情
     */
    // @PreAuthorize("@customSs.hasPermission('shop:couponDetails:edit')")
    @Log(title = "优惠券详情", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{id}")
    public R update(@PathVariable String id, @RequestBody PlayCouponDetailsEntity playCouponDetails) {
        playCouponDetails.setId(id);
        boolean success = playCouponDetailsService.update(playCouponDetails);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除优惠券详情
     */
    // @PreAuthorize("@customSs.hasPermission('shop:couponDetails:remove')")
    @Log(title = "优惠券详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        for (String id : ids) {
            PlayCouponDetailsEntity entity = new PlayCouponDetailsEntity();
            entity.setId(id);
            entity.setUseState("3");
            playCouponDetailsService.update(entity);
        }
        return R.ok();
    }
}
