ContainerService.cs 6.48 KB
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using System;
using System.Text;

namespace Hh.Mes.Service.Warehous
{
    public class ContainerService : RepositorySqlSugar<base_container>
    {
        /// <summary>
        /// 查询方法
        /// </summary>
        /// <param name="pageReq">页码</param>
        /// <param name="model">传入的参数</param>
        /// <returns></returns>
        public Response Load(PageReq pageReq, base_container model)
        {
            var result = new Response();
            string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " t1.createTime desc" : $"{pageReq.field} {pageReq.order} ";
            string sqlWhere = SqlWhere(model);
            var stringBuilder = new StringBuilder();
            //页码,页数
            //Exel ture 不分页
            if (!model.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  t2.name as warehouseName ,
		                                        t3.dictLabel as typeName ,
		                                        t4.dictLabel as isLockName ,
		                                        t5.dictLabel as statusName ,
                                                t1.*  
                                        from base_container t1 
                                        left join base_warehouse t2 on t1.warehouseCode=t2.code
                                        left join sys_dict_data t3   with(nolock)  on t1.containerType=t3.dictValue and t3.dictType='containerType'
                                        left join sys_dict_data t4   with(nolock)  on t1.IsLock=t4.dictValue and t4.dictType='IsLock'
                                        left join sys_dict_data t5   with(nolock)  on t1.status=t5.dictValue and t5.dictType='containerStatus'
                                        where {sqlWhere} order by {orderBy} ");
            //Exel ture 不分页
            if (!model.Exel)
            {
                stringBuilder.AppendLine("  offset @offset row fetch next @pageSize row only ");
                stringBuilder.Append($@" select rowTotal= count(*)  from base_container t1   with(nolock) where {sqlWhere}");
            }
            var ds = base.Context.Ado.GetDataSetAll(stringBuilder.ToString());
            result.Result = ds.Tables[0];
            result.Count = model.Exel ? (int)result.Result.Rows.Count : (int)ds.Tables[1].Rows[0]["rowTotal"];
            return result;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        public dynamic Ins(base_container model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                //var prefix = base.GetDictionaryDictValue("warehouse");
                //var maxValue = GetTableMaxValue<warehouse>("id");
                //model.code = prefix + maxValue;
                model.createBy = sysWebUser.Account;
                model.createTime = DateTime.Now;
                response.Status = Add(model);
                if (!response.Status)
                {
                    response.Message = "添加失败";
                }
                return response;
            });
        }

        /// <summary>
        /// 编辑方法
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        public dynamic Upd(base_container model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.updateBy = sysWebUser.Account;
                model.updateTime = DateTime.Now;
                response.Status = base.Context.Updateable(model).IgnoreColumns(it => new { it.createBy, it.createTime }).ExecuteCommand() > 0;
                if (!response.Status)
                {
                    response.Message = "编辑失败";
                }
                return response;
            });
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="ids">需要删除的id</param>
        /// <returns></returns>
        public dynamic DelByIds(string[] ids)
        {
            var response = new Response { Message = "" };
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var temp = Context.Deleteable<base_container>().In(x => x.containerCode, ids).ExecuteCommand() > 0;
                if (!temp)
                {
                    response.Message = "删除失败,反复出现请联系管理员";
                }
                return response;

            });
        }


        /// <summary>
        /// 批量创建容器 执行存储过程
        /// </summary>
        public dynamic BtchAdd(string type, int containerNum)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                base.Context.Ado.UseStoredProcedure().ExecuteCommand("P_Create_Container", new { type, containerNum, createBy= sysWebUser?.Account });
                return response;
            });
        }

        public string SqlWhere(base_container model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(" 1=1 ");
            if (model.containerCode != null) stringBuilder.Append($" and t1.code like '%{model.containerCode}%' ");
            return stringBuilder.ToString();
        }

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

    }
}