package com.yiqi.admin.controller.recharge;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.StatusType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yiqi.common.annotation.Log;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.recharge.domain.RechargeType;
import com.yiqi.recharge.service.IRechargeTypeService;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.common.core.page.TableDataInfo;

/**
 * 充值规则管理Controller
 *
 * @author xiaohei
 * @date 2023-08-28
 */
@Api(tags = "管理后台 - 充值规则管理")
@RestController
@RequestMapping(UrlConstants.managerApi + "/recharge/type")
public class RechargeTypeController extends BaseController {
    @Autowired
    private IRechargeTypeService rechargeTypeService;

    /**
     * 查询充值规则管理列表
     */
    @PreAuthorize("@ss.hasPermi('recharge:type:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询充值规则管理列表")
    public TableDataInfo list(RechargeType rechargeType) {
        startPage();
        List<RechargeType> list = rechargeTypeService.selectRechargeTypeList(rechargeType);
        return getDataTable(list);
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询所有优惠券类型")
    public R<List<RechargeType>> all() {
        List<RechargeType> list = rechargeTypeService.list(new QueryWrapper<RechargeType>().lambda()
                .eq(RechargeType::getStatus, StatusType.OK.getCode())
                .eq(RechargeType::getDelFlag, DeleteStatus.OK.getCode()));
        return R.ok(list);
    }

    /**
     * 导出充值规则管理列表
     */
    @PreAuthorize("@ss.hasPermi('recharge:type:export')")
    @Log(title = "充值规则管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出充值规则管理列表")
    public void export(HttpServletResponse response, RechargeType rechargeType) {
        List<RechargeType> list = rechargeTypeService.selectRechargeTypeList(rechargeType);
        ExcelUtil<RechargeType> util = new ExcelUtil<RechargeType>(RechargeType. class);
        util.exportExcel(response, list, "充值规则管理数据");
    }

    /**
     * 获取充值规则管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('recharge:type:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取充值规则管理详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(rechargeTypeService.selectRechargeTypeById(id));
    }

    /**
     * 新增充值规则管理
     */
    @PreAuthorize("@ss.hasPermi('recharge:type:add')")
    @Log(title = "充值规则管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增充值规则管理")
    public AjaxResult add(@RequestBody RechargeType rechargeType) {
        rechargeType.setCreateById(getUserId());
        rechargeType.setCreateBy(getUsername());
        return toAjax(rechargeTypeService.insertRechargeType(rechargeType));
    }

    /**
     * 修改充值规则管理
     */
    @PreAuthorize("@ss.hasPermi('recharge:type:edit')")
    @Log(title = "充值规则管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改充值规则管理")
    public AjaxResult edit(@RequestBody RechargeType rechargeType) {
        rechargeType.setUpdateById(getUserId());
        rechargeType.setUpdateBy(getUsername());
        return toAjax(rechargeTypeService.updateRechargeType(rechargeType));
    }

    /**
     * 删除充值规则管理
     */
    @PreAuthorize("@ss.hasPermi('recharge:type:remove')")
    @Log(title = "充值规则管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除充值规则管理")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(rechargeTypeService.deleteRechargeTypeByIds(ids));
    }

    /**
     * 修改充值规则管理状态
     */
    @PreAuthorize("@ss.hasPermi('recharge:type:edit')")
    @Log(title = "充值规则管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "updateStatus")
    @ApiOperation(value = "修改充值规则管理状态")
    public AjaxResult updateRechargeTypeStatus(@RequestBody RechargeType rechargeType) {
        return toAjax(rechargeTypeService.updateRechargeTypeStatus(rechargeType));
    }
}
