package com.yongqi.xinrenli.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.yongqi.xinrenli.domain.vo.DbUserCouponVo;
import com.yongqi.xinrenli.domain.bo.DbUserCouponBo;
import com.yongqi.xinrenli.service.IDbUserCouponService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户优惠券关联
 *
 * @author zyf
 * @date 2023-03-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xinrenli/userCoupon")
public class DbUserCouponController extends BaseController {

    private final IDbUserCouponService iDbUserCouponService;

    /**
     * 查询用户优惠券关联列表
     */
    @SaCheckPermission("xinrenli:userCoupon:list")
    @GetMapping("/list")
    public TableDataInfo<DbUserCouponVo> list(DbUserCouponBo bo, PageQuery pageQuery) {
        return iDbUserCouponService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户优惠券关联列表
     */
    @SaCheckPermission("xinrenli:userCoupon:export")
    @Log(title = "用户优惠券关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DbUserCouponBo bo, HttpServletResponse response) {
        List<DbUserCouponVo> list = iDbUserCouponService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户优惠券关联", DbUserCouponVo.class, response);
    }

    /**
     * 获取用户优惠券关联详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("xinrenli:userCoupon:query")
    @GetMapping("/{id}")
    public R<DbUserCouponVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iDbUserCouponService.queryById(id));
    }

    /**
     * 新增用户优惠券关联
     */
    @SaCheckPermission("xinrenli:userCoupon:add")
    @Log(title = "用户优惠券关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DbUserCouponBo bo) {
        return toAjax(iDbUserCouponService.insertByBo(bo));
    }

    /**
     * 修改用户优惠券关联
     */
    @SaCheckPermission("xinrenli:userCoupon:edit")
    @Log(title = "用户优惠券关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DbUserCouponBo bo) {
        return toAjax(iDbUserCouponService.updateByBo(bo));
    }

    /**
     * 删除用户优惠券关联
     *
     * @param ids 主键串
     */
    @SaCheckPermission("xinrenli:userCoupon:remove")
    @Log(title = "用户优惠券关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iDbUserCouponService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
