using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Variables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace PTMedicalInsurance.Common
{
public class Utils
{
#region 工具
///
/// 获取交易号
///
///
public static string GetTradeNo()
{
return Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff");
}
///
/// 日期转换为时间戳Timestamp
///
/// 要转换的日期
///
public static long GetTimeStamp(DateTime dateTime)
{
DateTime _dtStart = new DateTime(1970, 1, 1, 8, 0, 0);
//10位的时间戳
long timeStamp = Convert.ToInt32(dateTime.Subtract(_dtStart).TotalSeconds);
//13位的时间戳
//long timeStamp = Convert.ToInt64(dateTime.Subtract(_dtStart).TotalMilliseconds);
return timeStamp;
}
///
/// UTC时间戳Timestamp转换为北京时间
///
/// 要转换的时间戳
///
public static DateTime GetDateTime(long timestamp)
{
//DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
//使用上面的方式会显示TimeZone已过时
DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
DateTime targetDt = dtStart.AddMilliseconds(timestamp).AddHours(8);
return targetDt;
}
///
/// 将JObject对象中的timestamp数据转为yyyy-MM-dd格式
///
///
///
public static void convertTimestamp(JObject joObject,string key)
{
if (joObject.ContainsKey(key))
{
Global.writeLog(string.Format("当前对象[{0}]的类型为:{1}", key, joObject.GetValue(key).Type.GetType()));
}
if (joObject.ContainsKey(key) && joObject.GetValue(key).Type.GetType() != typeof(string))
{
long timestamp = long.Parse(joObject.GetValue(key).ToString());
string datetime = Utils.GetDateTime(timestamp).ToString("yyyy-MM-dd");
joObject[key] = datetime;
Global.writeLog(string.Format("转换后的日期为:{0}",datetime));
}
}
#endregion
#region BeanUtils
///
/// 用反射对实体进行属性值合并,返回合并后的属性并集
///
///
/// 合并对象1
/// 合并对象2
/// 是否强制覆盖
///
public static T merge(T entity, T addtional,bool forceCover)
{
if (entity == null) return addtional;
if (addtional == null) return entity;
// 进行合并
Type mType = typeof(T);
object result = Activator.CreateInstance(mType);
PropertyInfo[] pi = mType.GetProperties();
pi.ToList().ForEach((p) =>
{
object v1 = p.GetValue(entity);
object v2 = p.GetValue(addtional);
if (forceCover)
{
v1 = v2;
}
else {
v1 = (v1 == null ? v2 : v1);
}
p.SetValue(result, v1);
});
return (T)result;
}
///
/// 对2个对象进行合并,以第一个对象为重点
///
///
///
///
///
public static T merge(T entity, T addtional)
{
return merge(entity, addtional, false);
}
private static JObject merge(JObject to, JObject from)
{
to.Merge(from);
return to;
}
///
/// 采用Json的方式进行属性合并(默认会覆盖)
///
///
///
///
///
public static T mergeObject(T to, T from)
{
JObject toObj = JObject.FromObject(to);
JObject fromObj = JObject.FromObject(from);
JObject result = merge(toObj, fromObj);
return result.ToObject();
}
#endregion
#region 业务个性化
///
/// 在JObject对象基础上进行包装
///
///
///
public static JObject Wrapper(JObject jsonInParam)
{
dynamic request = new JObject();
request.arg0 = jsonInParam;
return request;
}
///
/// 删除JObject上的进行包装
///
///
///
public static JObject removeWrapper(JObject jsonInParam)
{
if (jsonInParam.ContainsKey("arg0"))
{
return (JObject)jsonInParam.GetValue("arg0");
}
return jsonInParam;
}
public static bool isOtherCityPatient()
{
// 重庆没有市内异地
string areaCode = Global.pat.insuplc_admdvs;
if (!string.IsNullOrEmpty(areaCode) && areaCode.Length>2 && areaCode.Substring(0, 2) != "50")
{
return true;
}
return false;
}
public static string convertAdmDr(string admNo)
{
// 重庆异地(每次唯一)
if (isOtherCityPatient())
{
// 住院号15位,控制总长度不超过20
string ret = admNo + "-" + DateTime.Now.ToString("HHmm");
// 保存到扩展信息字段中
JObject exp = new JObject();
exp.Add("admDr",ret);
Global.pat.ExpContent = exp.ToString();
return ret;
}
return admNo;
}
#endregion
}
}