| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- using ivf_tl_Manage.Converts;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace ivf_tl_Manage.UserControls
- {
- /// <summary>
- /// CustomerDateControl.xaml 的交互逻辑
- /// </summary>
- public partial class CustomerDateControl : UserControl
- {
- public event Action<string> SelectDateAction;
- public event Action<object> CloseAction;
- private string selectDay;
- private bool isToady = false;
- private int currentDay = 1;
- private int currentMouth = 1;
- private int upMonthday = 1;
- private int currentYear;
- private int currentHour;
- private int currentMinute;
- private DateUtil dataUtil = new DateUtil();
- private int defaultColumn = 7;
- private int row = 0;
- private int index = 0;
- private int currentMouthDay = 0;
- private int CalcCount = 1;
- private DateTime cuurentDate = DateTime.Now;
- private string[] hours =new string[24];
- private string [] mins = new string[60];
- public bool isNYR { get; set; }
- private List<DateItemData> data = new List<DateItemData>();
- /// <summary>
- /// 是否可以选择未来
- /// </summary>
- public bool isSelectWeiLai { get; set; } = false;
- private DateTime today = DateTime.Now;
- private bool CheckToday(DateTime time)
- {
- return today.Year == time.Year && today.Month == time.Month && today.Day == time.Day;
- }
- public CustomerDateControl()
- {
- for (int i = 0; i < 60; i++)
- {
- mins[i] = ":" + i.ToString("D2");
- }
- for (int i = 0; i < 24; i++)
- {
- hours[i] = i.ToString("D2");
- }
- var currentDate = DateTime.Now;
- InitializeComponent();
- lt_hour.ItemsSource = hours;
- lt_min.ItemsSource = mins;
- InitCurrentDate(currentDate);
- Init();
- //btnLeft.MouseLeftButtonDown += BtnLeft_MouseLeftButtonDown;
- //btnRight.MouseLeftButtonDown += BtnRight_MouseLeftButtonDown;
- }
- private void BtnConfrim_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- ReturnDate();
- }
- private void ReturnDate()
- {
- //string selectDate = "";
- //if (!isNYR)
- //{
- // 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"
- //}
- //else
- //{
- // selectDate = string.Format("{0}-{1}", tbYear.Text, selectDay.PadLeft(2, '0'));
- //}
- //SelectDateAction?.Invoke(selectDate);
- }
- private void BtnRight_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- cuurentDate = cuurentDate.AddMonths(1);
- Init();
- }
- private void InitCurrentDate(DateTime currentDate)
- {
- currentDay = currentDate.Day;
- currentMouth = currentDate.Month;
- currentYear = currentDate.Year;
- selectDay = currentDay + "";
- }
- /// <summary>
- /// 初始化数据
- /// </summary>
- /// <param name="dateTime"></param>
- public void InitData(DateTime dateTime)
- {
- int totalCount = 0;
- data.Clear();
- int starDayNum = upMonthday - index + 1;
- for (int i = 0; i < index; i++)
- {
- DateItemData dateItemData = new DateItemData();
- dateItemData.IsCurrentMonth = false;
- DateTime temp = dateTime.AddMonths(-1);
- dateItemData.Time = new DateTime(temp.Year, temp.Month, starDayNum + i);
- data.Add(dateItemData);
- totalCount++;
- }
- for (int i = 0; i < currentMouthDay; i++)
- {
- DateItemData dateItemData = new DateItemData();
- dateItemData.IsCurrentMonth = true;
- dateItemData.Time = new DateTime(dateTime.Year, dateTime.Month, i + 1);
- dateItemData.IsToday = CheckToday(dateItemData.Time);
- data.Add(dateItemData);
- totalCount++;
- }
- int j = 1;
- while (totalCount < 42)
- {
- DateItemData dateItemData = new DateItemData();
- dateItemData.IsCurrentMonth = false;
- DateTime temp = dateTime.AddMonths(1);
- dateItemData.Time = new DateTime(temp.Year, temp.Month, j);
- dateItemData.IsToday = CheckToday(dateItemData.Time);
- data.Add(dateItemData);
- j++;
- totalCount++;
- }
- }
- private void CheckIsToday(DateTime date)
- {
- isToady = (date.Year == currentYear && date.Month == currentMouth && date.Day == currentDay);
- }
- public void SetSelectTime()
- {
- InitCurrentDate(cuurentDate);
- Init();
- }
- private void Init()
- {
- //tb_Year.Text = cuurentDate.Year + "年";
- //tb_month.Text = cuurentDate.Month + "月";
- tb_Year.Text = cuurentDate.Year + KeyToStringConvert.GetLanguageStringByKey("0154");
- tb_month.Text = cuurentDate.Month + KeyToStringConvert.GetLanguageStringByKey("0155");
- CheckIsToday(cuurentDate);
- week.Children.Clear();
- InitParameter();
- //InitGrid();
- InitData(cuurentDate);
- InitWeek();
- currentHour = DateTime.Now.Hour;
- currentMinute = DateTime.Now.Minute;
- SetHourAndMin();
- }
- private void SetHourAndMin()
- {
- lt_hour.SelectedIndex = currentHour;
- lt_min.SelectedIndex = currentMinute;
- tb_time.Text = $"{lt_hour.SelectedValue}{lt_min.SelectedValue}";
- SelectDateAction?.Invoke($"{currentYear}-{currentMouth.ToString("D2")}-{currentDay.ToString("D2")} {currentHour.ToString("D2")}:{currentMinute.ToString("D2")}");
- }
- private void InitRows(int rowCount)
- {
- while (rowCount-- > 0)
- {
- RowDefinition rd = new RowDefinition();
- rd.Height = new GridLength();
- week.RowDefinitions.Add(rd);
- }
- }
- private void InitColumns(int colCount)
- {
- while (colCount-- > 0)
- {
- ColumnDefinition rd = new ColumnDefinition();
- rd.Width = new GridLength();
- week.ColumnDefinitions.Add(rd);
- }
- }
- private void InitParameter()
- {
- index = dataUtil.CurrentMonthFristDayOfWeek(cuurentDate) - 1;
- currentMouthDay = dataUtil.CurrentMonthCount(cuurentDate);
- upMonthday = dataUtil.CurrentMonthCount(cuurentDate.AddMonths(-1));
- //int temp = currentMouthDay - (7 - index);
- //row = 1 + temp / 7 + (temp % 7 == 0 ? 0 : 1);
- }
- private void InitGrid()
- {
- InitRows(row);
- InitColumns(defaultColumn);
- }
- private void ReSetSelected(TextBlock d)
- {
- d.Background = new SolidColorBrush(Colors.White);
- d.Foreground = new SolidColorBrush(Colors.Black);
- }
- private void SetSelected(TextBlock d)
- {
- d.Background = new SolidColorBrush(Colors.Black);
- d.Foreground = new SolidColorBrush(Colors.White);
- selectDay = d.Text;
- }
- private void InitWeek()
- {
- week.Children.Clear();
- for (int i = 0; i < 6; i++)
- {
- for (int j = 0; j < 7; j++)
- {
- DateItemData item = data[7 * i + j];
- DateItem dateItem = new DateItem();
- dateItem.DataItemProperty = item;
- dateItem.SetValue(Grid.RowProperty, i);
- dateItem.SetValue(Grid.ColumnProperty, j);
- dateItem.IsSelectProperty = CheckToday(item.Time);
- dateItem.clickAction = () =>
- {
- ClearAllItmeBg();
- dateItem.SetBg();
- currentYear = item.Time.Year;
- currentMouth = item.Time.Month;
- currentDay = item.Time.Day;
- SetHourAndMin();
- };
- week.Children.Add(dateItem);
- }
- }
- }
- public void ClearAllItmeBg()
- {
- var items = week.Children;
- foreach (var item in items)
- {
- if (item is DateItem)
- {
- DateItem dateItem = (DateItem)item;
- dateItem.bg.Fill = new SolidColorBrush(Colors.Transparent);
- if (dateItem.DataItemProperty.IsCurrentMonth)
- {
- dateItem.tx_text.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D4D4D"));
- }
- else
- {
- dateItem.tx_text.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#9B9B9B"));
- }
- dateItem.SetTodayBg();
- }
- }
- }
- private void Cancel_MouseUp(object sender, MouseButtonEventArgs e)
- {
- CloseAction?.Invoke(sender);
- }
- private void OkButton_MouseUp(object sender, MouseButtonEventArgs e)
- {
- ReturnDate();
- }
- private void StartDish_Click(object sender, RoutedEventArgs e)
- {
- ReturnDate();
- }
- private void Cancel_Click(object sender, RoutedEventArgs e)
- {
- CloseAction?.Invoke(sender);
- }
- private void img_YearLeft_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- cuurentDate = cuurentDate.AddYears(-1);
- Init();
- }
- private void img_YearRight_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- cuurentDate = cuurentDate.AddYears(1);
- Init();
- }
- private void img_MonthLeft_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- cuurentDate = cuurentDate.AddMonths(-1);
- Init();
- }
- private void img_MonthRight_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- cuurentDate = cuurentDate.AddMonths(1);
- Init();
- }
- private void img_YearLeft_MouseEnter(object sender, MouseEventArgs e)
- {
- img_YearLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_year_left.png", UriKind.Absolute));
- }
- private void img_YearLeft_MouseLeave(object sender, MouseEventArgs e)
- {
- img_YearLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_year_left.png", UriKind.Absolute));
- }
- private void img_YearRight_MouseEnter(object sender, MouseEventArgs e)
- {
- img_YearRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_year_right.png", UriKind.Absolute));
- }
- private void img_YearRight_MouseLeave(object sender, MouseEventArgs e)
- {
- img_YearRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_year_right.png", UriKind.Absolute));
- }
- private void img_MonthLeft_MouseEnter(object sender, MouseEventArgs e)
- {
- img_MonthLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_month_left.png", UriKind.Absolute));
- }
- private void img_MonthLeft_MouseLeave(object sender, MouseEventArgs e)
- {//date_gray_month_left
- img_MonthLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_month_left.png", UriKind.Absolute));
- }
- private void img_MonthRight_MouseEnter(object sender, MouseEventArgs e)
- {
- img_MonthRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_month_right.png", UriKind.Absolute));
- }
- private void img_MonthRight_MouseLeave(object sender, MouseEventArgs e)
- {//date_gray_month_right
- img_MonthRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_month_right.png", UriKind.Absolute));
- }
- private void Border_MouseEnter(object sender, MouseEventArgs e)
- {
- bd_today.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D75AC"));
- tb_today.Foreground = new SolidColorBrush(Colors.White);
- }
- private void Border_MouseLeave(object sender, MouseEventArgs e)
- {
- bd_today.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F6F6F6"));
- tb_today.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#707070"));
- }
- private void Border_MouseDown(object sender, MouseButtonEventArgs e)
- {
- ReSetDate();
- }
- public void ReSetDate()
- {
- cuurentDate = DateTime.Now;
- Init();
- SetHourAndMin();
- }
- private void bg_time_MouseEnter(object sender, MouseEventArgs e)
- {
- bg_time.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D75AC"));
- }
- private void bg_time_MouseLeave(object sender, MouseEventArgs e)
- {
- bg_time.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#C6C6C6"));
- }
- bool isOpen = false;
- private void bg_time_MouseDown(object sender, MouseButtonEventArgs e)
- {
- isOpen = !isOpen;
- pp_time.IsOpen= isOpen;
- }
- private void lt_hour_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- currentHour = lt_hour.SelectedIndex;
- SetHourAndMin();
- }
- private void lt_min_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- currentMinute = lt_min.SelectedIndex;
- SetHourAndMin();
- }
- private void pp_time_LostFocus(object sender, RoutedEventArgs e)
- {
- isOpen = false;
- pp_time.IsOpen = isOpen;
-
- }
- private void Border_PreviewMouseDown(object sender, MouseButtonEventArgs e)
- {
- if (!pp_time.IsMouseOver)
- {
- isOpen = false;
- pp_time.IsOpen = isOpen;
- }
-
- }
- }
- public class DateUtil
- {
- /// <summary>
- /// 当前月第一天是周几
- /// </summary>
- /// <returns>周几</returns>
- public int CurrentMonthFristDayOfWeek()
- {
- DateTime now = DateTime.Now;
- DateTime d1 = new DateTime(now.Year, now.Month, 1);
- int day = (int)d1.DayOfWeek;
- if (day == 0)
- day = 7;
- return day;
- }
- /// <summary>
- /// 当前月有几天
- /// </summary>
- /// <returns></returns>
- public int CurrentMonthCount()
- {
- DateTime now = DateTime.Now;
- int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
- return days;
- }
- /// <summary>
- /// 当前月有几天
- /// </summary>
- /// <param name="now">当前时间</param>
- /// <returns></returns>
- public int CurrentMonthCount(DateTime now)
- {
- int days = DateTime.DaysInMonth(now.Year, now.Month);
- return days;
- }
- /// <summary>
- /// 当前月第一天是周几
- /// </summary>
- /// <returns>周几</returns>
- public int CurrentMonthFristDayOfWeek(DateTime now)
- {
- DateTime d1 = new DateTime(now.Year, now.Month, 1);
- int day = (int)d1.DayOfWeek;
- if (day == 0)
- day = 7;
- return day;
- }
- public static long ConvertDataTimeLong(DateTime dt)
- {
- DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- TimeSpan toNow = dt.Subtract(dtStart);
- long timeStamp = toNow.Ticks;
- timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
- return timeStamp;
- }
- }
- }
|