WorkshopService.cs 7.4 KB
using Hh.Mes.Common.Reflect;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using Hh.Mes.Common.Exel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.Service.Repository;
using Hh.Mes.POJO.Entity;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Response;

namespace Hh.Mes.Service.Configure
{
    /// <summary>
    /// 车间
    /// </summary>
    public class WorkshopService : RepositorySqlSugar<base_workshop>
    {
        /// <summary>
        /// //获取列表
        /// </summary>
        public Task<DataSet> GetWorkshopList(PageReq pageReq, base_workshop model)
        {
            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
                                         from [dbo].[base_workshop] t1    with(nolock)
                                         left join base_factory t2   with(nolock) on  t1.factoryCode = t2.factoryCode
                                         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_workshop  t1  with(nolock) where {sqlWhere}");
            }
            var ds = base.Context.Ado.GetDataSetAllAsync(stringBuilder.ToString(), new List<SugarParameter>(){
                new SugarParameter("@factoryCode", model.factoryCode)
            });
            return ds;
        }

        public string SqlWhere(base_workshop model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("1=1");
            if (model.factoryCode != null)
            {
                stringBuilder.Append($" and  t1.factoryCode =@factoryCode ");
            }
            return stringBuilder.ToString();
        }

        public ISqlSugarClient GetContext()
        {
            return base.Context;
        }

        /// <summary>
        /// /左侧列表  公司+工厂 ,keys=ztreeId
        /// </summary>
        public Task<DataTable> GetTreeList()
        {
            string sql = $@" select id=companyId,
                                   name,
                                   keys ='00000000-0000-0000-0000-000000000000',
                                   parentId='-1',isok='','' as factoryCode
                            from  base_company where isDelete={SystemVariable.AddOrUpdateFlag}
                            union all
                            select  id, 
                                    name= factoryName, 
                                    keys=factoryKey,
                                    parentId ='00000000-0000-0000-0000-000000000000',isok='true',factoryCode
                            from  base_factory  ";
            return base.Context.Ado.GetDataTableAsync(sql);
        }


        /// <summary>
        /// 新增  code = 字典前缀+对应表标识最大值
        /// </summary>
        /// <returns></returns>
        public dynamic Ins(base_workshop model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.createBy = sysWebUser?.Account;
                model.createTime = DateTime.Now;
                model.workShopKey = Guid.NewGuid();
                var prefix = base.GetDictionaryDictValue("base_workshop");
                var maxValue = GetTableMaxValue<base_workshop>("id");
                model.workShopCode = 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 workShop = base.GetById(id);
                    if (workShop==null)
                    {
                        index++;
                        Result.Code = 500;
                        Result.Message = "车间信息不存在,请刷新页面后重新操作!";
                        break;
                    }
                    var line = Context.Queryable<base_line>().First(u => u.workshopCode == workShop.workShopCode);
                    if (line!=null)
                    {
                        index++;
                        //提示
                        Result.Code = 500;
                        Result.Message += $"车间【{workShop.workShopName}】下面存在线体【{line.lineName}】数据,请先删除线体后再删除车间数据!  <br>";
                        break;
                    }
                }
                if (index > 0) return Result;
                var result = Context.Deleteable<base_workshop>().In(ids).ExecuteCommand() > 0;
                if (!result)
                {
                    Result.Code = 500;
                    Result.Message = SystemVariable.dataActionError;
                }
                return Result;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(base_workshop 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_workshop entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var ds = GetWorkshopList(null, entity);
                if (ds == null || ds.Result.Tables[0].Rows.Count == 0)
                {
                    result.Result = "[]";
                    result.Count = 0;
                }
                else
                {
                    result.Result = ds.Result.Tables[0];
                    result.Count = result.Result.Rows.Count;
                }
                return result;
            }, catchRetrunValue: "list");

        }
    }

}