factoryService.cs 5.78 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.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace Hh.Mes.Service.Configure
{
    /// <summary>
    /// 工厂
    /// </summary>
    public class FactoryService : RepositorySqlSugar<base_factory>
    {
        /// <summary>
        /// 新增  code = 字典前缀+对应表标识最大值
        /// </summary>
        /// <returns></returns>
        public dynamic Ins(base_factory model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.factoryKey = Guid.NewGuid();
                model.createBy = base.sysWebUser?.Account;
                model.createTime = DateTime.Now;
                var prefix = base.GetDictionaryDictValue("base_factory");
                var maxValue = GetTableMaxValue<base_factory>("id");
                model.factoryCode = prefix + maxValue;
                response.Status= Add(model);
                if (!response.Status) response.Message = "新增失败";
                return response;
            });
        }

        /// <summary>
        /// 根据主键数组 删除
        /// </summary>
        public dynamic DelByIds(Guid[] ids)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var Result = new Response {Message = ""};
                var index = 0;
                foreach (var id in ids)
                {
                    var isExist = Context.Queryable<base_workshop>().Any(u => u.factoryKey == id);//是否存在
                    if (isExist)
                    {
                        index++;
                        //提示
                        Result.Code = 500;
                        Result.Message += $"ID为【{id}】的在车间里面存在数据不能删除  <br>";
                    }
                }
                if (index > 0) return Result;
                var result = Context.Deleteable<base_factory>().In(x=>x.factoryKey,ids).ExecuteCommand() > 0;
                if (!result)
                {
                    Result.Code = 300;
                    Result.Message = SystemVariable.dataActionError;
                }
                return Result;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(base_factory model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.updateBy = sysWebUser?.Account;
                model.updateTime = DateTime.Now;
                response.Status = Update(model);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }


        public Task<DataSet> Load(PageReq pageReq, base_factory mode)
        {
            string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " id desc" : $"{pageReq.field} {pageReq.order} ";
            string sqlWhere = LoadSqlWhere(mode);
            var stringBuilder = new StringBuilder();
            //页码,页数
            //Exel ture 不分页
            if (!mode.Exel && pageReq != null)
            {
                stringBuilder.Append("declare @pageIndex int,@pageSize int,@offset int");
                stringBuilder.AppendLine($"  select @pageIndex={pageReq.page}, @pageSize={pageReq.limit}, @offset=(@pageIndex - 1) * @pageSize");
            }

            stringBuilder.AppendLine($@" select t1.*,t2.name from base_factory t1 
                                         left join base_company  t2 on t1.companyGroup=t2.companyId
                                         where {sqlWhere}
                                         order by {orderBy} ");

            //Exel false 分页
            if (!mode.Exel)
            {
                stringBuilder.AppendLine("  offset @offset row fetch next @pageSize row only ");
                stringBuilder.Append($" select rowTotal= count(*) from base_factory  t1  with(nolock) where {sqlWhere}");
            }
            return base.Context.Ado.GetDataSetAllAsync(stringBuilder.ToString(), new List<SugarParameter>(){
                new SugarParameter("@factoryCode", $"%{mode.factoryCode}%"),
                new SugarParameter("@factoryName", $"%{mode.factoryName}%"),
            });
        }

        private string LoadSqlWhere(base_factory mode)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(" 1=1 ");
            if (!string.IsNullOrEmpty(mode.factoryCode))
            {
                stringBuilder.Append(" and  t1.factoryCode like  @factoryCode");
            }
            if (!string.IsNullOrEmpty(mode.factoryName))
            {
                stringBuilder.Append(" and  t1.factoryName like  @factoryName");
            }
            return stringBuilder.ToString();
        }


        /// <summary>
        /// 导出 
        /// </summary>
        public dynamic ExportData(base_factory 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");
        }
    }
}