package com.ruoyi.web.controller.warehouse.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.Charge;
import com.ruoyi.finance.excel.Detailed;
import com.ruoyi.finance.service.ITFeeService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.warehouseBusiness.domain.enums.FeesTypeEnum;
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/invoice")
public class TInvoiceController extends BaseController {
    @Autowired
    private ITFeeService tFeeService;

    /**
     * 查询财务数据主列表
     */
    @PreAuthorize("@ss.hasPermi('finance:charge:list')")
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/list")
    public TableDataInfo list(TFee tFee) {
        if (StringUtils.isEmpty(tFee.getfBilltype())) {
            tFee.setfBilltype(FeesTypeEnum.INVOICE.getType());
        }
        startPage();
        List<TFee> list = tFeeService.selectTFeeList(tFee);
        return getDataTable(list);
    }

    /**
     * 导出财务数据主列表
     */
    @PreAuthorize("@ss.hasPermi('finance:charge:export')")
    @Log(title = "财务数据主", businessType = BusinessType.EXPORT)
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/export")
    public AjaxResult export(TFee tFee) throws Exception {
        if (StringUtils.isEmpty(tFee.getfBilltype())) {
            tFee.setfBilltype(FeesTypeEnum.INVOICE.getType());
        }
        List<Charge> list = tFeeService.chargeExport(tFee);
        ExcelUtil<Charge> util = new ExcelUtil<Charge>(Charge.class);
        return util.exportExcel(list, FeesTypeEnum.INVOICE.getName());
    }

    /**
     * 获取财务数据主详细信息
     */
    @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) {
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.insertTFeeTFeeDo(tFee, tFeeDo, loginUser, FeesTypeEnum.INVOICE.getType());
    }

    /**
     * 删除财务数据主
     */
    @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("/invoiceList")
    public TableDataInfo chargeList(TWareHouseFees tWareHouseFees) {
        startPage();
        List<Map<String, Object>> list = tFeeService.warehouseBillsFeesList(tWareHouseFees, FeesTypeEnum.INVOICE.getType());
        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) {
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.confirm(tFee, tFeeDo, loginUser, FeesTypeEnum.INVOICE.getType());
    }


    /**
     * 撤销收费
     */
    @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) {
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.revoke(tFee, tFeeDo, loginUser, FeesTypeEnum.INVOICE_REVOKE.getType());
    }

    /**
     * 导出入库详情主表列表
     */
    @GetMapping("/exportItems/{fId}")
    public AjaxResult exportItems(@PathVariable("fId") Long fId) {
        List<Detailed> list = tFeeService.selectDetailedList(fId);
        String exportName = tFeeService.findCtrlcorpid(fId);
        ExcelUtil<Detailed> util = new ExcelUtil<Detailed>(Detailed.class);
        return util.exportExcel(list, exportName);
    }

}
