WarehouseService.cs 6.12 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 WarehouseService : RepositorySqlSugar<base_warehouse>
    {
        /// <summary>
        /// 查询方法
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="model">传入的参数</param>
        /// <returns></returns>
        public Response Load(PageReq page, base_warehouse model)
        {
            var result = new Response();
            var stringBuilder = new StringBuilder();
            string sqlWhere = SqlWhere(model);
            stringBuilder.Append($@"select t1.*, t2.workshopName workshopname,t3.lineName from  base_warehouse
									t1 left join base_workshop t2 on t1.workShopCode=t2.workShopCode
									left join base_line t3 on t1.lineCode=t3.lineCode where  {sqlWhere} ");
            //查询条件
            var parameters = new
            {
                name = model.name,
                code = model.code,
                workshopCode = model.workshopCode,
                lineCode = model.lineCode
            };

            var index = base.Context.Ado.GetDataTable(stringBuilder.ToString(), parameters).Rows.Count;//获取行数
            if (page != null)//分页
            {
                stringBuilder.Append($"  order by t1.id desc offset {(page.page - 1) * page.limit} row fetch next {page.limit} row only ");
            }
            var list = base.Context.Ado.GetDataTable(stringBuilder.ToString(), parameters);

            result.Result = list;
            result.Count = index;
            return result;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        public dynamic Ins(base_warehouse model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.lineId = null;
                model.workshopId = null;
                var prefix = base.GetDictionaryDictValue("base_warehouse");
                var maxValue = GetTableMaxValue<base_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_warehouse 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 index = 0;
                foreach (var code in ids)
                {
                    var isExit = Context.Queryable<base_location>().Any(u => u.warehouseCode == code);//是否存在
                    if (isExit)
                    {
                        index++;
                        //提示
                        response.Code = 300;
                        response.Message += $"仓库:{code}在库存里面存在数据不能删除  <br>";
                    }
                }
                if (index > 0) return response;
                var temp = Context.Deleteable<base_warehouse>().In(x => x.code, ids).ExecuteCommand() > 0;
                if (!temp)
                {
                    response.Message = "删除失败,反复出现请联系管理员";
                }
                return response;

            });
        }



        public string SqlWhere(base_warehouse model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("1=1");
            if (!string.IsNullOrWhiteSpace(model.name))
            {
                stringBuilder.Append($" and t1.name like '%'+@name+'%' ");
            }

            if (!string.IsNullOrWhiteSpace(model.code))
            {
                stringBuilder.Append($" and t1.code like '%'+@code+'%' ");
            }
            if (!string.IsNullOrWhiteSpace(model.workshopCode))
            {
                stringBuilder.Append($" and t1.workshopCode like '%'+@workshopCode+'%' ");
            }
            if (!string.IsNullOrWhiteSpace(model.lineCode))
            {
                stringBuilder.Append($" and t1.lineCode like '%'+@lineCode+'%' ");
            }
            return stringBuilder.ToString();
        }



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