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.web.work.domain.MoneyOut;
import com.ruoyi.web.work.domain.dto.MoneyOutDto;
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 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 ICompanyService companyService;

    @PreAuthorize("@ss.hasPermi('work:out:list')")
    @GetMapping("/list")
    public TableDataInfo list(MoneyOut moneyOut) {
        if (getLoginUser().getUser().getType() == 1) {
            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)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MoneyOut moneyOut) {
        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)));
    }
}
