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

namespace Hh.Mes.Service.Configure
{
    /// <summary>
    /// 焊接工艺
    /// </summary>
    public class WeldTechnologyService : RepositorySqlSugar<base_weld_technology_head>
    {
        #region 焊口清单
        /// <summary>
        /// //获取列表
        /// </summary>
        public Task<DataSet> GetWeldTechnologyHeadList(PageReq pageReq, base_weld_technology_head model)
        {
            string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " id desc" : $"{pageReq.field} {pageReq.order} ";
            var sqlWhere = SqlWhereWeldTechnologyHead(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.url ,t2.fileName  from base_weld_technology_head t1 
                                          Left JOIN sys_File t2 ON ( [t1].[id] =[t2].[targetId]) and   t2.targetTableName='weld_technology_head'
                                          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_weld_technology_head  t1  with(nolock) where {sqlWhere}");
            }
            return Context.Ado.GetDataSetAllAsync(stringBuilder.ToString());
        }

        /// <summary>
        /// //获取列表
        /// </summary>
        public Response GetWeldTechnologyBodyList(PageReq pageReq, base_weld_technology_detail model)
        {
            var data = Context.Queryable<base_weld_technology_detail>().Where(it => it.TechnologyHeadId == model.id).ToList();
            var result = new Response
            {
                Result = data,
                Count = data.Count
            };
            return result;
        }


        public string SqlWhereWeldTechnologyHead(base_weld_technology_head model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(" 1=1 ");
            if (model.MaterialCode != null)
            {
                stringBuilder.Append($" and  t1.materialCode like '%{model.MaterialCode}%' ");
            }
            if (model.WeldingWire != null)
            {
                stringBuilder.Append($" and  t1.weldingWire like '%{model.WeldingWire}%' ");
            }
            return stringBuilder.ToString();
        }


        /// <summary>
        /// 新增
        /// </summary>
        public dynamic Ins(base_weld_technology_head model, IFormFileCollection file, IHostingEnvironment environment)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.createBy = base.sysWebUser?.Account;
                model.createTime = DateTime.Now;

                var result = base.Context.Insertable(model).ExecuteReturnIdentity();
                if (result > 0)
                {
                    if (model.isFileChange)
                    {
                        var sys_File = new sys_File
                        {
                            targetTableName = "weld_technology_head",
                            targetId = result
                        }; 
                        List<sys_File> sysFiles = new List<sys_File>();
                        sysFiles.Add(sys_File);
                        UploadFile(file, environment, sysFiles);
                    }
                }
                else
                {
                    response.Message = "添加失败";
                }
                return response;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(base_weld_technology_head model, IFormFileCollection file, IHostingEnvironment environment)
        {
            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 (model.isFileChange)
                {
                    var sys_File = new sys_File
                    {
                        targetTableName = "weld_technology_head",
                        targetId = model.id
                    };
                    List<sys_File> sysFiles = new List<sys_File>();
                    sysFiles.Add(sys_File);
                    UploadFile(file, environment, sysFiles);
                    Context.Deleteable<sys_File>().Where(u => u.targetId == model.id && u.targetTableName == "weld_technology_head").ExecuteCommand();
                }
                if (!response.Status)
                {
                    response.Message = "编辑失败";
                }
                return response;
            });

        }

        /// <summary>
        /// 删除
        /// </summary>
        public dynamic DelByIds(int[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Context.Deleteable<base_weld_technology_head>().In(ids).AddQueue();
                foreach (var id in ids)
                {
                    Context.Deleteable<base_weld_technology_detail>().Where(it => it.TechnologyHeadId == id).AddQueue();
                }
                var result = Context.SaveQueues();
                if (result <= 0)
                {
                    response.Code = 400;
                    response.Status = false;
                    response.Message = "删除失败";
                }
                return response;
            });
        }


        #region 明细
        /// <summary>
        /// 新增
        /// </summary>
        public dynamic InsDesc(base_weld_technology_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 = "添加失败";
                }
                return response;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic UpdDesc(base_weld_technology_detail 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 = "编辑失败";
                }
                return response;
            });

        }

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


        public base_weld_technology_head GetPrintInfo(int id)
        {

            var weld = base.Context.Queryable<base_weld_technology_head>().Where(x => x.id == id).First();
            if (weld == null)
            {
                return null;
            }
            var detail = base.Context.Queryable<base_weld_technology_detail>().Where(x => x.TechnologyHeadId == weld.id).ToList();
            if (detail==null)
            {
                return weld;
            }
            weld.detail = detail;
            return weld;
        }
        #endregion
    }
}