using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CK.SCP.Models;
using CK.SCP.Models.ScpEntity;
using System.Data.Entity.Migrations;
using CK.SCP.Utils;
using CK.SCP.Models.Enums;
using System.Data.Entity.Core;
using CK.SCP.Models.ScpEntity.ExcelExportEnttity;
using CK.SCP.Models.AppBoxEntity;
using System.Collections;
namespace CK.SCP.Controller
{
///
/// 采购对应收货管理地址
///
public class SCP_TB_ADDRESS_BUYER_CONTROLLER
{
///
/// 获取采购收货地址对应关系
///
/// 采购
///
public static void GetTbAddressBuyerList(string Buyer,string SubSite, Action>> p_action)
{
ResultObject> _ret = new ResultObject>();
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
IQueryable q = db.TB_ADDRESS_BUYER;
if (!string.IsNullOrEmpty(Buyer))
{
q = q.Where(p => p.Buyer.Contains(Buyer));
}
if (!string.IsNullOrEmpty(SubSite))
{
q = q.Where(p => p.SubSite == SubSite);
}
_ret.State = ReturnStatus.Succeed;
_ret.Result = q;
p_action(_ret);
}
}
catch (Exception e)
{
_ret.State = ReturnStatus.Failed;
_ret.ErrorList.Add(e);
if (e.InnerException != null)
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TB_ADDRESS_BUYER_CONTROLLER), "GetTbAddressBuyerList(Exception)", LogHelper.GetExceptionMessage(e).Message);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TB_ADDRESS_BUYER_CONTROLLER), "GetTbAddressBuyerList", e.Message);
throw e;
}
}
///
/// 导入
///
///
///
///
public static ResultObject EXCEL_ADRESS_BUYER_LIST(List p_order_list,string SubSite, string p_creator)
{
ResultObject _ret = new ResultObject();
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
List lineError = new List();
List ErrorList = new List();
var _lst = p_order_list;
List uidList = db.TB_ADDRESS.Select(q => q.UID).ToList();
/// 校验
lineError = Checked(p_order_list, uidList);
if (lineError.Count > 0)
{
_ret.State = ReturnStatus.Failed;
_ret.MessageList.AddRange(lineError);
_ret.Result = false;
}
else
{
DateTime time = DateTime.Now;
_lst.ForEach(p =>
{
var _Sitelist = db.TB_ADDRESS_BUYER.Count(t => t.Buyer == p.采购人 && t.SubSite == SubSite);
if (_Sitelist == 0)
{
TB_ADDRESS_BUYER buyer = new TB_ADDRESS_BUYER();
buyer.AddressUID = long.Parse(p.收货地址ID);
buyer.Buyer = p.采购人;
buyer.Creator = p_creator;
buyer.CreateTime = time;
buyer.SubSite = SubSite;
db.TB_ADDRESS_BUYER.Add(buyer);
}
else if (_Sitelist == 1)
{
var buyer = db.TB_ADDRESS_BUYER.SingleOrDefault(t => t.Buyer == p.采购人 && t.SubSite == SubSite);
buyer.AddressUID = long.Parse(p.收货地址ID);
buyer.UpdateTime = time;
buyer.UpdateUser = p_creator;
db.TB_ADDRESS_BUYER.AddOrUpdate(a => a.UID, buyer);
}
});
if (_ret.MessageList.Count == 0)
{
int state = db.SaveChanges();
if (state != -1)
{
_ret.State = ReturnStatus.Succeed;
_ret.Result = true;
}
else
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
}
}
else
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
}
}
}
}
catch (Exception e)
{
_ret.State = ReturnStatus.Failed;
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TB_ADDRESS_BUYER_CONTROLLER), "EXCEL_ADRESS_BUYER_LIST", e.Message);
_ret.Result = false;
_ret.ErrorList.Add(e);
throw e;
}
return _ret;
}
///
/// 校验 查看是否有错误
///
/// 导入文件
/// 地址uidlist
///
public static List Checked(List p_order_list, List ulist)
{
List errorlist = new List();
List userName = new List();
using (AppBoxContext db = EntitiesFactory.CreateAppBoxInstance())
{
List users = db.Users.ToList();
userName = users.Select(q => q.Name).ToList();
}
long ID;
foreach (var item in p_order_list)
{
var count = p_order_list.Where(q => q.采购人 == item.采购人).Count();
if (count > 1)
{
errorlist.Add(String.Format("{0}重复", item.采购人));
break;
}
if (!userName.Contains(item.采购人))
{
errorlist.Add(string.Format("{0}不是当前系统人员", item.采购人));
break;
}
if (!long.TryParse(item.收货地址ID, out ID))
{
errorlist.Add(string.Format("{0}不能转化为数字", item.收货地址ID));
break;
}
else if (!ulist.Contains(ID))
{
errorlist.Add(string.Format("{0}不在收货信息管理(重庆专用)的UID里", item.收货地址ID));
break;
}
}
return errorlist;
}
///
/// 删除
///
/// uid列表
///
public static ResultObject DeleteByUid(List uidlist)
{
ResultObject _ret = new ResultObject();
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
var list = db.TB_ADDRESS_BUYER.Where(itm => uidlist.Contains(itm.UID));
db.TB_ADDRESS_BUYER.RemoveRange(list);
if (db.SaveChanges() != -1)
{
_ret.State = ReturnStatus.Succeed;
_ret.Result = true;
}
else
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
}
}
}
catch (Exception e)
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.ErrorList.Add(e);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TB_ADDRESS_BUYER_CONTROLLER), "DeleteByUid", e.Message); throw e;
}
return _ret;
}
}
}