package com.ruoyi.web.work.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.work.domain.Recharge;
import com.ruoyi.web.work.domain.dto.RechargeDto;
import com.ruoyi.web.work.service.IRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.rmi.ServerException;
import java.util.Arrays;
import java.util.List;

/**
 * 充值管理
 *
 * @author lsw
 * @date 2024-04-17
 */
@RestController
@RequestMapping("/work/recharge")
public class RechargeController extends BaseController {
    @Autowired
    private IRechargeService rechargeService;

    @PreAuthorize("@ss.hasPermi('work:recharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(Recharge recharge) {
        recharge.setCompanyId(getLoginUser().getUser().getCompanyId());
        startPage();
        List<Recharge> list = rechargeService.selectList(recharge);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('work:recharge:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        return AjaxResult.success(rechargeService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('work:recharge:add')")
    @Log(title = "充值申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Recharge recharge) {
        return rechargeService.add(recharge);
    }

    @PreAuthorize("@ss.hasPermi('work:recharge:edit')")
    @Log(title = "充值审核", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody RechargeDto dto) throws ServerException {
        return rechargeService.edit(dto);
    }

    @PreAuthorize("@ss.hasPermi('work:recharge:remove')")
    @Log(title = "充值记录删除", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(rechargeService.removeByIds(Arrays.asList(ids)));
    }

    @Log(title = "充值记录导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('work:recharge:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Recharge recharge) {
        recharge.setCompanyId(getLoginUser().getUser().getCompanyId());
        PageUtils.orderBy("r.id desc");
        List<Recharge> list = rechargeService.selectList(recharge);
        ExcelUtil<Recharge> util = new ExcelUtil<Recharge>(Recharge.class);
        util.exportExcel(response, list, "充值明细");
    }
}
