啊鑫
2025-01-22 eb744ecfecc0f1b6d9c0999c65bc740d93e30eda
StandardPda/MES.Service/service/BasicData/OrganizeManager.cs
@@ -1,261 +1,265 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using MES.Service.DB;
using MES.Service.Modes;
using MES.Service.DB;
using MES.Service.Dto.webApi;
using System.Xml.Linq;
using MES.Service.Modes;
using SqlSugar;
namespace MES.Service.service
{
namespace MES.Service.service.BasicData;
public class OrganizeManager : Repository<Organize>
{
    //当前类已经继承了 Repository 增、删、查、改的方法
        //当前类已经继承了 Repository 增、删、查、改的方法
    //这里面写的代码不会给覆盖,如果要重新生成请删除 OrganizeManager.cs
    public bool Save(ErpOrganize item)
    {
        var entity = GetOrganize(item);
        //这里面写的代码不会给覆盖,如果要重新生成请删除 OrganizeManager.cs
        public bool Save(ErpOrganize item)
        return UseTransaction(db =>
        {
            var entity = GetOrganize(item);
            return UseTransaction(db =>
            switch (item.Type)
            {
                switch (item.Type)
                case "0":
                    if (UpdateOrganizetatus(db, entity.Id, "A"))
                        return 1;
                    break;
                case "1":
                    if (UpdateOrganizetatus(db, entity.Id, "B"))
                        return 1;
                    break;
                //case "2":
                //    if (InsertItem(db, entity))
                //        return 1;
                //    break;
                case "3":
                    if (DeleteItem(db, Convert.ToDecimal(entity.Id)))
                        return 1;
                    break;
                case "2":
                case "4":
                    if (InsertOrUpdate(db, entity))
                        return 1;
                    break;
                default:
                    throw new ArgumentNullException(
                        $"type没有{item.Type}这个类型的参数");
            }
            throw new NotImplementedException("操作失败");
        }) > 0;
    }
    public bool SaveList(List<ErpOrganize> Organize)
    {
        var list = Organize.Select(GetOrganize).ToList();
        var groupBy = list.GroupBy(s => s.Type)
            .ToDictionary(g => g.Key, g => g.ToList());
        return UseTransaction(db =>
        {
            foreach (var itemGroup in groupBy)
                switch (itemGroup.Key)
                {
                    case "0":
                        if (UpdateOrganizetatus(db, entity.Id, "A"))
                            return 1;
                        if (!UpdateOrganizetatusBatch(db, itemGroup.Value, "A"))
                            throw new NotImplementedException("启用失败");
                        break;
                    case "1":
                        if (UpdateOrganizetatus(db, entity.Id, "B"))
                            return 1;
                        if (!UpdateOrganizetatusBatch(db, itemGroup.Value, "B"))
                            throw new NotImplementedException("禁用失败");
                        break;
                    //case "2":
                    //    if (InsertItem(db, entity))
                    //        return 1;
                    //    if (!InsertItemBatch(db, itemGroup.Value))
                    //        throw new NotImplementedException("插入失败");
                    //    break;
                    case "3":
                        if (DeleteItem(db, Convert.ToDecimal(entity.Id)))
                            return 1;
                        if (!DeleteItemBatch(db, itemGroup.Value))
                            throw new NotImplementedException("删除失败");
                        break;
                    case "2":
                    case "4":
                        if (InsertOrUpdate(db, entity))
                            return 1;
                        if (!InsertOrUpdateBatch(db, itemGroup.Value))
                            throw new NotImplementedException("同步失败");
                        break;
                    default:
                        throw new ArgumentNullException(
                            $"type没有{item.Type}这个类型的参数");
                            $"type没有{itemGroup.Key}这个类型的参数");
                }
                throw new NotImplementedException("操作失败");
            }) > 0;
        }
            return 1;
        }) > 0;
    }
        public bool SaveList(List<ErpOrganize> Organize)
    private bool UpdateOrganizetatus(SqlSugarScope db, decimal itemId,
        string status)
    {
        var result = db.Updateable<Organize>()
            .SetColumns(s => s.Fforbidstatus == status)
            .Where(s => s.Id == itemId).ExecuteCommand();
        if (result > 0)
            return true;
        throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败");
    }
    private bool InsertItem(SqlSugarScope db, Organize entity)
    {
        var insert = db.Insertable(entity).ExecuteCommand();
        if (insert > 0)
            return true;
        throw new NotImplementedException("插入失败");
    }
    private bool DeleteItem(SqlSugarScope db, decimal itemId)
    {
        var deleteById = db.Deleteable<Organize>().In(itemId).ExecuteCommand();
        if (deleteById > 0)
            return true;
        throw new NotImplementedException("删除失败");
    }
    private Organize GetOrganize(ErpOrganize Organize)
    {
        return new Organize
        {
            var list = Organize.Select(GetOrganize).ToList();
            var groupBy = list.GroupBy(s => s.Type)
                .ToDictionary(g => g.Key, g => g.ToList());
            Id = Convert.ToDecimal(Organize.Id),
            Fnumber = Organize.FNumber,
            Fname = Organize.FName,
            Fforbidstatus = Organize.FForbidStatus,
            Type = Organize.Type,
            //Fparentid=Organize.FParentID,
            Fcreatedate = Organize.fCreateDate != null
                ? DateTime.ParseExact(Organize.fCreateDate,
                    "yyyy-MM-dd HH:mm:ss", null)
                : null
        };
    }
            return UseTransaction(db =>
            {
                foreach (var itemGroup in groupBy)
                    switch (itemGroup.Key)
                    {
                        case "0":
                            if (!UpdateOrganizetatusBatch(db, itemGroup.Value, "A"))
                                throw new NotImplementedException("启用失败");
                            break;
                        case "1":
                            if (!UpdateOrganizetatusBatch(db, itemGroup.Value, "B"))
                                throw new NotImplementedException("禁用失败");
                            break;
                        //case "2":
                        //    if (!InsertItemBatch(db, itemGroup.Value))
                        //        throw new NotImplementedException("插入失败");
                        //    break;
                        case "3":
                            if (!DeleteItemBatch(db, itemGroup.Value))
                                throw new NotImplementedException("删除失败");
                            break;
                        case "2":
                        case "4":
                            if (!InsertOrUpdateBatch(db, itemGroup.Value))
                                throw new NotImplementedException("同步失败");
                            break;
                        default:
                            throw new ArgumentNullException(
                                $"type没有{itemGroup.Key}这个类型的参数");
                    }
    private bool UpdateOrganizetatusBatch(SqlSugarScope db,
        List<Organize> itemList, string status)
    {
        var ids = itemList.Select(it => it.Id).ToArray();
        var result = db.Updateable<Organize>()
            .SetColumns(s => s.Fforbidstatus == status)
            .Where(s => ids.Contains(s.Id)).ExecuteCommand();
                return 1;
            }) > 0;
        }
        if (result > 0)
            return true;
        private bool UpdateOrganizetatus(SqlSugarScope db, decimal itemId,
            string status)
        throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败");
    }
    private bool InsertItemBatch(SqlSugarScope db, List<Organize> itemList)
    {
        var insertRange = db.Insertable(itemList).ExecuteCommand();
        if (insertRange > 0)
            return true;
        throw new NotImplementedException("插入失败");
    }
    private bool DeleteItemBatch(SqlSugarScope db, List<Organize> itemList)
    {
        var ids = itemList.Select(it => it.Id).ToArray();
        var deleteByIds = db.Deleteable<Organize>().In(ids).ExecuteCommand();
        if (deleteByIds > 0)
            return true;
        throw new NotImplementedException("删除失败");
    }
    private bool InsertOrUpdate(SqlSugarScope db, Organize entity)
    {
        var exists = db.Queryable<Organize>().Any(e => e.Id == entity.Id);
        if (exists)
        {
            var result = db.Updateable<Organize>()
                .SetColumns(s => s.Fforbidstatus == status)
                .Where(s => s.Id == itemId).ExecuteCommand();
            if (result > 0)
            var update = db.Updateable(entity).ExecuteCommand();
            if (update > 0)
                return true;
            throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败");
        }
        private bool InsertItem(SqlSugarScope db, Organize entity)
        else
        {
            var insert = db.Insertable(entity).ExecuteCommand();
            if (insert > 0)
                return true;
            throw new NotImplementedException("插入失败");
        }
        private bool DeleteItem(SqlSugarScope db, decimal itemId)
        {
            var deleteById = db.Deleteable<Organize>().In(itemId).ExecuteCommand();
            if (deleteById > 0)
                return true;
        return false;
    }
            throw new NotImplementedException("删除失败");
        }
        private Organize GetOrganize(ErpOrganize Organize)
        {
            return new Organize
            {
                Id = Convert.ToDecimal(Organize.Id),
                Fnumber = Organize.FNumber,
                Fname = Organize.FName,
                Fforbidstatus = Organize.FForbidStatus,
                Type = Organize.Type,
                //Fparentid=Organize.FParentID,
                Fcreatedate = Organize.fCreateDate != null ?DateTime.ParseExact(Organize.fCreateDate, "yyyy-MM-dd HH:mm:ss", null):null
            };
        }
        private bool UpdateOrganizetatusBatch(SqlSugarScope db,
            List<Organize> itemList, string status)
        {
            var ids = itemList.Select(it => it.Id).ToArray();
            var result = db.Updateable<Organize>()
                .SetColumns(s => s.Fforbidstatus == status)
                .Where(s => ids.Contains(s.Id)).ExecuteCommand();
            if (result > 0)
                return true;
            throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败");
        }
        private bool InsertItemBatch(SqlSugarScope db, List<Organize> itemList)
        {
            var insertRange = db.Insertable(itemList).ExecuteCommand();
            if (insertRange > 0)
                return true;
            throw new NotImplementedException("插入失败");
        }
        private bool DeleteItemBatch(SqlSugarScope db, List<Organize> itemList)
        {
            var ids = itemList.Select(it => it.Id).ToArray();
            var deleteByIds = db.Deleteable<Organize>().In(ids).ExecuteCommand();
            if (deleteByIds > 0)
                return true;
            throw new NotImplementedException("删除失败");
        }
        private bool InsertOrUpdate(SqlSugarScope db, Organize entity)
        {
            var exists = db.Queryable<Organize>().Any(e => e.Id == entity.Id);
            if (exists)
            {
                var update = db.Updateable(entity).ExecuteCommand();
                if (update > 0)
                    return true;
            }
            else
            {
                var insert = db.Insertable(entity).ExecuteCommand();
                if (insert > 0)
                    return true;
            }
            return false;
        }
        private bool InsertOrUpdateBatch(SqlSugarScope db, List<Organize> itemList)
        {
            foreach (var entity in itemList)
                if (!InsertOrUpdate(db, entity))
                    return false;
            return true;
        }
        #region 教学方法
        /// <summary>
        /// 仓储方法满足不了复杂业务需求,业务代码请在这里面定义方法
        /// </summary>
        public void Study()
    private bool InsertOrUpdateBatch(SqlSugarScope db, List<Organize> itemList)
    {
      /*********查询*********/
        foreach (var entity in itemList)
            if (!InsertOrUpdate(db, entity))
                return false;
        var data1 = base.GetById(1);//根据ID查询
        var data2 = base.GetList();//查询所有
        var data3 = base.GetList(it => 1 == 1);  //根据条件查询
        return true;
    }
    #region 教学方法
    /// <summary>
    ///     仓储方法满足不了复杂业务需求,业务代码请在这里面定义方法
    /// </summary>
    public void Study()
    {
        /*********查询*********/
        var data1 = base.GetById(1); //根据ID查询
        var data2 = base.GetList(); //查询所有
        var data3 = base.GetList(it => 1 == 1); //根据条件查询
        //var data4 = base.GetSingle(it => 1 == 1);//根据条件查询一条,如果超过一条会报错
        var p = new PageModel() { PageIndex = 1, PageSize = 2 };// 分页查询
        var p = new PageModel { PageIndex = 1, PageSize = 2 }; // 分页查询
        var data5 = base.GetPageList(it => 1 == 1, p);
        Console.Write(p.TotalCount);//返回总数
        Console.Write(p.TotalCount); //返回总数
        var data6 = base.GetPageList(it => 1 == 1, p, it => SqlFunc.GetRandom(), OrderByType.Asc);// 分页查询加排序
        Console.Write(p.TotalCount);//返回总数
        List<IConditionalModel> conModels = new List<IConditionalModel>(); //组装条件查询作为条件实现 分页查询加排序
        conModels.Add(new ConditionalModel() { FieldName= typeof(Organize).GetProperties()[0].Name, ConditionalType = ConditionalType.Equal, FieldValue = "1" });//id=1
        var data7 = base.GetPageList(conModels, p, it => SqlFunc.GetRandom(), OrderByType.Asc);
        var data6 =
            base.GetPageList(it => 1 == 1, p,
                it => SqlFunc.GetRandom()); // 分页查询加排序
        Console.Write(p.TotalCount); //返回总数
        base.AsQueryable().Where(x => 1 == 1).ToList();//支持了转换成queryable,我们可以用queryable实现复杂功能
        List<IConditionalModel> conModels = new(); //组装条件查询作为条件实现 分页查询加排序
        conModels.Add(new ConditionalModel
        {
            FieldName = typeof(Organize).GetProperties()[0].Name,
            ConditionalType = ConditionalType.Equal, FieldValue = "1"
        }); //id=1
        var data7 = base.GetPageList(conModels, p, it => SqlFunc.GetRandom());
        AsQueryable().Where(x => 1 == 1)
            .ToList(); //支持了转换成queryable,我们可以用queryable实现复杂功能
        /*********插入*********/
        var insertData = new Organize() { };//测试参数
        var insertArray = new Organize[] { insertData };
        base.Insert(insertData);//插入
        base.InsertRange(insertArray);//批量插入
        var id = base.InsertReturnIdentity(insertData);//插入返回自增列
        base.AsInsertable(insertData).ExecuteCommand();//我们可以转成 Insertable实现复杂插入
        var insertData = new Organize(); //测试参数
        var insertArray = new[] { insertData };
        base.Insert(insertData); //插入
        base.InsertRange(insertArray); //批量插入
        var id = base.InsertReturnIdentity(insertData); //插入返回自增列
        AsInsertable(insertData).ExecuteCommand(); //我们可以转成 Insertable实现复杂插入
      /*********更新*********/
       var updateData = new Organize() {  };//测试参数
        var updateArray = new Organize[] { updateData };//测试参数
        base.Update(updateData);//根据实体更新
        base.UpdateRange(updateArray);//批量更新
        /*********更新*********/
        var updateData = new Organize(); //测试参数
        var updateArray = new[] { updateData }; //测试参数
        base.Update(updateData); //根据实体更新
        base.UpdateRange(updateArray); //批量更新
        //base.Update(it => new Organize() { ClassName = "a", CreateTime = DateTime.Now }, it => it.id==1);// 只更新ClassName列和CreateTime列,其它列不更新,条件id=1
        base.AsUpdateable(updateData).ExecuteCommand();  //转成Updateable可以实现复杂的插入
        AsUpdateable(updateData).ExecuteCommand(); //转成Updateable可以实现复杂的插入
      /*********删除*********/
       var deldata = new Organize() {  };//测试参数
        base.Delete(deldata);//根据实体删除
        base.DeleteById(1);//根据主键删除
        base.DeleteById(new int[] { 1,2});//根据主键数组删除
        base.Delete(it=>1==2);//根据条件删除
        base.AsDeleteable().Where(it=>1==2).ExecuteCommand();//转成Deleteable实现复杂的操作
        /*********删除*********/
        var deldata = new Organize(); //测试参数
        base.Delete(deldata); //根据实体删除
        base.DeleteById(1); //根据主键删除
        base.DeleteById(new[] { 1, 2 }); //根据主键数组删除
        base.Delete(it => 1 == 2); //根据条件删除
        AsDeleteable().Where(it => 1 == 2)
            .ExecuteCommand(); //转成Deleteable实现复杂的操作
    }
        #endregion
    }
    #endregion
}