package com.kingdee.eas.custom.expandassess.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.bos.util.BOSUuid;
import com.kingdee.eas.custom.expandassess.*;
import com.kingdee.eas.hr.ats.AtsUtil;
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 com.kingdee.eas.framework.CoreBaseCollection;
import java.lang.String;


/**
 * 拓展人员考核周期
 */
public class ExpandAssessControllerBean extends AbstractExpandAssessControllerBean {
    private static Logger logger =
            Logger.getLogger("com.kingdee.eas.custom.expandassess.app.ExpandAssessControllerBean");

    @Override
    protected IObjectPK _save(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        ExpandAssessInfo expandAssessInfo = (ExpandAssessInfo) model;
        //校验
        verify(ctx, expandAssessInfo);
        return super._save(ctx, model);
    }

    /**
     * 校验
     *
     * @param ctx
     * @param info
     * @throws BOSException
     * @throws EASBizException
     */
    protected void verify(Context ctx, ExpandAssessInfo info) throws BOSException, EASBizException {
        int year = info.getYear();//年份
        PeriodTypeEnum periodType = info.getPeriodType();
        String periodTypeValue = periodType.getValue();//周期类型
        int quarter = info.getQuarter();//季度
        String id = null;
        if (info.getId() != null) {
            id = info.getId().toString();
        }
        FilterInfo filterInfo = new FilterInfo();
        FilterItemCollection filterItems = filterInfo.getFilterItems();
        filterItems.add(new FilterItemInfo("year", year));
        filterItems.add(new FilterItemInfo("periodType", periodTypeValue));
        filterItems.add(new FilterItemInfo("quarter", quarter));
        filterItems.add(new FilterItemInfo("id", id, CompareType.NOTEQUALS));
        if (_exists(ctx, filterInfo)) {
            String errorMsg = null;
            if (periodTypeValue.equals("1")) {
                errorMsg = year + "年度已经存在,请勿重复创建";
            } else {
                errorMsg = year + "年 " + quarter + "季度绩效数据已经存在,请勿重复创建";
            }
            logger.error(errorMsg);
            throw new BOSException(errorMsg);
        }
    }

    /**
     * 启动考核
     *
     * @param ctx
     * @param billId
     * @throws BOSException
     */
    @Override
    protected void _startCheck(Context ctx, String billId) throws BOSException {
        if (StringUtils.isEmpty(billId)) {
            throw new BOSException("单据id不能为空！");
        }
        try {
            IExpandAssessEntry iExpandAssessEntry = ExpandAssessEntryFactory.getLocalInstance(ctx);
            ExpandAssessEntryCollection entrys = iExpandAssessEntry.getExpandAssessEntryCollection("where parent='" + billId + "'");
            CoreBaseCollection collection = new CoreBaseCollection();
            for (int i = 0; i < entrys.size(); i++) {
                ExpandAssessEntryInfo entryInfo = entrys.get(i);
                if (entryInfo.getAsseStatus() != ExpandStatusEunm.UNSTART) {
                    continue;
                } else {
                    entryInfo.setAsseStatus(ExpandStatusEunm.START);
                    collection.add(entryInfo);
                }
            }
            if (collection.size() > 0) {
                iExpandAssessEntry.updateBatchData(collection);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    /**
     * 关闭考核
     *
     * @param ctx
     * @param entryIds
     * @throws BOSException
     */
    @Override
    protected void _closeCheck(Context ctx, String entryIds) throws BOSException {
        if (StringUtils.isEmpty(entryIds)) {
            throw new BOSException("单据id不能为空！");
        }
        try {
            SelectorItemCollection sic = new SelectorItemCollection();
            sic.add("asseStatus");
            FilterInfo filterInfo = new FilterInfo();
            filterInfo.getFilterItems().add(new FilterItemInfo("id", AtsUtil.toSet(entryIds), CompareType.INCLUDE));
            EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, null, null);
            IExpandAssessEntry iExpandAssessEntry = ExpandAssessEntryFactory.getLocalInstance(ctx);
            ExpandAssessEntryCollection entrys = iExpandAssessEntry.getExpandAssessEntryCollection(entityViewInfo);
            if (entrys.size() > 0) {
                for (int i = 0; i < entrys.size(); i++) {
                    ExpandAssessEntryInfo expandAssessEntryInfo = entrys.get(i);
                        expandAssessEntryInfo.setAsseStatus(ExpandStatusEunm.UNSTART);
                        iExpandAssessEntry.updatePartial(expandAssessEntryInfo, sic);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 锁定周期
     *
     * @param ctx
     * @param billId
     * @throws BOSException
     */
    @Override
    protected void _lock(Context ctx, String billId) throws BOSException, EASBizException {
        if (StringUtils.isEmpty(billId)) {
            throw new BOSException("单据id不能为空！");
        }
        ExpandAssessInfo expandAssessInfo = getExpandAssessInfo(ctx, new ObjectUuidPK(BOSUuid.read(billId)));
        if (PeriodStatusEnum.LOCK.equals(expandAssessInfo.getPeriodStatus())) {
            throw new BOSException("周期已锁定，请勿重复操作！");
        }
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("periodStatus");
        expandAssessInfo.setPeriodStatus(PeriodStatusEnum.LOCK);
        updatePartial(ctx, expandAssessInfo, sic);
    }

    /**
     * 解锁周期
     *
     * @param ctx
     * @param billId
     * @throws BOSException
     */
    @Override
    protected void _unlock(Context ctx, String billId) throws BOSException, EASBizException {
        if (StringUtils.isEmpty(billId)) {
            throw new BOSException("单据id不能为空！");
        }
        ExpandAssessInfo expandAssessInfo = getExpandAssessInfo(ctx, new ObjectUuidPK(BOSUuid.read(billId)));
        if (PeriodStatusEnum.UNLOCK.equals(expandAssessInfo.getPeriodStatus())) {
            throw new BOSException("周期已解除，请勿重复操作！");
        }
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("periodStatus");
        expandAssessInfo.setPeriodStatus(PeriodStatusEnum.UNLOCK);
        updatePartial(ctx, expandAssessInfo, sic);
    }
}