package com.kingdee.eas.custom.expandassess.service.impl;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.expandassess.*;
import com.kingdee.eas.custom.expandassess.app.ExpandStatusEunm;
import com.kingdee.eas.custom.expandassess.app.PeriodStatusEnum;
import com.kingdee.eas.custom.expandassess.service.ExpandAssessService;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.hr.ats.AtsUtil;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.util.StringUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * @author qingwu
 * @date 2024/5/17
 * @apiNote
 */
public class ExpandAssessServiceImpl implements ExpandAssessService {
    @Override
    public boolean verifyeriodLockOrNot(Context ctx, String billId) throws BOSException, ShrWebBizException {
        IExpandAssess iExpandAssess = ExpandAssessFactory.getLocalInstance(ctx);
        ExpandAssessCollection expandAssessCollection = iExpandAssess.getExpandAssessCollection("where id = '" + billId + "'");
        if (expandAssessCollection.size() > 0) {
            ExpandAssessInfo expandAssessInfo = expandAssessCollection.get(0);
            if (expandAssessInfo.getPeriodStatus() == PeriodStatusEnum.LOCK) {
                return true;
            }
        }
        return false;
    }

    @Override
    public QuarterlyRatioCollection getQuarterlyRatioByYear(Context ctx, int year) throws BOSException {
        //季度比例
        IQuarterlyRatio iQuarterlyRatio = QuarterlyRatioFactory.getLocalInstance(ctx);
        QuarterlyRatioCollection quarterlyRatioCollection = iQuarterlyRatio.getQuarterlyRatioCollection(" where year = '" + year + "'");
        if (quarterlyRatioCollection.size() > 0) {
            QuarterlyRatioInfo quarterlyRatioInfo = quarterlyRatioCollection.get(0);
            if (quarterlyRatioInfo.getQuarterOne() != 0 ||
                    quarterlyRatioInfo.getQuarterTwo() != 0 ||
                    quarterlyRatioInfo.getQuarterThree() != 0 ||
                    quarterlyRatioInfo.getQuarterFour() != 0) {
                return quarterlyRatioCollection;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public ExpandAssessEntryCollection qeruyEntryByBillIdAndAdminOrg(Context ctx, String billId, String adminOrgId) throws BOSException {
        IExpandAssessEntry iExpandAssessEntry = ExpandAssessEntryFactory.getLocalInstance(ctx);
        //当前登录员工id
        String currentPersonId = ContextUtil.getCurrentUserInfo(ctx).getPerson().getId().toString();
        FilterInfo filterInfo = new FilterInfo();
        filterInfo.getFilterItems().add(new FilterItemInfo("parent.id", billId));
        filterInfo.getFilterItems().add(new FilterItemInfo("adminOrg.id", adminOrgId));
        filterInfo.getFilterItems().add(new FilterItemInfo("assestatus", 3));
        filterInfo.getFilterItems().add(new FilterItemInfo("principal.id", currentPersonId));
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("parent.*");
        sic.add("*");
        EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, sic, null);
        return iExpandAssessEntry.getExpandAssessEntryCollection(entityViewInfo);
    }

    @Override
    public void updateAsseStatusAll(Context ctx, String idArray, ExpandStatusEunm eunm1, ExpandStatusEunm eunm2, String errorText) throws BOSException, ShrWebBizException, EASBizException {
        IExpandAssessEntry iExpandAssessEntry = ExpandAssessEntryFactory.getLocalInstance(ctx);
        FilterInfo filterInfo = new FilterInfo();
        filterInfo.getFilterItems().add(new FilterItemInfo("id", AtsUtil.toSet(idArray), CompareType.INCLUDE));
        EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, null, null);
        ExpandAssessEntryCollection expandAssessEntryCollection = iExpandAssessEntry.getExpandAssessEntryCollection(entityViewInfo);
        CoreBaseCollection collection = new CoreBaseCollection();
        if (expandAssessEntryCollection.size() > 0) {
            for (int i = 0; i < expandAssessEntryCollection.size(); i++) {
                ExpandAssessEntryInfo expandAssessEntryInfo = expandAssessEntryCollection.get(i);
                if (expandAssessEntryInfo.getAsseStatus() == eunm1) {
                    expandAssessEntryInfo.setAsseStatus(eunm2);
                    collection.add(expandAssessEntryInfo);
                } else {
                    throw new ShrWebBizException(errorText);
                }

            }
        }
        iExpandAssessEntry.updateBatchData(collection);
    }

    /**
     * 获取当前登录人等于负责人的数据
     *
     * @param billId
     * @return
     */
    public ExpandAssessEntryCollection getCurrentPersonEqualsPrincipal(Context ctx, String billId, String adminId) throws ShrWebBizException, BOSException {
        //当前登录员工id
        String currentPersonId = ContextUtil.getCurrentUserInfo(ctx).getPerson().getId().toString();
        if (StringUtils.isEmpty(billId)) {
            throw new ShrWebBizException("拓展人员考核周期ID不可为空！");
        }
        IExpandAssessEntry iExpandAssessEntry = ExpandAssessEntryFactory.getLocalInstance(ctx);
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("*");
        sic.add("assessor.*");
        sic.add("adminOrg.*");
        sic.add("principal.*");
        FilterInfo filterInfo = new FilterInfo();
        FilterItemCollection filterItems = filterInfo.getFilterItems();
        filterItems.add(new FilterItemInfo("parent.id", billId));
        filterItems.add(new FilterItemInfo("principal.id", currentPersonId));
        filterItems.add(new FilterItemInfo("assestatus", 3));
        if (!StringUtils.isEmpty(adminId)) {
            filterItems.add(new FilterItemInfo("adminOrg.id", adminId));
        }
        EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, sic, null);
        return iExpandAssessEntry.getExpandAssessEntryCollection(entityViewInfo);
    }

    /**
     * 过滤组织
     */
    @Override
    public String getAdminOrgFilter(Context ctx, String billID) throws ShrWebBizException, BOSException {
        ExpandAssessEntryCollection entrys = getCurrentPersonEqualsPrincipal(ctx, billID, "");
        Set set = new HashSet<>();
        StringBuilder sb = new StringBuilder();
        //去重操作
        for (int i = entrys.size() - 1; i >= 0; i--) {
            ExpandAssessEntryInfo expandAssessEntryInfo = entrys.get(i);
            String adminId = expandAssessEntryInfo.getAdminOrg().getId().toString();
            if (set.contains(adminId)) {
                entrys.remove(expandAssessEntryInfo);
            } else {
                set.add(adminId);
                sb.append("'").append(adminId).append("',");
            }
        }
        // 删除最后一个逗号
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    @Override
    public void verifyStatus(String trs) {

    }

    @Override
    public void deleteExpandAssessEntry(Context ctx, String idArray) throws BOSException, ShrWebBizException, EASBizException {
        IExpandAssessEntry iExpandAssessEntry = ExpandAssessEntryFactory.getLocalInstance(ctx);
        FilterInfo filterInfo = new FilterInfo();
        filterInfo.getFilterItems().add(new FilterItemInfo("id", AtsUtil.toSet(idArray), CompareType.INCLUDE));
        EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, null, null);
        ExpandAssessEntryCollection expandAssessEntryCollection = iExpandAssessEntry.getExpandAssessEntryCollection(entityViewInfo);
        if (expandAssessEntryCollection.size() > 0) {
            for (int i = 0; i < expandAssessEntryCollection.size(); i++) {
                ExpandAssessEntryInfo expandAssessEntryInfo = expandAssessEntryCollection.get(i);
                if (expandAssessEntryInfo.getAsseStatus() == (ExpandStatusEunm.START) ||
                        expandAssessEntryInfo.getAsseStatus() == (ExpandStatusEunm.ASSESUBMINT) ||
                        expandAssessEntryInfo.getAsseStatus() == (ExpandStatusEunm.PRINCIPALSUBMIT)) {
                    throw new ShrWebBizException("只能删除未启动考核的数据！！");
                }
            }
        }
        iExpandAssessEntry.delete(filterInfo);
    }

    @Override
    public ExpandAssessEntryCollection getEntrysByBill(Context ctx, String billId) throws ShrWebBizException, BOSException {
        if (StringUtils.isEmpty(billId)) {
            throw new ShrWebBizException("拓展人员考核周期ID不可为空！");
        }
        IExpandAssessEntry iExpandAssessEntry = ExpandAssessEntryFactory.getLocalInstance(ctx);
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("*");
        sic.add("assessor.name");
        sic.add("adminOrg.name");
        sic.add("principal.name");
        FilterInfo filterInfo = new FilterInfo();
        FilterItemCollection filterItems = filterInfo.getFilterItems();
        filterItems.add(new FilterItemInfo("parent.id", billId));
        EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, sic, null);
        return iExpandAssessEntry.getExpandAssessEntryCollection(entityViewInfo);
    }


}
