MaterialDistributeLoadHeaderService.cs 8.33 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.Text;
using System.Threading.Tasks;

namespace Hh.Mes.Service.Distribution
{
    /// <summary>
    /// 组焊工单 (组焊清单)
    /// </summary>
    public class MaterialDistributeLoadHeaderService : RepositorySqlSugar<bus_material_distribute_load_header>
    {
        /// <summary>
        /// //获取列表
        /// </summary>
        public Response Load(PageReq pageReq, bus_material_distribute_load_header 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  * 
                                         from bus_workOrder_head  with(nolock)
                                         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 bus_material_distribute_load_header    with(nolock) where {sqlWhere}");
            }
            var ds = base.Context.Ado.GetDataSetAll(stringBuilder.ToString(), new List<SugarParameter>(){
                //new SugarParameter("@productHeaderCode",model.productHeaderCode),
                //new SugarParameter("@workOrderCode", model.workOrderCode),
                //new SugarParameter("@otherOrderCode", model.otherOrderCode),
                //new SugarParameter("@planStartTime", model.planStartTime),
                //new SugarParameter("@planEndTime", model.planEndTime),
            });
            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(bus_material_distribute_load_header model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("1=1");
            if (!string.IsNullOrWhiteSpace(model.id.ToString())) stringBuilder.Append(" and  productHeaderCode like  '%'+@productHeaderCode+'%' ");
            return stringBuilder.ToString();
        }


        /// <summary>
        /// //获取列表
        /// </summary>
        public Response LoadDesc(PageReq pageReq, bus_material_distribute_load_detail model)
        {
            var data = model.headKeyList != null
                ? base.Context.Queryable<bus_material_distribute_load_detail>().In(it => it.headKeys, model.headKeyList).ToList()
                : base.Context.Queryable<bus_material_distribute_load_detail>().Where(it => it.headKeys == model.headKeys).ToList();
            var result = new Response
            {
                Code = 0,
                Result = data,
                Count = data.Count
            };
            return result;
        }

        /// <summary>
        /// 新增
        /// </summary>
        public dynamic Ins(bus_material_distribute_load_header model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.createBy = base.sysWebUser?.Account;
                model.createTime = DateTime.Now;
                var result =Add(model);
                if (!result) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(bus_material_distribute_load_header model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.updateBy = base.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 = SystemVariable.dataActionError;
                return response;
            });

        }

        /// <summary>
        /// 删除
        /// </summary>
        public dynamic DelByIds(Guid[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Context.Deleteable<bus_material_distribute_load_header>().In(ids).AddQueue();
                foreach (var id in ids)
                {
                    Context.Deleteable<bus_material_distribute_load_detail>().Where(it => it.headKeys == id).AddQueue();
                }
                var result = Context.SaveQueues();
                if (result<0) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }


        #region 明细

        /// <summary>
        /// 新增
        /// </summary>
        public dynamic InsDesc(bus_material_distribute_load_detail model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.createBy = base.sysWebUser?.Account;
                model.createTime = DateTime.Now;
                response.Status = Context.Insertable(model).ExecuteCommand() > 0;
                if (!response.Status)
                {
                    response.Message = SystemVariable.dataActionError;
                }
                return response;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic UpdDesc(bus_material_distribute_load_detail model)
        {

            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Context.Updateable(model).IgnoreColumns(x => new { x.createTime, x.createBy }).AddQueue();
                var result = Context.SaveQueues();
                if (result <= 0) return response.ResponseError();
                return response;

            });

        }



        /// <summary>
        /// 删除
        /// </summary>
        public dynamic DelByIdsDesc(int[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                response.Status = Context.Deleteable<bus_material_distribute_load_detail>().In(ids).ExecuteCommand() > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }
        #endregion


        /// <summary>
        /// 导出方法
        /// </summary>
        /// <returns></returns>
        public Task<dynamic> Export(bus_material_distribute_load_header Table_Entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var dataTable = Load(null, Table_Entity);
                if (dataTable.Count == 0)
                {
                    result.Result = null;
                    result.Message = "没有查询到数据!";
                    result.Code = -1;
                    return result;
                }
                var guids = new List<Guid>();
                foreach (var item in dataTable.Result)
                {
                    guids.Add((Guid)item.keys);
                }
                var data = LoadDesc(null, new bus_material_distribute_load_detail { headKeyList = guids });
                result.Result = new
                {
                    head = dataTable.Result,
                    body = data.Result
                };
                return result;
            });
        }

    }
}