CustomerDateControl.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace ivf_tl_Operate.CustomUserControls
  16. {
  17. /// <summary>
  18. /// CustomerDateControl.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class CustomerDateControl : UserControl
  21. {
  22. public event Action<string> SelectDateAction;
  23. public event Action<object> CloseAction;
  24. private string selectDay;
  25. private bool isToady = false;
  26. private int currentDay = 1;
  27. private int currentMouth = 1;
  28. private int currentYear;
  29. private DateUtil dataUtil = new DateUtil();
  30. private int defaultColumn = 7;
  31. private int row = 0;
  32. private int index = 0;
  33. private int currentMouthDay = 0;
  34. private int CalcCount = 1;
  35. private DateTime cuurentDate = DateTime.Now;
  36. private int[] hours = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
  37. private int[] mins = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 };
  38. public bool isNYR { get; set; }
  39. /// <summary>
  40. /// 是否可以选择未来
  41. /// </summary>
  42. public bool isSelectWeiLai { get; set; } = false;
  43. public CustomerDateControl()
  44. {
  45. var currentDate = DateTime.Now;
  46. InitializeComponent();
  47. combH.ItemsSource = hours;
  48. combH.SelectedIndex = 0;
  49. combM.ItemsSource = mins;
  50. combM.SelectedIndex = 0;
  51. InitCurrentDate(currentDate);
  52. Init(currentDate);
  53. btnLeft.MouseLeftButtonDown += BtnLeft_MouseLeftButtonDown;
  54. btnRight.MouseLeftButtonDown += BtnRight_MouseLeftButtonDown;
  55. btnConfrim.MouseLeftButtonDown += BtnConfrim_MouseLeftButtonDown;
  56. }
  57. public void SetNYR(bool isnyr)
  58. {
  59. isNYR = isnyr;
  60. if (isNYR)
  61. {
  62. c1.Visibility = Visibility.Hidden;
  63. }
  64. else
  65. {
  66. c1.Visibility = Visibility.Visible;
  67. }
  68. }
  69. private void BtnConfrim_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  70. {
  71. ReturnDate();
  72. }
  73. private void ReturnDate()
  74. {
  75. string selectDate = "";
  76. if (!isNYR)
  77. {
  78. selectDate = string.Format("{0}-{1} {2}", tbYear.Text, selectDay.PadLeft(2, '0'), combH.SelectedValue.ToString().PadLeft(2, '0') + ":" + combM.SelectedValue.ToString().PadLeft(2, '0'));//+ ":00"
  79. }
  80. else
  81. {
  82. selectDate = string.Format("{0}-{1}", tbYear.Text, selectDay.PadLeft(2, '0'));
  83. }
  84. SelectDateAction?.Invoke(selectDate);
  85. }
  86. private void BtnRight_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  87. {
  88. cuurentDate = cuurentDate.AddMonths(1);
  89. Init(cuurentDate);
  90. }
  91. private void InitCurrentDate(DateTime currentDate)
  92. {
  93. currentDay = currentDate.Day;
  94. currentMouth = currentDate.Month;
  95. currentYear = currentDate.Year;
  96. selectDay = currentDay + "";
  97. }
  98. private void CheckIsToday(DateTime date)
  99. {
  100. isToady = (date.Year == currentYear && date.Month == currentMouth && date.Day == currentDay);
  101. }
  102. private void BtnLeft_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  103. {
  104. cuurentDate = cuurentDate.AddMonths(-1);
  105. Init(cuurentDate);
  106. }
  107. public void SetSelectTime(DateTime currentDate)
  108. {
  109. InitCurrentDate(currentDate);
  110. Init(currentDate);
  111. }
  112. /// <summary>
  113. /// 可以选择未来
  114. /// </summary>
  115. public void SetSelectWeiLai()
  116. {
  117. var currentDate = DateTime.Now;
  118. isSelectWeiLai = true;
  119. InitCurrentDate(currentDate);
  120. Init(currentDate);
  121. }
  122. private void Init(DateTime now)
  123. {
  124. tbYear.Text = now.ToString("yyyy-MM");
  125. CheckIsToday(now);
  126. week.Children.Clear();
  127. InitParameter(now);
  128. InitGrid();
  129. InitWeek();
  130. }
  131. private void ClearStyle()
  132. {
  133. foreach (var item in week.Children)
  134. {
  135. TextBlock tb = item as TextBlock;
  136. if (tb != null)
  137. {
  138. if (DateTime.TryParse($"{this.tbYear.Text}-{tb.Text}", out DateTime itemDatetime))
  139. {
  140. if (!isSelectWeiLai)
  141. {
  142. if (itemDatetime > DateTime.Now)
  143. {
  144. }
  145. else
  146. {
  147. ReSetSelected(tb);
  148. tb = null;
  149. }
  150. }
  151. else
  152. {
  153. ReSetSelected(tb);
  154. tb = null;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. private void InitRows(int rowCount)
  161. {
  162. while (rowCount-- > 0)
  163. {
  164. RowDefinition rd = new RowDefinition();
  165. rd.Height = new GridLength();
  166. week.RowDefinitions.Add(rd);
  167. }
  168. }
  169. private void InitColumns(int colCount)
  170. {
  171. while (colCount-- > 0)
  172. {
  173. ColumnDefinition rd = new ColumnDefinition();
  174. rd.Width = new GridLength();
  175. week.ColumnDefinitions.Add(rd);
  176. }
  177. }
  178. private void InitParameter(DateTime now)
  179. {
  180. index = dataUtil.CurrentMonthFristDayOfWeek(now) - 1;
  181. currentMouthDay = dataUtil.CurrentMonthCount(now);
  182. int temp = currentMouthDay - (7 - index);
  183. row = 1 + temp / 7 + (temp % 7 == 0 ? 0 : 1);
  184. }
  185. private void InitGrid()
  186. {
  187. InitRows(row);
  188. InitColumns(defaultColumn);
  189. }
  190. private void ReSetSelected(TextBlock d)
  191. {
  192. d.Background = new SolidColorBrush(Colors.White);
  193. d.Foreground = new SolidColorBrush(Colors.Black);
  194. }
  195. private void SetSelected(TextBlock d)
  196. {
  197. d.Background = new SolidColorBrush(Colors.Black);
  198. d.Foreground = new SolidColorBrush(Colors.White);
  199. selectDay = d.Text;
  200. }
  201. private void InitWeek()
  202. {
  203. for (int i = 0; i < row; i++)
  204. {
  205. for (int j = 0; j < defaultColumn; j++)
  206. {
  207. TextBlock d = new TextBlock();
  208. d.Foreground = new SolidColorBrush(Colors.Black);
  209. d.Width = week.Width / defaultColumn;
  210. d.Height = week.Height / row;
  211. if (!isSelectWeiLai)
  212. {
  213. if (DateTime.TryParse($"{this.tbYear.Text}-{CalcCount}", out DateTime itemDatetime))
  214. {
  215. if (itemDatetime > DateTime.Now)
  216. {
  217. d.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#B0B4B9"));
  218. goto CC;
  219. }
  220. }
  221. }
  222. //d.MouseEnter += (o, e) => {
  223. // SetSelected(d);
  224. //};
  225. //d.MouseLeave += (o, e) => {
  226. // ReSetSelected(d);
  227. //};
  228. d.MouseLeftButtonDown += (o, e) =>
  229. {
  230. TextBlock tb = o as TextBlock;
  231. if (o != null)
  232. {
  233. ClearStyle();
  234. SetSelected(tb);
  235. //if (isNYR)
  236. //{
  237. // Visibility = Visibility.Hidden;
  238. // ReturnDate();
  239. //}
  240. }
  241. };
  242. CC:
  243. d.FontSize = 60;
  244. d.Text = CalcCount.ToString();
  245. d.HorizontalAlignment = HorizontalAlignment.Center;
  246. d.VerticalAlignment = VerticalAlignment.Center;
  247. d.TextAlignment = TextAlignment.Center;
  248. d.SetValue(Grid.RowProperty, i);
  249. if (i != 0)
  250. {
  251. index = j;
  252. d.SetValue(Grid.ColumnProperty, index);
  253. }
  254. else
  255. {
  256. d.SetValue(Grid.ColumnProperty, index);
  257. if (index == 7)
  258. break;
  259. index++;
  260. }
  261. if (CalcCount == currentDay && isToady)//当天设置背景
  262. SetSelected(d);
  263. week.Children.Add(d);
  264. if (CalcCount == currentMouthDay)
  265. {
  266. CalcCount = 1;
  267. break;
  268. }
  269. CalcCount++;
  270. }
  271. }
  272. }
  273. private void Cancel_MouseUp(object sender, MouseButtonEventArgs e)
  274. {
  275. CloseAction?.Invoke(sender);
  276. }
  277. private void OkButton_MouseUp(object sender, MouseButtonEventArgs e)
  278. {
  279. ReturnDate();
  280. }
  281. private void StartDish_Click(object sender, RoutedEventArgs e)
  282. {
  283. ReturnDate();
  284. }
  285. private void Cancel_Click(object sender, RoutedEventArgs e)
  286. {
  287. CloseAction?.Invoke(sender);
  288. }
  289. }
  290. public class DateUtil
  291. {
  292. /// <summary>
  293. /// 当前月第一天是周几
  294. /// </summary>
  295. /// <returns>周几</returns>
  296. public int CurrentMonthFristDayOfWeek()
  297. {
  298. DateTime now = DateTime.Now;
  299. DateTime d1 = new DateTime(now.Year, now.Month, 1);
  300. int day = (int)d1.DayOfWeek;
  301. if (day == 0)
  302. day = 7;
  303. return day;
  304. }
  305. /// <summary>
  306. /// 当前月有几天
  307. /// </summary>
  308. /// <returns></returns>
  309. public int CurrentMonthCount()
  310. {
  311. DateTime now = DateTime.Now;
  312. int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
  313. return days;
  314. }
  315. /// <summary>
  316. /// 当前月有几天
  317. /// </summary>
  318. /// <param name="now">当前时间</param>
  319. /// <returns></returns>
  320. public int CurrentMonthCount(DateTime now)
  321. {
  322. int days = DateTime.DaysInMonth(now.Year, now.Month);
  323. return days;
  324. }
  325. /// <summary>
  326. /// 当前月第一天是周几
  327. /// </summary>
  328. /// <returns>周几</returns>
  329. public int CurrentMonthFristDayOfWeek(DateTime now)
  330. {
  331. DateTime d1 = new DateTime(now.Year, now.Month, 1);
  332. int day = (int)d1.DayOfWeek;
  333. if (day == 0)
  334. day = 7;
  335. return day;
  336. }
  337. public static long ConvertDataTimeLong(DateTime dt)
  338. {
  339. DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  340. TimeSpan toNow = dt.Subtract(dtStart);
  341. long timeStamp = toNow.Ticks;
  342. timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
  343. return timeStamp;
  344. }
  345. }
  346. }