package com.awspaas.user.apps.donenow_ctt.controller;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.sdk.local.SDK;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Controller
public class commissionController {
    /**
     * 佣金调整
     * @param uc
     * @param commissionBindid
     * @return
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_ctt.commission_adjust")
    public ResponseObject commissionAdjust(UserContext uc, String commissionBindid) {

        BO commissionBO = SDK.getBOAPI().getByProcess("BO_EU_DNCTT_COMMISSION", commissionBindid);
        List<BO> commissionAgentList = SDK.getBOAPI().query("BO_EU_DNCTT_COMMISSION_AGENT").addQuery("BINDID =", commissionBindid).list();
        List<BO> commissionSalesList = SDK.getBOAPI().query("BO_EU_DNCTT_COMMISSION_SALES").addQuery("BINDID =", commissionBindid).list();

        commissionBO.remove("ID");
        commissionBO.remove("BINDID");

        ProcessInstance contractInstance = SDK.getProcessAPI().createBOProcessInstance("obj_290337ce889c4a1295852e7b76aa82d4", uc.getUID(), "佣金调整-" + "-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));

        BO editBO = new BO();
        commissionBO.remove("CREATEDATE");
        commissionBO.remove("CREATEUSER");
        editBO.setAll(commissionBO.asMap());
        editBO.set("OLD_DOLLARS", commissionBO.get("DOLLARS"));
        editBO.set("OLD_COMMISSION_DOLLARS", commissionBO.get("COMMISSION_DOLLARS"));
        editBO.set("COMMISSION_BINDID", commissionBindid);
        editBO.set("IS_EFFECTIVE", "未执行");
        SDK.getBOAPI().create("BO_EU_DNCTT_COMMISSION_ADJUST", editBO, contractInstance, uc);

        if (commissionAgentList != null && commissionAgentList.size() > 0) {
            for (BO bo : commissionAgentList) {
                BO editAgentBO = new BO();
                bo.remove("CREATEDATE");
                bo.remove("CREATEUSER");
                bo.remove("ID");
                bo.remove("BINDID");
                editAgentBO.setAll(bo.asMap());
                editAgentBO.setBindId(contractInstance.getId());
                editAgentBO.set("OLD_RATIO", bo.get("RATIO"));
                editAgentBO.set("OLD_DOLLARS", bo.get("DOLLARS"));
                editAgentBO.set("OLD_TAX_DEDUCT", bo.get("TAX_DEDUCT"));
                editAgentBO.set("OLD_COMMISSION_DOLLARS", bo.get("COMMISSION_DOLLARS"));
                SDK.getBOAPI().create("BO_EU_DNCTT_COMMISSION_AGENT_ADJUST", editAgentBO, contractInstance, uc);
            }
        }

        if (commissionSalesList != null && commissionSalesList.size() > 0) {
            for (BO bo : commissionSalesList) {
                BO editSalesBO = new BO();
                bo.remove("CREATEDATE");
                bo.remove("CREATEUSER");
                bo.remove("ID");
                bo.remove("BINDID");
                editSalesBO.setAll(bo.asMap());
                editSalesBO.setBindId(contractInstance.getId());
                editSalesBO.set("OLD_RATIO", bo.get("RATIO"));
                editSalesBO.set("OLD_DOLLARS", bo.get("DOLLARS"));
                editSalesBO.set("OLD_TAX_DEDUCT", bo.get("TAX_DEDUCT"));
                editSalesBO.set("OLD_COMMISSION_DOLLARS", bo.get("COMMISSION_DOLLARS"));
                SDK.getBOAPI().create("BO_EU_DNCTT_COMMISSION_SALES_ADJUST", editSalesBO, contractInstance, uc);
            }
        }

        String processUrl = SDK.getFormAPI().getFormURL(SDK.getPortalAPI().getPortalUrl(), uc.getSessionId(), contractInstance.getId(), null, 1, null, editBO.getId(), null, true);

        ResponseObject responseObject = ResponseObject.newOkResponse();
        responseObject.setData(processUrl);

        return responseObject;
    }


    //合同销售佣金

}
