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.DbUserDiscountVo;
import com.yongqi.xinrenli.domain.bo.DbUserDiscountBo;
import com.yongqi.xinrenli.service.IDbUserDiscountService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户折扣
 *
 * @author zyf
 * @date 2023-03-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xinrenli/userDiscount")
public class DbUserDiscountController extends BaseController {

    private final IDbUserDiscountService iDbUserDiscountService;

    /**
     * 查询用户折扣列表
     */
    @SaCheckPermission("xinrenli:userDiscount:list")
    @GetMapping("/list")
    public TableDataInfo<DbUserDiscountVo> list(DbUserDiscountBo bo, PageQuery pageQuery) {
        return iDbUserDiscountService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户折扣列表
     */
    @SaCheckPermission("xinrenli:userDiscount:export")
    @Log(title = "用户折扣", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DbUserDiscountBo bo, HttpServletResponse response) {
        List<DbUserDiscountVo> list = iDbUserDiscountService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户折扣", DbUserDiscountVo.class, response);
    }

    /**
     * 获取用户折扣详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("xinrenli:userDiscount:query")
    @GetMapping("/{id}")
    public R<DbUserDiscountVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(iDbUserDiscountService.queryById(id));
    }

    /**
     * 新增用户折扣
     */
    @SaCheckPermission("xinrenli:userDiscount:add")
    @Log(title = "用户折扣", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DbUserDiscountBo bo) {
        return toAjax(iDbUserDiscountService.insertByBo(bo));
    }

    /**
     * 修改用户折扣
     */
    @SaCheckPermission("xinrenli:userDiscount:edit")
    @Log(title = "用户折扣", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DbUserDiscountBo bo) {
        return toAjax(iDbUserDiscountService.updateByBo(bo));
    }

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