using Senparc.Weixin.Entities.TemplateMessage;
using System.Collections.Generic;
using System.Threading.Tasks;
using Wx;
using Znyc.Admin.Commons.Cache;
using Znyc.Admin.Commons.Const;
using Znyc.Admin.Commons.Entitys;
using Znyc.Admin.Commons.Enums;
using Znyc.Admin.Commons.Mapping;
using Znyc.Admin.Commons.Pages;
using Znyc.Admin.Commons.Services;
using Znyc.Admin.Security.Dtos;
using Znyc.Admin.Security.Entitys;
using Znyc.Admin.Security.IRepositories;
using Znyc.Admin.Security.IServices;
namespace Znyc.Admin.Security.Services
{
///
///
///
public class CompanyService : BaseService, ICompanyService
{
private readonly ICompanyRepository _companyRepository;
private readonly IVehicleRepository _vehicleRepository;
private readonly IUserRepository _userRepository;
private readonly ICacheService _cacheService;
public CompanyService(ICompanyRepository companyRepository,
IVehicleRepository vehicleRepository,
IUserRepository userRepository,
ICacheService cacheService
) : base(companyRepository)
{
_companyRepository = companyRepository;
_vehicleRepository = vehicleRepository;
_userRepository = userRepository;
_cacheService = cacheService;
}
public async Task> FindWithPagerSearchAsync(SearchUserModel search)
{
bool order = search.Order == "asc" ? false : true;
string where = GetDataPrivilege(false);
if (!string.IsNullOrEmpty(search.Keywords))
{
@where += string.Format(" and (CompanyName like '%{0}%')", search.Keywords);
}
if (search.Status > (int)CommonStatus.DELETED)
{
@where += string.Format(" and Status={0}", search.Status);
}
if (!string.IsNullOrEmpty(search.StartTime))
{
@where += " and CreatedTime >='" + search.StartTime + " 00:00:00'";
}
if (!string.IsNullOrEmpty(search.EndTime))
{
@where += " and CreatedTime <='" + search.EndTime + " 23:59:59'";
}
PagerInfo pagerInfo = new PagerInfo
{
CurrenetPageIndex = search.CurrenetPageIndex,
PageSize = search.PageSize
};
List list = await repository.FindWithPagerAsync(where, pagerInfo, search.Sort, order);
List companyOutput = list.MapTo();
foreach (var item in companyOutput)
{
//string condition = string.Format(@" CompanyId=" + item.Id + " Status=1");
item.VehicleCount = await _vehicleRepository.GetCountByWhereAsync(" CompanyId=" + item.Id + " and Status=1");
item.CompanyLogo = CommonConst.Default_Image_Prefix + item.CompanyLogo;
}
PageResult pageResult = new PageResult
{
CurrentPage = pagerInfo.CurrenetPageIndex,
Items = companyOutput,
ItemsPerPage = pagerInfo.PageSize,
TotalItems = pagerInfo.RecordCount
};
return pageResult;
}
///
/// 审核公司状态
///
///
///
///
public async Task AuditAsync(long id, int status)
{
CommonResult result = new CommonResult();
string where = $@" Id={id}";
Company company = await _companyRepository.GetWhereAsync(where);
User user = await _userRepository.GetWhereAsync($@"Phone={company.ContactPhone}");
var isExistCount = await _companyRepository.GetCountByWhereAsync($@"ContactPhone={company.ContactPhone} and Status=1");
if (isExistCount > 0)
{
result.ResData = ReturnConst.Audit_Repeat;
result.Success = false;
result.ErrMsg = ReturnConst.Audit_Repeat;
return result;
}
if (company.Status != (int)CommonStatus.ENABLE)
{
result.Success = await _companyRepository.AuditAsync(id, status);
string phrase2 = ReturnConst.Audit_Failed;
string thing3 = ReturnConst.Audit_Failed_Note;
if (status == (int)CommonStatus.ENABLE)
{
phrase2 = ReturnConst.Audit_Success;
thing3 = ReturnConst.Audit_Success_Note;
}
await CommonHelper.SendCompanyAuditAsync(company.CompanyName, phrase2, thing3, user.OpenId).ConfigureAwait(false);
return result;
}
else
{
result.ResData = ReturnConst.Audit_Error;
result.Success = false;
return result;
}
}
///
/// 修改公司状态
///
///
///
///
public async Task UpdateAsync(long id, int status)
{
CommonResult result = new CommonResult();
string where = string.Format(@" Id={0}", id);
Company company = await _companyRepository.GetWhereAsync(where);
if (company.Status == (int)CommonStatus.ENABLE)
{
result.Success = await _companyRepository.UpdateStatusAsync(id, status);
await ClearCompanyCacheAsync(company.Id);
return result;
}
else
{
result.ResData = "该公司已停用";
result.Success = false;
return result;
}
}
///
///
///
///
///
public async Task ClearCompanyCacheAsync(long companyId)
{
//清除缓存
await _cacheService.RemoveAsync(CacheKey.CACHE_KEY_COMPANY + $"{companyId}");
}
}
}