package com.sunxung.factoring.web.ledger;

import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.ledger.LedgerSupplierBankRef;
import com.sunxung.factoring.entity.ledger.vo.LedgerAccountInfoVo;
import com.sunxung.factoring.entity.ledger.vo.LedgerPlanFactoringSearch;
import com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask;
import com.sunxung.factoring.service.ledger.LedgerAccountBasicInfoService;
import com.sunxung.factoring.service.ledger.LedgerManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 分账管理controller
 *
 * @author liutao
 * @date 2021.11.11
 */
@Controller
@RequestMapping("/ledger")
public class LedgerController {

    private final String prefix = "ledgerManagement";

    @Autowired
    private LedgerManagementService ledgerManagementService;

    @Autowired
    private LedgerAccountBasicInfoService ledgerAccountBasicInfoService;

    /**
     * 跳转待处理页面
     *
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return prefix + "/pendingList";
    }


    /**
     * 列表页查询
     */
    @PostMapping(value = "/doList")
    @ResponseBody
    public Object doList(@RequestBody SearchBusinessProcessingTask search) {
        return ledgerAccountBasicInfoService.list(search);
    }

    /**
     * 跳转分账录入页面
     *
     * @param id 分账管理Id
     * @return
     */
    @RequestMapping("/add")
    public String add(Model model, Long id) {
        model.addAttribute("ledgerAccountInfo", ledgerManagementService.getById(id));
        model.addAttribute("supplierList", ledgerManagementService.getSupplierList(id));
        return prefix + "/assign";
    }


    /**
     * 分账查看
     *
     * @param id 分账管理Id
     * @return
     */
    @GetMapping("/doView")
    @ResponseBody
    public Object doView(Long id) {
        return ledgerManagementService.getById(id);
    }

    /**
     * 选择已融资货物
     *
     * @return
     */
    @RequestMapping("/doFindFinancingGoodsList")
    @ResponseBody
    public Object doFindFinancingGoodsList(LedgerPlanFactoringSearch search) {
        return ledgerManagementService.findFinancingGoodsList(search);
    }

    /**
     * 选择已验收货物
     *
     * @return
     */
    @RequestMapping("/doFindAcceptanceGoodsList")
    @ResponseBody
    public Object doFindAcceptanceGoodsList(LedgerPlanFactoringSearch search) {
        return ledgerManagementService.findAcceptanceGoodsList(search);
    }

    /**
     * 查询收支计划
     *
     * @param search 收放款计划查询类
     * @return
     */
    @RequestMapping("/doFindPayCollPlanFactoringList")
    @ResponseBody
    public Object doFindPayCollPlanFactoringList(LedgerPlanFactoringSearch search) {
        return ledgerManagementService.findPayCollPlanFactoringList(search);
    }

    /**
     * 查询供应商收款账号
     *
     * @param supplierBankRef 供应商银行信息
     * @return
     */
    @RequestMapping("/doFindSupplierBankInfoList")
    @ResponseBody
    public Object doFindSupplierBankInfoList(LedgerSupplierBankRef supplierBankRef) {
        return ledgerManagementService.findSupplierBankInfoList(supplierBankRef);
    }

    /**
     * 分配计算融资利息、融资本金、平台利润等
     *
     * @param ledgerAccountInfoVo
     * @return
     */
    @PostMapping("/doCalculateAssign")
    @ResponseBody
    public Object doCalculateAssign(LedgerAccountInfoVo ledgerAccountInfoVo) {
        return null;
    }

    /**
     * 提交分账信息
     *
     * @param ledgerAccountInfoVo
     * @return
     */
    @ResponseBody
    @RequestMapping("/doAdd")
    @OperationLog(operationModule = "分账管理", operationType = OperationTypeEnum.INSERT, description = "提交分账信息")
    public Object doAdd(LedgerAccountInfoVo ledgerAccountInfoVo) {
        ledgerManagementService.add(ledgerAccountInfoVo);
        return new ResponseJson("提交成功");
    }

    /**
     * 跳转查看页面
     */
    @RequestMapping(value = "/basicView")
    public Object viewSalesContract(Long historyId, Long viewInitiation) {
        return prefix + "/basicView";
    }


    /**
     * 跳转历史页面
     */
    @RequestMapping(value = "/history")
    public Object history() {
        return prefix + "/history";
    }
}
