| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Linq;
|
|---|
| 4 |
|
|---|
| 5 | namespace Wierszowki.Core.Linq
|
|---|
| 6 | {
|
|---|
| 7 | public partial class WierszowkiDataContext
|
|---|
| 8 | {
|
|---|
| 9 | public Author GetAuthorById(int id)
|
|---|
| 10 | {
|
|---|
| 11 | return Authors.SingleOrDefault(a => a.Id == id);
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | public List<Author> GetAuthors()
|
|---|
| 15 | {
|
|---|
| 16 | return Authors.ToList();
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | public List<User> GetUsers()
|
|---|
| 20 | {
|
|---|
| 21 | this.GetTable<User>();
|
|---|
| 22 |
|
|---|
| 23 | return Users.ToList();
|
|---|
| 24 | }
|
|---|
| 25 | public List<Issue> GetIssue()
|
|---|
| 26 | {
|
|---|
| 27 | return Issues.ToList();
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | public User GetUserById(int id)
|
|---|
| 31 | {
|
|---|
| 32 | return Users.SingleOrDefault(u => u.Id == id);
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | public User GetUserByLogin(string login)
|
|---|
| 36 | {
|
|---|
| 37 | return Users.SingleOrDefault(u => u.Login == login);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | public bool LoginUser(string login, string password)
|
|---|
| 41 | {
|
|---|
| 42 | var query = from u in Users
|
|---|
| 43 | where u.Login == login && u.Password == password
|
|---|
| 44 | select u;
|
|---|
| 45 |
|
|---|
| 46 | return query.Count() == 1;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | public List<Magazine> GetMagazines()
|
|---|
| 50 | {
|
|---|
| 51 | return Magazines.ToList();
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | public List<EmploymentType> GetEmploymentTypes()
|
|---|
| 55 | {
|
|---|
| 56 | return EmploymentTypes.ToList();
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | public List<ItemType> GetItemTypes()
|
|---|
| 60 | {
|
|---|
| 61 | return ItemTypes.ToList();
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | public IQueryable<Issue> FindIssuesByMagazineId(int magazineId, int addId)
|
|---|
| 65 | {
|
|---|
| 66 | var startDate = DateTime.Now.AddMonths(-3);
|
|---|
| 67 | var endDate = DateTime.Now.AddMonths(1);
|
|---|
| 68 | if (addId != 0)
|
|---|
| 69 | {
|
|---|
| 70 | var query = from i in Issues
|
|---|
| 71 | where (i.Id == addId) ||
|
|---|
| 72 | (i.MagazineId == magazineId && i.Date >= startDate && i.Date <= endDate )
|
|---|
| 73 | select i;
|
|---|
| 74 | return query;
|
|---|
| 75 | }
|
|---|
| 76 | else
|
|---|
| 77 | {
|
|---|
| 78 | var query = from i in Issues
|
|---|
| 79 | where i.MagazineId == magazineId
|
|---|
| 80 | && i.Date >= startDate
|
|---|
| 81 | && i.Date <= endDate
|
|---|
| 82 | select i;
|
|---|
| 83 | return query;
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | public IQueryable<Issue> FindIssuesByMagazineItemId(int issueId, int addId)
|
|---|
| 88 | {
|
|---|
| 89 | var issue = Issues.Single(i => i.Id == issueId);
|
|---|
| 90 | return FindIssuesByMagazineId(issue.MagazineId, addId);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | public List<Report> FindItemsByYearMonthAndEmploymentType(int year, int month, int employmentType)
|
|---|
| 94 | {
|
|---|
| 95 | var query = from magazineItem in MagazineItems
|
|---|
| 96 | where magazineItem.Date.Month == month
|
|---|
| 97 | && magazineItem.Date.Year == year
|
|---|
| 98 | group magazineItem by new { magazineItem.Author, magazineItem.Issue.MagazineId}
|
|---|
| 99 | into m
|
|---|
| 100 | select new Report { FirstName = m.Key.Author.FirstName, LastName = m.Key.Author.LastName, Price = m.Sum(magazineItem => magazineItem.Price), Bonus = m.Sum(magazineItem => magazineItem.Bonus), Magazine = m.Key.MagazineId.ToString() };
|
|---|
| 101 |
|
|---|
| 102 | return query.ToList();
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | public List<MagazineItem> FindItemsByYearMonthAndAuthor(int year, int month, int author)
|
|---|
| 106 | {
|
|---|
| 107 | var query = from magazineItem in MagazineItems
|
|---|
| 108 | where magazineItem.Date.Month == month
|
|---|
| 109 | && magazineItem.Date.Year == year
|
|---|
| 110 | && magazineItem.AuthorId == author
|
|---|
| 111 | orderby magazineItem.Issue.MagazineId descending
|
|---|
| 112 | select magazineItem;
|
|---|
| 113 | return query.ToList();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | public List<Report> GetAuthorsReport(int year, int month)
|
|---|
| 117 | {
|
|---|
| 118 | var authors = GetAuthors(year, month);
|
|---|
| 119 |
|
|---|
| 120 | var reports = new List<Report>();
|
|---|
| 121 | foreach (var author in authors)
|
|---|
| 122 | {
|
|---|
| 123 | var report = new Report
|
|---|
| 124 | {
|
|---|
| 125 | FirstName = author.FirstName,
|
|---|
| 126 | LastName = author.LastName,
|
|---|
| 127 | Price = author.Price.Value,
|
|---|
| 128 | Bonus = author.Bonus.Value
|
|---|
| 129 | };
|
|---|
| 130 | reports.Add(report);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | return reports;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | public Dictionary<int, Dictionary<string, decimal>> GetAuthorsByMagzinesReport(int year, int month, List<Author> authors, List<Magazine> magazines)
|
|---|
| 137 | {
|
|---|
| 138 | var result = new Dictionary<int, Dictionary<string, decimal>>();
|
|---|
| 139 |
|
|---|
| 140 | foreach (var author in authors)
|
|---|
| 141 | {
|
|---|
| 142 | foreach (var magazine in magazines)
|
|---|
| 143 | {
|
|---|
| 144 | if (!result.ContainsKey(author.Id))
|
|---|
| 145 | result.Add(author.Id, new Dictionary<string, decimal>());
|
|---|
| 146 |
|
|---|
| 147 | if (!result[author.Id].ContainsKey(magazine.NickName))
|
|---|
| 148 | result[author.Id].Add(magazine.NickName, decimal.Zero);
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | var authorsByMagazines = GetAuthorsByMagzines(year, month);
|
|---|
| 153 | foreach (var authorsByMagazine in authorsByMagazines)
|
|---|
| 154 | {
|
|---|
| 155 | if (result.ContainsKey(authorsByMagazine.Id))
|
|---|
| 156 | result[authorsByMagazine.Id][authorsByMagazine.NickName] = authorsByMagazine.Price.Value +
|
|---|
| 157 | authorsByMagazine.Bonus.Value;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | return result;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | public void AddUser(User user)
|
|---|
| 164 | {
|
|---|
| 165 | InsertUser(user);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | }
|
|---|
| 169 | } |
|---|