BasicDataApp.cs 5.7 KB
using System;
using System.Collections.Generic;
using Infrastructure;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 基础数据接口App
    /// </summary>

    public partial class BasicDataApp: ApiApp
    {
        public BasicDataApp(IUnitWork unitWork, IAuth auth, BaseDBContext context) : base(unitWork, auth, context)
        {

        }

        public string Customer(CustomersModel Customers)
        {
            Response<List<Customer>> Response = new Response<List<Customer>>();

            if (!CheckLogin())
            {
                Response.Code = 500;
                Response.Status = false;
                Response.Message = "请先登录!";
                return JsonHelper.Instance.Serialize(Response);
            }

            foreach (var item in Customers.Data)
            {
                try
                {
                    Customer data = _unitWork.FindSingle<Customer>(u => u.Code.Equals(item.Code));
                    if (data == null)
                    {
                        item.Id = null;
                        if (item.CreateBy == null)
                        {
                            item.CreateBy = "wms";
                            item.CreateTime = DateTime.Now;
                        }
                        _unitWork.Add(item);
                    }
                    else
                    {
                        item.Id = data.Id;
                        if (item.UpdateBy == null)
                        {
                            item.UpdateBy = "wms";
                            item.UpdateTime = DateTime.Now;
                        }
                        _unitWork.Update(item);
                    }
                }
                catch (Exception)
                {
                    Response.Code = 500;
                    Response.Status = false;
                    Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "Code:" + item.Code + "同步失败!";

                    Response.Result.Add(item);
                }
            }

            return JsonHelper.Instance.Serialize(Response);
        }

        public string Material(MaterialsModel Materials)
        {
            Response<List<Material>> Response = new Response<List<Material>>();

            if (!CheckLogin())
            {
                Response.Code = 500;
                Response.Status = false;
                Response.Message = "请先登录!";
                return JsonHelper.Instance.Serialize(Response);
            }

            foreach (var item in Materials.Data)
            {
                try
                {
                    Material data = _unitWork.FindSingle<Material>(u => u.Code.Equals(item.Code));
                    if (data == null)
                    {
                        item.Id = null;
                        if (item.CreateBy == null)
                        {
                            item.CreateBy = "wms";
                            item.CreateTime = DateTime.Now;
                        }
                        _unitWork.Add(item);
                    }
                    else
                    {
                        item.Id = data.Id;
                        if (item.UpdateBy == null)
                        {
                            item.UpdateBy = "wms";
                            item.UpdateTime = DateTime.Now;
                        }
                        _unitWork.Update(item);
                    }
                }
                catch (Exception)
                {
                    Response.Code = 500;
                    Response.Status = false;
                    Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "Code:" + item.Code + "同步失败!";

                    Response.Result.Add(item);
                }
            }

            return JsonHelper.Instance.Serialize(Response);
        }

        public string Supplier(SuppliersModel Suppliers)
        {
            Response<List<Supplier>> Response = new Response<List<Supplier>>();

            if (!CheckLogin())
            {
                Response.Code = 500;
                Response.Status = false;
                Response.Message = "请先登录!";
                return JsonHelper.Instance.Serialize(Response);
            }

            foreach (var item in Suppliers.Data)
            {
                try
                {
                    Supplier data = _unitWork.FindSingle<Supplier>(u => u.Code.Equals(item.Code));
                    if (data == null)
                    {
                        item.Id = null;
                        if (item.CreateBy == null)
                        {
                            item.CreateBy = "wms";
                            item.CreateTime = DateTime.Now;
                        }
                        _unitWork.Add(item);
                    }
                    else
                    {
                        item.Id = data.Id;
                        if (item.UpdateBy == null)
                        {
                            item.UpdateBy = "wms";
                            item.UpdateTime = DateTime.Now;
                        }
                        _unitWork.Update(item);
                    }
                }
                catch (Exception)
                {
                    Response.Code = 500;
                    Response.Status = false;
                    Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "Code:" + item.Code + "同步失败!";

                    Response.Result.Add(item);
                }
            }

            return JsonHelper.Instance.Serialize(Response);
        }
    }
}