package nckd.jimin.jyyy.fi.business.plugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.util.StringUtils;
import kd.fi.er.business.trip.exception.TripSyncLogParam;

import java.util.Map;

/**
 * 获取人员常驻工作地插件
 */
public class LocationCityPlugin {

    private static final Log logger = LogFactory.getLog(LocationCityPlugin.class);

    public static LocationCityPlugin getInstance() {
        return LocationCityPlugin.loadClass.service;
    }

    public LocationCityPlugin() {
    }

    public Object getLocationCity(Map<String, Object> map, TripSyncLogParam tripSyncLogParam) {
        logger.info("商旅集成，字段映射插件执行：{} ", tripSyncLogParam.getTraceid());
        DynamicObject billInfo = (DynamicObject)map.get("bill");
        String billfield = (String)map.get("billfield");
        logger.info("billfield：{}  ", billfield);
        if(billInfo != null){
            String applierId = billInfo.getString(billfield);
            if(StringUtils.isNotEmpty(applierId)){
                Object locationCity = getLocationCity(Long.valueOf(applierId));
                if(locationCity != null){
                    // 查询行政区划名称
                    return QueryServiceHelper.queryOne("bd_admindivision", "name",
                            new QFilter("id", QCP.equals, locationCity).toArray()).getString("name");

                }
            }
        }
        return null;
    }

    public Object getLocationCity(Object userId){
        logger.info("userId:" + userId);
        // 申请人 -> HR人员 -> 常驻工作地 -> 工作地
        DynamicObject userInfo = QueryServiceHelper.queryOne("bos_user", "id,number", new QFilter("id", QCP.equals, userId).toArray());
        DynamicObject hrPerson = QueryServiceHelper.queryOne("hrpi_person", "id", new QFilter[]{
                new QFilter("number", QCP.equals, userInfo.getString("number")),
                new QFilter("iscurrentversion", QCP.equals, Boolean.TRUE),
                new QFilter("datastatus", QCP.equals, "1"),
        });

        if(hrPerson != null){
            logger.info("hrPerson_id:" + hrPerson.getLong("id"));
            DynamicObject baseLocation = QueryServiceHelper.queryOne("hrpi_baselocation", "location.city.id", new QFilter[]{
                    new QFilter("person", QCP.equals, hrPerson.getLong("id")),
                    new QFilter("iscurrentversion", QCP.equals, Boolean.TRUE),
                    new QFilter("datastatus", QCP.equals, "1"),
                    new QFilter("businessstatus", QCP.equals, "1")
            });

            if(baseLocation != null){
                return baseLocation.getLong("location.city.id");
            }

        }
        return null;
    }

    private static class loadClass {
        private static LocationCityPlugin service = new LocationCityPlugin();

        private loadClass() {
        }
    }

}
