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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.MetaDataLoaderFactory;
import com.kingdee.bos.metadata.MetaDataPK;
import com.kingdee.bos.metadata.entity.EntityObjectInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.PropertyInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.sql.ParserException;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.dormitorysystem.application.CheckInApplicationEntryInfo;
import com.kingdee.eas.custom.dormitorysystem.application.CheckInApplicationFactory;
import com.kingdee.eas.custom.dormitorysystem.application.CheckInApplicationInfo;
import com.kingdee.eas.custom.dormitorysystem.application.ICheckInApplication;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryCollection;
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.waterpower.WaterPowerMeterReadingInfo;
import com.kingdee.eas.custom.dormitorysystem.waterpower.utils.WaterPowerUtil;
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.ats.web.util.NumberCodeRule;
import com.kingdee.shr.base.syssetting.api.bean.BaseItemParam;
import com.kingdee.shr.base.syssetting.api.service.SHRBaseItemApi;
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.BaseRowInfo;
import com.kingdee.shr.base.syssetting.app.io.fileImport.ImportException;
import com.kingdee.shr.ml.util.SHRServerResource;
import com.kingdee.util.DateTimeUtils;
import com.kingdee.util.StringUtils;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description 入住申请单导入实现类
 * @Date 2025/4/24 13:48
 * @Created by Heyuan
 */
public class CheckInApplicationImportService 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.dormitory.app.Dormitory".equals(entityName)) {
            //房间
            FilterInfo filter = new FilterInfo();
            filter.getFilterItems().add(new FilterItemInfo("RoomClassification", "3"));
            return filter;
        }
        return defaultFilterByEntityInfo;
    }

    protected void verifyRowBiz(CoreBaseInfo coreBaseInfo) throws ImportException {
        DormitoryInfo dormitoryInfo = null;
        try {
            Context ctx = this.getContext();
            CheckInApplicationEntryInfo entryInfo = (CheckInApplicationEntryInfo) coreBaseInfo;
            DormitoryInfo checkInRoom = entryInfo.getCheckInRoom();
            String dormitoryId = checkInRoom.getId().toString();
            IDormitory iDormitory = DormitoryFactory.getLocalInstance(ctx);
            dormitoryInfo = iDormitory.getDormitoryInfo(new ObjectUuidPK(dormitoryId));
        } catch (Exception e) {
            throw new ImportException(e);
        }
        DormitoryClassification roomClassification = dormitoryInfo.getRoomClassification();
        if (!DormitoryClassification.ROOM.equals(roomClassification)) {
            throw new ImportException("当前入住房间为" + roomClassification.getAlias() + "，入住房间必须是房间类型!");
        }
    }

    protected void submitData(CoreBaseInfo coreBaseInfo) throws ImportException {
        Context ctx = this.getContext();
        try {
            CheckInApplicationInfo checkInApplicationInfo = new CheckInApplicationInfo();
            checkInApplicationInfo.setBillState(HRBillStateEnum.SAVED);
            HROrgUnitInfo hrOrgUnitInfo = new HROrgUnitInfo();
            hrOrgUnitInfo.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
            checkInApplicationInfo.setHrOrgUnit(hrOrgUnitInfo);
            checkInApplicationInfo.setManagementNotes("本人愿意遵守以下条款：" +
                    "\n1、自觉遵守公司《宿舍管理制度》和员工宿舍区有关管理办法，不私自调换宿舍和床位。" +
                    "\n2、自觉维护宿舍公共区域的卫生，爱护公物，损坏公物自觉赔偿。 3、自觉遵守消防安全，节约用电用水。" +
                    "\n3、自觉遵守消防安全，节约用电用水。");
            //分录
            CheckInApplicationEntryInfo entryInfo = (CheckInApplicationEntryInfo) coreBaseInfo;
            Date checkoutDate = entryInfo.getCheckoutDate();
            if (checkoutDate == null) {
                entryInfo.setCheckoutDate(DateTimeUtils.parseDate("2199-12-31", "yyyy-MM-dd"));
            }
            String personId = entryInfo.getPerson().getId().toString();
            PersonPositionInfo pp = PersonPositionFactory.getLocalInstance(ctx).getPersonPositionInfo("select personDep.*,primaryPosition.* where person = '" + personId + "'");
            entryInfo.setAdminOrg(pp.getPersonDep());
            entryInfo.setPosition(pp.getPrimaryPosition());
            checkInApplicationInfo.getEntrys().add(entryInfo);
            CheckInApplicationFactory.getLocalInstance(ctx).save(checkInApplicationInfo);
        } 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;
        }
    }
}
