UpstreamSendUser.cs 3.22 KB
using Hh.Mes.Common;
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.ApiEntity;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using System;
using static Microsoft.AspNetCore.Hosting.Internal.HostingApplication;

namespace Hh.Mes.Service.ApiService
{
    public partial class UpstreamService
    {
        public dynamic SendUser(SysUserEntity entity)
        {
            var response = new ResponseUpstream<string>(entity.plmeid);
            var user = new sys_user
            {
                account = entity.code,
                plmeid = entity.plmeid,
                name = entity.name,
                sex = entity.gender == "男" ? 1 : 0,
                password = Encryption.Encrypt("123456"),

                otherCode = entity.code,
                phoneNumber = entity.tel,
                remarks = entity.remarks,
                status = 1,
            };
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                int resultCount = 0;
                if (entity.type == EnumAction.I.ToString())
                {
                    if (Context.Queryable<sys_user>().Any(t => t.plmeid == user.plmeid && t.status == 1))
                    {
                        return response.ResponseError($"【MOM】【name,plmeid】用户信息[{entity.name},{entity.plmeid}]已经存在,请勿重复添加!");
                    }
                    user.createTime = DateTime.Now;
                    resultCount = Context.Insertable(user).ExecuteCommand();
                    if (resultCount > 0)
                    {
                        var deptId = Context.Queryable<sys_dept>().Select(x => x.id).First();
                        var userId = Context.Queryable<sys_user>().First(x => x.plmeid == user.plmeid && x.status == 1).id;
                        var relevance = new sys_relevance
                        {
                            relKey = "UserOrg",
                            firstId = userId,
                            secondId = deptId,
                            createTime = DateTime.Now,
                            createBy = entity.code,
                        };
                        Context.Insertable(relevance).ExecuteCommand();
                    }
                }
                else if (entity.type == EnumAction.U.ToString())
                {
                    user.updateTime = DateTime.Now;
                    resultCount = Context.Updateable(user)
                    .IgnoreColumns(it => new { it.createBy, it.createTime })
                    .Where(x => x.plmeid == entity.plmeid && x.status == 1).ExecuteCommand();
                }
                else if (entity.type == EnumAction.D.ToString())
                {
                    user.updateTime = DateTime.Now;
                    resultCount = Context.Updateable<sys_user>()
                                             .SetColumns(t => t.status == 0)
                                             .Where(x => x.plmeid == entity.plmeid && x.status == 1)
                                             .ExecuteCommand();
                }
                return resultCount > 0 ? response.ResponseSuccess() : response.ResponseError();
            });
        }
    }
}