package com.ruoyi.web.controller.warehouse.finance;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.finance.domain.TFee;
import com.ruoyi.finance.domain.TWareHouseFees;
import com.ruoyi.finance.service.ITFeeService;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 财务收费数据主Controller
 *
 * @author ruoyi
 * @date 2021-01-18
 */
@RestController
@RequestMapping("/finance/charge")
public class TChargeController extends BaseController {
    @Autowired
    private ITFeeService tFeeService;

    /**
     * 查询财务数据主列表
     */
    @PreAuthorize("@ss.hasPermi('finance:charge:list')")
    @GetMapping("/list")
    public TableDataInfo list(TFee tFee) {
        tFee.setfBilltype("SF");
        startPage();
        List<TFee> list = tFeeService.selectTFeeList(tFee);
        return getDataTable(list);
    }

    /**
     * 导出财务数据主列表
     */
    @PreAuthorize("@ss.hasPermi('finance:charge:export')")
    @Log(title = "财务数据主", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TFee tFee) {
        List<TFee> list = tFeeService.selectTFeeList(tFee);
        ExcelUtil<TFee> util = new ExcelUtil<TFee>(TFee.class);
        return util.exportExcel(list, "fee");
    }

    /**
     * 获取财务数据主详细信息
     */
    @PreAuthorize("@ss.hasPermi('finance:charge:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        return AjaxResult.success(tFeeService.selectTFeeById(fId));
    }

    /**
     * 新增财务数据主
     */
    @PreAuthorize("@ss.hasPermi('finance:charge:add')")
    @Log(title = "财务数据主", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @RepeatSubmit
    public AjaxResult add(@RequestParam("tFee") String tFee,
                          @RequestParam("tFeeDo") String tFeeDo ) {
        String billsType = "SF";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.insertTFeeTFeeDo(tFee,tFeeDo,loginUser,billsType);
    }

    /**
     * 修改财务数据主
     */
    @PreAuthorize("@ss.hasPermi('finance:charge:edit')")
    @Log(title = "财务数据主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TFee tFee) {
        return toAjax(tFeeService.updateTFee(tFee));
    }

    /**
     * 删除财务数据主
     */
    @PreAuthorize("@ss.hasPermi('finance:charge:remove')")
    @Log(title = "财务数据主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds) {
        return toAjax(tFeeService.deleteTFeeByIds(fIds));
    }

    @PreAuthorize("@ss.hasPermi('finance:charge:remove')")
    @Log(title = "财务数据主", businessType = BusinessType.DELETE)
    @DeleteMapping("/queryRemove/{fId}")
    public AjaxResult queryRemove(@PathVariable("fId") Long fId) {
        return tFeeService.queryRemove(fId);
    }

    /**
     *  收费查询
     */
    @PreAuthorize("@ss.hasPermi('finance:charge:list')")
    @GetMapping("/chargeList")
    public TableDataInfo chargeList(TWareHouseFees tWareHouseFees) {
        tWareHouseFees.setfDc("D");
        startPage();
        String billsType = "SF";
        List<Map<String, Object>> list = tFeeService.warehouseBillsFeesList(tWareHouseFees,billsType);
        return getDataTable(list);
    }

    /**
     *   确认收费
     */
    @PreAuthorize("@ss.hasPermi('finance:charge:add')")
    @Log(title = "财务数据主", businessType = BusinessType.INSERT)
    @PostMapping(value = "/confirm")
    @RepeatSubmit
    public AjaxResult confirm(@RequestParam("tFee") String tFee,
                          @RequestParam("tFeeDo") String tFeeDo ) {
        String billsType = "SF";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.confirm(tFee,tFeeDo,loginUser,billsType);
    }


    /**
     *   撤销收费
     */
    @PreAuthorize("@ss.hasPermi('finance:charge:add')")
    @Log(title = "财务数据主", businessType = BusinessType.INSERT)
    @PostMapping(value = "/revoke")
    @RepeatSubmit
    public AjaxResult revoke(@RequestParam("tFee") String tFee,
                              @RequestParam("tFeeDo") String tFeeDo ) {
        String billsType = "DCRevoke";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.revoke(tFee,tFeeDo,loginUser,billsType);
    }


}
