|
|
|
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.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace Coldairarrow.Business.HuiYan
|
|
|
|
{
|
|
|
|
public class pricetasklogBusiness : BaseBusiness<pricetasklog>, IpricetasklogBusiness, ITransientDependency
|
|
|
|
{
|
|
|
|
IuserBusiness _iuserBusiness;
|
|
|
|
readonly IOperator _operator;
|
|
|
|
public pricetasklogBusiness(IDbAccessor db, IuserBusiness iuserBusiness,
|
|
|
|
IOperator @operator)
|
|
|
|
: base(db)
|
|
|
|
{
|
|
|
|
_operator = @operator;
|
|
|
|
_iuserBusiness = iuserBusiness;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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()
|
|
|
|
{
|
|
|
|
var list = Db.GetIQueryable<pricetasklog>().Where(c => c.UserId == _operator.UserId).Select(c => c.State);
|
|
|
|
|
|
|
|
return Success(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
var users = _iuserBusiness.GetPriceTaskUserList().ToList();
|
|
|
|
|
|
|
|
Expression<Func<user, pricetasklog, PricetaskUser>> select = (a, b) => new PricetaskUser()
|
|
|
|
{
|
|
|
|
UserName = a.UserName,
|
|
|
|
MaxTaskCount = a.MaxPriceTaskCount,
|
|
|
|
Uid = a.Id
|
|
|
|
};
|
|
|
|
|
|
|
|
select = select.BuildExtendSelectExpre();
|
|
|
|
|
|
|
|
var q_User = Db.GetIQueryable<pricetasklog>().Where(c => c.CreateTime.Date == DateTime.Now.Date || c.CreateTime == DateTime.MinValue);
|
|
|
|
|
|
|
|
var q = from a in users
|
|
|
|
join b in q_User on a.Id equals b.UserId into ab
|
|
|
|
from b in ab.DefaultIfEmpty()
|
|
|
|
select @select.Invoke(a, b);
|
|
|
|
|
|
|
|
|
|
|
|
var userList = q.GroupBy(c => c.Uid).Where(c => c.Count() < c.FirstOrDefault()?.MaxTaskCount).Select(c => new PriceUserCount(c.Key, c.Count(), c.Max(c => c.MaxTaskCount) ?? 30)).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
var teamItemList = Db.GetIQueryable<teamitems>().Where(c => c.State == (int)TeamItemState.已发布比价 && string.IsNullOrEmpty(c.PriceTaskUserId)).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var result = Db.RunTransaction(() =>
|
|
|
|
{
|
|
|
|
|
|
|
|
List<pricetasklog> pricetasklogs = new List<pricetasklog>();
|
|
|
|
|
|
|
|
teamItemList.ForEach(teamItem =>
|
|
|
|
{
|
|
|
|
var user = userList.Where(c => c.Count < c.MaxCount).OrderBy(c => c.Count).FirstOrDefault();
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
teamItem.PriceTaskUserId = user.UserId;
|
|
|
|
|
|
|
|
user.Count += 1;
|
|
|
|
|
|
|
|
pricetasklogs.Add(new pricetasklog()
|
|
|
|
{
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
Id = IdHelper.GetId(),
|
|
|
|
CreatorId = _operator.UserId,
|
|
|
|
Deleted = false,
|
|
|
|
ItemId = teamItem.ItemId,
|
|
|
|
UserId = user.UserId,
|
|
|
|
State = PriceTaskState.待比价,
|
|
|
|
TeamItemId = teamItem.Id
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
int row = Db.Insert(pricetasklogs);
|
|
|
|
if (row <= 0)
|
|
|
|
{
|
|
|
|
throw new Exception("执行定时任务分配错误!");
|
|
|
|
}
|
|
|
|
row = Db.Update(teamItemList, new List<string>() { "PriceTaskUserId" });
|
|
|
|
if (row <= 0)
|
|
|
|
{
|
|
|
|
throw new Exception("执行定时任务分配更新任务信息错误!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!result.Success)
|
|
|
|
{
|
|
|
|
DingHelper.DingApiHelper.Main.SendNotify("齐越慧眼比价系统任务分配异常!");
|
|
|
|
}
|
|
|
|
|
|
|
|
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("任务不存在!");
|
|
|
|
}
|
|
|
|
|
|
|
|
var users = _iuserBusiness.GetPriceTaskUserList().ToList();
|
|
|
|
|
|
|
|
Expression<Func<user, pricetasklog, PricetaskUser>> select = (a, b) => new PricetaskUser()
|
|
|
|
{
|
|
|
|
UserName = a.UserName,
|
|
|
|
MaxTaskCount = a.MaxPriceTaskCount,
|
|
|
|
Uid = a.Id
|
|
|
|
};
|
|
|
|
|
|
|
|
select = select.BuildExtendSelectExpre();
|
|
|
|
|
|
|
|
var q_User = Db.GetIQueryable<pricetasklog>().Where(c => c.CreateTime.Date == DateTime.Now.Date || c.CreateTime == DateTime.MinValue);
|
|
|
|
|
|
|
|
var q = from a in users
|
|
|
|
join b in q_User on a.Id equals b.UserId into ab
|
|
|
|
from b in ab.DefaultIfEmpty()
|
|
|
|
select @select.Invoke(a, b);
|
|
|
|
|
|
|
|
//获取当日可分配的一个用户
|
|
|
|
var user = q.GroupBy(c => c.Uid).Where(c => c.Count() < c.FirstOrDefault()?.MaxTaskCount).Select(c => new { Id = c.Key, Count = c.Count() }).OrderBy(c => c.Count).FirstOrDefault();
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
|
|
|
|
DingHelper.DingApiHelper.Main.SendNotify("齐越慧眼比价系统任务量已达上限!");
|
|
|
|
|
|
|
|
//钉钉推送
|
|
|
|
// return Error("当前任务量已超过任务池可用任务量!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var result = Db.RunTransaction(() =>
|
|
|
|
{
|
|
|
|
int row = Db.Update<teamitems>(c => c.Id == teamItem.Id, (item) =>
|
|
|
|
{
|
|
|
|
if (user != null)
|
|
|
|
{
|
|
|
|
item.PriceTaskUserId = user.Id;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
item.PriceTaskUserId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
item.State = (int)TeamItemState.已发布比价;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (row <= 0)
|
|
|
|
throw new Exception("系统繁忙!");
|
|
|
|
|
|
|
|
if (user != null)
|
|
|
|
{
|
|
|
|
row = Db.Insert(new pricetasklog()
|
|
|
|
{
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
Id = IdHelper.GetId(),
|
|
|
|
CreatorId = _operator.UserId,
|
|
|
|
Deleted = false,
|
|
|
|
ItemId = teamItem.ItemId,
|
|
|
|
UserId = user.Id,
|
|
|
|
State = PriceTaskState.待比价,
|
|
|
|
TeamItemId = teamItem.Id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (row <= 0)
|
|
|
|
throw new Exception("系统任务繁忙!");
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.Success)
|
|
|
|
{
|
|
|
|
return Success("发布任务成功!");
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|