LineService.cs 7.79 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 Microsoft.AspNetCore.Mvc.Rendering;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
 

namespace Hh.Mes.Service.Configure
{
    /// <summary>
    /// 线体
    /// </summary>
    public class LineService : RepositorySqlSugar<base_line>
    {
        public ISqlSugarClient context { get; set; }

        public LineService()
        {
            context = Context;
        }

        /// <summary>
        /// //获取列表
        /// </summary>
        public Response GetLineList(PageReq pageReq, base_line model)
        {
            var result = new Response();
            if (model.workshopCode == null) model.workshopCode = null;
            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.workshopName
                                         from [dbo].base_line t1    with(nolock)
                                         left join base_workshop t2   with(nolock) on  t1.workshopCode = t2.workshopCode
                                         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_line  t1  with(nolock) where {sqlWhere}");
            }
            var ds = base.Context.Ado.GetDataSetAll(stringBuilder.ToString(), new List<SugarParameter>(){
                new SugarParameter("@workshopCode", model.workshopCode)
            });
            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_line model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("1=1");
            if (model.workshopCode != null)
            {
                stringBuilder.Append(" and  t1.workShopCode = @workshopCode ");
            }
            return stringBuilder.ToString();
        }


        /// <summary>
        /// /左侧列表 公司+ 工厂++车间
        /// </summary>
        public DataTable GetTreeList()
        {
            string sql = $@"select id=companyId,
                                   name,
                                   keys ='00000000-0000-0000-0000-000000000000',
                                   parentId=null,isok='','' as  workShopCode
                            from  base_company where isDelete={SystemVariable.AddOrUpdateFlag}
                            union all
                            select  id, 
                                    factoryName= factoryName, 
                                    keys=factoryKey,
                                    parentId ='00000000-0000-0000-0000-000000000000',isok='','' as  workShopCode
                            from  base_factory 
							 union all

							select id,workshopName,keys=workShopKey,
							       parentId=(select factoryKey from base_factory  t1 where t1.factoryKey=base_workshop.factoryKey  ),
								   isok='true',workShopCode
							from base_workshop";
            var dt = base.Context.Ado.GetDataTable(sql);
            return dt == null || dt.Rows.Count == 0 ? null : dt;
        }


        /// <summary>
        /// 新增  code = 字典前缀+对应表标识最大值
        /// </summary>
        /// <returns></returns>
        public dynamic Ins(base_line model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.createBy = sysWebUser?.Account;
                model.createTime = DateTime.Now;
                model.lineKey = Guid.NewGuid();
                var prefix = base.GetDictionaryDictValue("base_line");
                var maxValue = GetTableMaxValue<base_line>("id");
                model.lineCode = prefix + maxValue;
                response.Status = Add(model);
                if (!response.Status)response.Message = SystemVariable.dataActionError;
                return response;
            });
        }


        /// <summary>
        /// 根据主键数组 删除
        /// </summary>
        public dynamic DelByIds(int[] ids)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Response Result = new Response();
                Result.Message = "";
                var index = 0;
                foreach (var id in ids)
                {
                    var line = context.Queryable<base_line>().First(u=>u.id== id);
                    var isExit = Context.Queryable<base_work_station>().Any(u => u.lineCode == line.lineCode);//是否存在
                    if (isExit)
                    {
                        index++;
                        //提示
                        Result.Code = 500;
                        Result.Message += $" ID为【{id}】在工位里面存在数据不能删除  <br>";
                    }
                }
                if (index > 0) return Result;
                var result = Context.Deleteable<base_line>().In(ids).ExecuteCommand() > 0;
                if (!result)
                {
                    Result.Code = 500;
                    Result.Message = SystemVariable.dataActionError;
                }
                return Result;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(base_line model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.updateBy = sysWebUser?.Account;
                model.updateTime = DateTime.Now;
                response.Status = Update(model);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }


        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public dynamic ExportData(base_line entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var ds = GetLineList(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");
        }


        public List<SelectListItem> GetLineList()
        { 
           var lineList = base.GetList().Select(x => new SelectListItem
             {
                 Value = x.lineCode,
                 Text = x.lineName
             }).ToList();
            lineList.Insert(0, new SelectListItem() { Value = "all", Text = "全部" });
            return lineList;
        }
    }
}