EquipmentService.cs 8.04 KB
using Hh.Mes.Common.log;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hh.Mes.Common.Request;
using Hh.Mes.Service.Repository;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;

namespace Hh.Mes.Service.Equipment
{
    public class EquipmentService : RepositorySqlSugar<base_equipment>
    {
        /// <summary>
        /// 查询方法
        /// </summary>
        /// <param name="pageReq">页码</param>
        /// <param name="model">传入的参数</param>
        /// <returns></returns>
        public Response Load(PageReq pageReq, base_equipment 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.workStationName stationName,t3.name equipmentTypeName
                                         from base_equipment t1   with(nolock)
                                         left join base_work_station t2   with(nolock) on  t1.workStationCode = t2.workStationCode
                                         left join base_equipment_type t3 on t1.equipmentTypeId=t3.id
                                         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_equipment 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_equipment model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("1=1");
            if (model.name != null) stringBuilder.Append($" and  t1.name like '%{model.name}%' ");
            if (model.code != null) stringBuilder.Append($" and  t1.code like '%{model.code}%' ");
            if (model.workStationCode != null) stringBuilder.Append($" and  t1.workStationCode ='{model.workStationCode}' ");
            return stringBuilder.ToString();
        }

        public base_equipment SelectAllId(base_equipment model)
        {
            return null;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        public dynamic Ins(base_equipment model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var prefix = base.GetDictionaryDictValue("equipment");
                var maxValue = GetTableMaxValue<base_equipment>("id");
                //model.code = prefix + maxValue;
                model.createBy = sysWebUser?.Account;
                model.createTime = DateTime.Now;
                SelectAllId(model);
                response.Status = Add(model);
                if (!response.Status)
                {
                    response.Message = "添加失败";
                }
                return response;
            });
        }
        /// <summary>
        /// 编辑方法
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>

        public dynamic Upd(base_equipment model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.updateBy = sysWebUser?.Account;
                model.updateTime = DateTime.Now;
                SelectAllId(model);
                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(int[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                response.Status = Context.Deleteable<base_equipment>().In(ids).ExecuteCommand() > 0;
                if (!response.Status)
                {
                    response.Message = "删除失败";
                }
                return response;

            });
        }

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


        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="excelfile"></param>
        /// <param name="hostingEnvironment"></param>
        /// <returns></returns>
        public dynamic ImportIn(IFormFile excelfile, IHostingEnvironment hostingEnvironment)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Response result = new Response();

                return result;
            });
        }

        public dynamic GetStationList()
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Dictionary<string, dynamic> pairs = new Dictionary<string, dynamic>();
                Dictionary<string, string> groupData = new Dictionary<string, string>();
                var allList = Context.Queryable<base_line, base_work_station>((t1, t2) => new JoinQueryInfos(JoinType.Left, t1.lineCode == t2.lineCode)).
                Select((t1, t2) => new { t1.id, t1.lineName, t1.lineCode, t2.workStationCode, t2.workStationName }).ToList();
                var lineDatas = allList.Select(x => new
                {
                    code = x.lineCode,
                }).Distinct().ToList();
                var groupCount = lineDatas.Count();
                foreach (var lineData in lineDatas)
                {
                    var lineStationData = allList.Where(x => x.lineCode == lineData.code).ToList();
                    pairs.Add(lineData.code, lineStationData);

                    var lineName = lineStationData.FirstOrDefault()?.lineName;
                    groupData.Add(lineData.code, lineName);
                }
                var result = new
                {
                    code = 200,
                    status = true,
                    msg = "ok",

                    data = new
                    {
                        groupCount = groupCount,
                        groupData = groupData,
                        data = pairs
                    }
                };
                return result;
            });
        }


    }
}