@using Newtonsoft.Json.Linq; @using Newtonsoft.Json; @functions{ public class M_GraphData { // 性别数据 public int[] UserSexJson = new int[2]; //年龄数据 public int[] AgeSetJson = new int[6]; //登录数据 public int[] LoginRateJson = new int[5]; //省份数据 public int[] ProvinceSet = new int[32]; //用户活跃度数据 public int[] UserActivityJson = new int[5]; //消费积分数据 public int[] ConsumeExpJson = new int[6]; //余额、银币、积分数据 public int[] FundJson = new int[6], SliverCoinJson = new int[6], UserExpJson = new int[6]; //登录时间数据 public int[] UserRegTimeJson = new int[6]; public List ProvinceName = new List(); } B_User _buser = null; B_User buser { get { if (_buser == null) { _buser = new B_User(); }return _buser; } } M_GraphData dataMod = new M_GraphData(); private void AnalyseData(DataRow dr) { int userId = Convert.ToInt32(dr["UserID"]); M_Uinfo baseUserInfo = buser.GetUserBaseByuserid(userId); M_UserInfo userInfo = buser.GetUserByUserID(userId); //统计年龄 string birthday = baseUserInfo.BirthDay; SetAgeGroup(birthday); //统计性别 //注意:数据库中按位储存性别信息,1为男,0为女 bool userSex = baseUserInfo.UserSex; if (userSex) dataMod.UserSexJson[0]++; else dataMod.UserSexJson[1]++; //统计登录频率 int freq = userInfo.LoginTimes; SetLoginRate(freq); //统计省份信息 string provinc = baseUserInfo.Province; SetProvince(provinc); //统计活跃度 DateTime lastLoginTimes = userInfo.LastLoginTimes; SetActivity(lastLoginTimes); //消费积分 int consumeExp = userInfo.ConsumeExp; GenerialSetFunds(dataMod.ConsumeExpJson, consumeExp); //用户余额、银币、积分 double purse = userInfo.Purse; double sliverCoin = userInfo.SilverCoin; double userExp = userInfo.UserExp; SetUserFunds(purse, sliverCoin, userExp); //用户注册时间统计 DateTime regTime = userInfo.RegTime; SetUserRegTime(regTime); } // 根据注册时间填充数据 private void SetUserRegTime(DateTime regTime) { DateTime thisDay = DateTime.Now; TimeSpan last = new TimeSpan(regTime.Ticks); TimeSpan now = new TimeSpan(thisDay.Ticks); int days = now.Subtract(last).Duration().Days; if (days == 0) dataMod.UserRegTimeJson[0]++; if (days < 7) dataMod.UserRegTimeJson[1]++; if (days < 30) dataMod.UserRegTimeJson[2]++; if (days < 120) dataMod.UserRegTimeJson[3]++; if (days < 365) dataMod.UserRegTimeJson[4]++; dataMod.UserRegTimeJson[5]++; } // 判断用户的余额、银币、用户积分 private void SetUserFunds(double purse, double sliverCoin, double userExp) { GenerialSetFunds(dataMod.FundJson, purse); GenerialSetFunds(dataMod.SliverCoinJson, sliverCoin); GenerialSetFunds(dataMod.UserExpJson, userExp); } // 对消费指数、余额、银币、积分的计算 private void GenerialSetFunds(int[] dic, double value) { if (value < 100) dic[0]++; else if (value < 500) dic[1]++; else if (value < 1000) dic[2]++; else if (value < 5000) dic[3]++; else if (value < 10000) dic[4]++; else dic[5]++; } // 根据用户上次登录时间填充活跃度数据 private void SetActivity(DateTime lastLoginTimes) { if (lastLoginTimes == null) { dataMod.UserActivityJson[4] += 1; return; } TimeSpan now = new TimeSpan(DateTime.Now.Ticks); TimeSpan last = new TimeSpan(lastLoginTimes.Ticks); int days = now.Subtract(last).Duration().Days; if (days < 3) dataMod.UserActivityJson[0] += 1; else if (days < 7) dataMod.UserActivityJson[1] += 1; else if (days < 30) dataMod.UserActivityJson[2] += 1; else if (days < 90) dataMod.UserActivityJson[3] += 1; } //填充省份信息 private void SetProvince(string provinc) { int index = GetProvinceIndex(provinc); dataMod.ProvinceSet[index]++; } // 获取省份index private int GetProvinceIndex(string provinc) { if (string.IsNullOrEmpty(provinc)) return dataMod.ProvinceSet.Length - 1; if (dataMod.ProvinceName.Contains(provinc)) { return dataMod.ProvinceName.IndexOf(provinc); } return dataMod.ProvinceName.Count - 1; } // 根据用户登录次数填充登录频率数据 private void SetLoginRate(int freq) { if (freq > 500) dataMod.LoginRateJson[0]++; else if (freq > 250) dataMod.LoginRateJson[1]++; else if (freq > 100) dataMod.LoginRateJson[2]++; else if (freq > 50) dataMod.LoginRateJson[3]++; else dataMod.LoginRateJson[4]++; } // 获得用户年龄填充年龄数据 private void SetAgeGroup(string birthday) { if (string.IsNullOrEmpty(birthday)) { dataMod.AgeSetJson[5]++; return; } int age = DataConverter.CLng(birthday.Split('-')[0]); if (age < 1) { dataMod.AgeSetJson[5]++;return; } int thisYear = DateTime.Now.Year; age = thisYear - age; if (age > 50) dataMod.AgeSetJson[4]++; else if (age > 40) dataMod.AgeSetJson[3]++; else if (age > 30) dataMod.AgeSetJson[2]++; else if (age > 20) dataMod.AgeSetJson[1]++; else dataMod.AgeSetJson[0]++; } } @{ string json = SafeSC.ReadFileStr("/js/ICMS/area.js").Replace("var AreaMod=", ""); JArray jarr = JsonConvert.DeserializeObject(json); foreach (JObject jobj in jarr) { dataMod.ProvinceName.Add(jobj["Name"].ToString()); } dataMod.ProvinceName.Add("未知省份"); //throw new Exception(JsonConvert.SerializeObject(test)); DataTable usersTable = buser.Sel(); foreach (DataRow dr in usersTable.Rows) { AnalyseData(dr); } } @section head{@L.用户画像 } @section content{ @Call.SetBread(new Bread[] { new Bread("{admin}"), new Bread("UserManage",L.用户管理), new Bread(Context.Request.RawUrl(),L.用户画像) })
@L.性别统计
@L.年龄统计
@L.登录频率
@L.用户地区分布
@L.活跃指数
@L.消费指数
} @section script{ }