FactoryCalendarService.cs 6.5 KB
using Hh.Mes.Common.Date;
using Hh.Mes.Common.log;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using System;
using System.Globalization;
using System.Text;

namespace Hh.Mes.Service.Planned
{
    public class FactoryCalendarService : RepositorySqlSugar<base_factory_calendar>
    {
        /// <summary>
        /// 新增  code = 字典前缀+对应表标识最大值
        /// </summary>
        /// <returns></returns>
        public dynamic Ins(base_factory_calendar model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.createBy = sysWebUser?.Account;
                model.createTime = DateTime.Now;
                var result = Add(model);
                response.Status = result;
                return response;
            });
        }

        /// <summary>
        /// 编辑  code = 字典前缀+对应表标识最大值
        /// </summary>
        /// <returns></returns>
        public dynamic Edit(base_factory_calendar model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.updateBy = sysWebUser?.Account;
                model.updateTime = DateTime.Now;
                var result = Update(model);
                response.Status = result;
                return response;
            });
        }

        /// <summary>
        /// 加载日历
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public dynamic GetFactoryCalendarList(string model)
        {
            #region 加载生产班别日历
            //if (string.IsNullOrEmpty(model))
            //{
            //    model = DateTime.Now.ToString();
            //}
            ////加载日历显示的时候 是显示班别里面的时间(范围) 还是日历里面的时间(范围)???????
            //var result = new TableData();
            //var factoryCalendar = GetList();
            //var data = Convert.ToDateTime(model);
            //var s = data.Month <= 9 ? "0" + data.Month : data.Month.ToString();
            //string modeldata = data.Year.ToString() + "-" + s;
            //string sql = string.Format($" CONVERT(varchar,statrCalendarDay,120) like '%{modeldata}%' ");
            //var list = Context.Queryable<factory_calendar, product_shift>((fc, ps) => new JoinQueryInfos(JoinType.Left, fc.tableCode == ps.code)).Where(sql).Select((fc, ps) => new { fc.id, title = ps.shiftName, start = fc.statrCalendarDay, end = fc.endCalendarDay }).ToList();
            //return ExceptionsHelp.Instance.ExecuteT(() =>
            //{
            //    result.data = list;
            //    result.count = result.data.Count;
            //    return result;
            //}, catchRetrunValue: "list");
            #endregion
            return null;

        }

        /// <summary>
        /// 线体日历详情获取
        /// </summary>
        /// <param name="calendarTime"></param>
        /// <param name="falg">查询月还是天</param>
        /// <returns></returns>
        public dynamic GetCalendarLineList(string calendarTime, string falg)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var stringBuilder = new StringBuilder();
                string sql = "";
                var date = Convert.ToDateTime(calendarTime);
                var FirstDayOfMonth = DateHelper.FirstDayOfMonth(date);
                var LastDayOfMonth = DateHelper.LastDayOfMonth(date);
                if (string.IsNullOrEmpty(calendarTime)) calendarTime = DateTime.Now.ToString(CultureInfo.InvariantCulture);
                if (falg == "month")
                {
                    sql = string.Format($" t2.CalendarDay >='{FirstDayOfMonth}' and  t2.CalendarDay <='{LastDayOfMonth}' ");
                    stringBuilder.AppendLine($@" select t2.CalendarDay as start, t2.[Weekday], t2.LineCode, t1.lineCode  
                                                 from line t1 left join [factory_calendar] t2 on t1.lineCode = t2.LineCode 
                                                 where  {sql} ");
                }
                else if (falg == "day")
                {
                    sql = string.Format($" t2.CalendarDay ='{date}' ");
                    stringBuilder.AppendLine($@" select t2.*,t1.lineName, t1.lineCode  from line t1 
                                                 left join(
                                                             select t2.CalendarDay as start, t2.[Weekday], code= t2.LineCode 
                                                             from line t1 left join [factory_calendar] t2 on t1.lineCode = t2.LineCode
                                                             where  {sql}
                                                          ) t2 on t1.lineCode = t2.code ");
                }
                var dt = base.Context.Ado.GetDataTable(stringBuilder.ToString());
                var result = new Response
                {
                    Result = dt,
                    Count = dt.Rows.Count
                };
                return result;
            }, catchRetrunValue: "list");
        }

        /// <summary>
        /// 线体日历线体操作   ****要么一个一个执行  要么全部执行***
        /// </summary>
        public Response CalendarLineList(string calendartime, string lines, bool flag)
        {
            var dateTime = Convert.ToDateTime(calendartime);
            var result= new Response();
            var calendar = new base_factory_calendar();
            foreach (var line in lines.Split(','))
            {
                var factorycalendar = Context.Queryable<base_factory_calendar>().Where(u => u.LineCode == line && u.CalendarDay == dateTime).First();
                if (factorycalendar==null&&flag)
                {
                    calendar.LineCode = line;
                    calendar.CalendarDay = dateTime;
                    calendar.Weekday = flag;
                    calendar.createBy = sysWebUser?.Account;
                    calendar.createTime = DateTime.Now;
                    Add(calendar);
                }
                else if (!flag)
                {
                    Context.Deleteable<base_factory_calendar>().Where(u => u.id == factorycalendar.id).ExecuteCommand();
                }

            }
            result.Result = GetCalendarLineList(calendartime, "month");
            return result;

        }
    }
}