using iWareCommon.Common.Service;
|
using iWareCommon.Utils;
|
using iWareDataCore.BASE.Dao;
|
using iWareDataCore.BASE.Entity;
|
using iWareDataCore.ORM;
|
using iWareDataCore.Properties;
|
using iWareExcel.EXCEL.Entity;
|
using iWareExcel.Utils;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace iWareDataCore.BASE.Service
|
{
|
public class PlaceViewService : CommonService<PlaceViewEntity, BASEPlaceView, DbModelCore>
|
{
|
|
private static object Lock = new object();
|
|
private PlaceViewService() : base(PlaceViewDao.GetInstance()) { }
|
|
private static PlaceViewService Instance = null;
|
|
/// <summary>
|
/// 获取单例的方法
|
/// </summary>
|
/// <returns>角色服务的单例实体</returns>
|
public static PlaceViewService GetInstance()
|
{
|
|
if (Instance == null)
|
{
|
lock (Lock)
|
{
|
if (Instance == null)
|
{
|
Instance = new PlaceViewService();
|
}
|
}
|
}
|
return Instance;
|
}
|
|
|
|
|
|
|
/// <summary>
|
/// 导入Excel
|
/// </summary>
|
/// <param name="path">文件在服务器上的路径</param>
|
/// <param name="workBookEntity">导入使用的目板</param>
|
/// <param name="name">导入者姓名</param>
|
/// <param name="msg">异常错误消息</param>
|
/// <returns>是否导入成功</returns>
|
public bool ImportExcel(string path, WorkBookEntity workBookEntity, out string msg)
|
{
|
msg = "";
|
|
using (var dbModel = new DbModelCore())
|
{
|
try
|
{
|
var ds = ExcelHelper.FromExcelByEPPlus(path, workBookEntity, out msg);
|
if (!string.IsNullOrEmpty(msg))
|
{
|
throw new Exception(msg);
|
}
|
|
var places = ds[0];
|
if (places.Count < 1)
|
{
|
msg = "库位记录数为0,请修改后重新导入";
|
return false;
|
}
|
var types = dbModel.BASEPlaceTypes.Where(x => 1 == 1).ToList();
|
var i = 0;
|
var codes = new List<string>();
|
var shelfCodeRowColLayers = new List<string>();
|
foreach (PlaceViewEntity place in places)
|
{
|
if (string.IsNullOrEmpty(place.Code))
|
{
|
msg = string.Format("在第{0}行,库位编号不能为空,请修改后重新导入", i + 1);
|
return false;
|
}
|
|
if (string.IsNullOrEmpty(place.PlaceTypeName))
|
{
|
msg = string.Format("在第{0}行,库位类型不能为空,请修改后重新导入", i + 1);
|
return false;
|
}
|
|
|
|
var type = types.FirstOrDefault(x => x.name == place.PlaceTypeName);
|
if (type == null)
|
{
|
msg = string.Format("在第{0}行,不存在库位类型{1},请修改后重新导入", i + 1, place.PlaceTypeName);
|
return false;
|
}
|
place.TypeId = type.id;
|
|
if (codes.Contains(place.Code))
|
{
|
msg = string.Format("在第{0}行,库位编号为{1}的库位已重复出现,请修改后重新导入", i + 1, place.Code);
|
return false;
|
}
|
|
|
if (shelfCodeRowColLayers.Contains(place.Row + "#" + place.Col + "#" + place.Layer))
|
{
|
msg = string.Format("在第{0}行,第{1}排第{2}列第{3}列的库位已出现过,请修改后重新导入", i + 1, place.Row, place.Col, place.Layer);
|
return false;
|
}
|
shelfCodeRowColLayers.Add(place.Row + "#" + place.Col + "#" + place.Layer);
|
|
codes.Add(place.Code);
|
}
|
|
var placeSaves = new List<PlaceEntity>();
|
var placeUpdates = new List<PlaceEntity>();
|
|
var existPlaces = dbModel.BASEPlaces.Select(x => new { x.id, x.code }).Where(x => codes.Contains(x.code)).ToList();
|
|
foreach (PlaceViewEntity place in places)
|
{
|
var one = existPlaces.FirstOrDefault(x => x.code == place.Code);
|
if (one == null) { placeSaves.Add(new PlaceEntity(place)); }
|
else
|
{
|
place.Id = one.id;
|
placeUpdates.Add(new PlaceEntity(place));
|
}
|
}
|
|
var sum = placeSaves.Count % 1000 == 0 ? placeSaves.Count / 1000 : placeSaves.Count / 1000 + 1;
|
for (var j = 0; j < (int)Math.Ceiling(placeSaves.Count / 1000.0); j++)
|
{
|
var sql = string.Format("INSERT INTO [dbo].[BASEPlace] (code,typeid,heightlevel,row,col,layer,length,width,height,posx,posy,posz,maxweight,status,islock,isexecute) VALUES");
|
for (var r = j * 1000; r < j * 1000 + 1000; r++)
|
{
|
if (r == placeSaves.Count)
|
{
|
break;
|
}
|
sql += string.Format("( '{0}', '{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}'),",
|
StringHelper.RelpaceQuot(placeSaves[r].Code),
|
placeSaves[r].TypeId,
|
placeSaves[r].HeightLevel,
|
placeSaves[r].Row,
|
placeSaves[r].Col,
|
placeSaves[r].Layer,
|
placeSaves[r].Length,
|
placeSaves[r].Width,
|
placeSaves[r].Height,
|
placeSaves[r].Posx,
|
placeSaves[r].Posy,
|
placeSaves[r].Posz,
|
placeSaves[r].MaxWeight,
|
placeSaves[r].Status,
|
placeSaves[r].IsLock,
|
placeSaves[r].IsExecute);
|
}
|
sql = sql.Substring(0, sql.Length - 1);
|
var num = dbModel.Database.ExecuteSqlCommand(sql);
|
}
|
|
PlaceDao.GetInstance().Update(placeUpdates, dbModel);
|
dbModel.SaveChanges();
|
return string.IsNullOrEmpty(msg);
|
}
|
catch (Exception ex)
|
{
|
msg = ex.Message;
|
LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "ImportExcel", ex.Message);
|
return false;
|
}
|
}
|
|
}
|
|
/// <summary>
|
/// 获取指定字符串开头的库位列表
|
/// </summary>
|
/// <param name="term">指定字符串</param>
|
/// <param name="size">返回的最大条数</param>
|
/// <param name="msg">异常错误消息</param>
|
/// <returns>送货单号列表</returns>
|
public List<string> GetPlaceCodes(string term, int size, out string msg)
|
{
|
using (var dbModel = new DbModelCore())
|
{
|
try
|
{
|
msg = "";
|
|
if (string.IsNullOrEmpty(term))
|
{
|
return new List<string>();
|
}
|
|
|
var placeviews = dbModel.BASEPlaceViews.Select(x => new { x.id, x.code, }).OrderByDescending(x => x.id).Where(x => x.code.StartsWith(term)).Skip(0).Take(size).ToList();
|
|
var placecodes = new List<string>();
|
|
placeviews.ForEach(x => placecodes.Add(x.code));
|
|
return placecodes;
|
|
}
|
catch (Exception ex)
|
{
|
msg = ex.Message;
|
LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "GetPlaceCodes", ex.Message);
|
return new List<string>();
|
}
|
}
|
}
|
|
|
}
|
}
|