AcsTaskApp.cs 7.38 KB
using Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 任务表
    /// </summary>

    public partial class AcsTaskApp
    {
        private IUnitWork _unitWork;
        private IUnitWorkAcs _unitWorkAcs;
        public IRepository<AcsTask> _app;
        private static IHostingEnvironment _hostingEnvironment;
        ExcelHelper imp = new ExcelHelper(_hostingEnvironment);

        public AcsTaskApp(IUnitWork unitWork, IUnitWorkAcs unitWorkAcs, IRepository<AcsTask> repository, IHostingEnvironment hostingEnvironment)
        {
            _unitWork = unitWork;
            _unitWorkAcs = unitWorkAcs;
            _app = repository;
            _hostingEnvironment = hostingEnvironment;
        }

        public AcsTaskApp SetLoginInfo(LoginInfo loginInfo)
        {
            _app._loginInfo = loginInfo;
            return this;
        }

        public TableData Load(PageReq pageRequest, AcsTask entity)
        {
            var result = new TableData();

            var data = Find(EntityToExpression<AcsTask>.GetExpressions(entity));

            int skipPage = 0;
            int takeCount = data.Count();
            string odby = "";
            string sfield = "";

            if (pageRequest != null)
            {
                skipPage = (pageRequest.page - 1) * pageRequest.limit;
                takeCount = pageRequest.limit;
                odby = pageRequest.order;
                sfield = pageRequest.field;
            }

            if (string.IsNullOrEmpty(sfield))
            {
                odby = "asc";
                sfield = "Id";
            }

            bool isAsc = odby == "desc" ? false : true;

            List<AcsTask> data1;
            if (string.IsNullOrEmpty(sfield))
                data1 = data.Skip(skipPage).Take(takeCount).ToList();
            else
                data1 = data.OrderBy(sfield, isAsc).Skip(skipPage).Take(takeCount).ToList();

            var data2 = _unitWorkAcs.Find<TTask>(null).Select(u => new { u.StrTaskNo, u.StrTaskAgv, u.StrTaskState, u.TaskErrMsg }).ToList();
            var data3 = _unitWork.Find<AcsWorkstation>(null).Select(u => new { u.Code, u.Zonename }).ToList();

            var taskData = from assignData in data1
                           join acsTaskData in data2 on assignData.TaskNo equals acsTaskData.StrTaskNo into temp1
                           join acsWorkstation in data3 on assignData.StartStation equals acsWorkstation.Code into temp2
                           from t1 in temp1.DefaultIfEmpty()
                           from t2 in temp2.DefaultIfEmpty()
                           select new
                           {
                               assignData.Id,
                               assignData.TaskNo,
                               assignData.OrderNo,
                               assignData.TaskType,
                               assignData.StartStation,
                               assignData.EndStation,
                               assignData.Status,
                               assignData.CreateTime,
                               assignData.CreateBy,
                               assignData.UpdateTime,
                               assignData.UpdateBy,
                               TaskAgv = t1 == null ? "" : t1.StrTaskAgv,
                               TaskState = t1 == null ? "" : t1.StrTaskState,
                               ErrorMsg = t1 == null ? "" : t1.TaskErrMsg,
                               StartZone = t2 == null ? "" : t2.Zonename,
                           };

            result.data = taskData;
            result.count = data1.Count();
            return result;
        }

        public void Ins(AcsTask entity)
        {
            _app.Add(entity);
        }

        public void Upd(AcsTask entity)
        {
            _app.Update(entity);
        }

        public void DelByIds(int[] ids)
        {
            _app.Delete(u => ids.Contains(u.Id.Value));
        }

        public AcsTask FindSingle(Expression<Func<AcsTask, bool>> exp)
        {
            return _app.FindSingle(exp);
        }

        public IQueryable<AcsTask> Find(Expression<Func<AcsTask, bool>> exp)
        {
            return _app.Find(exp);
        }

        public Response ImportIn(IFormFile excelfile)
        {
            Response result = new Infrastructure.Response();
            List<AcsTask> exp = imp.ConvertToModel<AcsTask>(excelfile);
            string sErrorMsg = "";

            for (int i = 0; i < exp.Count; i++)
            {
                try
                {
                    AcsTask e = exp[i];
                    e.Id = null;
                    _app.Add(e);
                }
                catch (Exception ex)
                {
                    sErrorMsg += "第" + (i + 2) + "行:" + ex.Message + "<br>";
                    result.Message = sErrorMsg;
                    break;
                }
            }
            if (sErrorMsg.Equals(string.Empty))
            {
                if (exp.Count == 0)
                {
                    sErrorMsg += "没有发现有效数据, 请确定模板是否正确, 或是否有填充数据!";
                    result.Message = sErrorMsg;
                }
                else
                {
                    result.Message = "导入完成";
                }
            }
            else
            {
                result.Status = false;
                result.Message = result.Message;
            }
            return result;
        }

        public TableData ExportData(AcsTask entity)
        {
            return _app.ExportData(entity);
        }

        public TableData Query(AcsTask entity)
        {
            var result = new TableData();
            var data = _app.Find(EntityToExpression<AcsTask>.GetExpressions(entity));

            GetData(data, result);
            result.count = data.Count();

            return result;
        }

        public void GetData(IQueryable<AcsTask> data, TableData result, PageReq pageRequest = null)
        {
            _app.GetData(data, result, pageRequest);
        }

        public Response<TTask> Init()
        {
            var response = new Response<TTask>();
            var workSt = _unitWorkAcs.FindSingle<TBaseStation>(u => u.StrStationNo == "BeiLiao1");
            var sonTask = _unitWorkAcs.Find<TSonTask>(u => u.SonTaskState == 5 && u.EndPoint == workSt.StrBarcode)
                .ToList();
            if (sonTask.Count == 0) return response;
            response.Result =
                _unitWorkAcs.FindSingle<TTask>(u =>
                    u.StrTaskID == "PeiSong" && sonTask.Exists(a => a.StrTaskNo == u.StrTaskNo));
            return response;
        }

        public Response<TTask> Init1()
        {
            var response = new Response<TTask>();
            var workSt = _unitWorkAcs.FindSingle<TBaseStation>(u => u.StrStationNo == "BeiLiao2");
            var sonTask = _unitWorkAcs.Find<TSonTask>(u => u.SonTaskState == 5 && u.EndPoint == workSt.StrBarcode)
                .ToList();
            if (sonTask.Count == 0) return response;
            response.Result =
                _unitWorkAcs.FindSingle<TTask>(u =>
                    u.StrTaskID == "PeiSong" && sonTask.Exists(a => a.StrTaskNo == u.StrTaskNo));
            return response;
        }
    }
}