public JsonResult CartFont(int user_seq, int page = 1) { try { var purchased_font_list = new List <MyFontViewModel>(); var payment_query = db.Payment .Where(p => !p.deleted_at.HasValue && p.user_seq == user_seq) .ToList(); var query = db.MyFont .Where(mf => !mf.deleted_at.HasValue && mf.user_seq == user_seq && !mf.purchased_at.HasValue); query = query .OrderByDescending(q => q.created_at) .Skip((page - 1) * 30) .Take(30); foreach (var q in query) { var font_seq_string = q.font_seq.ToString(); var payment_record = payment_query .Where(pq => pq.fontids != null && pq.fontids.Split('/').Count(s => s == font_seq_string) > 0 && pq.is_personal == q.is_personal) .OrderByDescending(pq => pq.paid_at) .FirstOrDefault(); if (payment_record != null) { q.purchased_at = payment_record.paid_at; continue; } var model = new MyFontViewModel { my_font_seq = q.seq, font_info = new FontViewModel { created_at = q.created_at.HasValue ? q.created_at.Value.ToString("yyyy-MM-dd") : null, font_seq = q.font_seq.HasValue ? q.font_seq.Value : 0, format = q.Font.format, info = q.Font.info, is_free = q.Font.is_free.HasValue ? q.Font.is_free.Value : Free.no, is_personal = q.is_personal.HasValue ? q.is_personal.Value : 1, name = q.Font.name, os = q.Font.os, period = q.Font.period.HasValue ? q.Font.period.Value : 0, photos = db.FontPhoto .Where(fp => !fp.deleted_at.HasValue && fp.font_seq == q.font_seq) .Select(fp => fp.photo_url) .ToList(), price = q.Font.price.HasValue ? q.Font.price.Value : 0, price_commercial = q.Font.price_commercial.HasValue ? q.Font.price_commercial.Value : 0, seller_name = q.Font.seller_name, file_url = q.Font.file_url, link_url = q.Font.link_url }, is_personal = q.is_personal, purchased_at = q.purchased_at.HasValue ? q.purchased_at.Value.ToString("yyyy-MM-dd") : null, user_seq = user_seq }; if (model.font_info.is_personal == 1) { model.font_info.period = 0; } if (q.Font.brand_seq.HasValue) { model.font_info.brand_info = new BrandViewModel { brand_seq = q.Font.brand_seq, name = db.Brand .Where(b => !b.deleted_at.HasValue && b.seq == q.Font.brand_seq) .FirstOrDefault() != null?db.Brand .Where(b => !b.deleted_at.HasValue && b.seq == q.Font.brand_seq) .FirstOrDefault().name : null }; } purchased_font_list.Add(model); } db.SaveChanges(); var result = new { state = "ok", data = purchased_font_list, total_count = query.Count() }; return(Json(result, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet)); } catch (HttpException e) { Response.StatusCode = e.GetHttpCode(); return(Json(e.Message, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet)); } }
public JsonResult PurchasedFont(int user_seq, int page = 1) { try { var today = DateTime.Now; var purchased_font_list = new List <MyFontViewModel>(); var font_query = db.Font .Where(f => !f.deleted_at.HasValue); var query = db.Payment .Where(mf => !mf.deleted_at.HasValue && mf.user_seq == user_seq && mf.ok == 1); foreach (var q in query) { var font_ids = q.fontids.Split('/'); foreach (var font_id in font_ids) { if (font_id == "") { continue; } var font_seq = Convert.ToInt32(font_id); var font = font_query .Where(fq => fq.seq == font_seq) .FirstOrDefault(); if (font == null) { continue; } if (q.is_personal == 0) { if (q.paid_at.Value.AddMonths(font.period.Value) < today && font.period != 0) { continue; } } var model = new MyFontViewModel { my_font_seq = q.seq, font_info = new FontViewModel { created_at = q.created_at.HasValue ? q.created_at.Value.ToString("yyyy-MM-dd") : null, font_seq = font.seq, format = font.format, info = font.info, is_free = font.is_free.HasValue ? font.is_free.Value : Free.no, is_personal = q.is_personal.HasValue ? q.is_personal.Value : 1, name = font.name, os = font.os, period = font.period.HasValue ? font.period.Value : 0, photos = db.FontPhoto .Where(fp => !fp.deleted_at.HasValue && fp.font_seq == font.seq) .Select(fp => fp.photo_url) .ToList(), price = font.price.HasValue ? font.price.Value : 0, price_commercial = font.price_commercial.HasValue ? font.price_commercial.Value : 0, seller_name = font.Brand.name, file_url = font.file_url, link_url = font.link_url }, is_personal = q.is_personal.HasValue ? q.is_personal.Value : 1, purchased_at = q.paid_at.HasValue ? q.paid_at.Value.ToString("yyyy-MM-dd") : null, user_seq = user_seq }; if (q.is_personal == 1) { model.font_info.period = 0; } if (font.brand_seq.HasValue) { model.font_info.brand_info = new BrandViewModel { brand_seq = font.brand_seq, name = db.Brand .Where(b => !b.deleted_at.HasValue && b.seq == font.brand_seq) .FirstOrDefault() != null?db.Brand .Where(b => !b.deleted_at.HasValue && b.seq == font.brand_seq) .FirstOrDefault().name : null }; } purchased_font_list.Add(model); } } purchased_font_list = purchased_font_list .OrderByDescending(q => q.font_info.created_at) .Skip((page - 1) * 30) .Take(30) .ToList(); var result = new { state = "ok", data = purchased_font_list, total_count = query.Count() }; return(Json(result, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet)); } catch (HttpException e) { Response.StatusCode = e.GetHttpCode(); return(Json(e.Message, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet)); } }