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

import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.custom.projectbonus.*;
import com.kingdee.eas.custom.projectbonus.bizEnum.BusDepProjectStateEnum;
import com.kingdee.eas.custom.projectbonus.bizEnum.CycleStateEnum;
import com.kingdee.eas.framework.CoreBillBaseInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.kingdee.bos.*;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.eas.common.EASBizException;

import java.lang.String;

public class BonusCycleControllerBean extends AbstractBonusCycleControllerBean {
    private static Logger logger =
            Logger.getLogger(BonusCycleControllerBean.class);

    /**
     * 保存 新增周期名称重复校验
     *
     * @param ctx
     * @param model
     * @return
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected IObjectPK _save(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        BonusCycleInfo info = (BonusCycleInfo) model;
        String cycleName = info.getCycleName();//周期名称
        info.setName(cycleName);
        String id = null;
        if (info.getId() != null) {
            id = info.getId().toString();
        }
        FilterInfo filterInfo = new FilterInfo();
        FilterItemCollection filterItems = filterInfo.getFilterItems();
        filterItems.add(new FilterItemInfo("cycleName", cycleName));
        filterItems.add(new FilterItemInfo("id", id, CompareType.NOTEQUALS));
        if (_exists(ctx, filterInfo)) {
            String errorMsg = "周期名称[ " + cycleName + " ]已经存在，请勿重复创建！";
            logger.error(errorMsg);
            throw new BOSException(errorMsg);
        }
        return super._save(ctx, model);
    }

    /**
     * 解决更新周期数据,事业部数据丢失错误
     *
     * @param ctx
     * @param pk
     * @param model
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _update(Context ctx,
                           IObjectPK pk,
                           IObjectValue model) throws BOSException, EASBizException {
        BonusCycleInfo info = (BonusCycleInfo) model;
        String id = info.getId().toString();
        //事业部项目
        BonusCycleBusDepProjectCollection busDepProjectCols = info.getBusDepProject();
        if (busDepProjectCols == null || busDepProjectCols.size() <= 0) {
            IBonusCycleBusDepProject iBonusCycleBusDepProject = BonusCycleBusDepProjectFactory.getLocalInstance(ctx);
            FilterInfo filterInfo = new FilterInfo();
            filterInfo.getFilterItems().add(new FilterItemInfo("parent.id", id));
            EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, null, null);
            BonusCycleBusDepProjectCollection bonusCycleBusDepProjectCollection = iBonusCycleBusDepProject.getBonusCycleBusDepProjectCollection(entityViewInfo);
            info.put("BusDepProject", bonusCycleBusDepProjectCollection);
        }
        super._update(ctx, pk, model);
    }

    /**
     * 重新新增方法
     * 新增周期时，自动同步事业部项目
     *
     * @param ctx
     * @param model
     * @return
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected IObjectPK _addnew(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        BonusCycleInfo info = (BonusCycleInfo) model;
        info.setState(CycleStateEnum.UNLOCK);
        IObjectPK iObjectPK = super._addnew(ctx, model);
        //同步事业部项目
        IBonusCycleBusDepProject iBonusCycleBusDepProject = BonusCycleBusDepProjectFactory.getLocalInstance(ctx);
        iBonusCycleBusDepProject.syncDepProject(iObjectPK.toString());
        return iObjectPK;
    }

    /**
     * 能否删除
     *
     * @param ctx
     * @param pk
     * @param coreBillInfo
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void isCanDelete(Context ctx,
                               IObjectPK pk,
                               CoreBillBaseInfo coreBillInfo)
            throws BOSException, EASBizException {
        super.isCanDelete(ctx, pk, coreBillInfo);
        SelectorItemCollection selector = new SelectorItemCollection();
        selector.add(new SelectorItemInfo("BusDepProject.id"));
        BonusCycleInfo info = (BonusCycleInfo) this.getCoreBillBaseInfo(ctx, pk, selector);
        BonusCycleBusDepProjectCollection busDepProjectCols = info.getBusDepProject();
        if (busDepProjectCols.size() > 0) {
            logger.error("不允许删除有事业部项目的周期");
            throw new BOSException("不允许删除有事业部项目的周期");
        }
    }

    /**
     * 锁定周期
     *
     * @param ctx
     * @param billId
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _lock(Context ctx, String billId) throws BOSException, EASBizException {
        if (StringUtils.isEmpty(billId)) {
            logger.error("单据id不能为空！");
            throw new BOSException("单据id不能为空！");
        }
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("BusDepProject.state");
        BonusCycleInfo info = getBonusCycleInfo(ctx, new ObjectUuidPK(billId), sic);
        if (CycleStateEnum.LOCK.equals(info.getState())) {
            //周期状态等于未锁定
            return;
        }
        BonusCycleBusDepProjectCollection busDepProjectCols = info.getBusDepProject();
        for (int i = 0; i < busDepProjectCols.size(); i++) {
            BusDepProjectStateEnum state = busDepProjectCols.get(i).getState();
            if (!BusDepProjectStateEnum.RECHECK.equals(state) && !BusDepProjectStateEnum.DISABLE.equals(state)) {
                //状态不等于已复核 && 禁用
                logger.error("当前周期下存在未复核的事业部项目，请先复核或禁用后 再锁定周期！");
                throw new BOSException("当前周期下存在未复核的事业部项目，请先复核或禁用后 再锁定周期！");
            }
        }
        info.setState(CycleStateEnum.LOCK);
        update(ctx, new ObjectUuidPK(billId), info);
    }

    /**
     * 解锁周期
     *
     * @param ctx
     * @param billId
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _unLock(Context ctx, String billId) throws BOSException, EASBizException {
        if (StringUtils.isEmpty(billId)) {
            logger.error("单据id不能为空！");
            throw new BOSException("单据id不能为空！");
        }
        BonusCycleInfo info = getBonusCycleInfo(ctx, new ObjectUuidPK(billId));
        CycleStateEnum state = info.getState();
        if (state == null || CycleStateEnum.LOCK.equals(state)) {
            info.setState(CycleStateEnum.UNLOCK);
            update(ctx, new ObjectUuidPK(billId), info);
        }
    }
}