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.DbCouponVo;
import com.yongqi.xinrenli.domain.bo.DbCouponBo;
import com.yongqi.xinrenli.service.IDbCouponService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠卷
 *
 * @author zyf
 * @date 2023-03-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xinrenli/coupon")
public class DbCouponController extends BaseController {

    private final IDbCouponService iDbCouponService;

    /**
     * 查询优惠卷列表
     */
    @SaCheckPermission("xinrenli:coupon:list")
    @GetMapping("/list")
    public TableDataInfo<DbCouponVo> list(DbCouponBo bo, PageQuery pageQuery) {
        return iDbCouponService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出优惠卷列表
     */
    @SaCheckPermission("xinrenli:coupon:export")
    @Log(title = "优惠卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DbCouponBo bo, HttpServletResponse response) {
        List<DbCouponVo> list = iDbCouponService.queryList(bo);
        ExcelUtil.exportExcel(list, "优惠卷", DbCouponVo.class, response);
    }

    /**
     * 获取优惠卷详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("xinrenli:coupon:query")
    @GetMapping("/{id}")
    public R<DbCouponVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
        return R.ok(iDbCouponService.queryById(id));
    }

    /**
     * 新增优惠卷
     */
    @SaCheckPermission("xinrenli:coupon:add")
    @Log(title = "优惠卷", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DbCouponBo bo) {
        return toAjax(iDbCouponService.insertByBo(bo));
    }

    /**
     * 修改优惠卷
     */
    @SaCheckPermission("xinrenli:coupon:edit")
    @Log(title = "优惠卷", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DbCouponBo bo) {
        return toAjax(iDbCouponService.updateByBo(bo));
    }

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