ProductionOrderService.cs 5.05 KB
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.WebEntity.bus;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hh.Mes.Service.Material
{
    public class ProductionOrderService : RepositorySqlSugar<dynamic>
    {
        /// <summary>
        /// 获取工单列表
        /// </summary>
        public Response GetProductionOrderList(PageReq pageReq, bus_pro_plan_head entity)
        {
            var result = new Response();
            string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " id desc" : $"{pageReq.field} {pageReq.order} ";
            string sqlWhere = SqlProductionOrderWhere(entity);
            var stringBuilder = new StringBuilder();
            //页码,页数
            //Exel ture 不分页
            if (!entity.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_pro_plan_head where {sqlWhere} order by {orderBy} ");

            //Exel ture 不分页
            if (!entity.Exel)
            {
                stringBuilder.AppendLine("  offset @offset row fetch next @pageSize row only ");
                stringBuilder.Append($@" select rowTotal= count(*) from bus_pro_plan_head where {sqlWhere}");
            }

            var parameters = new List<SugarParameter>(){
                new SugarParameter("@planCode",entity.planCode),
                new SugarParameter("@planName",entity.planName),
                new SugarParameter("@productCode",entity.productCode),
                new SugarParameter("@productName",entity.productName)
            };
            
            var ds = base.Context.Ado.GetDataSetAll(stringBuilder.ToString(), parameters);
            result.Result = ds.Tables[0];
            result.Count = entity.Exel ? (int)result.Result.Rows.Count : (int)ds.Tables[1].Rows[0]["rowTotal"];
            return result;
        }

        public string SqlProductionOrderWhere(bus_pro_plan_head model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(" 1=1 ");
            if (!string.IsNullOrEmpty(model.planCode))
            {
                stringBuilder.Append(" and  t1.planCode = @planCode ");
            }
            if (!string.IsNullOrEmpty(model.planName))
            {
                stringBuilder.Append(" and  t1.planName like '%'+@planName+'%' ");
            }
            if (!string.IsNullOrEmpty(model.productCode))
            {
                stringBuilder.Append(" and  t1.productCode = @productCode ");
            }
            if (!string.IsNullOrEmpty(model.productName))
            {
                stringBuilder.Append(" and  t1.productName like '%'+@productName+'%' ");
            }
            //下拉弹出搜索框
            //if (!string.IsNullOrEmpty(model.codeOrName))
            //{
            //    stringBuilder.Append($" and  (t1.materialCode like '%'+@codeOrName+'%' or  t1.materialName like '%'+@codeOrName+'%' )   ");
            //}
            return stringBuilder.ToString();
        }

        /// <summary>
        /// 新增工单
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public dynamic Ins(bus_pro_plan_head entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                entity.keys = Guid.NewGuid();
                entity.createBy = sysWebUser?.Account;
                entity.createTime = DateTime.Now;
                Context.Insertable(entity).ExecuteCommand();
                return response;
            });
        }

        /// <summary>
        /// 更新工单
        /// </summary>
        public dynamic Upd(bus_pro_plan_head model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.updateBy = sysWebUser?.Account;
                model.updateTime = DateTime.Now;
                response.Status = Context.Updateable(model).Where(u => u.id == model.id).ExecuteCommand() > 0;
                if (!response.Status) response.Message = "更新失败";
                return response;
            });
        }

        /// <summary>
        /// 删除工单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public dynamic DelByIds(int[] ids)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                Context.Deleteable<bus_pro_plan_head>(t => ids.Contains(t.id)).ExecuteCommand();
                return response;
            });
        }
    }
}