package com.kingdee.eas.custom.expandassess.app;

import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import org.apache.log4j.Logger;

import java.math.BigDecimal;

import com.kingdee.bos.*;
import com.kingdee.bos.dao.IObjectValue;

import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.expandassess.ExpandAssessEntryInfo;

public class ExpandAssessEntryControllerBean extends AbstractExpandAssessEntryControllerBean {
    private static Logger logger =
            Logger.getLogger("com.kingdee.eas.custom.expandassess.app.ExpandAssessEntryControllerBean");


    @Override
    protected void _update(Context ctx, IObjectPK pk, IObjectValue model) throws BOSException, EASBizException {
        saveSum(ctx, (ExpandAssessEntryInfo) model);
        super._update(ctx, pk, model);
    }

    @Override
    protected void _updatePartial(Context ctx, IObjectValue model, SelectorItemCollection selector) throws BOSException, EASBizException {
        saveSum(ctx, (ExpandAssessEntryInfo) model);
        super._updatePartial(ctx, model, selector);
    }

    private void saveSum(Context ctx, ExpandAssessEntryInfo info) throws BOSException, EASBizException {
        ExpandAssessEntryInfo oldInfo = null;
        try {
            oldInfo = this.getExpandAssessEntryInfo(ctx, new ObjectUuidPK(info.getId()));
        } catch (Exception e) {
            e.printStackTrace();

        }
        //相加签约总金额
        BigDecimal sum = BigDecimal.ZERO;
        if (oldInfo != null) {
            if (info.getOneLift() == null) {
                info.setOneLift(oldInfo.getOneLift());
            }
            if (info.getUniteLift() == null) {
                info.setUniteLift(oldInfo.getUniteLift());
            }
            if (info.getShareLiftLC() == null) {
                info.setShareLiftLC(oldInfo.getShareLiftLC());
            }
            if (info.getShareLiftZY() == null) {
                info.setShareLiftZY(oldInfo.getShareLiftZY());
            }
            if (info.getShareLiftRes() == null) {
                info.setShareLiftRes(oldInfo.getShareLiftRes());
            }
        }

        BigDecimal oneLift = info.getOneLift() == null ? BigDecimal.ZERO : info.getOneLift();//自拓型项目
        BigDecimal uniteLift = info.getUniteLift() == null ? BigDecimal.ZERO : info.getUniteLift();//联拓联管型项目
        BigDecimal shareLiftLC = info.getShareLiftLC() == null ? BigDecimal.ZERO : info.getShareLiftLC();//共拓型项目-流程配合型
        BigDecimal shareLiftZY = info.getShareLiftZY() == null ? BigDecimal.ZERO : info.getShareLiftZY();//共拓型项目-专业配合型
        BigDecimal shareLiftRes = info.getShareLiftRes() == null ? BigDecimal.ZERO : info.getShareLiftRes();//共拓型项目-资源贡献型
        sum = oneLift.add(uniteLift).add(shareLiftLC).add(shareLiftZY).add(shareLiftRes);
        info.setContracts(sum);
    }
}