245 lines
9.1 KiB
C#
245 lines
9.1 KiB
C#
using BaseOUDAL;
|
|
using System.Data;
|
|
using System.Text;
|
|
using YLErp.Commons;
|
|
using YLErp.Helpers;
|
|
|
|
namespace YLErp.Modules.FinancialModule
|
|
{
|
|
public class YYClientCodeMaintenanceService : YLBaseService
|
|
{
|
|
public YYClientCodeMaintenanceService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
}
|
|
|
|
private ErpBaseContext basedb = new ErpBaseContext();
|
|
|
|
public List<DictionaryItem> GetYonYouClientCodeFormCsv(string path, bool needDeleteAll)
|
|
{
|
|
|
|
var resultList = new List<string>();
|
|
var originaltype = EncodingType.GetType(path, resultList);
|
|
ChangeFileEncoding(path, originaltype);
|
|
|
|
string Name;
|
|
string ShortName;
|
|
DictionaryItem item;
|
|
int DicId = basedb.Dictionaries.Where(o => o.Name == "用友客商编码").Select(o => o.Id).FirstOrDefault();
|
|
int indexNum = basedb.DictionaryItems.Where(o => o.DictId == DicId).Max(o => o.IndexNum);
|
|
if (needDeleteAll)
|
|
{
|
|
indexNum = 0;
|
|
}
|
|
|
|
var items = new List<DictionaryItem>();
|
|
|
|
int rowNum = 0;
|
|
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
|
|
{
|
|
using (var sr = new StreamReader(fs, Encoding.UTF8))
|
|
{
|
|
while (!sr.EndOfStream)
|
|
{
|
|
string line = sr.ReadLine();
|
|
if (rowNum == 0)
|
|
{
|
|
rowNum++;
|
|
continue;
|
|
}
|
|
rowNum++;
|
|
|
|
if (line == "")
|
|
{
|
|
continue;
|
|
}
|
|
|
|
string[] res = line.Split(new char[] { ',' }, StringSplitOptions.None);
|
|
if (res != null && res.Length > 0)
|
|
{
|
|
var isAllEmpty = true;
|
|
for (int i = 0; i < res.Length; i++)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(res[i]))
|
|
{
|
|
isAllEmpty = false;
|
|
}
|
|
}
|
|
|
|
if (isAllEmpty)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (res.Length < 19)
|
|
{
|
|
throw new ServiceException("导入文件列缺失!请检查后再导入!");
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(res[0]))
|
|
{
|
|
throw new ServiceException("第" + rowNum + "行名称未填写,导入失败");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(res[0]))
|
|
{
|
|
throw new ServiceException("第" + rowNum + "行简称未填写,导入失败");
|
|
}
|
|
|
|
Name = res[0];
|
|
ShortName = res[1];
|
|
|
|
indexNum++;
|
|
|
|
item = new DictionaryItem()
|
|
{
|
|
Name = Name,
|
|
ShortName = ShortName,
|
|
DictId = DicId,
|
|
IndexNum = indexNum
|
|
};
|
|
|
|
items.Add(item);
|
|
}
|
|
}
|
|
|
|
if (items == null || items.Count == 0)
|
|
{
|
|
throw new ServiceException("请确保上传文件数据不为空,导入失败");
|
|
}
|
|
}
|
|
|
|
return items;
|
|
}
|
|
|
|
private void ChangeFileEncoding(string filename, Encoding encoding)
|
|
{
|
|
var fs = new FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
|
|
byte[] flieByte = new byte[fs.Length];
|
|
fs.Read(flieByte, 0, flieByte.Length);
|
|
fs.Close();
|
|
|
|
StreamWriter docWriter;
|
|
var ec = Encoding.GetEncoding("UTF-8");
|
|
docWriter = new StreamWriter(filename, false, ec);
|
|
docWriter.Write(encoding.GetString(flieByte));
|
|
docWriter.Close();
|
|
}
|
|
|
|
public List<DictionaryItem> GetYonYouClientCodeFromExcel(Stream stream, int userId, string userName, bool needDeleteAll)
|
|
{
|
|
var Clients = new ExcelHelper().ExcelToDataTable(stream, null, true);
|
|
string Name = "";
|
|
string ShortName = "";
|
|
DictionaryItem item;
|
|
int DicId = basedb.Dictionaries.Where(o => o.Name == "用友客商编码").Select(o => o.Id).FirstOrDefault();
|
|
int indexNum = basedb.DictionaryItems.Where(o => o.DictId == DicId).Max(o => o.IndexNum);
|
|
if (needDeleteAll)
|
|
{
|
|
indexNum = 0;
|
|
}
|
|
|
|
var items = new List<DictionaryItem>();
|
|
var rowNum = 0;
|
|
for (int i = 0; i < Clients.Rows.Count; i++)
|
|
{
|
|
rowNum = i + 2;
|
|
for (int j = 0; j < Clients.Columns.Count; j++)
|
|
{
|
|
switch (Clients.Columns[j].ColumnName)
|
|
{
|
|
case "名称":
|
|
if (string.IsNullOrWhiteSpace(Clients.Rows[i][j].ToString()))
|
|
{
|
|
throw new ServiceException("第" + rowNum + "行名称未填写,导入失败");
|
|
}
|
|
|
|
if (Clients.Rows[i][j].ToString().Length > 50)
|
|
{
|
|
throw new ServiceException("第" + rowNum + "行名称超长,导入失败");
|
|
}
|
|
|
|
Name = Clients.Rows[i][j].ToString();
|
|
break;
|
|
case "简称":
|
|
if (string.IsNullOrWhiteSpace(Clients.Rows[i][j].ToString()))
|
|
{
|
|
throw new ServiceException("第" + rowNum + "行简称未填写,导入失败");
|
|
}
|
|
if (Clients.Rows[i][j].ToString().Length > 50)
|
|
{
|
|
throw new ServiceException("第" + rowNum + "行简称超长,导入失败");
|
|
}
|
|
ShortName = Clients.Rows[i][j].ToString();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(Name) && !string.IsNullOrWhiteSpace(ShortName))
|
|
{
|
|
indexNum++;
|
|
item = new DictionaryItem()
|
|
{
|
|
Name = Name,
|
|
ShortName = ShortName,
|
|
DictId = DicId,
|
|
IndexNum = indexNum
|
|
};
|
|
|
|
items.Add(item);
|
|
}
|
|
|
|
}
|
|
|
|
if (items == null || items.Count == 0)
|
|
{
|
|
throw new ServiceException("请确保上传文件数据不为空,导入失败");
|
|
}
|
|
return items;
|
|
}
|
|
|
|
public HandleResult DeleteAllYonYouClientCode()
|
|
{
|
|
int DicId = basedb.Dictionaries.Where(o => o.Name == "用友客商编码").Select(o => o.Id).FirstOrDefault();
|
|
var AllYYClientCode = basedb.DictionaryItems.Where(o => o.DictId == DicId);
|
|
basedb.DictionaryItems.RemoveRange(AllYYClientCode);
|
|
|
|
return HandleResult.Success;
|
|
}
|
|
|
|
public HandleResult UploadYYClientCodeToDb(List<DictionaryItem> items)
|
|
{
|
|
if (items == null || items.Count == 0)
|
|
{
|
|
throw new ServiceException("上传数据为空,导入失败");
|
|
}
|
|
|
|
//选出有的 删除
|
|
var names = items.Select(o => o.Name);
|
|
var exists = basedb.DictionaryItems.Where(o => names.Contains(o.Name));
|
|
basedb.DictionaryItems.RemoveRange(exists);
|
|
|
|
basedb.DictionaryItems.AddRange(items);
|
|
|
|
basedb.SaveChanges();
|
|
return HandleResult.Success;
|
|
}
|
|
|
|
public byte[] ExportYYClientCode()
|
|
{
|
|
int DicId = basedb.Dictionaries.Where(o => o.Name == "用友客商编码").Select(o => o.Id).FirstOrDefault();
|
|
var AllYYClientCode = basedb.DictionaryItems.Where(o => o.DictId == DicId);
|
|
|
|
List<ExcelHelper.DataColumnModel> dc = new List<ExcelHelper.DataColumnModel>();
|
|
|
|
dc.Add(new ExcelHelper.DataColumnModel("名称", "Name"));
|
|
dc.Add(new ExcelHelper.DataColumnModel("简称", "ShortName"));
|
|
|
|
new ExcelHelper().ListToExcel<DictionaryItem>(dc.ToArray(), AllYYClientCode.ToList(), "用友客商代码", true, out byte[] buffer);
|
|
return buffer;
|
|
}
|
|
}
|
|
}
|