StationUserRelService.cs 5.09 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 System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace Hh.Mes.Service.Configure
{
    /// <summary>
    /// 工位人员
    /// </summary>
    public class StationUserRelService : RepositorySqlSugar<base_station_user_rel>
    {
        /// <summary>
        /// //获取  列表
        /// </summary>
        public Task<DataSet> Load(PageReq pageReq, base_station_user_rel 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.id,t2.lineName,workStationCode ,workStationName,t1.createBy,t1.createTime,t1.updateBy,t1.updateTime
                                         ,stuff((select ','+t5.name from  base_station_user_rel t3
                                         join sys_user t5 on t3.userId=t5.id
                                         where t1.id=t3.stationId  for xml path('')),1,1,'')  peopleStation
                                          from  base_work_station t1 left join  base_line  t2 on t1.lineCode=t2.lineCode
                                         where {sqlWhere}
                                         order by {orderBy} ");

            //Exel false 分页
            if (!model.Exel)
            {
                stringBuilder.AppendLine("  offset @offset row fetch next @pageSize row only ");
                stringBuilder.Append($" select rowTotal= count(*) from  base_work_station t1 left join  base_line  t2 on t1.lineCode=t2.lineCode where {sqlWhere}");
            }
            return base.Context.Ado.GetDataSetAllAsync(stringBuilder.ToString());
        }

        public string SqlWhere(base_station_user_rel model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("1=1");
            if (!string.IsNullOrEmpty(model.lineName))
            {
                stringBuilder.Append($" and  t2.lineName like '%{model.lineName}%' ");
            }
            if (!string.IsNullOrEmpty(model.name))
            {
                stringBuilder.Append($" and  t1.name like '%{model.name}%' ");
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// 查找工位符合条件的用户
        /// </summary>
        public dynamic GetStationUserById(int stationId)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = base.Context.Queryable<base_station_user_rel>()
                                 .Where(x => x.stationId == stationId)
                                 .Select(it => new
                                 {
                                    it.userId
                                 }).ToList();
                response.Result = result;
                return response;
            });
        }

        /// <summary>
        /// 工位分配人员 删除后新增。 1个工位对应过个用户
        /// </summary>
        /// <param name="stationId"></param>
        /// <param name="userId"></param>
        /// <param name="flag">(check选择,uncheck 取消)</param>
        public dynamic Assign(string stationId ,string userId, string flag)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                var tempStationId = int.Parse(stationId.Split(",")[0]);
                var userIdS = userId.Split(",");
                var rels = new List<base_station_user_rel>();
                foreach (var t in userIdS)
                {
                    var model = new base_station_user_rel
                    {
                        stationId = tempStationId,
                        userId = int.Parse(t),
                        createBy = sysWebUser?.Account,
                        createTime = DateTime.Now
                    };
                    rels.Add(model);
                    Context.Deleteable<base_station_user_rel>().Where(x => x.stationId == tempStationId && x.userId == model.userId).AddQueue();
                }
                if (flag.Equals("check")) Context.Insertable(rels).AddQueue();
                response.Status = Context.SaveQueues() > 0;
                if (!response.Status)
                {
                    response.Message = SystemVariable.dataActionError;
                }
                return response;
            });
        }
    }
}