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.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.Logger;
import com.awspaas.user.apps.donenow_ctt.cttConstant;
import com.awspaas.user.apps.donenow_ctt.service.contractServiceAdj;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;

import static com.awspaas.user.apps.donenow_ctt.service.contractService.LocalDateYYYYMMDD;
import static com.awspaas.user.apps.donenow_ctt.service.contractService.getLocalDate;

@Controller
public class contractServiceController {
    private static final Logger contractLogger = SDK.getLogAPI().getLogger(contractServiceController.class);//记录日志

    /**
     * 合同服务调整--销售
     * @param uc
     * @param serviceId
     * @return
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_ctt.service_sale_adjust")
    public ResponseObject editServiceSale(UserContext uc, String serviceId) {
        if (DBSql.getInt("SELECT count(1) CNT FROM BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD WHERE CONTRACT_SERVICE_ID=?", new Object[]{serviceId}) == 0)
            return ResponseObject.newErrResponse("请先完成未完成的合同服务");

        //服务调整销售的表单地址
        String processUrl = SDK.getFormAPI().getFormURL(SDK.getPortalAPI().getPortalUrl(), uc.getSessionId(), contractServiceAdj.getInstance().editServiceSale(uc, serviceId, null), null, 1, null, null, null, true);

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

        return responseObject;
    }

    /**
     * 执行合同服务调整--销售
     * @param uc
     * @param serviceId
     * @return
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_ctt.service_sale_execAdjust")
    public ResponseObject execServiceSaleAdj(UserContext uc, String boId, String serviceId) {

        //调整 服务
        BO contractService = SDK.getBOAPI().get("BO_EU_DNCTT_CONTRACT_SERVICE_SALE_ADJUST", boId);

        if (contractService.getString("IS_EFFECTIVE").equals("未执行") == false)
            return ResponseObject.newErrResponse("请先完成未完成的调整单");


        LocalDate EFFECTIVE_DATE = getLocalDate(contractService.get("EFFECTIVE_DATE"));//服务生效日期


        //2025年10月15日 一次性收费服务
        if (contractService.getString("PERIOD_TYPE").equals(cttConstant.PERIOD_TYPE_ONE_TIME)) {

            //有审批通过的；
            if (DBSql.getInt("SELECT count(1) FROM BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD WHERE APPROVE_AND_POST_USER_ID IS NOT NULL AND LENGTH(APPROVE_AND_POST_USER_ID)>1 AND APPROVE_AND_POST_DATE IS NOT NULL and CONTRACT_SERVICE_ID=? ", new Object[]{serviceId}) > 0) {
                return ResponseObject.newErrResponse("服务已审批不能再进行调整");
            }

        } else {
            //生效日期之后有审批通过的；
            if (DBSql.getInt("SELECT count(1) FROM BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD WHERE APPROVE_AND_POST_USER_ID IS NOT NULL AND LENGTH(APPROVE_AND_POST_USER_ID)>1 AND APPROVE_AND_POST_DATE IS NOT NULL and CONTRACT_SERVICE_ID=? AND  PERIOD_END_DATE>=?", new Object[]{serviceId, LocalDateYYYYMMDD(EFFECTIVE_DATE)}) > 0) {
                return ResponseObject.newErrResponse("生效日期之后的合同服务已经进行审批！");
            }

            //服务生效日期 不能大于合同服务上的生效日期
            /*
            if (DBSql.getInt("select count(1) CNT from BO_EU_DNCTT_CONTRACT_SERVICE where  ID =? and EFFECTIVE_DATE <=?", new Object[]{serviceId, LocalDateYYYYMMDD(EFFECTIVE_DATE)}) == 0) {
                return ResponseObject.newErrResponse("调整服务生效日期不能大于合同服务上的生效日期");
            }

             */
        }


        if (contractServiceAdj.getInstance().execServiceSaleAdj(uc, boId))

            return ResponseObject.newOkResponse();

        return ResponseObject.newErrResponse("失败");
    }

    /**
     * 合同服务调整--采购
     * @param uc
     * @param serviceId
     * @return
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_ctt.service_purchase_adjust")
    public ResponseObject editServicePurchase(UserContext uc, String serviceId) {
        if (DBSql.getInt("select count(1) CNT from BO_EU_DNIVT_ORDER_PAYMENT_PLAN where  CONTRACT_SERVICE_ID=? ", new Object[]{serviceId}) == 0) {
            return ResponseObject.newErrResponse("请先生成该合同服务下的所有付款计划");
        }

        String processUrl = SDK.getFormAPI().getFormURL(SDK.getPortalAPI().getPortalUrl(), uc.getSessionId(), contractServiceAdj.getInstance().editServicePurchase(uc, serviceId, null), null, 1, null, null, null, true);

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

        return responseObject;
    }


    /**
     * 执行合同服务调整--采购
     * @param uc
     * @param serviceId
     * @return
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_ctt.service_purchase_execAdjust")
    public ResponseObject execServicePurchaseAdj(UserContext uc, String boId, String serviceId) {
        contractLogger.info("执行合同服务调整--采购");
        contractLogger.info("boId:" + boId);
        contractLogger.info("serviceId:" + serviceId);

        //调整 服务
        BO contractService = SDK.getBOAPI().get("BO_EU_DNCTT_CONTRACT_SERVICE_PURCHASE_ADJUST", boId);

        if (contractService.getString("IS_EFFECTIVE").equals("未执行") == false)
            return ResponseObject.newErrResponse("请先完成未完成的调整单");

        LocalDate EFFECTIVE_DATE = getLocalDate(contractService.get("PURCHASE_START_DATE"));//服务生效日期

        if (DBSql.getInt("select count(1) CNT from BO_EU_DNIVT_ORDER_PAYMENT_PLAN where  CONTRACT_SERVICE_ID=? and PERIOD_END_DATE >=?  and PAY_AMOUNT<>0 and PAY_AMOUNT is not NULL ", new Object[]{contractService.get("CONTRACT_SERVICE_ID"), LocalDateYYYYMMDD(EFFECTIVE_DATE)}) > 0) {
            return ResponseObject.newErrResponse("生效日期之后，已经进行支付，不能改到当前日期");
        }


        if (contractServiceAdj.getInstance().execServicePurchaseAdj(uc, boId, null)) {
            return ResponseObject.newOkResponse();
        }
        return ResponseObject.newOkResponse();
    }


    @Mapping(value = "com.awspaas.user.apps.donenow_ctt.service_purchase_adjustTEST")
    public ResponseObject editServicePurchaseTest(UserContext uc, String serviceId) {

        contractServiceAdj.getInstance().paySplitServiceMonth(uc, serviceId);

        return ResponseObject.newOkResponse();
    }


    @Mapping(value = "com.awspaas.user.apps.donenow_ctt.service_period_detail")
    public ResponseObject getServicePeriodDetail(UserContext uc, String serviceId) {
        String bindid = DBSql.getString("select BINDID from BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD_SHOW where CONTRACT_SERVICE_ID=?", new Object[]{serviceId});
        if (StringUtils.isNotBlank(bindid)) return ResponseObject.newOkResponse().put("BINDID", bindid);

        //新的调整单
        ProcessInstance contractInstance = SDK.getProcessAPI().createBOProcessInstance("obj_e70c97f1244c4107ae421f3b4a650518", uc.getUID(), serviceId);
        BO editBO = new BO();
        editBO.set("CONTRACT_SERVICE_ID", serviceId);
        editBO.setBindId(contractInstance.getId());
        SDK.getBOAPI().create("BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD_SHOW", editBO, contractInstance, uc);
        return ResponseObject.newOkResponse().put("BINDID", contractInstance.getId());
    }


}
