package com.kingdee.eas.custom.dormitorysystem.application.service;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemCollection;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.SelectorInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.dormitorysystem.application.*;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryFactory;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryInfo;
import com.kingdee.eas.custom.dormitorysystem.dormitory.IDormitory;
import com.kingdee.eas.custom.dormitorysystem.enumerationset.DormitoryClassification;
import com.kingdee.eas.custom.dormitorysystem.occupants.IOccupants;
import com.kingdee.eas.custom.dormitorysystem.occupants.OccupantsFactory;
import com.kingdee.eas.custom.dormitorysystem.occupants.OccupantsInfo;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.eas.hr.emp.PersonPositionFactory;
import com.kingdee.eas.hr.emp.PersonPositionInfo;
import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseColumnInfo;
import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseImportService;
import com.kingdee.shr.base.syssetting.app.io.fileImport.ImportException;
import com.kingdee.shr.ml.util.SHRServerResource;
import com.kingdee.util.DateTimeUtils;
import org.apache.log4j.Logger;

import java.util.Date;

/**
 * @Description 入住申请单导入实现类
 * @Date 2025/4/24 13:48
 * @Created by Heyuan
 */
public class CheckoutApplicationImportService extends BaseImportService {
    Logger logger = Logger.getLogger(this.getClass());

    @Override
    protected FilterInfo getDefaultFilterByEntityInfo(String entityName, BaseColumnInfo columnInfo) {
        FilterInfo defaultFilterByEntityInfo = super.getDefaultFilterByEntityInfo(entityName, columnInfo);
        if ("com.kingdee.eas.custom.dormitorysystem.occupants.app.Occupants".equals(entityName)) {
            Date date = new Date();
            //房间
            FilterInfo filter = new FilterInfo();
            FilterItemCollection filterItems = filter.getFilterItems();
            filterItems.add(new FilterItemInfo("CheckInDate", date, CompareType.LESS_EQUALS));
            filterItems.add(new FilterItemInfo("CheckOutDate", date, CompareType.GREATER));
            return filter;
        }
        return defaultFilterByEntityInfo;
    }

    protected void verifyRowBiz(CoreBaseInfo coreBaseInfo) throws ImportException {
        try {
            Context ctx = this.getContext();
            CheckoutApplicationEntryInfo entryInfo = (CheckoutApplicationEntryInfo) coreBaseInfo;
            Date checkoutDate = entryInfo.getCheckoutDate();
            OccupantsInfo occupants = entryInfo.getOccupants();
            String occupantsId = occupants.getId().toString();
            IOccupants iOccupants = OccupantsFactory.getLocalInstance(ctx);
            occupants = iOccupants.getOccupantsInfo(new ObjectUuidPK(occupantsId));
            Date checkInDate = occupants.getCheckInDate();
            Date oldCheckOutDate = occupants.getCheckOutDate();
            if (!checkoutDate.after(checkInDate)) {
                throw new ImportException("退宿日期不能小于等于入住日期!");
            } else if (checkoutDate.compareTo(oldCheckOutDate) == 0) {
                throw new ImportException("退宿日期不能等于原退宿日期!");
            }
        } catch (BOSException e) {
            logger.error(e);
            throw new ImportException(e);
        } catch (EASBizException e) {
            logger.error(e);
            throw new ImportException(e);
        }
    }

    protected void submitData(CoreBaseInfo coreBaseInfo) throws ImportException {
        Context ctx = this.getContext();
        try {
            CheckoutApplicationInfo checkoutApplicationInfo = new CheckoutApplicationInfo();
            checkoutApplicationInfo.setBillState(HRBillStateEnum.SAVED);
            HROrgUnitInfo hrOrgUnitInfo = new HROrgUnitInfo();
            hrOrgUnitInfo.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
            checkoutApplicationInfo.setHrOrgUnit(hrOrgUnitInfo);
            //分录
            CheckoutApplicationEntryInfo entryInfo = (CheckoutApplicationEntryInfo) coreBaseInfo;
            OccupantsInfo occupants = entryInfo.getOccupants();
            String occupantsId = occupants.getId().toString();
            SelectorItemCollection sic = new SelectorItemCollection();
            sic.add("person.id");
            sic.add("CheckOutDate");
            sic.add("CheckInDate");
            sic.add("Dormitory.id");
            sic.add("personPosition.personDep.id");
            sic.add("personPosition.primaryPosition.id");
            IOccupants iOccupants = OccupantsFactory.getLocalInstance(ctx);
            occupants = iOccupants.getOccupantsInfo(new ObjectUuidPK(occupantsId),sic);
            entryInfo.setPerson(occupants.getPerson());
            entryInfo.setAdminOrg(occupants.getPersonPosition().getPersonDep());
            entryInfo.setPosition(occupants.getPersonPosition().getPrimaryPosition());
            //原退宿日期
            entryInfo.setOldCheckoutDate(occupants.getCheckOutDate());
            entryInfo.setCurrentRoom(occupants.getDormitory());
            entryInfo.setCheckInDate(occupants.getCheckInDate());
            checkoutApplicationInfo.getEntrys().add(entryInfo);
            CheckoutApplicationFactory.getLocalInstance(ctx).save(checkoutApplicationInfo);
        } catch (EASBizException e) {
            ImportException importException = new ImportException(e.getMessage(ctx.getLocale()), e);
            throw importException;
        } catch (BOSException e) {
            ImportException importException = new ImportException(SHRServerResource.getString("com.kingdee.shr.base.syssetting.CommonserviceResource", "save_fails_bos", ctx), e);
            throw importException;
        } catch (Exception e) {
            ImportException importException = new ImportException(SHRServerResource.getString("com.kingdee.shr.base.syssetting.CommonserviceResource", "save_submit_fails", ctx), e);
            throw importException;
        }
    }
}
