package com.sunxung.factoring.web.creditresolution;

import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.creditresolution.CreditResolution;
import com.sunxung.factoring.entity.creditresolution.ReconsiderAdjust;
import com.sunxung.factoring.entity.creditresolution.ReconsiderAdjustApproval;
import com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask;
import com.sunxung.factoring.service.creditresolution.ICreditResolutionApprovalService;
import com.sunxung.factoring.service.creditresolution.ICreditResolutionService;
import com.sunxung.factoring.service.creditresolution.IReconsiderAdjustApprovalService;
import com.sunxung.factoring.service.creditresolution.IReconsiderAdjustService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @author liutao
 * @data 2023-12-23
 */
@RestController
@RequestMapping("creditResolution")
public class CreditResolutionController {

    @Resource(name = "creditResolutionService")
    private ICreditResolutionService creditResolutionService;

    @Autowired
    private ICreditResolutionApprovalService creditResolutionApprovalService;

    @Autowired
    private IReconsiderAdjustService reconsiderAdjustService;

    @Autowired
    private IReconsiderAdjustApprovalService reconsiderAdjustApprovalService;

    /**
     * 手动同步sop接口
     * @param id
     */
    @RequestMapping("auto")
    public void test(@RequestParam Long id){
        creditResolutionApprovalService.test(id);

    }

    /**
     * 手动开启流程
     * @param id
     */
    @RequestMapping("startProcess")
    public void startProcess(@RequestParam Long id){
        creditResolutionService.startProcess(id);
    }

    /**
     * 全部列表
     * @return
     */
    @RequestMapping("allList")
    public GridPage allList(@RequestBody SearchBusinessProcessingTask search){
        GridPage<CreditResolution> page = creditResolutionService.allList(search);
        return page;
    }

    /**
     * 获取授信决议信息
     *
     * @param id
     * @return
     */
    @RequestMapping("get")
    public ResponseJson getInfo(@RequestParam Long id) {
        CreditResolution creditResolution = creditResolutionService.get(id);
        creditResolution.setFinancingRatioLow(format(creditResolution.getFinancingRatioLow() == null? 0: creditResolution.getFinancingRatioLow() * 100));
        creditResolution.setFinancingRatioHigh(format(creditResolution.getFinancingRatioHigh() == null? 0: creditResolution.getFinancingRatioHigh() * 100));
        creditResolution.setPlanIrr(format(creditResolution.getPlanIrr() == null? 0: creditResolution.getPlanIrr() * 100));
        creditResolution.setProposedFinancingRatioTwoLow(format(creditResolution.getProposedFinancingRatioTwoLow() == null? 0: creditResolution.getProposedFinancingRatioTwoLow() * 100));
        creditResolution.setProposedFinancingRatioTwoHigh(format(creditResolution.getProposedFinancingRatioTwoHigh() == null? 0: creditResolution.getProposedFinancingRatioTwoHigh() * 100));
        creditResolution.setPlanIrrTwo(format(creditResolution.getPlanIrrTwo() == null? 0: creditResolution.getPlanIrrTwo() * 100));
        creditResolution.setFinancingRateLow(format(creditResolution.getFinancingRateLow() == null? 0: creditResolution.getFinancingRateLow() * 100));
        creditResolution.setFinancingRateHigh(format(creditResolution.getFinancingRateHigh() == null? 0: creditResolution.getFinancingRateHigh() * 100));
        creditResolution.setManagementRateLow(format(creditResolution.getManagementRateLow() == null? 0: creditResolution.getManagementRateLow() * 100));
        creditResolution.setManagementRateHigh(format(creditResolution.getManagementRateHigh() == null? 0: creditResolution.getManagementRateHigh() * 100));
        creditResolution.setMarginRatioLow(format(creditResolution.getMarginRatioLow() == null? 0: creditResolution.getMarginRatioLow() * 100));
        creditResolution.setMarginRatioHigh(format(creditResolution.getMarginRatioHigh() == null? 0: creditResolution.getMarginRatioHigh() * 100));
        creditResolution.setComprehensiveQuotation(format(creditResolution.getComprehensiveQuotation() == null? 0: creditResolution.getComprehensiveQuotation() * 100));
        return new ResponseJson(creditResolution);
    }

    private Float format(Float number){
        if(number != 0){
            DecimalFormat decimalFormat = new DecimalFormat(".00");
            String financingRatioLowStr =decimalFormat.format(number);
            number = Float.parseFloat(financingRatioLowStr);
        }
        return number;
    }

    /**
     * 获取授信决议录入页信息
     *
     * @return
     */
    @RequestMapping("getDetail")
    public ResponseJson getDetail(@RequestParam Long id) {
        CreditResolution creditResolution = creditResolutionService.getDetail(id);
        return new ResponseJson(creditResolution);
    }

    /**
     * 授信决议录入
     *
     * @return
     */
    @RequestMapping("add")
    public ResponseJson addInfo(@RequestBody CreditResolution creditResolution) {
        creditResolutionService.addInfo(creditResolution);
        return new ResponseJson();
    }

    /**
     * 授信决议确认
     *
     * @return
     */
    @RequestMapping("/confirm/add")
    public ResponseJson creditResolutionConfirm(@RequestBody CreditResolution creditResolution) {
        creditResolutionApprovalService.addInfo(creditResolution);
        return new ResponseJson();
    }

    /**
     * 复议调整申请
     *
     * @param reconsiderAdjust
     * @return
     */
    @RequestMapping("reconsider/add")
    public ResponseJson addReconsiderAdjustInfo(@RequestBody ReconsiderAdjust reconsiderAdjust) {
        reconsiderAdjustService.add(reconsiderAdjust);
        return new ResponseJson();
    }

    /**
     * 复议调整审批
     *
     * @param approval
     * @return
     */
    @RequestMapping("reconsider/approval/add")
    public ResponseJson approval(@RequestBody ReconsiderAdjustApproval approval) {
        reconsiderAdjustApprovalService.add(approval);
        return new ResponseJson();
    }
}
