using cylsg.Core.Attributes;
using cylsg.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cylsg.Application.Users.Dtos;
using cylsg.Model.UserModel;
using System.Security.Claims;
using System.Runtime.Intrinsics.Arm;
using MapsterMapper;
namespace cylsg.Application.Users
{
///
/// 用户接口
///
[Authorize]
public class UserAppService : IDynamicApiController
{
private readonly ISystemService _systemService;
private ISqlSugarClient _sqlSugarClient;
private IMapper _mapper;
public UserAppService(ISystemService systemService, ISqlSugarClient sqlSugarClient, IMapper mapper)
{
_systemService = systemService;
_sqlSugarClient = sqlSugarClient;
_mapper = mapper;
}
///
/// 查询当前用户
///
///
public async Task getUser()
{
var UserID = App.User?.FindFirstValue("UserID");
int userid = 0;
if (!string.IsNullOrEmpty(UserID))
{
userid = int.Parse(UserID);
}
UserDto userDto = new UserDto();
var userRes = new BaseRepository();
var userWorkerRes = new BaseRepository();
var userCompanyRes = new BaseRepository();
var user = await userRes.GetByIdAsync(userid);
userDto = _mapper.Map(user);
var userWorker = await userWorkerRes.GetFirstAsync(p=>p.UserId== userid);
if (userWorker != null)
{
userDto.userWorker = _mapper.Map(userWorker);
}
var userCompany = await userCompanyRes.GetFirstAsync(p => p.UserId == userid);
if (userCompany != null)
{
userDto.userCompany = _mapper.Map(userCompany);
}
return userDto;
}
///
/// 保存用户工人信息
///
///
public async Task saveUserWorker(UserWorkerDto userWorkerDto)
{
var UserID = App.User?.FindFirstValue("UserID");
int userid = 0;
string NickName = App.User?.FindFirstValue("NickName");
if (!string.IsNullOrEmpty(UserID))
{
userid = int.Parse(UserID);
}
if (!string.IsNullOrEmpty(userWorkerDto.IdCardFace) && userWorkerDto.IdCardFace.Length > 500)
{
throw Oops.Oh("身份证正面路径长度过长!");
}
if (!string.IsNullOrEmpty(userWorkerDto.IdCardBack) && userWorkerDto.IdCardBack.Length > 500)
{
throw Oops.Oh("身份证反面路径长度过长!");
}
if (!string.IsNullOrEmpty(userWorkerDto.name) && userWorkerDto.name.Length > 100)
{
throw Oops.Oh("姓名长度过长!");
}
if (!string.IsNullOrEmpty(userWorkerDto.Phone) && userWorkerDto.Phone.Length > 30)
{
throw Oops.Oh("电话号码长度过长!");
}
if (!string.IsNullOrEmpty(userWorkerDto.IdCode) && userWorkerDto.IdCode.Length > 30)
{
throw Oops.Oh("身份证号长度过长!");
}
if (!string.IsNullOrEmpty(userWorkerDto.Address) && userWorkerDto.Address.Length > 500)
{
throw Oops.Oh("联系地址长度过长!");
}
if (!string.IsNullOrEmpty(userWorkerDto.Resume) && userWorkerDto.Resume.Length > 2000)
{
throw Oops.Oh("简历描述长度过长!");
}
var userWorkerRes = new BaseRepository();
bool res;
if (userWorkerDto.Id > 0)
{
var userWorker1 = await userWorkerRes.GetByIdAsync(userWorkerDto.Id);
userWorker1.IdCardBack = userWorkerDto.IdCardBack;
userWorker1.IdCardFace = userWorkerDto.IdCardFace;
userWorker1.name = userWorkerDto.name;
userWorker1.Phone = userWorkerDto.Phone;
userWorker1.IdCode = userWorkerDto.IdCode;
userWorker1.Address = userWorkerDto.Address;
userWorker1.Resume = userWorkerDto.Resume;
userWorker1.UpDataBy = NickName;
userWorker1.UpDataTime = DateTime.Now;
res = await userWorkerRes.UpdateAsync(userWorker1);
}
else
{
var userWorker = _mapper.Map(userWorkerDto);
userWorker.UpDataBy = NickName;
userWorker.UpDataTime = DateTime.Now;
userWorker.CreateBy = NickName;
userWorker.CreateTime = DateTime.Now;
userWorker.UserId = userid;
res = await userWorkerRes.InsertAsync(userWorker);
}
return res;
}
///
/// 保存用户公司信息
///
///
public async Task saveUserCompany(UserCompanyDto userCompanyDto)
{
var UserID = App.User?.FindFirstValue("UserID");
int userid = 0;
string NickName = App.User?.FindFirstValue("NickName");
if (!string.IsNullOrEmpty(UserID))
{
userid = int.Parse(UserID);
}
if(!string.IsNullOrEmpty(userCompanyDto.BusinessLicense) && userCompanyDto.BusinessLicense.Length > 500)
{
throw Oops.Oh("营业执照路径长度过长!");
}
if (!string.IsNullOrEmpty(userCompanyDto.Suppliername) && userCompanyDto.Suppliername.Length > 100)
{
throw Oops.Oh("企业名称长度过长!");
}
if (!string.IsNullOrEmpty(userCompanyDto.Regtime) && userCompanyDto.Regtime.Length > 50)
{
throw Oops.Oh("注册时间长度过长!");
}
if (!string.IsNullOrEmpty(userCompanyDto.Address) && userCompanyDto.Address.Length > 500)
{
throw Oops.Oh("联系地址长度过长!");
}
if (!string.IsNullOrEmpty(userCompanyDto.Suppliercode) && userCompanyDto.Suppliercode.Length > 30)
{
throw Oops.Oh("企业注册号长度过长!");
}
if (!string.IsNullOrEmpty(userCompanyDto.Contact) && userCompanyDto.Contact.Length > 50)
{
throw Oops.Oh("联系人长度过长!");
}
if (!string.IsNullOrEmpty(userCompanyDto.Phone) && userCompanyDto.Phone.Length > 50)
{
throw Oops.Oh("联系电话长度过长!");
}
if (!string.IsNullOrEmpty(userCompanyDto.Resume) && userCompanyDto.Resume.Length > 2000)
{
throw Oops.Oh("公司简介长度过长!");
}
var userCompanyRes = new BaseRepository();
bool res;
if (userCompanyDto.Id > 0)
{
var userCompany1 = await userCompanyRes.GetByIdAsync(userCompanyDto.Id);
userCompany1.BusinessLicense = userCompanyDto.BusinessLicense;
userCompany1.Suppliername = userCompanyDto.Suppliername;
userCompany1.Regtime = userCompanyDto.Regtime;
userCompany1.Address = userCompanyDto.Address;
userCompany1.Suppliercode = userCompanyDto.Suppliercode;
userCompany1.Contact = userCompanyDto.Contact;
userCompany1.Phone = userCompanyDto.Phone;
userCompany1.Resume = userCompanyDto.Resume;
userCompany1.UpDataBy = NickName;
userCompany1.UpDataTime = DateTime.Now;
res = await userCompanyRes.UpdateAsync(userCompany1);
}
else
{
var userCompany = _mapper.Map(userCompanyDto);
userCompany.UpDataBy = NickName;
userCompany.UpDataTime = DateTime.Now;
userCompany.CreateBy = NickName;
userCompany.CreateTime = DateTime.Now;
userCompany.UserId = userid;
res = await userCompanyRes.InsertAsync(userCompany);
}
return res;
}
///
/// 根据Id查询用户
///
///
public async Task getUser(int userid)
{
UserDto userDto = new UserDto();
var userRes = new BaseRepository();
var userWorkerRes = new BaseRepository();
var userCompanyRes = new BaseRepository();
var user = await userRes.GetByIdAsync(userid);
userDto = _mapper.Map(user);
//var userWorker = await userWorkerRes.GetFirstAsync(p => p.UserId == userid);
//userDto.userWorker = _mapper.Map(userWorker);
//var userCompany = await userCompanyRes.GetFirstAsync(p => p.UserId == userid);
//userDto.userCompany = _mapper.Map(userCompany);
var userWorker = await userWorkerRes.GetFirstAsync(p => p.UserId == userid);
if (userWorker != null)
{
userDto.userWorker = _mapper.Map(userWorker);
}
var userCompany = await userCompanyRes.GetFirstAsync(p => p.UserId == userid);
if (userCompany != null)
{
userDto.userCompany = _mapper.Map(userCompany);
}
return userDto;
}
///
/// 保存用户信息
///
///
public async Task saveUser(UserDto userDto)
{
var UserID = App.User?.FindFirstValue("UserID");
int userid = 0;
string NickName = App.User?.FindFirstValue("NickName");
if (!string.IsNullOrEmpty(UserID))
{
userid = int.Parse(UserID);
}
if (!string.IsNullOrEmpty(userDto.Nickname) && userDto.Nickname.Length > 100)
{
throw Oops.Oh("昵称长度过长!");
}
if (!string.IsNullOrEmpty(userDto.Avatar) && userDto.Avatar.Length > 500)
{
throw Oops.Oh("头像长度过长!");
}
var userRes = new BaseRepository();
bool res;
var user = await userRes.GetByIdAsync(userid);
if (!string.IsNullOrEmpty(userDto.Nickname))
user.Nickname = userDto.Nickname;
if (!string.IsNullOrEmpty(userDto.Avatar))
user.Avatar = userDto.Avatar;
user.UpDataBy = NickName;
user.UpDataTime = DateTime.Now;
res = await userRes.UpdateAsync(user);
return res;
}
}
}