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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 优惠券详情Controller
 *
 * @author admin
 * @since 2024-07-04
 */
@Api(tags = "优惠券记录管理", description = "优惠券使用记录管理相关接口，包括查询、新增、修改和删除等操作")
@RestController
@RequestMapping("/shop/couponRecord")
public class PlayCouponDetailsController {

    @Resource
    private IPlayCouponDetailsService playCouponDetailsService;

    /**
     * 查询优惠券详情列表
     */
    @ApiOperation(value = "分页查询优惠券记录", notes = "分页查询优惠券使用记录列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayCouponDetailsReturnVo.class, responseContainer = "Page")})
    // @PreAuthorize("@customSs.hasPermission('shop:couponDetails:list')")
    @PostMapping("/listByPage")
    public R listByPage(
            @ApiParam(value = "查询条件", required = true) @Validated @RequestBody PlayCouponDetailsQueryVo vo) {
        IPage<PlayCouponDetailsReturnVo> list = playCouponDetailsService.selectByPage(vo);
        return R.ok(list);
    }

    /**
     * 获取优惠券详情详细信息
     */
    @ApiOperation(value = "获取优惠券记录详情", notes = "根据ID获取优惠券使用记录详细信息")
    @ApiImplicitParam(name = "id", value = "记录ID", required = true, paramType = "path", dataType = "String", example = "1")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = PlayCouponDetailsEntity.class)})
    // @PreAuthorize("@customSs.hasPermission('shop:couponDetails:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") String id) {
        return R.ok(playCouponDetailsService.selectPlayCouponDetailsById(id));
    }

    /**
     * 新增优惠券详情
     */
    @ApiOperation(value = "新增优惠券记录", notes = "新增优惠券使用记录")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "添加失败，包含详细错误信息")})
    // @PreAuthorize("@customSs.hasPermission('shop:couponDetails:create')")
    @Log(title = "优惠券详情", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(
            @ApiParam(value = "优惠券记录信息", required = true) @RequestBody PlayCouponDetailsEntity playCouponDetails) {
        boolean success = playCouponDetailsService.create(playCouponDetails);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 更新优惠券使用状态
     *
     * @param vo
     *            优惠券实体信息
     * @since 2024/7/4 17:15
     **/
    @ApiOperation(value = "更新优惠券使用状态", notes = "批量更新优惠券使用状态")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "更新失败，包含详细错误信息")})
    @PostMapping("/updateUseState")
    public R update(@ApiParam(value = "更新状态信息", required = true) 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();
    }

    /**
     * 修改优惠券详情
     */
    @ApiOperation(value = "修改优惠券记录", notes = "修改优惠券使用记录信息")
    @ApiImplicitParam(name = "id", value = "记录ID", required = true, paramType = "path", dataType = "String", example = "1")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "修改失败，包含详细错误信息")})
    // @PreAuthorize("@customSs.hasPermission('shop:couponDetails:edit')")
    @Log(title = "优惠券详情", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{id}")
    public R update(@PathVariable String id,
            @ApiParam(value = "优惠券记录信息", required = true) @RequestBody PlayCouponDetailsEntity playCouponDetails) {
        playCouponDetails.setId(id);
        boolean success = playCouponDetailsService.update(playCouponDetails);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除优惠券详情
     */
    @ApiOperation(value = "删除优惠券记录", notes = "批量删除优惠券使用记录（逻辑删除）")
    @ApiImplicitParam(name = "ids", value = "记录ID数组", required = true, paramType = "path", dataType = "String[]", example = "1,2,3")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    // @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();
    }
}
