InventoryService.cs 6.17 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Hh.Mes.Service.Warehous
{
    public class InventoryService : RepositorySqlSugar<base_inventory>
    {
        /// <summary>
        /// 查询方法
        /// </summary>
        /// <param name="pageReq">页码</param>
        /// <param name="model">传入的参数</param>
        /// <returns></returns>
        public Response Load(PageReq pageReq, base_inventory model)
        {
            var result = new Response();
            string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " id 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 t1.*,t2.factoryName,t3.lineName,t4.name warehouseName,t5.MaterialName materialName,
                                        t6.name materialTypeName from base_inventory t1 
                                        left join base_factory t2 on t1.factoryCode=t2.factoryCode 
                                        left join base_line t3 on t1.lineCode=t3.lineCode 
                                        left join base_warehouse t4 on t1.warehouseCode=t4.code 
                                        left join base_material t5 on t1.materialCode=t5.MaterialCode
                                        left join base_material_type t6 on t1.materialType=t6.code
                                        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_inventory 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;
        }


        public string SqlWhere(base_inventory model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(" 1=1 ");
            //stringBuilder.Append($" and t1.useState={(int)InventoryUseState.未套料} and t1.oddments=1 and t1.qty>0 and t5.MaterialCategory='PIPE'");
            if (model.id != 0) stringBuilder.Append($" and t1.id=${model.id}");
            if (model.lineCode != null) stringBuilder.Append($" and t1.lineCode like '%{model.lineCode}%' ");
            if (model.locationCode != null) stringBuilder.Append($" and t1.locationCode like '%{model.locationCode}%' ");
            if (model.materialCode != null) stringBuilder.Append($" and t1.materialCode like '%{model.materialCode}%' ");
            //if (model.oddments != null) stringBuilder.Append($" and t1.oddments = 1 ");
            if (!string.IsNullOrEmpty(model.pipeSN)) stringBuilder.Append($" and t1.pipeSN  like '%{model.pipeSN}%'");
            if (!string.IsNullOrEmpty(model.zoneCode)) stringBuilder.Append($" and t1.zoneCode ='{model.zoneCode}'");
            return stringBuilder.ToString();
        }


        /// <summary>
        /// 库存盘点
        /// </summary>
        /// <returns></returns>
        public dynamic InventoryCheck(List<base_inventory> model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.ForEach(x =>
                {
                    x.updateBy = sysWebUser?.Account;
                    x.updateTime = DateTime.Now;
                });

                #region 提交的数据管材SN是否有重复数据,提交的管材sn是否存在数据库
                var fields = model.Select(x => x.pipeSN).Distinct();
                var pipeSNArr = fields as string[] ?? fields.ToArray();
                var strPipeSn = string.Join(",", pipeSNArr);
                if (pipeSNArr.Length != model.Count) return response.ResponseError("库存盘点:提交的数据管材SN存在重复的数据:" + strPipeSn);
                //var pipeSnList = base.Context.Queryable<inventory>().Where(it => pipeSNArr.Contains(it.pipeSN)).Select(x => x.pipeSN).ToList();
                //if (pipeSnList.Count > 0)
                //{
                //    return response.ResponseError("库存盘点:管材SN数据库已存在重复的SN:" + strPipeSn);
                //} 
                #endregion

                var result = base.Context.Updateable(model).UpdateColumns(x => new
                {
                    x.qty,
                    x.pipeLength,

                    x.locationCode,
                    //x.furnaceNo,
                    //x.lotNo,
                    x.batchNo
                }).ExecuteCommand();
                if (result <= 0) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

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

    }
}