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

/**
 * @Author ZhaoQian La
 * @Date 2021/5/14 13:54
 * @Version 1.0
 */

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.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/outputMakeOut")
public class OutputMakeOut extends BaseController {
    @Autowired
    private ITFeeService tFeeService;
    @Autowired
    private ITFeeDoService tFeeDoService;
    /**
    * 查询销项开票数据主列表
     */
    @PreAuthorize("@ss.hasPermi('financial:outputMakeOut:list')")
    //@DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/list")
    public TableDataInfo list(TFee tFee) {
        tFee.setfBilltype("INV");
        startPage();
        List<Map<String, Object>> list = tFeeService.selectTFeeList1(tFee);
        return getDataTable(list);
    }
    /**
     * 获取发票数据主详细信息
     */
    @PreAuthorize("@ss.hasPermi('finance:outputMakeOut: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:outputMakeOut: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);
    }
    /**
     * 删除财务数据主
     */
    @PreAuthorize("@ss.hasPermi('finance:outputMakeOut:remove')")
    @Log(title = "财务数据主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds) {
        return toAjax(tFeeService.deleteTFeeByIds(fIds));
    }

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