CustomerDateControl.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. using ivf_tl_Manage.Converts;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Controls.Primitives;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace ivf_tl_Manage.UserControls
  19. {
  20. /// <summary>
  21. /// CustomerDateControl.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class CustomerDateControl : UserControl
  24. {
  25. public event Action<string> SelectDateAction;
  26. public event Action<object> CloseAction;
  27. private string selectDay;
  28. private bool isToady = false;
  29. private int currentDay = 1;
  30. private int currentMouth = 1;
  31. private int upMonthday = 1;
  32. private int currentYear;
  33. private int currentHour;
  34. private int currentMinute;
  35. private DateUtil dataUtil = new DateUtil();
  36. private int defaultColumn = 7;
  37. private int row = 0;
  38. private int index = 0;
  39. private int currentMouthDay = 0;
  40. private int CalcCount = 1;
  41. private DateTime cuurentDate = DateTime.Now;
  42. private string[] hours =new string[24];
  43. private string [] mins = new string[60];
  44. public bool isNYR { get; set; }
  45. private List<DateItemData> data = new List<DateItemData>();
  46. /// <summary>
  47. /// 是否可以选择未来
  48. /// </summary>
  49. public bool isSelectWeiLai { get; set; } = false;
  50. private DateTime today = DateTime.Now;
  51. private bool CheckToday(DateTime time)
  52. {
  53. return today.Year == time.Year && today.Month == time.Month && today.Day == time.Day;
  54. }
  55. public CustomerDateControl()
  56. {
  57. for (int i = 0; i < 60; i++)
  58. {
  59. mins[i] = ":" + i.ToString("D2");
  60. }
  61. for (int i = 0; i < 24; i++)
  62. {
  63. hours[i] = i.ToString("D2");
  64. }
  65. var currentDate = DateTime.Now;
  66. InitializeComponent();
  67. lt_hour.ItemsSource = hours;
  68. lt_min.ItemsSource = mins;
  69. InitCurrentDate(currentDate);
  70. Init();
  71. //btnLeft.MouseLeftButtonDown += BtnLeft_MouseLeftButtonDown;
  72. //btnRight.MouseLeftButtonDown += BtnRight_MouseLeftButtonDown;
  73. }
  74. private void BtnConfrim_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  75. {
  76. ReturnDate();
  77. }
  78. private void ReturnDate()
  79. {
  80. //string selectDate = "";
  81. //if (!isNYR)
  82. //{
  83. // 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"
  84. //}
  85. //else
  86. //{
  87. // selectDate = string.Format("{0}-{1}", tbYear.Text, selectDay.PadLeft(2, '0'));
  88. //}
  89. //SelectDateAction?.Invoke(selectDate);
  90. }
  91. private void BtnRight_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  92. {
  93. cuurentDate = cuurentDate.AddMonths(1);
  94. Init();
  95. }
  96. private void InitCurrentDate(DateTime currentDate)
  97. {
  98. currentDay = currentDate.Day;
  99. currentMouth = currentDate.Month;
  100. currentYear = currentDate.Year;
  101. selectDay = currentDay + "";
  102. }
  103. /// <summary>
  104. /// 初始化数据
  105. /// </summary>
  106. /// <param name="dateTime"></param>
  107. public void InitData(DateTime dateTime)
  108. {
  109. int totalCount = 0;
  110. data.Clear();
  111. int starDayNum = upMonthday - index + 1;
  112. for (int i = 0; i < index; i++)
  113. {
  114. DateItemData dateItemData = new DateItemData();
  115. dateItemData.IsCurrentMonth = false;
  116. DateTime temp = dateTime.AddMonths(-1);
  117. dateItemData.Time = new DateTime(temp.Year, temp.Month, starDayNum + i);
  118. data.Add(dateItemData);
  119. totalCount++;
  120. }
  121. for (int i = 0; i < currentMouthDay; i++)
  122. {
  123. DateItemData dateItemData = new DateItemData();
  124. dateItemData.IsCurrentMonth = true;
  125. dateItemData.Time = new DateTime(dateTime.Year, dateTime.Month, i + 1);
  126. dateItemData.IsToday = CheckToday(dateItemData.Time);
  127. data.Add(dateItemData);
  128. totalCount++;
  129. }
  130. int j = 1;
  131. while (totalCount < 42)
  132. {
  133. DateItemData dateItemData = new DateItemData();
  134. dateItemData.IsCurrentMonth = false;
  135. DateTime temp = dateTime.AddMonths(1);
  136. dateItemData.Time = new DateTime(temp.Year, temp.Month, j);
  137. dateItemData.IsToday = CheckToday(dateItemData.Time);
  138. data.Add(dateItemData);
  139. j++;
  140. totalCount++;
  141. }
  142. }
  143. private void CheckIsToday(DateTime date)
  144. {
  145. isToady = (date.Year == currentYear && date.Month == currentMouth && date.Day == currentDay);
  146. }
  147. public void SetSelectTime()
  148. {
  149. InitCurrentDate(cuurentDate);
  150. Init();
  151. }
  152. private void Init()
  153. {
  154. //tb_Year.Text = cuurentDate.Year + "年";
  155. //tb_month.Text = cuurentDate.Month + "月";
  156. tb_Year.Text = cuurentDate.Year + KeyToStringConvert.GetLanguageStringByKey("0154");
  157. tb_month.Text = cuurentDate.Month + KeyToStringConvert.GetLanguageStringByKey("0155");
  158. CheckIsToday(cuurentDate);
  159. week.Children.Clear();
  160. InitParameter();
  161. //InitGrid();
  162. InitData(cuurentDate);
  163. InitWeek();
  164. currentHour = DateTime.Now.Hour;
  165. currentMinute = DateTime.Now.Minute;
  166. SetHourAndMin();
  167. }
  168. private void SetHourAndMin()
  169. {
  170. lt_hour.SelectedIndex = currentHour;
  171. lt_min.SelectedIndex = currentMinute;
  172. tb_time.Text = $"{lt_hour.SelectedValue}{lt_min.SelectedValue}";
  173. SelectDateAction?.Invoke($"{currentYear}-{currentMouth.ToString("D2")}-{currentDay.ToString("D2")} {currentHour.ToString("D2")}:{currentMinute.ToString("D2")}");
  174. }
  175. private void InitRows(int rowCount)
  176. {
  177. while (rowCount-- > 0)
  178. {
  179. RowDefinition rd = new RowDefinition();
  180. rd.Height = new GridLength();
  181. week.RowDefinitions.Add(rd);
  182. }
  183. }
  184. private void InitColumns(int colCount)
  185. {
  186. while (colCount-- > 0)
  187. {
  188. ColumnDefinition rd = new ColumnDefinition();
  189. rd.Width = new GridLength();
  190. week.ColumnDefinitions.Add(rd);
  191. }
  192. }
  193. private void InitParameter()
  194. {
  195. index = dataUtil.CurrentMonthFristDayOfWeek(cuurentDate) - 1;
  196. currentMouthDay = dataUtil.CurrentMonthCount(cuurentDate);
  197. upMonthday = dataUtil.CurrentMonthCount(cuurentDate.AddMonths(-1));
  198. //int temp = currentMouthDay - (7 - index);
  199. //row = 1 + temp / 7 + (temp % 7 == 0 ? 0 : 1);
  200. }
  201. private void InitGrid()
  202. {
  203. InitRows(row);
  204. InitColumns(defaultColumn);
  205. }
  206. private void ReSetSelected(TextBlock d)
  207. {
  208. d.Background = new SolidColorBrush(Colors.White);
  209. d.Foreground = new SolidColorBrush(Colors.Black);
  210. }
  211. private void SetSelected(TextBlock d)
  212. {
  213. d.Background = new SolidColorBrush(Colors.Black);
  214. d.Foreground = new SolidColorBrush(Colors.White);
  215. selectDay = d.Text;
  216. }
  217. private void InitWeek()
  218. {
  219. week.Children.Clear();
  220. for (int i = 0; i < 6; i++)
  221. {
  222. for (int j = 0; j < 7; j++)
  223. {
  224. DateItemData item = data[7 * i + j];
  225. DateItem dateItem = new DateItem();
  226. dateItem.DataItemProperty = item;
  227. dateItem.SetValue(Grid.RowProperty, i);
  228. dateItem.SetValue(Grid.ColumnProperty, j);
  229. dateItem.IsSelectProperty = CheckToday(item.Time);
  230. dateItem.clickAction = () =>
  231. {
  232. ClearAllItmeBg();
  233. dateItem.SetBg();
  234. currentYear = item.Time.Year;
  235. currentMouth = item.Time.Month;
  236. currentDay = item.Time.Day;
  237. SetHourAndMin();
  238. };
  239. week.Children.Add(dateItem);
  240. }
  241. }
  242. }
  243. public void ClearAllItmeBg()
  244. {
  245. var items = week.Children;
  246. foreach (var item in items)
  247. {
  248. if (item is DateItem)
  249. {
  250. DateItem dateItem = (DateItem)item;
  251. dateItem.bg.Fill = new SolidColorBrush(Colors.Transparent);
  252. if (dateItem.DataItemProperty.IsCurrentMonth)
  253. {
  254. dateItem.tx_text.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D4D4D"));
  255. }
  256. else
  257. {
  258. dateItem.tx_text.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#9B9B9B"));
  259. }
  260. dateItem.SetTodayBg();
  261. }
  262. }
  263. }
  264. private void Cancel_MouseUp(object sender, MouseButtonEventArgs e)
  265. {
  266. CloseAction?.Invoke(sender);
  267. }
  268. private void OkButton_MouseUp(object sender, MouseButtonEventArgs e)
  269. {
  270. ReturnDate();
  271. }
  272. private void StartDish_Click(object sender, RoutedEventArgs e)
  273. {
  274. ReturnDate();
  275. }
  276. private void Cancel_Click(object sender, RoutedEventArgs e)
  277. {
  278. CloseAction?.Invoke(sender);
  279. }
  280. private void img_YearLeft_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  281. {
  282. cuurentDate = cuurentDate.AddYears(-1);
  283. Init();
  284. }
  285. private void img_YearRight_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  286. {
  287. cuurentDate = cuurentDate.AddYears(1);
  288. Init();
  289. }
  290. private void img_MonthLeft_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  291. {
  292. cuurentDate = cuurentDate.AddMonths(-1);
  293. Init();
  294. }
  295. private void img_MonthRight_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  296. {
  297. cuurentDate = cuurentDate.AddMonths(1);
  298. Init();
  299. }
  300. private void img_YearLeft_MouseEnter(object sender, MouseEventArgs e)
  301. {
  302. img_YearLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_year_left.png", UriKind.Absolute));
  303. }
  304. private void img_YearLeft_MouseLeave(object sender, MouseEventArgs e)
  305. {
  306. img_YearLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_year_left.png", UriKind.Absolute));
  307. }
  308. private void img_YearRight_MouseEnter(object sender, MouseEventArgs e)
  309. {
  310. img_YearRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_year_right.png", UriKind.Absolute));
  311. }
  312. private void img_YearRight_MouseLeave(object sender, MouseEventArgs e)
  313. {
  314. img_YearRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_year_right.png", UriKind.Absolute));
  315. }
  316. private void img_MonthLeft_MouseEnter(object sender, MouseEventArgs e)
  317. {
  318. img_MonthLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_month_left.png", UriKind.Absolute));
  319. }
  320. private void img_MonthLeft_MouseLeave(object sender, MouseEventArgs e)
  321. {//date_gray_month_left
  322. img_MonthLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_month_left.png", UriKind.Absolute));
  323. }
  324. private void img_MonthRight_MouseEnter(object sender, MouseEventArgs e)
  325. {
  326. img_MonthRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_month_right.png", UriKind.Absolute));
  327. }
  328. private void img_MonthRight_MouseLeave(object sender, MouseEventArgs e)
  329. {//date_gray_month_right
  330. img_MonthRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_month_right.png", UriKind.Absolute));
  331. }
  332. private void Border_MouseEnter(object sender, MouseEventArgs e)
  333. {
  334. bd_today.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D75AC"));
  335. tb_today.Foreground = new SolidColorBrush(Colors.White);
  336. }
  337. private void Border_MouseLeave(object sender, MouseEventArgs e)
  338. {
  339. bd_today.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F6F6F6"));
  340. tb_today.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#707070"));
  341. }
  342. private void Border_MouseDown(object sender, MouseButtonEventArgs e)
  343. {
  344. ReSetDate();
  345. }
  346. public void ReSetDate()
  347. {
  348. cuurentDate = DateTime.Now;
  349. Init();
  350. SetHourAndMin();
  351. }
  352. private void bg_time_MouseEnter(object sender, MouseEventArgs e)
  353. {
  354. bg_time.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D75AC"));
  355. }
  356. private void bg_time_MouseLeave(object sender, MouseEventArgs e)
  357. {
  358. bg_time.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#C6C6C6"));
  359. }
  360. bool isOpen = false;
  361. private void bg_time_MouseDown(object sender, MouseButtonEventArgs e)
  362. {
  363. isOpen = !isOpen;
  364. pp_time.IsOpen= isOpen;
  365. }
  366. private void lt_hour_SelectionChanged(object sender, SelectionChangedEventArgs e)
  367. {
  368. currentHour = lt_hour.SelectedIndex;
  369. SetHourAndMin();
  370. }
  371. private void lt_min_SelectionChanged(object sender, SelectionChangedEventArgs e)
  372. {
  373. currentMinute = lt_min.SelectedIndex;
  374. SetHourAndMin();
  375. }
  376. private void pp_time_LostFocus(object sender, RoutedEventArgs e)
  377. {
  378. isOpen = false;
  379. pp_time.IsOpen = isOpen;
  380. }
  381. private void Border_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  382. {
  383. if (!pp_time.IsMouseOver)
  384. {
  385. isOpen = false;
  386. pp_time.IsOpen = isOpen;
  387. }
  388. }
  389. }
  390. public class DateUtil
  391. {
  392. /// <summary>
  393. /// 当前月第一天是周几
  394. /// </summary>
  395. /// <returns>周几</returns>
  396. public int CurrentMonthFristDayOfWeek()
  397. {
  398. DateTime now = DateTime.Now;
  399. DateTime d1 = new DateTime(now.Year, now.Month, 1);
  400. int day = (int)d1.DayOfWeek;
  401. if (day == 0)
  402. day = 7;
  403. return day;
  404. }
  405. /// <summary>
  406. /// 当前月有几天
  407. /// </summary>
  408. /// <returns></returns>
  409. public int CurrentMonthCount()
  410. {
  411. DateTime now = DateTime.Now;
  412. int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
  413. return days;
  414. }
  415. /// <summary>
  416. /// 当前月有几天
  417. /// </summary>
  418. /// <param name="now">当前时间</param>
  419. /// <returns></returns>
  420. public int CurrentMonthCount(DateTime now)
  421. {
  422. int days = DateTime.DaysInMonth(now.Year, now.Month);
  423. return days;
  424. }
  425. /// <summary>
  426. /// 当前月第一天是周几
  427. /// </summary>
  428. /// <returns>周几</returns>
  429. public int CurrentMonthFristDayOfWeek(DateTime now)
  430. {
  431. DateTime d1 = new DateTime(now.Year, now.Month, 1);
  432. int day = (int)d1.DayOfWeek;
  433. if (day == 0)
  434. day = 7;
  435. return day;
  436. }
  437. public static long ConvertDataTimeLong(DateTime dt)
  438. {
  439. DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  440. TimeSpan toNow = dt.Subtract(dtStart);
  441. long timeStamp = toNow.Ticks;
  442. timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
  443. return timeStamp;
  444. }
  445. }
  446. }