using FHT.Agency.CommonTypes; using FHT.ApiCommon; using FHT.Agency.Web.Filters; using FHT.Management.Interfaces.DependentInterfaces.Services; using FHT.Management.Interfaces.InputModels; using FHT.Utilities; using System.Web.Http; using System.Web.Http.Description; using System.Threading.Tasks; using FHT.Management.Interfaces.ViewModels; using System.Collections.Generic; using FHT.Core.CommonTypes; using FHT.Core.UI.ViewModels; using System; using System.IO; using System.Linq; using System.Text; using FHT.Core.Queries.ImplementedInterfaces; using FHT.Agency.Web.Models; using FHT.Core.InfrastructureServiceInterfaces.InfrastructureServices; using System.Configuration; using FHT.Agency.Web.Utility; using FHT.DDD; using System.Web.Http.Cors; namespace FHT.Agency.Web.ApiControllers { [Authorize] [RoutePrefix("api/keywords")] [EnableCors(origins: "*", headers: "*", methods: "*")] public class KeywordApiController : ApiController { private readonly string _ossHost = ConfigurationManager.AppSettings["ImgServerHost"]; private readonly IManagementKeywordService _managementKeywordService; private readonly IManagementCompanyKeywordService _managementCompanyKeywordService; private readonly IManagementMarketingKeywordService _managementMarketingKeywordService; private readonly IIndustryQuery _industryQuery; private readonly IFileUploadInfrastructureService _fileUploadInfrastructureService; public KeywordApiController(IManagementKeywordService managementKeywordService, IManagementCompanyKeywordService managementCompanyKeywordService, IManagementMarketingKeywordService managementMarketingKeywordService, IIndustryQuery industryQuery, IFileUploadInfrastructureService fileUploadInfrastructureService) { _managementKeywordService = managementKeywordService; _managementCompanyKeywordService = managementCompanyKeywordService; _managementMarketingKeywordService = managementMarketingKeywordService; _industryQuery = industryQuery; _fileUploadInfrastructureService = fileUploadInfrastructureService; } [HttpPost] [Route("_keywordCreateOrUpdate")] [ResponseType(typeof(void))] [ValidateModel] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public IHttpActionResult KeywordCreateOrUpdate(KeywordInputModel model) { //判断是否存在相同名称及拼音 model.keywordId = model.keywordId == 0 ? null : model.keywordId; bool isExistKeyword = _managementKeywordService.CheckKeyword(model.keywordId, model.Keyword); bool isExistPinYin = _managementKeywordService.CheckPinYin(model.keywordId, model.PinYin); if (isExistKeyword || isExistPinYin) { return BadRequest(); } _managementKeywordService.CreateOrUpdate(model); return Ok(); } [HttpGet] [ResponseType(typeof(KeywordInputModel))] [Route("GetKeyword")] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public IHttpActionResult GetKeyword(int? keywordId=null) { KeywordInputModel model = _managementKeywordService.Get(keywordId); return Ok(model); } [HttpGet] [Route("GetKeywordPinYin")] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] [ResponseType(typeof(StringInputModel))] public IHttpActionResult GetKeywordPinYin(string keyword) { string pinYin = keyword.PinYin().Replace(" ", ""); return Ok(new StringInputModel { Content = pinYin }); } [HttpGet] [Route("CheckKeyword")] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] [ResponseType(typeof(BooleanInputModel))] public IHttpActionResult CheckKeyword(string keyword="", int? keywordId = null) { keywordId = keywordId == 0 ? null : keywordId; bool isExists = _managementKeywordService.CheckKeyword(keywordId, keyword); return Ok(new BooleanInputModel { Bool = isExists }); } [HttpGet] [Route("CheckPinYin")] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] [ResponseType(typeof(BooleanInputModel))] public IHttpActionResult CheckPinYin(string pinYin="", int? keywordId = null) { keywordId = keywordId == 0 ? null : keywordId; bool isExists = _managementKeywordService.CheckPinYin(keywordId, pinYin); return Ok(new BooleanInputModel { Bool = isExists }); } [HttpPost] [Route("_audit")] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement,FhtUserClaimType.CompanyKeywordManagement)] [ResponseType(typeof(void))] public IHttpActionResult Audit(int keywordId, KeywordStatus status) { _managementKeywordService.Audit(keywordId, status); return Ok(); } #region Company Keyword [HttpGet] [Route("~/company/keywordPinYinExist")] [ResponseType(typeof(BooleanInputModel))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task CompanyIndustryKeywordPinYinExistAsync(string pinyin, long? excludedKeywordId = null) { var keywordIds = new List(); if (excludedKeywordId.HasValue) keywordIds.Add(excludedKeywordId.Value); bool isExist = await _managementCompanyKeywordService.PinYinExistAsync(pinyin, keywordIds); return Ok(new BooleanInputModel { Bool = isExist }); } [HttpGet] [Route("~/company/keywordExist")] [ResponseType(typeof(BooleanInputModel))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task CompanyIndustryKeywordExistAsync(string keyword, long? excludedKeywordId = null) { var keywordIds = new List(); if (excludedKeywordId.HasValue) keywordIds.Add(excludedKeywordId.Value); bool isExist = await _managementCompanyKeywordService.ExistAsync(keyword, keywordIds); return Ok(new BooleanInputModel { Bool = isExist }); } [HttpGet] [Route("~/company/keywordIdExist")] [ResponseType(typeof(BooleanInputModel))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task CompanyIndustryKeywordIdExistAsync(long keywordId, long? excludedKeywordId = null) { var keywordIds = new List(); if (excludedKeywordId.HasValue) keywordIds.Add(excludedKeywordId.Value); bool isExist = await _managementCompanyKeywordService.ExistAsync(keywordId, keywordIds); return Ok(new BooleanInputModel { Bool = isExist }); } [HttpGet] [Route("~/company/industryKeyword/{keywordId:long:min(1)}", Name = nameof(GetCompanyIndustryKeywordAsync))] [ResponseType(typeof(CompanyKeywordViewModel))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task GetCompanyIndustryKeywordAsync(long keywordId) { CompanyKeywordViewModel keyword = await _managementCompanyKeywordService.GetKeywordAsync(keywordId, false); return Ok(keyword); } [HttpPost] [Route("~/company/createIndustryKeyword", Name = nameof(CreateCompanyKeywordWithIndustryAsync))] [ResponseType(typeof(CreatedViewModel))] [ValidateModel] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task CreateCompanyKeywordWithIndustryAsync(CompanyKeywordInputModel model) { bool isIndustyExist = _industryQuery.IsExistedIndustry(model.IndustryId); if (!isIndustyExist) throw new InvalidOperationException($"行业id(IndustryId={model.IndustryId})无效!"); long keywordId = await _managementCompanyKeywordService.CreateAsync(model); string url = Url.Link(nameof(GetCompanyIndustryKeywordAsync), new { keywordId }); return Created(url, new CreatedViewModel { Id = keywordId, Location = url }); } [HttpPost] [Route("~/company/updateIndustryKeyword", Name = nameof(UpdateCompanyKeywordWithIndustryAsync))] [ResponseType(typeof(void))] [ValidateModel] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task UpdateCompanyKeywordWithIndustryAsync(CompanyKeywordInputModel model) { bool isIndustyExist = _industryQuery.IsExistedIndustry(model.IndustryId); if (!isIndustyExist) throw new InvalidOperationException($"行业id(IndustryId={model.IndustryId})无效!"); long keywordId = await _managementCompanyKeywordService.UpdateAsync(model); return Ok(); } [HttpPost] [Route("~/company/deleteKeyword", Name = nameof(DeleteCompanyKeywordAsync))] [ResponseType(typeof(void))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task DeleteCompanyKeywordAsync(long[] keywordIds) { await _managementCompanyKeywordService.ChangeStatusAsync(KeywordStatus.Deleted, keywordIds); return Ok(); } [HttpPost] [Route("~/company/disabledKeyword", Name = nameof(DisabledCompanyKeywordAsync))] [ResponseType(typeof(void))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task DisabledCompanyKeywordAsync(long[] keywordIds) { await _managementCompanyKeywordService.ChangeStatusAsync(KeywordStatus.NotUsedAnyMore, keywordIds); return Ok(); } [HttpPost] [Route("~/company/enabledKeyword", Name = nameof(EnabledCompanyKeywordAsync))] [ResponseType(typeof(void))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task EnabledCompanyKeywordAsync(long[] keywordIds) { await _managementCompanyKeywordService.ChangeStatusAsync(KeywordStatus.InUse, keywordIds); return Ok(); } [HttpGet] [Route("~/company/industryKeywords", Name = nameof(GetCompanyKeywordListWithIndustryAsync))] [ResponseType(typeof(QueryResult))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task GetCompanyKeywordListWithIndustryAsync(string q = "", bool? isOnlyInUse = null, int? industryId = null, int offset = 0, int limit = 20) { KeywordStatus? keywordStatus = null; if (isOnlyInUse.HasValue) { keywordStatus = isOnlyInUse.Value ? KeywordStatus.InUse : KeywordStatus.NotUsedAnyMore; } QueryResult results = await _managementCompanyKeywordService.GetListAsync(q, keywordStatus, industryId, offset, limit); return Ok(results); } [HttpPost] [Route("~/company/bulkImportKeyword")] [ResponseType(typeof(BulkImportResultViewModel>))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task BulkImportCompanyKeywordAsync(string fileUrl, bool hasLogFile = false) { Stream stream = _fileUploadInfrastructureService.DownloadDocument(new Uri(fileUrl)); CompanyKeywordCsvViewModel[] csvKeywords = CsvHelper.Read(new StreamReader(stream)); List industries = _industryQuery.GetIndustries(); List companyKeywords = csvKeywords.Select(s => new CompanyKeywordInputModel { Keyword = s.Keyword, IndustryId = industries.Single(sl => sl.IndustryName == s.IndustryName).IndustryId, PinYin = s.Keyword.PinYinInitials().ToLower(), Status = KeywordStatus.InUse }).ToList(); BulkImportResultViewModel> result = await _managementCompanyKeywordService.BulkImportAsync(companyKeywords); if (hasLogFile) { result.ErrorUrl = GerenratorBulkImportLogFileToOss(result.Data); } return Ok(result); } #endregion #region Marketing Keyword [HttpGet] [Route("~/marketingKeywords/pinyinExist")] [ResponseType(typeof(BooleanInputModel))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task MarketingKeywordPinYinExistAsync(string pinyin, MarketingKeywordType keywordType, long? excludedKeywordId = null) { var keywordIds = new List(); if (excludedKeywordId.HasValue) keywordIds.Add(excludedKeywordId.Value); bool isExist = await _managementMarketingKeywordService.PinYinExistAsync(pinyin, keywordType, keywordIds); return Ok(new BooleanInputModel { Bool = isExist }); } [HttpGet] [Route("~/marketingKeywords/keywordExist", Name = nameof(MarketingKeywordExistAsync))] [ResponseType(typeof(BooleanInputModel))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task MarketingKeywordExistAsync(string keyword, MarketingKeywordType keywordType, long? excludedKeywordId = null) { var keywordIds = new List(); if (excludedKeywordId.HasValue) keywordIds.Add(excludedKeywordId.Value); bool isExist = await _managementMarketingKeywordService.ExistAsync(keyword, keywordType, keywordIds); return Ok(new BooleanInputModel { Bool = isExist }); } [HttpGet] [Route("~/marketingKeywords/keywordIdExist", Name = nameof(MarketingKeywordIdExistAsync))] [ResponseType(typeof(BooleanInputModel))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task MarketingKeywordIdExistAsync(long keywordId, MarketingKeywordType keywordType, long? excludedKeywordId = null) { var keywordIds = new List(); if (excludedKeywordId.HasValue) keywordIds.Add(excludedKeywordId.Value); bool isExist = await _managementMarketingKeywordService.ExistAsync(keywordId, keywordType, keywordIds); return Ok(new BooleanInputModel { Bool = isExist }); } [HttpGet] [Route("~/marketingKeywords/{keywordId:long:min(1)}", Name = nameof(GetMarketingKeywordAsync))] [ResponseType(typeof(MarketingKeywordViewModel))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task GetMarketingKeywordAsync(long keywordId) { MarketingKeywordViewModel keyword = await _managementMarketingKeywordService.GetAsync(keywordId, false); return Ok(keyword); } [HttpPost] [Route("~/marketingKeywords/_create", Name = nameof(CreateMarketingKeywordAsync))] [ResponseType(typeof(CreatedViewModel))] [ValidateModel] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task CreateMarketingKeywordAsync(MarketingKeywordInputModel model) { long keywordId = await _managementMarketingKeywordService.CreateAsync(model); string url = Url.Link(nameof(GetMarketingKeywordAsync), new { keywordId }); return Created(url, new CreatedViewModel { Id = keywordId, Location = url }); } [HttpPost] [Route("~/marketingKeywords/_update", Name = nameof(UpdateMarketingKeywordAsync))] [ResponseType(typeof(long))] [ValidateModel] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task UpdateMarketingKeywordAsync(MarketingKeywordInputModel model) { long keywordId = await _managementMarketingKeywordService.UpdateAsync(model); return Ok(keywordId); } [HttpPost] [Route("~/marketingKeywords/disabled", Name = nameof(DisabledMarketingKeywordAsync))] [ResponseType(typeof(void))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task DisabledMarketingKeywordAsync(long[] keywordIds) { await _managementMarketingKeywordService.ChangeStatusAsync(KeywordStatus.NotUsedAnyMore, keywordIds); return Ok(); } [HttpPost] [Route("~/marketingKeywords/enabled", Name = nameof(EnabledMarketingKeywordAsync))] [ResponseType(typeof(void))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task EnabledMarketingKeywordAsync(long[] keywordIds) { await _managementMarketingKeywordService.ChangeStatusAsync(KeywordStatus.InUse, keywordIds); return Ok(); } [HttpPost] [Route("~/marketingKeywords/delete", Name = nameof(DeleteMarketingKeywordAsync))] [ResponseType(typeof(void))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task DeleteMarketingKeywordAsync(long[] keywordIds) { await _managementMarketingKeywordService.ChangeStatusAsync(KeywordStatus.Deleted, keywordIds); return Ok(); } [HttpGet] [Route("~/marketingKeywords", Name = nameof(GetMarketingKeywordListAsync))] [ResponseType(typeof(QueryResult))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task GetMarketingKeywordListAsync(string q = "", bool? isOnlyInUse = null, MarketingKeywordType? keywordType = null, int offset = 0, int limit = 20) { KeywordStatus? keywordStatus = null; if (isOnlyInUse.HasValue) { keywordStatus = isOnlyInUse.Value ? KeywordStatus.InUse : KeywordStatus.NotUsedAnyMore; } QueryResult results = await _managementMarketingKeywordService.GetListAsync(q, keywordStatus, keywordType, offset, limit); return Ok(results); } [HttpPost] [Route("~/marketingKeywords/bulkImport")] [ResponseType(typeof(BulkImportResultViewModel>))] [ApiClaimsAuthorize(FhtUserClaimType.KeywordManagement)] public async Task BulkImportMarketingKeywordAsync(string fileUrl, bool hasLogFile = false) { Stream stream = _fileUploadInfrastructureService.DownloadDocument(new Uri(fileUrl)); MarketingKeywordCsvViewModel[] csvKeywords = CsvHelper.Read(new StreamReader(stream)); List industries = _industryQuery.GetIndustries(); List marketingKeywords = csvKeywords.Select(s => new MarketingKeywordInputModel { Keyword = s.Keyword, KeywordType = s.KeywordType, PinYin = s.Keyword.PinYinInitials().ToLower(), Status = KeywordStatus.InUse }).ToList(); BulkImportResultViewModel> result = await _managementMarketingKeywordService.BulkImport(marketingKeywords); if (hasLogFile) { List lines = result.Data.Select(s => $"{s.Keyword},{s.KeywordType}").ToList(); result.ErrorUrl = GerenratorBulkImportLogFileToOss(lines); } return Ok(result); } private string GerenratorBulkImportLogFileToOss(List lines) { string content = string.Join("\r\n", lines); byte[] byteContent = Encoding.UTF8.GetBytes(content); var mStream = new MemoryStream(byteContent); string path = $"import_keyword_logs/{Guid.NewGuid()}.txt"; _fileUploadInfrastructureService.UploadDocument(path, mStream); return _ossHost + path; } #endregion } }