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.MoneyOut;
import com.ruoyi.web.work.domain.dto.MoneyOutDto;
import com.ruoyi.web.work.domain.dto.SmsDto;
import com.ruoyi.web.work.service.ICommonService;
import com.ruoyi.web.work.service.ICompanyService;
import com.ruoyi.web.work.service.IMoneyOutService;
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-18
 */
@RestController
@RequestMapping("/work/out")
public class MoneyOutController extends BaseController {
    @Autowired
    private IMoneyOutService moneyOutService;

    @Autowired
    private ICommonService commonService;

    @Autowired
    private ICompanyService companyService;

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

    @PreAuthorize("@ss.hasPermi('work:out:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        MoneyOut out = moneyOutService.getById(id);
        out.setCompany(companyService.getById(out.getCompanyId()));
        return AjaxResult.success(out);
    }

    @PreAuthorize("@ss.hasPermi('work:out:add')")
    @Log(title = "提现申请获取验证码", businessType = BusinessType.INSERT)
    @GetMapping("/sms")
    public AjaxResult sms() {
        return commonService.sms(new SmsDto().setTemplateCode("SMS_465690916"));
    }

    @PreAuthorize("@ss.hasPermi('work:out:add')")
    @Log(title = "提现申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MoneyOut moneyOut) throws ServerException {
        return moneyOutService.add(moneyOut);
    }

    @PreAuthorize("@ss.hasPermi('work:out:edit')")
    @Log(title = "提现审核", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MoneyOutDto dto) throws ServerException {
        return moneyOutService.edit(dto);
    }

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

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