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

import com.ruoyi.common.annotation.DataScope;
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.StringUtils;
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.excel.Detailed;
import com.ruoyi.finance.excel.Payment;
import com.ruoyi.finance.service.ITFeeDoService;
import com.ruoyi.finance.service.ITFeeService;
import com.ruoyi.finance.shipping.FinancialTFee;
import com.ruoyi.finance.shipping.FinancialTFeeDoExcel;
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;

/**
 * 凯和财务付费
 */
@RestController
@RequestMapping("/shipping/financialPay")
public class FinancialPay extends BaseController {
    @Autowired
    private ITFeeService tFeeService;
    @Autowired
    private ITFeeDoService tFeeDoService;

    /**
     * 查询财务数据主列表
     */
    @PreAuthorize("@ss.hasPermi('finance:payment:list')")
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/list")
    public TableDataInfo list(TFee tFee) {
        tFee.setfBilltype("KHFF");
        startPage();
        List<FinancialTFee> list = tFeeService.selectFinancialTFeeList(tFee);
        return getDataTable(list);
    }

    /**
     * 导出财务数据主列表
     */
    @PreAuthorize("@ss.hasPermi('finance:payment:export')")
    @Log(title = "财务数据主", businessType = BusinessType.EXPORT)
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/export")
    public AjaxResult export(TFee tFee) throws Exception {
        tFee.setfBilltype("KHFF");
        List<FinancialTFee> list = tFeeService.selectFinancialTFeeList(tFee);
        ExcelUtil<FinancialTFee> util = new ExcelUtil<FinancialTFee>(FinancialTFee.class);
        return util.exportExcel(list, "付费");
    }

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

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

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

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

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

    /**
     *  付费查询
     */
    @PreAuthorize("@ss.hasPermi('finance:payment:list')")
    @GetMapping("/contrastList")
    public TableDataInfo chargeList(TWareHouseFees tWareHouseFees) {
        tWareHouseFees.setfDc("C");
        tWareHouseFees.setfBilltype("KHFF");
        startPage();
        List<Map<String, Object>> list = tFeeService.FinancialFeesList(tWareHouseFees);
        return getDataTable(list);
    }

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

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

    /**
     * 导出付费明细
     */
    @GetMapping("/exportItems/{fId}")
    public AjaxResult exportItems(@PathVariable("fId") Long fId) {
        List<FinancialTFeeDoExcel> list = tFeeService.FinancialTFeeDoExcel(fId);
        String exportName = tFeeService.findCtrlcorpid(fId);
        ExcelUtil<FinancialTFeeDoExcel> util = new ExcelUtil<FinancialTFeeDoExcel>(FinancialTFeeDoExcel.class);
        return util.exportExcel(list, exportName);
    }
    /**
     * 删除对账数据主
     */
    @PreAuthorize("@ss.hasPermi('finance:contrast:remove')")
    @Log(title = "财务数据主", businessType = BusinessType.DELETE)
    @DeleteMapping("/tFeeDo/{fIds}")
    public AjaxResult removeTFeeDo(@PathVariable Long[] fIds) {
        return toAjax(tFeeDoService.deleteTFeeDoByIds(fIds));
    }

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