package com.ruoyi.web.controller.fleet.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.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.orderManagement.finance.domain.FTmsaccbills;
import com.ruoyi.orderManagement.finance.domain.FTmsaccbillsFees;
import com.ruoyi.orderManagement.finance.service.IFTmsaccbillsService;
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("/finances/contrast")
public class FTmsaccbillsController extends BaseController {

    @Autowired
    private IFTmsaccbillsService fTmsaccbillsService;

    /**
     * 查询财务主列表
     */
    @PreAuthorize("@ss.hasPermi('finance:contrast:list')")
    @GetMapping("/list")
    public TableDataInfo list(FTmsaccbills fTmsaccbills)
    {
        fTmsaccbills.setfBilltype("SFDZ");
        startPage();
        List<Map<String, Object>> list = fTmsaccbillsService.selectFTmsaccbillsMapList(fTmsaccbills);
        return getDataTable(list);
    }

    /**
     *  对账查询
     */
    @GetMapping("/contrastList")
    public TableDataInfo chargeList(FTmsaccbillsFees fTmsaccbillsFees) {
        startPage();
        String billsType = "SFDZ";
        List<Map<String, Object>> list = fTmsaccbillsService.fTmsaccbillsList(fTmsaccbillsFees,billsType);
        return getDataTable(list);
    }

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

    /**
     *  查询详情
     */
    @PreAuthorize("@ss.hasPermi('finance:contrast:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        return AjaxResult.success(fTmsaccbillsService.selectFTmsaccbillsAndItemsById(fId));
    }

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

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

    /**
     *   确认对账
     */
    @PreAuthorize("@ss.hasPermi('finance:contrast:add')")
    @Log(title = "财务数据主", businessType = BusinessType.INSERT)
    @PostMapping(value = "/confirm")
    @RepeatSubmit
    public AjaxResult confirm(@RequestParam("tFee") String tFee,
                              @RequestParam("tFeeDo") String tFeeDo ) {
        String billsType = "SFDZ";
        if (StringUtils.isEmpty(tFee) || "{}".equals(tFee)) {
            return AjaxResult.error("未找到财务数据，请确认");
        }
        if (StringUtils.isEmpty(tFeeDo) || "[]".equals(tFeeDo)) {
            return AjaxResult.error("未找到财务明细数据，请确认");
        }
        return fTmsaccbillsService.confirm(tFee,tFeeDo,billsType);
    }

}
