/***********************************************************************
* Project: baifenBinfa
* ProjectName: 百分兵法管理系统
* Web: http://chuanyin.com
* Author:
* Email:
* CreateTime: 2024/3/29 11:16:20
* Description: 暂无
***********************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.baifenbingfa.Promote;
using CoreCms.Net.Model.Entities.baifenbingfa.Promote.OffLineDistributor;
using CoreCms.Net.Model.ViewModels.Basics;
using CoreCms.Net.Model.ViewModels.UI;
using SqlSugar;
namespace CoreCms.Net.Services
{
///
/// 线下经销商采集 接口实现
///
public class OfflineDistributorServices : BaseServices, IOfflineDistributorServices
{
private readonly IOfflineDistributorRepository _dal;
private readonly IUnitOfWork _unitOfWork;
private readonly IHttpContextUser _contextUser;
public OfflineDistributorServices(IUnitOfWork unitOfWork, IOfflineDistributorRepository dal,IHttpContextUser httpContextUser)
{
this._dal = dal;
base.BaseDal = dal;
_unitOfWork = unitOfWork;
_contextUser = httpContextUser;
}
#region 实现重写增删改查操作==========================================================
///
/// 重写异步插入方法
///
/// 实体数据
///
public async Task InsertAsync(OfflineDistributor entity)
{
entity.createTime = DateTime.Now;
entity.createBy = $"{_contextUser.Name}";
return await _dal.InsertAsync(entity);
}
///
/// 重写异步插入方法
///
/// 实体数据
///
public async Task UserInsertAsync(OfflineDistributor entity)
{
entity.createTime = DateTime.Now;
entity.createBy = $"{_contextUser.Name}";
var bol = await _unitOfWork.GetDbClient().InsertNav(entity).Include(x => x.meetings).ExecuteCommandAsync();
if (bol)
{
return new AdminUiCallBack()
{
code = 0,
data = bol,
msg = "操作成功"
};
}
else
{
return new AdminUiCallBack()
{
code = 1,
data = bol,
msg = "操作失败"
};
}
return await _dal.InsertAsync(entity);
}
public async Task GetInfo(int id)
{
var data = await _unitOfWork.GetDbClient().Queryable().Where(x => x.Id == id)
.Includes(x => x.meetings)
.Includes(x => x.Oders).FirstAsync();
if (data == null)
return new AdminUiCallBack()
{
msg = "没有找到该数据"
};
else
return new AdminUiCallBack()
{
code = 0,
data = data
};
}
///
/// 重写异步更新方法方法
///
///
///
public async Task UpdateAsync(OfflineDistributor entity)
{
var db = _unitOfWork.GetDbClient();
var oldmod = await _dal.QueryByIdAsync(entity.Id);
if (oldmod == null)
{
return new AdminUiCallBack()
{
data = "没有找到相关数据"
};
}
oldmod.upDataTime = DateTime.Now;
oldmod.upDataBy = $"{_contextUser.Name}";
oldmod.BusinessManager = entity.BusinessManager;
oldmod.PhoneNumber = entity.PhoneNumber;
oldmod.PUserID = entity.PUserID;
oldmod.Region = entity.Region;
oldmod.Remake = entity.Remake;
oldmod.DealerName = entity.DealerName;
oldmod.UserID = entity.UserID;
oldmod.IsMeeting = entity.IsMeeting;
oldmod.StoreLogoUrl = entity.StoreLogoUrl;
oldmod.Adder = entity.Adder;
oldmod.ContactPerson = entity.ContactPerson;
oldmod.Category = entity.Category;
oldmod.CooperationIntent = entity.CooperationIntent;
oldmod.School = entity.School;
var oldMeetings = await db.Queryable().Where(x => x.OfflineDistributorID == oldmod.Id).OrderBy(x => x.id).ToListAsync();
var oldIntentions = await db.Queryable().Where(x => x.OfflineDistributorID == oldmod.Id).OrderBy(x => x.Id).ToListAsync();
foreach (var intention in entity.Oders)
{
if (intention.Id == 9999999)
{
intention.Id = 0;
intention.createBy = $"{_contextUser.Name}";
intention.createTime = DateTime.Now;
intention.isdelete = false;
continue;
}
var old = oldIntentions.Where(x=>x.Id==intention.Id).SingleOrDefault();
if (old != null)
{
intention.isdelete = old.isdelete;
intention.createBy = old.createBy;
intention.createTime = old.createTime;
intention.upDataBy = $"{_contextUser.Name}";
intention.upDataTime = DateTime.Now;
}
}
oldmod.Oders = entity.Oders;
foreach (OfflineDistributorMeeting meetin in entity.meetings)
{
if (meetin.id == 9999999)
{
meetin.id = 0;
meetin.createBy = $"{_contextUser.Name}";
meetin.createTime = DateTime.Now;
meetin.isdelete = false;
continue;
}
var old = oldMeetings.Where(x => x.id == meetin.id).SingleOrDefault();
if (old != null)
{
meetin.isdelete = old.isdelete;
meetin.createBy = old.createBy;
meetin.createTime = old.createTime;
meetin.upDataBy = $"{_contextUser.Name}";
meetin.upDataTime = DateTime.Now;
}
}
oldmod.meetings = entity.meetings;
var bol = await _unitOfWork.GetDbClient().UpdateNav(oldmod).Include(x => x.meetings).Include(x => x.Oders).ExecuteCommandAsync();
if (bol)
{
return new AdminUiCallBack()
{
code = 0,
data = bol,
msg = "操作成功"
};
}
else
{
return new AdminUiCallBack()
{
code = 1,
data = bol,
msg = "操作失败"
};
}
//return await _dal.UpdateAsync(entity);
}
///
/// 重写异步更新方法方法
///
///
///
public async Task UpdateAsync(List entity)
{
return await _dal.UpdateAsync(entity);
}
///
/// 重写删除指定ID的数据
///
///
///
public async Task DeleteByIdAsync(object id)
{
return await _dal.DeleteByIdAsync(id);
}
///
/// 重写删除指定ID集合的数据(批量删除)
///
///
///
public async Task DeleteByIdsAsync(int[] ids)
{
return await _dal.DeleteByIdsAsync(ids);
}
#endregion
#region 获取缓存的所有数据==========================================================
///
/// 获取缓存的所有数据
///
///
public async Task> GetCaChe()
{
return await _dal.GetCaChe();
}
#endregion
#region 重写根据条件查询分页数据
///
/// 重写根据条件查询分页数据
///
/// 判断集合
/// 排序方式
/// 当前页面索引
/// 分布大小
///
/// 是否使用WITH(NOLOCK)
///
public async Task> QueryPageAsync(Expression> predicate,
Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
{
return await _dal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize, blUseNoLock);
}
#endregion
}
}