ReworkCheckService.cs 5.02 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 SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Hh.Mes.Service.Quality
{
    /// <summary>
    /// 报检报工
    /// </summary>
    public class ReworkCheckService : RepositorySqlSugar<bus_work_report_check_body>
    {
        /// <summary>
        /// //获取列表
        /// </summary>
        public dynamic Load(PageReq pageReq, bus_work_report_check_body entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " id desc" : $"{pageReq.field} {pageReq.order} ";
                var sqlWhere = SqlWhereReworkCheck(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 t1.*  
                                        from {nameof(bus_work_report_check_body)} t1 
                                        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 {nameof(bus_work_report_check_body)} t1 with(nolock)
                                         where {sqlWhere}");
                }
                var parameters = new List<SugarParameter>{
                    new SugarParameter("@headCode",entity.headCode),
                    new SugarParameter("@rFINo",entity.rFINo),
                    new SugarParameter("@state",entity.state)
                };
                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;
            }, catchRetrunValue: "list");
        }

        public string SqlWhereReworkCheck(bus_work_report_check_body entity)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(" 1=1 ");
            if (!string.IsNullOrEmpty(entity.headCode)) stringBuilder.Append(" and  t1.headCode like '%'+@headCode+'%' ");
            if (!string.IsNullOrEmpty(entity.rFINo)) stringBuilder.Append(" and  t1.rFINo like '%'+@rFINo+'%' ");
            if (entity.state!=null) stringBuilder.Append(" and  t1.state =@state ");
            return stringBuilder.ToString();
        }

        /// <summary>
        /// //获取明细列表 支持导出查询
        /// </summary>
        public Response LoadDesc(PageReq pageReq, bus_work_report_check_body model)
        {
            var totalCount = 0;
            var result = new Response();
            var query = base.Context.Queryable<bus_work_report_check_footer>();
            var data = model.headkeysList != null ? query.In(x => x.bodyKeys, model.headkeysList).ToList() :
                       query.Where(x => x.bodyKeys == model.bodyKeys).OrderBy(x => x.id).ToOffsetPage(pageReq.page, pageReq.limit, ref totalCount);
            //页码,页数
            result.Result = data;
            result.Count = totalCount;
            return result;
        }

        /// <summary>
        /// 导出 
        /// </summary>
        public dynamic Export(bus_work_report_check_body entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Response result = Load(null, entity);
                if (result.Count == 0|| result.Result==null)
                {
                    result.Result = null;
                    result.Message = SystemVariable.queryNotData;
                    result.Code = -1;
                    return result;
                }
                var guids = new List<Guid>();
                DataTable dt = result.Result;
                foreach (DataRow item in dt.Rows)
                {
                    guids.Add((Guid)item["bodyKeys"]);
                }
                var data = LoadDesc(null, new bus_work_report_check_body { headkeysList = guids });
                result.Result = new
                {
                    head = result.Result,
                    body = data.Result
                };
                return result;
            }, catchRetrunValue: "list");
        }
    }
}