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.
458 lines
14 KiB
458 lines
14 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 Microsoft.Extensions.Caching.Distributed;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Dynamic.Core;
|
|
using System.Linq.Expressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Coldairarrow.Business.HuiYan
|
|
{
|
|
public class pricetasklogBusiness : BaseBusiness<pricetasklog>, IpricetasklogBusiness, ITransientDependency
|
|
{
|
|
IuserBusiness _iuserBusiness;
|
|
IteamitemsBusiness _iteamitemsBusiness;
|
|
readonly IOperator _operator;
|
|
readonly ILogger _logger;
|
|
readonly IDistributedCache _cache;
|
|
public pricetasklogBusiness(IDbAccessor db, IuserBusiness iuserBusiness, IteamitemsBusiness iteamitemsBusiness, ILogger<pricetasklogBusiness> logger,
|
|
IOperator @operator, IDistributedCache cache)
|
|
: base(db)
|
|
{
|
|
_operator = @operator;
|
|
_iuserBusiness = iuserBusiness;
|
|
_iteamitemsBusiness = iteamitemsBusiness;
|
|
_logger = logger;
|
|
_cache = cache;
|
|
}
|
|
|
|
#region 外部接口
|
|
|
|
public async Task<PageResult<pricetasklog>> GetDataListAsync(PageInput<ConditionDTO> input)
|
|
{
|
|
var q = GetIQueryable();
|
|
var where = LinqHelper.True<pricetasklog>();
|
|
var search = input.Search;
|
|
|
|
//筛选
|
|
if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
|
|
{
|
|
var newWhere = DynamicExpressionParser.ParseLambda<pricetasklog, bool>(
|
|
ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
|
|
where = where.And(newWhere);
|
|
}
|
|
|
|
return await q.Where(where).GetPageResultAsync(input);
|
|
}
|
|
|
|
public async Task<pricetasklog> GetTheDataAsync(string id)
|
|
{
|
|
return await GetEntityAsync(id);
|
|
}
|
|
|
|
public async Task AddDataAsync(pricetasklog data)
|
|
{
|
|
await InsertAsync(data);
|
|
}
|
|
|
|
public async Task UpdateDataAsync(pricetasklog data)
|
|
{
|
|
await UpdateAsync(data);
|
|
}
|
|
|
|
public async Task DeleteDataAsync(List<string> ids)
|
|
{
|
|
await DeleteAsync(ids);
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
public AjaxResult GetTaskCount()
|
|
{
|
|
Expression<Func<teamitems, items, pricetasklog, TeamitemDto>> select = (a, b, c) => new TeamitemDto
|
|
{
|
|
GoodsId = b.GoodsId,
|
|
HasFilter = b.HasFilter,
|
|
Platform = b.Platform,
|
|
GoodsUrl = b.GoodsUrl,
|
|
PriceTaskId = c.Id,
|
|
PriceTaskState = (int)c.State,
|
|
Extensions = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TeamItemExtension>>(a.ExtensionJson)
|
|
};
|
|
|
|
select = select.BuildExtendSelectExpre();
|
|
|
|
var q_titem = Db.GetIQueryable<teamitems>();
|
|
var q = from a in q_titem.AsExpandable()
|
|
join b in Db.GetIQueryable<items>() on a.ItemId equals b.Id into ab
|
|
from ba in ab.DefaultIfEmpty()
|
|
join c in Db.GetIQueryable<pricetasklog>() on a.Id equals c.TeamItemId into ac
|
|
from bb in ac.DefaultIfEmpty()
|
|
select @select.Invoke(a, ba, bb);
|
|
|
|
//查询对应状态
|
|
var where = LinqHelper.True<TeamitemDto>().And(c => c.PriceTaskUserId == _operator.UserId);
|
|
|
|
where = where.And(c => c.PriceTaskState != null);
|
|
|
|
var list = q.Where(where).Select(c => c.PriceTaskState).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)
|
|
{
|
|
Expression<Func<teamitems, items, pricetasklog, TeamitemDto>> select = (a, b, c) => new TeamitemDto
|
|
{
|
|
GoodsId = b.GoodsId,
|
|
HasFilter = b.HasFilter,
|
|
Platform = b.Platform,
|
|
GoodsUrl = b.GoodsUrl,
|
|
PriceTaskId=c.Id,
|
|
PriceTaskState = (int)c.State,
|
|
Extensions = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TeamItemExtension>>(a.ExtensionJson)
|
|
};
|
|
|
|
var search = input.Search;
|
|
select = select.BuildExtendSelectExpre();
|
|
|
|
var q_titem = Db.GetIQueryable<teamitems>();
|
|
var q = from a in q_titem.AsExpandable()
|
|
join b in Db.GetIQueryable<items>() on a.ItemId equals b.Id into ab
|
|
from ba in ab.DefaultIfEmpty()
|
|
join c in Db.GetIQueryable<pricetasklog>() on a.Id equals c.TeamItemId into ac
|
|
from bb in ac.DefaultIfEmpty()
|
|
select @select.Invoke(a, ba, bb);
|
|
|
|
//查询对应状态
|
|
var where = LinqHelper.True<TeamitemDto>().And(c => c.PriceTaskUserId == _operator.UserId);
|
|
|
|
int state = int.Parse(search.Keyword);
|
|
|
|
where = where.And(c => c.PriceTaskState == state);
|
|
|
|
where = where.AndIf(!string.IsNullOrEmpty(keyWord), d => d.Title.Contains(keyWord));
|
|
|
|
var list = q.Where(where).GetPageResultAsync(input).Result;
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当天可执行数量
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public AjaxResult GetMyCount()
|
|
{
|
|
var nowCount = Db.GetIQueryable<pricetasklog>().Count(c => c.CreateTime.Date == DateTime.Now.Date || c.CreateTime == DateTime.MinValue && c.UserId == _operator.UserId);
|
|
|
|
var user = Db.GetIQueryable<user>().FirstOrDefault(c => c.Id == _operator.UserId);
|
|
|
|
return Success(new { MaxCount = user.MaxPriceTaskCount, Count = user.MaxPriceTaskCount - nowCount });
|
|
}
|
|
|
|
public AjaxResult DayTaskSet()
|
|
{
|
|
|
|
CheckIsTimeOut();
|
|
|
|
return Success("任务分配成功!");
|
|
}
|
|
|
|
|
|
public AjaxResult CheckIsTimeOut()
|
|
{
|
|
try
|
|
{
|
|
//&& (DateTime.Now - c.WorkDate.Value).TotalHours >= 48
|
|
|
|
var needUpdates = Db.GetIQueryable<pricetasklog>().Where(c => c.State == PriceTaskState.已比价 && c.WorkDate != null).AsEnumerable<pricetasklog>().Where(c=>(DateTime.Now - c.WorkDate.Value).TotalHours >= 48).ToList();
|
|
|
|
if (needUpdates.Count > 0)
|
|
{
|
|
_iteamitemsBusiness.UpdateTaskSuccess(needUpdates);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
|
|
return Success();
|
|
}
|
|
|
|
public AjaxResult CanelTask(string teamItemId)
|
|
{
|
|
var teamItem = Db.GetIQueryable<teamitems>().FirstOrDefault(c => c.Id == teamItemId && c.TeamId == _operator.TeamId);
|
|
|
|
if (teamItem == null)
|
|
{
|
|
return Error("任务不存在!");
|
|
}
|
|
|
|
|
|
var result = Db.RunTransaction(() =>
|
|
{
|
|
var row = Db.Update<teamitems>(c => c.Id == teamItem.Id, item =>
|
|
{
|
|
item.PriceTaskUserId = null;
|
|
item.State = (int)TeamItemState.待比价;
|
|
});
|
|
|
|
if (row <= 0)
|
|
{
|
|
throw new Exception("操作失败");
|
|
}
|
|
|
|
Db.Delete<pricetasklog>(c => c.UserId == teamItem.PriceTaskUserId && c.TeamItemId == teamItem.Id);
|
|
});
|
|
|
|
|
|
if (result.Success)
|
|
{
|
|
return Success("操作成功");
|
|
}
|
|
else
|
|
{
|
|
return Error(result.ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 添加比价任务
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public AjaxResult AddTask(string teamItemId)
|
|
{
|
|
var teamItem = Db.GetIQueryable<teamitems>().FirstOrDefault(c => c.Id == teamItemId && c.TeamId == _operator.TeamId);
|
|
|
|
if (teamItem == null)
|
|
{
|
|
return Error("任务不存在!");
|
|
}
|
|
|
|
if (teamItem.RivalPrice==null||string.IsNullOrEmpty(teamItem.RivalImg))
|
|
{
|
|
return Error("对标商品信息不能为空!");
|
|
}
|
|
|
|
var result = Db.RunTransaction(() =>
|
|
{
|
|
int row = Db.Update<teamitems>(c => c.Id == teamItem.Id, (item) =>
|
|
{
|
|
item.State = (int)TeamItemState.已发布比价;
|
|
item.PriceTaskCreateDate = DateTime.Now;
|
|
});
|
|
|
|
if (row <= 0)
|
|
throw new Exception("系统繁忙!");
|
|
|
|
});
|
|
|
|
if (result.Success)
|
|
{
|
|
return Success("发布任务成功!");
|
|
}
|
|
|
|
return Error(result.ex.Message);
|
|
}
|
|
|
|
|
|
bool CheckIsMaxTaskCount()
|
|
{
|
|
|
|
|
|
var list = Db.GetListBySql<TaskCountDto>(@"SELECT
|
|
CAST( `p`.`State` AS signed ) AS State,
|
|
COUNT(*) AS count
|
|
FROM
|
|
`teamitems` AS `t`
|
|
LEFT JOIN `items` AS `i` ON `t`.`ItemId` = `i`.`Id`
|
|
LEFT JOIN `pricetasklog` AS `p` ON `t`.`Id` = `p`.`TeamItemId`
|
|
WHERE
|
|
( `t`.`PriceTaskUserId` = @uid )
|
|
AND `p`.`State` IS NOT NULL
|
|
GROUP BY
|
|
State",("uid",_operator.UserId));
|
|
|
|
|
|
int? hasTaskCount = list.Select(c => new { Id = c.State, Count = c.Count }).Where(c => c.Id == 0).FirstOrDefault()?.Count;
|
|
|
|
|
|
|
|
return hasTaskCount > 10;
|
|
}
|
|
|
|
public AjaxResult AcceptTask(string teamItemId)
|
|
{
|
|
|
|
string cacheKey = $"acceptTask:{_operator.UserId}";
|
|
string keyTime = _cache.GetString(cacheKey);
|
|
|
|
AcceptTime lastAccept = null;
|
|
|
|
//判断是否没有请求过
|
|
if (!string.IsNullOrEmpty(keyTime))
|
|
{
|
|
var time=Newtonsoft.Json.JsonConvert.DeserializeObject<AcceptTime>(keyTime);
|
|
|
|
//如果是新一轮请求
|
|
if (time.Date < DateTime.Now)
|
|
{
|
|
time.Date= DateTime.Now.AddMinutes(1);
|
|
time.Count = 1;
|
|
}
|
|
|
|
|
|
if (time.Count >= 3)
|
|
{
|
|
return Error("请求过于频繁!");
|
|
}
|
|
|
|
time.Count = time.Count + 1;
|
|
lastAccept = time;
|
|
}
|
|
else {
|
|
lastAccept = new AcceptTime() { Date = DateTime.Now.AddMinutes(1), Count = 1 };
|
|
}
|
|
|
|
_cache.SetString(cacheKey, Newtonsoft.Json.JsonConvert.SerializeObject(lastAccept));
|
|
|
|
var teamItem = Db.GetIQueryable<teamitems>().FirstOrDefault(c => c.Id == teamItemId);
|
|
|
|
if (teamItem == null)
|
|
{
|
|
return Error("任务不存在!");
|
|
}
|
|
|
|
|
|
if (CheckIsMaxTaskCount())
|
|
{
|
|
return Error("任务接取已达上限!");
|
|
}
|
|
|
|
|
|
var result = Db.RunTransaction(() =>
|
|
{
|
|
string orderId = DateTime.Now.ToString("yyyyMMddHHmmssff");
|
|
|
|
var price = new pricetasklog()
|
|
{
|
|
CreateTime = DateTime.Now,
|
|
Id = IdHelper.GetId(),
|
|
CreatorId = _operator.UserId,
|
|
Deleted = false,
|
|
ItemId = teamItem.ItemId,
|
|
UserId = _operator.UserId,
|
|
State = PriceTaskState.待比价,
|
|
TeamItemId = teamItem.Id,
|
|
OrderId = orderId
|
|
};
|
|
|
|
int row = Db.Insert(price);
|
|
|
|
if (row <= 0)
|
|
throw new Exception("任务已被接取!");
|
|
|
|
row = Db.Update<teamitems>(c => c.Id == teamItem.Id, (item) =>
|
|
{
|
|
item.PriceTaskUserId =_operator.UserId;
|
|
});
|
|
|
|
if (row <= 0)
|
|
throw new Exception("系统繁忙!");
|
|
|
|
});
|
|
|
|
if (result.Success)
|
|
{
|
|
return Success("接取任务成功!");
|
|
}
|
|
else {
|
|
if (result.ex.Message.Contains("teamiderror"))
|
|
{
|
|
return Error("任务已被接取!");
|
|
}
|
|
|
|
if (result.ex.Message.Contains("orderiderror"))
|
|
{
|
|
return Error("任务接取失败,请重试!");
|
|
}
|
|
}
|
|
|
|
return Error(result.ex.Message);
|
|
}
|
|
|
|
|
|
public AjaxResult SetState(string id, int state)
|
|
{
|
|
|
|
var task = Db.GetIQueryable<pricetasklog>().FirstOrDefault(c => c.Id == id && c.UserId == _operator.UserId);
|
|
|
|
if (task == null)
|
|
{
|
|
return Error("该任务已取消或不存在!");
|
|
}
|
|
|
|
var taskState = (PriceTaskState)state;
|
|
|
|
if (taskState == PriceTaskState.已比价)
|
|
{
|
|
var result = Db.RunTransaction(() =>
|
|
{
|
|
int row = Db.Update<pricetasklog>(c => c.Id == task.Id, item =>
|
|
{
|
|
item.State = taskState;
|
|
item.WorkDate = DateTime.Now;
|
|
});
|
|
|
|
if (row <= 0)
|
|
{
|
|
throw new Exception("操作失败!");
|
|
}
|
|
|
|
|
|
row = Db.Update<teamitems>(c => c.Id == task.TeamItemId, item =>
|
|
{
|
|
item.State = (int)TeamItemState.已比价;
|
|
});
|
|
|
|
if (row <= 0)
|
|
{
|
|
throw new Exception("操作失败!");
|
|
}
|
|
|
|
|
|
});
|
|
|
|
if (result.Success)
|
|
{
|
|
return Success("更新成功");
|
|
}
|
|
}
|
|
return Error("操作失败!");
|
|
}
|
|
|
|
#region 私有成员
|
|
|
|
#endregion
|
|
}
|
|
}
|