package nckd.jimin.jyyy.fi.plugin.form;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.FormShowParameter;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.PreOpenFormEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import nckd.jimin.jyyy.fi.common.constant.LimitRelasetConstant;

import java.math.BigDecimal;
import java.util.Date;
import java.util.EventObject;
import java.util.Optional;

public class LimitRelasetAdjustEdit extends AbstractFormPlugin {

    @Override
    public void preOpenForm(PreOpenFormEventArgs e) {
        super.preOpenForm(e);
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        FormShowParameter parameter = this.getView().getFormShowParameter();
        if(parameter.getCustomParams().containsKey("billid")){
            Object billid = parameter.getCustomParam("billid");
            if(billid != null){
                DynamicObject limitRelasetBill = BusinessDataServiceHelper.loadSingle(billid, LimitRelasetConstant.ENTITYID, String.join(",", LimitRelasetConstant.ID, LimitRelasetConstant.KEY_NCKD_AMOUNT));
                this.getModel().setValue("nckd_limitrelaset", limitRelasetBill);
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs args) {
        super.afterDoOperation(args);
        String operateKey = args.getOperateKey();
        if(args.getOperationResult() != null && args.getOperationResult().isSuccess()){
            if("confirm".equals(operateKey)){
                DynamicObject limitRelasetBillF7 = (DynamicObject)this.getModel().getValue("nckd_limitrelaset");
                BigDecimal amount = (BigDecimal)this.getModel().getValue("nckd_amount");
                BigDecimal preAmount = limitRelasetBillF7.getBigDecimal(LimitRelasetConstant.KEY_NCKD_AMOUNT);
                if(limitRelasetBillF7 != null){
                    DynamicObject limitRelasetBill = BusinessDataServiceHelper.loadSingle(limitRelasetBillF7.getPkValue(), LimitRelasetConstant.ENTITYID, String.join(",", LimitRelasetConstant.ID, LimitRelasetConstant.KEY_NCKD_AMOUNT));
                    limitRelasetBill.set(LimitRelasetConstant.KEY_NCKD_AMOUNT, amount);
                    String message = String.format("额度月额度从%s调整成%s。", Optional.ofNullable(preAmount).orElse(BigDecimal.ZERO).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString(),
                            Optional.ofNullable(amount).orElse(BigDecimal.ZERO).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
                    SaveServiceHelper.save(new DynamicObject[]{ limitRelasetBill });
                    saveLimitRelasetLog(limitRelasetBillF7.getLong(LimitRelasetConstant.ID),"adjustquota", message);
                }
                this.getView().close();
            }
        }
    }

    private void saveLimitRelasetLog(long limitRelasetId, String operate ,String description){
        DynamicObject limitRelasetOperateInfo = BusinessDataServiceHelper.newDynamicObject("nckd_limitrelasetoperate");
        limitRelasetOperateInfo.set("nckd_operator", RequestContext.get().getCurrUserId());
        limitRelasetOperateInfo.set("nckd_operatetime",new Date());
        limitRelasetOperateInfo.set("nckd_limitrelaset", limitRelasetId);
        limitRelasetOperateInfo.set("nckd_operate", operate);
        limitRelasetOperateInfo.set("nckd_description", description);
        SaveServiceHelper.save(new DynamicObject[]{ limitRelasetOperateInfo });
    }
}
