EntityConfigs.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using TravelAgencyWinForms.Models;
  2. namespace TravelAgencyWinForms.Services;
  3. public static class EntityConfigs
  4. {
  5. public static EntityConfig Clients => new()
  6. {
  7. Title = "Клиенты",
  8. TableName = "clients",
  9. OrderBy = "client_id",
  10. Fields = new()
  11. {
  12. Id("client_id", "ID"),
  13. Text("first_name", "Имя", true),
  14. Text("last_name", "Фамилия", true),
  15. Text("middle_name", "Отчество"),
  16. Text("passport_data", "Паспорт", true),
  17. Text("phone", "Телефон"),
  18. Text("email", "Email"),
  19. Date("registration_date", "Дата регистрации", true)
  20. }
  21. };
  22. public static EntityConfig Positions => new()
  23. {
  24. Title = "Должности",
  25. TableName = "positions",
  26. OrderBy = "position_id",
  27. Fields = new()
  28. {
  29. Id("position_id", "ID"),
  30. Text("name", "Название", true),
  31. Decimal("salary", "Зарплата", true)
  32. }
  33. };
  34. public static EntityConfig Employees => new()
  35. {
  36. Title = "Сотрудники",
  37. TableName = "employees",
  38. OrderBy = "employee_id",
  39. Fields = new()
  40. {
  41. Id("employee_id", "ID"),
  42. Text("first_name", "Имя", true),
  43. Text("last_name", "Фамилия", true),
  44. Text("middle_name", "Отчество"),
  45. Text("passport_data", "Паспорт", true),
  46. Text("phone", "Телефон"),
  47. Date("birth_date", "Дата рождения"),
  48. Lookup("position_id", "Должность", true,
  49. "SELECT position_id AS id, name FROM positions ORDER BY name", "id", "name")
  50. }
  51. };
  52. public static EntityConfig Cities => new()
  53. {
  54. Title = "Города",
  55. TableName = "cities",
  56. OrderBy = "city_id",
  57. Fields = new()
  58. {
  59. Id("city_id", "ID"),
  60. Text("city_name", "Название города", true)
  61. }
  62. };
  63. public static EntityConfig Tours => new()
  64. {
  65. Title = "Туры",
  66. TableName = "tours",
  67. OrderBy = "tour_id",
  68. Fields = new()
  69. {
  70. Id("tour_id", "ID"),
  71. Text("name", "Название тура", true),
  72. Decimal("price", "Стоимость", true)
  73. }
  74. };
  75. public static EntityConfig TourCities => new()
  76. {
  77. Title = "Города в турах",
  78. TableName = "tour_cities",
  79. OrderBy = "tour_id, city_id",
  80. Fields = new()
  81. {
  82. Lookup("tour_id", "Тур", true,
  83. "SELECT tour_id AS id, name FROM tours ORDER BY name", "id", "name", isKey: true),
  84. Lookup("city_id", "Город", true,
  85. "SELECT city_id AS id, city_name AS name FROM cities ORDER BY city_name", "id", "name", isKey: true)
  86. }
  87. };
  88. public static EntityConfig TourList => new()
  89. {
  90. Title = "Расписание туров",
  91. TableName = "tour_list",
  92. OrderBy = "tour_list_id",
  93. Fields = new()
  94. {
  95. Id("tour_list_id", "ID"),
  96. Lookup("tour_id", "Тур", true,
  97. "SELECT tour_id AS id, name FROM tours ORDER BY name", "id", "name"),
  98. Date("start_date", "Дата начала", true),
  99. Date("end_date", "Дата окончания", true)
  100. }
  101. };
  102. public static EntityConfig Bookings => new()
  103. {
  104. Title = "Бронирования",
  105. TableName = "bookings",
  106. OrderBy = "booking_id",
  107. Fields = new()
  108. {
  109. Id("booking_id", "ID"),
  110. Text("booking_number", "Номер бронирования", true),
  111. Lookup("client_id", "Клиент", true,
  112. "SELECT client_id AS id, concat_ws(' ', last_name, first_name, middle_name) AS name FROM clients ORDER BY last_name, first_name", "id", "name"),
  113. Lookup("employee_id", "Сотрудник", true,
  114. "SELECT employee_id AS id, concat_ws(' ', last_name, first_name, middle_name) AS name FROM employees ORDER BY last_name, first_name", "id", "name"),
  115. Lookup("tour_list_id", "Тур из расписания", true,
  116. "SELECT tour_list_id AS id, tour_name || ' (' || start_date || ' - ' || end_date || ')' AS name FROM v_tour_schedule ORDER BY start_date", "id", "name"),
  117. Date("booking_date", "Дата оформления", true),
  118. Enum("status", "Статус", true, "новая", "подтверждена", "оплачена", "отменена")
  119. }
  120. };
  121. public static EntityConfig AppUsers => new()
  122. {
  123. Title = "Пользователи приложения",
  124. TableName = "app_users",
  125. OrderBy = "user_id",
  126. Fields = new()
  127. {
  128. Id("user_id", "ID"),
  129. Text("login", "Логин", true),
  130. Password("password_hash", "Пароль", false),
  131. Enum("role", "Роль", true, "admin", "user"),
  132. Bool("is_active", "Активен", true),
  133. Lookup("employee_id", "Связанная запись", false,
  134. "SELECT employee_id AS id, concat_ws(' ', last_name, first_name, middle_name) AS name FROM employees ORDER BY last_name, first_name", "id", "name")
  135. }
  136. };
  137. public static EntityConfig ClientDetails => ReadOnlyView("Мои данные клиента", "clients", "client_id", true, "client_id");
  138. public static EntityConfig BookingDetails => ReadOnlyView("Бронирования подробно", "v_booking_details", "booking_id", false, null);
  139. public static EntityConfig TourScheduleView => ReadOnlyView("Расписание туров", "v_tour_schedule", "tour_list_id", false, null);
  140. public static EntityConfig OverdueView => ReadOnlyView("Просроченные бронирования", "v_overdue_client_bookings", "booking_id", false, null);
  141. public static EntityConfig AuditLog => ReadOnlyView("Журнал операций", "audit_log", "changed_at DESC", false, null);
  142. public static EntityConfig UserBookings => ReadOnlyView("Мои бронирования", "v_booking_details", "booking_id", true, "client_id");
  143. public static EntityConfig UserOverdue => ReadOnlyView("Мои просроченные бронирования", "v_overdue_client_bookings", "booking_id", true, "client_id");
  144. private static EntityConfig ReadOnlyView(string title, string view, string orderBy, bool restrict, string? restrictColumn)
  145. {
  146. return new EntityConfig
  147. {
  148. Title = title,
  149. TableName = view,
  150. ViewName = view,
  151. OrderBy = orderBy,
  152. ReadOnly = true,
  153. RestrictByCurrentEmployee = restrict,
  154. RestrictColumn = restrictColumn,
  155. Fields = new()
  156. };
  157. }
  158. private static FieldConfig Id(string name, string label) => new()
  159. {
  160. Name = name,
  161. Label = label,
  162. Kind = FieldKind.Integer,
  163. IsKey = true,
  164. AutoIdentity = true,
  165. ReadOnlyOnEdit = true
  166. };
  167. private static FieldConfig Text(string name, string label, bool required = false) => new()
  168. {
  169. Name = name,
  170. Label = label,
  171. Kind = FieldKind.Text,
  172. Required = required
  173. };
  174. private static FieldConfig Password(string name, string label, bool required = false) => new()
  175. {
  176. Name = name,
  177. Label = label,
  178. Kind = FieldKind.Password,
  179. Required = required,
  180. ShowInGrid = false
  181. };
  182. private static FieldConfig Decimal(string name, string label, bool required = false) => new()
  183. {
  184. Name = name,
  185. Label = label,
  186. Kind = FieldKind.Decimal,
  187. Required = required
  188. };
  189. private static FieldConfig Date(string name, string label, bool required = false) => new()
  190. {
  191. Name = name,
  192. Label = label,
  193. Kind = FieldKind.Date,
  194. Required = required
  195. };
  196. private static FieldConfig Bool(string name, string label, bool required = false) => new()
  197. {
  198. Name = name,
  199. Label = label,
  200. Kind = FieldKind.Boolean,
  201. Required = required
  202. };
  203. private static FieldConfig Enum(string name, string label, bool required, params string[] options) => new()
  204. {
  205. Name = name,
  206. Label = label,
  207. Kind = FieldKind.Enum,
  208. Required = required,
  209. Options = options
  210. };
  211. private static FieldConfig Lookup(
  212. string name,
  213. string label,
  214. bool required,
  215. string sql,
  216. string valueColumn,
  217. string displayColumn,
  218. bool isKey = false) => new()
  219. {
  220. Name = name,
  221. Label = label,
  222. Kind = FieldKind.Lookup,
  223. Required = required,
  224. LookupSql = sql,
  225. LookupValueColumn = valueColumn,
  226. LookupDisplayColumn = displayColumn,
  227. IsKey = isKey,
  228. AutoIdentity = false,
  229. ReadOnlyOnEdit = isKey
  230. };
  231. }