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.spring.SpringUtils;
import com.ruoyi.finance.domain.TFee;
import com.ruoyi.finance.domain.TWareHouseFees;
import com.ruoyi.finance.service.ITFeeDoService;
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;

/**
 * 发票申请
 */
@RestController
@RequestMapping("/financial/invoiceApplyFor")
public class InvoiceApplyFor extends BaseController {
    @Autowired
    private ITFeeService tFeeService;
    @Autowired
    private ITFeeDoService tFeeDoService;

    /**
     * 查询申请发票数据主列表
     */
    @PreAuthorize("@ss.hasPermi('financial:invoiceApplyFor:list')")
    //@DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/list")
    public TableDataInfo list(TFee tFee) {
        tFee.setfBilltype("ApplyFP");
        startPage();
        List<Map<String, Object>> list = tFeeService.selectTFeeList1(tFee);
        return getDataTable(list);
    }

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

    /**
     * 新增发票申请
     *
     * @param tFee
     * @param tFeeDo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('financial:invoiceApplyFor:add')")
    @Log(title = "财务数据主", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @RepeatSubmit
    public AjaxResult add(@RequestParam("tFee") String tFee,
                          @RequestParam("tFeeDo") String tFeeDo,
                          @RequestParam(value = "tFeeInvoice",required = false) String tFeeInvoice,
                          @RequestParam("billsType") String billsType) {
        if (StringUtils.isEmpty(tFee)){
            return AjaxResult.error("财务主表信息不能为空");
        }
        if (StringUtils.isEmpty(tFeeDo)) {
            return AjaxResult.error("财务明细表信息不能为空");
        }
        if (StringUtils.isEmpty(billsType)){
            return AjaxResult.error("单据类型不能为空");
        }
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.insertKHTFeeTFeeDo(tFee, tFeeDo, tFeeInvoice, loginUser, billsType);
    }

    /**
     * 新增开发票信息
     *
     * @param tFee
     * @param tFeeDo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('financial:invoiceApplyFor:invoiceAdd')")
    @Log(title = "财务数据主", businessType = BusinessType.INSERT)
    @PostMapping(value = "/invoice/add")
    @RepeatSubmit
    public AjaxResult invoiceAdd(@RequestParam("tFee") String tFee,
                          @RequestParam("tFeeDo") String tFeeDo,
                          @RequestParam(value = "tFeeInvoice", required = false) String tFeeInvoice) {
        if (StringUtils.isEmpty(tFee)) {
            return AjaxResult.error("财务主表信息不能为空");
        }
        if (StringUtils.isEmpty(tFeeDo)) {
            return AjaxResult.error("财务明细表信息不能为空");
        }
        String billsType = "invoiceFP";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.insertKHTFeeTFeeDo(tFee, tFeeDo, tFeeInvoice, loginUser, billsType);
    }

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

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

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

    @PreAuthorize("@ss.hasPermi('financial:invoiceApplyFor:remove')")
    @Log(title = "财务数据主", businessType = BusinessType.DELETE)
    @DeleteMapping("/queryRemove/{fId}")
    public AjaxResult queryRemove(@PathVariable("fId") Long fId) {
        return tFeeService.remove(fId);
    }
    /**
     * 发票申请查询
     */
    //@PreAuthorize("@ss.hasPermi('financial:invoiceApplyFor:list')")
    @GetMapping("/contrastList")
    public TableDataInfo chargeList(TWareHouseFees tWareHouseFees) {
        startPage();
        String billsType = "ApplyFP";
        if (StringUtils.isNotEmpty(tWareHouseFees.getfSystemType())
                && "3".equals(tWareHouseFees.getfSystemType())) {
            tWareHouseFees.setfTypeid(1);
        }
        tWareHouseFees.setfBillstatus("6");
        tWareHouseFees.setfDc("D");
        List<Map<String, Object>> list = tFeeService.warehouseBillsFeesList(tWareHouseFees, billsType);
        return getDataTable(list);
    }
    /**
     * 开发票查询
     */
    @PreAuthorize("@ss.hasPermi('financial:invoiceApplyFor:invoiceList')")
    @GetMapping("/invoiceList")
    public TableDataInfo invoiceList(TWareHouseFees tWareHouseFees) {
        startPage();
        String billsType = "invoiceFP";
        if (StringUtils.isNotEmpty(tWareHouseFees.getfSystemType())
                && "3".equals(tWareHouseFees.getfSystemType())) {
            tWareHouseFees.setfTypeid(1);
        }
        tWareHouseFees.setfBillstatus("6");
        List<Map<String, Object>> list = tFeeService.warehouseBillsFeesList(tWareHouseFees, billsType);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('financial:invoiceApplyFor:removeFeeDo')")
    @Log(title = "财务数据主", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeFeeDo/{fId}")
    public AjaxResult removeFeeDo(@PathVariable("fId") Long fId) {
        return tFeeDoService.removeFeeDoById(fId);
    }
    /**
     *   撤销开票
     */
    @PreAuthorize("@ss.hasPermi('financial:invoiceApplyFor: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) {
        if (StringUtils.isEmpty(billsType)){
            billsType = "DZApplyFP";
        }
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.revoke(tFee,tFeeDo,loginUser,billsType);
    }
}
