京东慧眼
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

672 lines
23 KiB

using Coldairarrow.Business.MDS;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.Enum;
using Coldairarrow.Entity.HuiYan;
using Coldairarrow.Entity.MDS;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Coldairarrow.Business.HuiYan
{
public class teamitemsBusiness : BaseBusiness<teamitems>, IteamitemsBusiness, ITransientDependency
{
readonly IOperator _operator;
IuserdepartmentBusiness _iuserdepartmentBusiness;
IuserBusiness _iuserBusiness;
public teamitemsBusiness(IDbAccessor db, IOperator @operator, IuserdepartmentBusiness iuserdepartmentBusiness, IuserBusiness iuserBusiness)
: base(db)
{
_operator = @operator;
_iuserdepartmentBusiness = iuserdepartmentBusiness;
_iuserBusiness = iuserBusiness;
}
#region 外部接口
public async Task<PageResult<teamitems>> GetDataListAsync(PageInput<ConditionDTO> input)
{
var q = GetIQueryable();
var where = LinqHelper.True<teamitems>();
var search = input.Search;
//筛选
if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
{
var newWhere = DynamicExpressionParser.ParseLambda<teamitems, bool>(
ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
where = where.And(newWhere);
}
return await q.Where(where).GetPageResultAsync(input);
}
public async Task<teamitems> GetTheDataAsync(string id)
{
return await GetEntityAsync(id);
}
public async Task AddDataAsync(teamitems data)
{
await InsertAsync(data);
}
public async Task UpdateDataAsync(teamitems data)
{
await UpdateAsync(data);
}
public async Task DeleteDataAsync(List<string> ids)
{
await DeleteAsync(ids);
}
#endregion
#region 私有成员
#endregion
public AjaxResult GetTeamCount()
{
var list= Db.GetIQueryable<teamitems>().Where(c => c.TeamId == _operator.TeamId).Select(c => c.State).ToList();
return Success(list.GroupBy(c => c).Select(c => new { Id = c.Key, Count = c.Count() }));
}
public PageResult<TeamitemDto> GetItems(PageInput<ConditionDTO> input, string keyWord)
{
3 years ago
try
{
3 years ago
Expression<Func<teamitems, items, cats, TeamitemDto>> select = (a, b, c) => new TeamitemDto
{
3 years ago
GoodsId = b.GoodsId,
HasFilter = b.HasFilter,
Platform = b.Platform,
GoodsUrl = b.GoodsUrl,
CatName = c == null ? String.Empty : c.Name,
Extensions = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TeamItemExtension>>(a.ExtensionJson)
};
var search = input.Search;
select = select.BuildExtendSelectExpre();
var q_titem = GetIQueryable();
var q = from a in q_titem.AsExpandable()
join b in Db.GetIQueryable<items>() on a.ItemId equals b.Id into ab
from b in ab.DefaultIfEmpty()
join c in Db.GetIQueryable<cats>() on a.CatId equals c.Id into bc
from c in bc.DefaultIfEmpty()
select @select.Invoke(a, b, c);
int state = 0;
bool searchState= int.TryParse(search.Keyword, out state);
//查询对应状态
var where = LinqHelper.True<TeamitemDto>();
if (searchState)
{
3 years ago
if (state == 0)
{
where = where.And(c => c.State == state || c.State == (int)TeamItemState.);
}
else
{
if (state == 6)
{
where = where.And(c => c.State == state || c.State == (int)TeamItemState.);
}
else
{
where = where.And(c => c.State == state);
}
}
3 years ago
if (state == 4)
{
var task = q.Where(c=> c.State == (int)TeamItemState.&&string.IsNullOrEmpty(c.PriceTaskUserId)).OrderBy(c=>c.PriceTaskCreateDate).FirstOrDefault();
List<TeamitemDto> result= new List<TeamitemDto>();
if (task != null)
{
result.Add(task);
}
return new PageResult<TeamitemDto>() { Data = result, Total = 1, Success = true };
}
}
3 years ago
where = where.AndIf(!string.IsNullOrEmpty(keyWord), d => string.IsNullOrEmpty(d.CatName) ? true : d.CatName.Contains(keyWord));
3 years ago
where = where.And(c => c.TeamId == _operator.TeamId);
3 years ago
input.SortField = "CreateTime";
input.SortType = "desc";
3 years ago
var list = q.Where(where).GetPageResultAsync(input).Result;
3 years ago
return list;
}
catch (Exception ex)
{
throw new Exception(ex.StackTrace);
}
}
public AjaxResult AddItem(TeamitemDto model)
{
model.Extensions = new List<TeamItemExtension>();
for (int i = 0; i < 5; i++)
model.Extensions.Add(new TeamItemExtension() { Platform = model.Platform, KDPrice = 6,TaxPrice=null });
if (string.IsNullOrEmpty(model.ItemId))
{
var item = Db.GetIQueryable<items>().FirstOrDefault(c => c.GoodsId == model.GoodsId);
if (item == null)
{
item = new items()
{
GoodsId = model.GoodsId,
GoodsUrl = model.GoodsUrl,
CreateTime = DateTime.Now,
CreatorId = _operator.UserId,
Deleted = false,
HasFilter = false,
Id = IdHelper.GetId(),
Platform = 0
};
item.GoodsUrl = string.Empty;
switch (model.Platform)
{
case (int)ItemPlatform.Taobao:
item.GoodsUrl = $"https://item.taobao.com/item.htm?id={item.GoodsId}";
break;
case (int)ItemPlatform.Jd:
item.GoodsUrl = $"https://item.jd.com/{item.GoodsId}.html";
break;
case (int)ItemPlatform.ALBB:
item.GoodsUrl = $"https://detail.1688.com/offer/{item.GoodsId}.html";
break;
}
if (Db.Insert<items>(item) <= 0)
{
return Error("商品添加失败!");
}
}
model.ItemId = item.Id;
}
//设置为初选
int row = Db.Insert<teamitems>(new teamitems()
{
State = 0,
CreateTime = DateTime.Now,
CreatorId = _operator.UserId,
Deleted = false,
ExtensionJson = Newtonsoft.Json.JsonConvert.SerializeObject(model.Extensions),
Id = IdHelper.GetId(),
TeamId = _operator.TeamId,
UserId = _operator.UserId,
Price = model.Price,
ItemImg = model.ItemImg,
Sales = model.Sales,
Title = model.Title,
3 years ago
CatId=model.CatId,
ItemId = model.ItemId
});
if (row <= 0)
return Error("商品信息添加失败!");
return Success();
}
public AjaxResult RemoveItem(string goodsId)
{
var item = Db.GetIQueryable<items>().FirstOrDefault(c => c.GoodsId == goodsId);
int row= Db.Delete<teamitems>(c => c.ItemId == item.Id && c.TeamId == _operator.TeamId);
if (row > 0)
return Success();
return Error();
}
public AjaxResult SetItem(TeamitemDto model)
{
try
{
int row = Db.Update<teamitems>(c => c.Id == model.Id, (item) =>
{
item.ExtensionJson = Newtonsoft.Json.JsonConvert.SerializeObject(model.Extensions);
item.RivalGoodsId = model.RivalGoodsId;
item.RivalPLCount = model.RivalPLCount;
item.RivalPrice = model.RivalPrice;
item.RivalTitle = model.RivalTitle;
item.RivalImg = model.RivalImg;
});
if (row > 0)
return Success();
return Error("操作失败!");
}
catch (Exception ex)
{
return Error("操作失败,缺少必填数据!");
}
}
public bool UpdateTaskSuccess(List<pricetasklog> logs)
{
var ids= logs.Select(c => c.TeamItemId).ToList();
var list = GetIQueryable().Where(c => ids.Contains(c.Id)).ToList();
list.ForEach(item => {
item.State = (int)TeamItemState.;
//完成日期
item.UpdateDate = DateTime.Now;
//判断是否合格
item.IsQualified = CheckIsPass(item);
});
logs.ForEach(item => {
item.State = PriceTaskState.;
});
var result= Db.RunTransaction(() => {
Db.Update(list);
Db.Update(logs);
});
return result.Success;
}
public async Task<PriceTaskInfo> GetMyTaskInfo(int type,DateTime start,DateTime end)
{
Expression<Func< pricetasklog, teamitems, TaskInfoDto >> select = (a, b) => new TaskInfoDto
{
IsPass = b.IsQualified,
TeamId = b.TeamId,
TeamUserId = b.UserId,
TeamDate = b.CreateTime,
TeamOverDate = b.UpdateDate,
PriceTaskCreateDate = b.PriceTaskCreateDate == null ? null : b.PriceTaskCreateDate.Value
};
select = select.BuildExtendSelectExpre();
var q_titem = Db.GetIQueryable<teamitems>();
var q = from a in q_titem.AsExpandable()
join b in Db.GetIQueryable<pricetasklog>() on a.Id equals b.TeamItemId into ab
from b in ab.DefaultIfEmpty()
select @select.Invoke(type==1?b:(b==null?new pricetasklog():b), a);
var where = LinqHelper.True<TaskInfoDto>();
start = new DateTime(start.Year, start.Month, start.Day);
end = new DateTime(end.Year, end.Month, end.Day, 23, 59, 59);
where = where.And(c => c.PriceTaskCreateDate >= start && c.PriceTaskCreateDate <= end);
//团队
if (type == 0)
{
where = where.And(c => c.TeamId == _operator.TeamId);
}
//比价
else
{
where = where.And(c => c.UserId == _operator.UserId);
}
var list = await q.Where(where).ToListAsync();
var model = new PriceTaskInfo()
{
AuditCount = list.Count(c => c.State == PriceTaskState.),
OverCount = list.Count(c => c.State == PriceTaskState.),
PassCount = list.Count(c => c.IsPass==true),
PublishCount = list.Count,
FailedCount = list.Count(c => c.IsPass==false),
NeedWorkCount = list.Count(c => c.State != PriceTaskState.)
};
if (model.OverCount > 0) {
model.PassRate = Math.Round(((decimal)model.PassCount / model.OverCount) * 100, 2);
}
return model;
}
public AjaxResult GetTjInfo(DateTime start, DateTime end)
{
Expression<Func<pricetasklog, teamitems, TaskInfoDto>> select = (a, b) => new TaskInfoDto
{
IsPass = b.IsQualified == null ? null : b.IsQualified.Value,
TeamId = b.TeamId,
TeamUserId = b.UserId,
TeamDate = b.CreateTime,
TeamOverDate = b.UpdateDate==null?null:b.UpdateDate.Value,
PriceTaskUserId = b.PriceTaskUserId,
TeamItemState = (TeamItemState?)b.State,
PriceTaskCreateDate = b.PriceTaskCreateDate == null ? null : b.PriceTaskCreateDate.Value
};
select = select.BuildExtendSelectExpre();
var q_titem = Db.GetIQueryable<teamitems>();
var q = from a in q_titem.AsExpandable()
join b in Db.GetIQueryable<pricetasklog>() on a.Id equals b.TeamItemId into ab
from b in ab.DefaultIfEmpty()
select @select.Invoke(b==null?new pricetasklog():b, a);
var where = LinqHelper.True<TaskInfoDto>();
start = new DateTime(start.Year, start.Month, start.Day);
end = new DateTime(end.Year, end.Month, end.Day, 23, 59, 59);
//where = where.And(c => c.PriceTaskCreateDate >= start && c.PriceTaskCreateDate <= end);
var list = q.Where(where).ToList();
int waitOver = list.Count(c => c.TeamItemState == TeamItemState. && !string.IsNullOrEmpty(c.PriceTaskUserId) && c.State != PriceTaskState.);
int waitCheck = list.Count(c => c.TeamItemState == TeamItemState. && string.IsNullOrEmpty(c.PriceTaskUserId));
list = list.Where(c => c.PriceTaskCreateDate >= start && c.PriceTaskCreateDate <= end).ToList();
Expression<Func<TaskInfoDto, userdepartment, TaskInfoDto>> select2 = (a, b) => new TaskInfoDto
{
TeamName =b==null?"未知" :b.DepartmentName
};
select2 = select2.BuildExtendSelectExpre();
var q2 = from a in list
join b in _iuserdepartmentBusiness.GetDepQueryable() on a.TeamId equals b.Id into ab
from b in ab.DefaultIfEmpty()
select @select2.Invoke(a, b);
list = q2.ToList();
Expression<Func<TaskInfoDto, user, TaskInfoDto>> select3 = (a, b) => new TaskInfoDto
{
UserName = b == null ? "未知" : b.UserNick
};
select3 = select3.BuildExtendSelectExpre();
var q3 = from a in list
join b in _iuserBusiness.GetUserQueryable() on a.UserId equals b.Id into ab
from b in ab.DefaultIfEmpty()
select @select3.Invoke(a, b);
list = q3.ToList();
var jtInfo = new PriceTaskInfo()
{
AuditCount = list.Count(c => c.State == PriceTaskState.),
OverCount = list.Count(c => c.State == PriceTaskState.),
PassCount = list.Count(c => c.IsPass == true),
PublishCount = list.Count,
FailedCount = list.Count(c => c.IsPass == false),
NeedWorkCount = list.Count(c => c.State != PriceTaskState.)
};
if (jtInfo.OverCount > 0)
{
jtInfo.PassRate = Math.Round(((decimal)jtInfo.PassCount / jtInfo.OverCount) * 100, 2);
}
var fbList= new List<PriceTaskInfo>();
var groups= list.GroupBy(c => c.TeamName).ToList();
foreach (var group in groups)
{
var sonList = group.ToList();
var price = new PriceTaskInfo()
{
Title = group.Key,
AuditCount = sonList.Count(c => c.State == PriceTaskState.),
OverCount = sonList.Count(c => c.State == PriceTaskState.),
PassCount = sonList.Count(c => c.IsPass == true),
PublishCount = sonList.Count,
FailedCount = sonList.Count(c => c.IsPass == false),
NeedWorkCount = sonList.Count(c => c.State != PriceTaskState.)
};
if (price.OverCount > 0)
{
price.PassRate = Math.Round(((decimal)price.PassCount / price.OverCount) * 100, 2);
}
fbList.Add(price);
}
var bjList = new List<PriceTaskInfo>();
var bjGroup = list.GroupBy(c => c.UserName).ToList();
foreach (var group in bjGroup)
{
var sonList = group.ToList();
var price = new PriceTaskInfo()
{
Title = group.Key,
AuditCount = sonList.Count(c => c.State == PriceTaskState.),
OverCount = sonList.Count(c => c.State == PriceTaskState.),
PassCount = sonList.Count(c => c.IsPass == true),
PublishCount = sonList.Count,
FailedCount = sonList.Count(c => c.IsPass == false),
NeedWorkCount = sonList.Count(c => c.State != PriceTaskState.)
};
if (price.OverCount > 0)
{
price.PassRate = Math.Round(((decimal)price.PassCount / price.OverCount) * 100, 2);
}
bjList.Add(price);
}
var result = new
{
//待完成
WaitOver = waitOver,
//待接入
WaitCheck= waitCheck,
JT=new List<PriceTaskInfo>() { jtInfo },
FB=fbList,
BJ=bjList
};
return Success(result) ;
}
public AjaxResult SetState(string id, int state)
{
//删除
if (state == -1)
{
return DeleteItem(id);
}
var priceUserId = Db.GetIQueryable<teamitems>().FirstOrDefault(c => c.Id == id).PriceTaskUserId;
var result= Db.RunTransaction(() =>
{
int row = Db.Update<teamitems>(c => c.Id == id, (item) =>
{
item.State = state;
if ((Entity.Enum.TeamItemState)state == TeamItemState.)
{
//完成日期
item.UpdateDate = DateTime.Now;
//判断是否合格
item.IsQualified = CheckIsPass(item);
}
});
if (row <= 0)
throw new Exception("任务状态设置失败!");
//同步更新比价任务状态
if ((Entity.Enum.TeamItemState)state == TeamItemState.)
{
if (!string.IsNullOrEmpty(priceUserId))
{
row = Db.Update<pricetasklog>(c => c.UserId == priceUserId && c.TeamItemId == id, (item) =>
{
item.State = PriceTaskState.;
});
if (row <= 0)
throw new Exception("比价任务设置失败!");
}
}
if (!string.IsNullOrEmpty(priceUserId))
{
if ((Entity.Enum.TeamItemState)state == TeamItemState.)
{
row = Db.Update<pricetasklog>(c => c.UserId == priceUserId && c.TeamItemId == id, (item) =>
{
item.State = PriceTaskState.;
});
if (row <= 0)
throw new Exception("比价任务设置失败!");
}
}
});
if (result.Success)
{
return Success("更新成功");
}
return Error("操作失败!");
}
/// <summary>
/// 检测是否合格
/// </summary>
/// <returns></returns>
private bool CheckIsPass(teamitems item)
{
decimal profits = 0;
var extList= Newtonsoft.Json.JsonConvert.DeserializeObject<List<TeamItemExtension>>(item.ExtensionJson);
var ex= extList.OrderByDescending(c => c.Profits).FirstOrDefault();
if (ex == null)
return false;
//利润率
profits = ex.Profits;
//竞品客单为50元以下,利润率达80%
if (item.RivalPrice < 50)
{
return profits >= 80;
}
//竞品客单为50-100元,利润率达70%
if (item.RivalPrice >= 50&&item.RivalPrice<100)
{
return profits >= 70;
}
//竞品客单为100-150元,利润率达60%
if (item.RivalPrice >= 100 && item.RivalPrice < 150)
{
return profits >= 60;
}
//竞品客单为150-300元,利润率达50%
if (item.RivalPrice >= 150 && item.RivalPrice < 300)
{
return profits >= 50;
}
//竞品客单为300元以上,利润率达40%
if (item.RivalPrice >= 300)
{
return profits >= 40;
}
return false;
}
public AjaxResult DeleteItem(string id)
{
string itemId = Db.GetIQueryable<teamitems>().FirstOrDefault(c => c.Id == id)?.ItemId;
var result = Db.RunTransaction(() =>
{
var row = Db.Delete<teamitems>(c => c.Id == id);
if (row <= 0)
{
throw new Exception("操作失败!");
}
row = Db.Update<itemlabels>(c => c.ItemsId == itemId && c.TeamId == _operator.TeamId, (item) =>
{
//item.IsScreening = false;
item.IsAdded = false;
});
//if (row <= 0)
//{
// throw new Exception($"删除失败!");
//}
});
if (result.Success)
{
return Success();
}
return Error(result.ex.Message);
}
}
}