package com.kingdee.eas.custom.handover.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.handover.*;
import com.kingdee.eas.custom.handover.utils.WorkHandoverUtil;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.affair.AffairBizException;
import com.kingdee.eas.hr.affair.IResignBizBill;
import com.kingdee.eas.hr.affair.ResignBizBillFactory;
import com.kingdee.eas.hr.affair.ResignBizBillInfo;
import com.kingdee.eas.hr.base.HRBillBaseInfo;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.util.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;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;

public class WorkHandoverControllerBean extends AbstractWorkHandoverControllerBean {
    private static Logger logger = Logger.getLogger(WorkHandoverControllerBean.class);

    /**
     * 删除
     *
     * @param ctx
     * @param pk
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _delete(Context ctx, IObjectPK pk) throws BOSException, EASBizException {
        HRBillBaseInfo info = (HRBillBaseInfo) this._getValue(ctx, pk);
        HRBillStateEnum billState = info.getBillState();
        boolean isRemovable = true;
        //已提交、审批中、审批通过的单据不允许删除
        if (HRBillStateEnum.SUBMITED.equals(billState)
                || HRBillStateEnum.AUDITING.equals(billState)
                || HRBillStateEnum.AUDITED.equals(billState)) {
            isRemovable = false;
        }
        if (!isRemovable) {
            throw new AffairBizException(AffairBizException.BILLCANTDELETE);
        }
        super._delete(ctx, pk);
    }


    @Override
    protected IObjectPK _submitEffect(Context ctx, CoreBaseInfo model) throws BOSException, EASBizException {
        //校验工作交接单
        checkWorkHandoverInfo(ctx, (WorkHandoverInfo) model);
        return super._submitEffect(ctx, model);
    }


    @Override
    protected IObjectPK _submit(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        //校验工作交接单
        checkWorkHandoverInfo(ctx, (WorkHandoverInfo) model);
        return super._submit(ctx, model);
    }

    /**
     * 保存
     *
     * @param ctx
     * @param model
     * @return
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected IObjectPK _save(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        //校验工作交接单
        checkWorkHandoverInfo(ctx, (WorkHandoverInfo) model);
        return super._save(ctx, model);
    }

    /**
     * 校验交接单
     *
     * @param ctx
     * @param info
     * @throws BOSException
     */
    protected void checkWorkHandoverInfo(
            Context ctx,
            WorkHandoverInfo info
    ) throws BOSException, EASBizException {
        WorkHandoverEntryCollection entrys = info.getEntrys();
        if (entrys.isEmpty()) {
            throw new BOSException("交接单分录不能为空!");
        }
        //存储本单据中所有分录绑定的离职单id
        Set<String> resignBizBillIds = new HashSet<String>();
        IWorkHandoverEntry iWorkHandoverEntry = WorkHandoverEntryFactory.getLocalInstance(ctx);
        IResignBizBill iResignBizBill = ResignBizBillFactory.getLocalInstance(ctx);
        for (int i = 0; i < entrys.size(); i++) {
            WorkHandoverEntryInfo workHandoverEntryInfo = entrys.get(i);
            ResignBizBillInfo resignBizBill = workHandoverEntryInfo.getResignBizBill();
            if (resignBizBill == null) {
                throw new BOSException("离职单不能为空!");
            }
            //离职单编码
            String resignBizBillNumber = resignBizBill.getNumber();
            //离职单id
            String resignBizBillId = resignBizBill.getId().toString();
            //1.判断本单据分录中是否包含针对同一离职单做交接单
            if (resignBizBillIds.contains(resignBizBillId)) {
                throw new BOSException("本单据中其他分录已经针对同一离职单做了交接单!");
            }
            //2.判断本单据分录中是否包含针对离职单重复做交接单
            FilterInfo filterInfo = new FilterInfo();
            FilterItemCollection filterItems = filterInfo.getFilterItems();
            filterItems.add(new FilterItemInfo("resignBizBill", resignBizBillId));
            if (info.getId() != null) {
                filterItems.add(new FilterItemInfo("parent.id", info.getId(), CompareType.NOTEQUALS));
            }
            SelectorItemCollection sic = new SelectorItemCollection();
            sic.add("parent.number");
            EntityViewInfo viewInfo = EntityViewInfo.getInstance(filterInfo, sic, null);
            WorkHandoverEntryCollection workHandoverEntryCol = iWorkHandoverEntry.getWorkHandoverEntryCollection(viewInfo);
            if (!workHandoverEntryCol.isEmpty()) {
                if (StringUtils.isEmpty(resignBizBillNumber)) {
                    ResignBizBillInfo resignBizBillInfo = iResignBizBill.getResignBizBillInfo(new ObjectUuidPK(resignBizBillId));
                    resignBizBillNumber = resignBizBillInfo.getNumber();
                }
                String number = workHandoverEntryCol.get(0).getParent().getNumber();
                throw new BOSException("离职单[" + resignBizBillNumber + "]已经绑定在其他交接单[" + number + "]，请勿重复绑定!");
            }
            resignBizBillIds.add(resignBizBillId);
            //3.计算离职日期
            Date leftDate = workHandoverEntryInfo.getDate("leftDate");
            WorkHandoverUtil.setResignBizDate(ctx, leftDate, workHandoverEntryInfo);
        }
    }
}