BaseTeamService.cs 9.47 KB
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.WebEntity;
using Hh.Mes.Service.Repository;
using NPOI.POIFS.FileSystem;
using NPOI.SS.Formula.Functions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Hh.Mes.Service.Configure
{
    /// <summary>
    /// 工厂
    /// </summary>
    public class BaseTeamService : RepositorySqlSugar<base_team>
    {
        /// <summary>
        /// 新增  code = 字典前缀+对应表标识最大值
        /// </summary>
        /// <returns></returns>
        public dynamic Ins(base_team model)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                Context.Insertable(model).ExecuteCommand();
                return response;
            });
        }

        /// <summary>
        /// 根据主键数组 删除
        /// </summary>
        public dynamic DelByIds(int[] ids)
        {
            //var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var Result = new Response { Message = "" };
                var index = 0;

                //根据id获取班组编码
                var teamCode = Context.Queryable<base_team>().In(x => x.id, ids).Select(x => x.teamCode).ToList();

                //查看班组与用户关系表是否存在,存在报错
                var Exit = Context.Queryable<base_team_user_rel>().In(x => x.teamCode, teamCode).ToList();
                if (Exit != null)
                {
                    if(Exit.Count > 0)
                    {
                        Result.Code = 500;
                        Result.Message += $"teamCode为【{teamCode[0]}】的班组已经分配了用户,不能删除.";
                        return Result;
                    }
                }
                //查看班组与工作中心关系表是否存在,存在报错
                var Exit1 = Context.Queryable<base_work_center_team_rel>().In(x => x.teamCode, teamCode).ToList();
                if (Exit1 != null)
                {
                    if (Exit1.Count > 0)
                    {
                        Result.Code = 500;
                        Result.Message += $"teamCode为【{teamCode[0]}】的班组已经被分配到了工作中心,不能删除.";
                        return Result;
                    }
                }

                var result = Context.Deleteable<base_team>().Where(x => ids.Contains(x.id)).ExecuteCommand();
                if (result <= 0)
                {
                    Result.Code = 300;
                    return Result.ResponseError(SystemVariable.dataActionError);
                }
                return Result;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(base_team model)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                Context.Updateable(model).ExecuteCommand();
                return response;
            });
        }

        /// <summary>
        /// 界面数据加载
        /// </summary>
        public dynamic Load(PageReq pageReq, base_team mode)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response<List<base_team>>();
                var expression = LinqWhere(mode);
                //先组合查询表达式
                var query = Context.Queryable<base_team>().Where(expression);
                //Exel为ture就不分页,因为导出的话是全部导出
                if (pageReq != null)
                {
                    int total = 0;
                    result.Result = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
                    result.Count = total;
                }
                else
                {
                    result.Result = query.ToList();
                    result.Count = result.Result.Count();
                }
                return result;
            }, catchRetrunValue: "list");
        }

        /// <summary>
        /// 数据过滤条件
        /// </summary>
        public Expression<Func<base_team, bool>> LinqWhere(base_team model)
        {
            try
            {
                var exp = Expressionable.Create<base_team>();
                
                if (!string.IsNullOrWhiteSpace(model.teamCode))
                {
                    exp.And(x => x.teamCode.Contains(model.teamCode));
                }
                if (!string.IsNullOrWhiteSpace(model.otherCode))
                {
                    exp.And(x => x.otherCode.Contains(model.otherCode));
                }
                if (!string.IsNullOrWhiteSpace(model.teamName))
                {
                    exp.And(x => x.teamName.Contains(model.teamName));
                }
                return exp.ToExpression();//拼接表达式
            }
            catch (Exception ex)
            {
                throw new Exception($"{ex.Message}");
            }
        }

        /// <summary>
        /// 查找班组符合条件的用户
        /// </summary>
        public dynamic GetTeamCodeUserById(string teamCode)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = base.Context.Queryable<base_team_user_rel>()
                                 .Where(x => x.teamCode == teamCode)
                                 .Select(it => new
                                 {
                                     it.userId
                                 }).ToList();
                response.Result = result;
                return response;
            });
        }

        /// <summary>
        /// 班组分配人员 删除后新增。 1个班组对应多个用户
        /// </summary>
        /// <param name="teamCode"></param>
        /// <param name="userId"></param>
        /// <param name="flag">(check选择,uncheck 取消)</param>
        public dynamic Assign(string teamCode, string userId, string flag)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                var BaseTeamCode = teamCode.Split(",")[0];
                var userIdS = userId.Split(",");
                var rels = new List<base_team_user_rel>();
                foreach (var t in userIdS)
                {
                    var model = new base_team_user_rel
                    {
                        teamCode = BaseTeamCode,
                        userId = int.Parse(t),
                        createBy = sysWebUser?.Account,
                        createTime = DateTime.Now
                    };
                    rels.Add(model);
                    Context.Deleteable<base_team_user_rel>().Where(x => x.teamCode == teamCode && x.userId == model.userId).AddQueue();
                }
                if (flag.Equals("check")) Context.Insertable(rels).AddQueue();
                response.Status = Context.SaveQueues() > 0;
                if (!response.Status)
                {
                    response.Message = SystemVariable.dataActionError;
                }
                return response;
            });
        }

        #region 明细
        /// <summary>
        /// 查询班组明细
        /// </summary>
        public Response LoadDesc(PageReq pageReq, base_team_user_rel model)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var test = new Response();
                //获取班组已添加的用户ID
                var temp = base.Context.Queryable<base_team_user_rel>()
                                 .Where(x => x.teamCode == model.teamCode).Select(it => new {it.userId}).ToList();
                List<int> list = new List<int>();
                temp.ForEach(x => list.Add(x.userId));
                //根据获取到的用户ID查询用户信息
                var result = base.Context.Queryable<sys_user>().In(x => x.id, list).ToList();
                List<base_team_user> userList = new List<base_team_user>();
                result.ForEach(x => {
                    base_team_user user = new base_team_user();
                    user.teamCode = model.teamCode;
                    user.userId = x.id;
                    user.userName = x.name;
                    userList.Add(user);
                });
                test.Result = userList;
                return test;
            }, catchRetrunValue: "list");
        }
        #endregion

        /// <summary>
        /// 数据导出 
        /// </summary>
        public dynamic ExportData(base_team entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var ds = Load(null, entity);
                if (ds == null || ds.Result.Tables[0].Rows.Count == 0)
                {
                    result.Result = "[]";
                    result.Count = 0;
                }
                else
                {
                    result.Result = ds.Result.Tables[0];
                    result.Count = result.Result.Rows.Count;
                }
                return result;
            }, catchRetrunValue: "list");
        }
    }
}