颐和api

ApplicationsVersionController.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using MadRunFabric.Common;
  7. using Microsoft.Extensions.Logging;
  8. using MessageApi.IRepositories;
  9. using MadRunFabric.Model;
  10. using MongoDB.Driver;
  11. using Microsoft.AspNetCore.Authorization;
  12. using System.Security.Claims;
  13. using Microsoft.Extensions.Configuration;
  14. namespace MessageApi.Controllers
  15. {
  16. /// <summary>
  17. /// App版本信息控制器 - 升级提示接口
  18. /// </summary>
  19. [Authorize]
  20. [ApiVersion("6.0")]
  21. [Produces("application/json")]
  22. [Route("api/[controller]")]
  23. public class ApplicationsVersionController : BaseController
  24. {
  25. private readonly IConfiguration _configuration;
  26. private readonly ILogger<ApplicationsVersionController> _logger;
  27. private readonly IApp_ApplicationsVersionRepository _app_applicationsVersionRepository;
  28. public ApplicationsVersionController(
  29. IConfiguration configuration,
  30. ILogger<ApplicationsVersionController> logger,
  31. IApp_ApplicationsVersionRepository app_applicationsVersionRepository
  32. )
  33. {
  34. _configuration = configuration;
  35. _logger = logger;
  36. _app_applicationsVersionRepository = app_applicationsVersionRepository;
  37. }
  38. /// <summary>
  39. /// 获取所有App版本列表
  40. /// </summary>
  41. /// <returns></returns>
  42. [HttpGet("getall")]
  43. public async Task<IActionResult> GetAllListsAsync()
  44. {
  45. //Task<IEnumerable<App_ApplicationsVersion>>
  46. var list = await _app_applicationsVersionRepository.GetAll();
  47. return Success("获取全部数据成功", list);
  48. }
  49. /// <summary>
  50. /// 获取App版本列表 by page
  51. /// </summary>
  52. /// <param name="apptype">应用分类(0android 1ios)</param>
  53. /// <param name="keyword">版本编号</param>
  54. /// <param name="createtime">添加开始时间</param>
  55. /// <param name="createtime">添加结束时间</param>
  56. /// <param name="pageindex">当前页</param>
  57. /// <param name="pagesize">每页条数</param>
  58. /// <returns></returns>
  59. [HttpGet("getlistbypage")]
  60. public async Task<IActionResult> GetListsByPageAsync(int apptype, string keyword, string stime, string etime, int pageindex = 1, int pagesize = 10)
  61. {
  62. #region 条件信息
  63. ////排序字段
  64. var sort = Builders<App_ApplicationsVersion>.Sort.Descending("createtime");
  65. var list = await _app_applicationsVersionRepository.GetByPage(null, pageindex, pagesize, sort);
  66. var redCount = await _app_applicationsVersionRepository.CountAsync(null); //获取总数
  67. //根据条件查询集合
  68. var listApp = new List<FilterDefinition<App_ApplicationsVersion>>();
  69. listApp.Add(Builders<App_ApplicationsVersion>.Filter.Eq("isdelete", 0));
  70. if (apptype>0)
  71. listApp.Add(Builders<App_ApplicationsVersion>.Filter.Eq("apptype", apptype));
  72. if (!string.IsNullOrEmpty(stime))
  73. listApp.Add(Builders<App_ApplicationsVersion>.Filter.Gt("createtime", stime));
  74. if (!string.IsNullOrEmpty(etime))
  75. listApp.Add(Builders<App_ApplicationsVersion>.Filter.Lt("createtime", etime));
  76. //模糊查询
  77. if (!string.IsNullOrEmpty(keyword))
  78. listApp.Add(Builders<App_ApplicationsVersion>.Filter.Where(s => s.versionname.Contains(keyword) || s.versioncode.Contains(keyword)));
  79. #endregion
  80. int recordCount = 0;
  81. if (listApp.Count > 0)
  82. {
  83. var filter = Builders<App_ApplicationsVersion>.Filter.And(listApp);
  84. list = await _app_applicationsVersionRepository.GetByPage(filter, pageindex, pagesize, sort);
  85. redCount = await _app_applicationsVersionRepository.CountAsync(filter); //获取总数
  86. }
  87. recordCount = int.Parse(redCount.ToString());
  88. var obj = new
  89. {
  90. rows = list,
  91. total = recordCount
  92. };
  93. return Success("获取成功", obj);
  94. }
  95. /// <summary>
  96. /// 获取App版本详情 by id
  97. /// </summary>
  98. /// <param name="id">App版本id</param>
  99. /// <returns></returns>
  100. [HttpGet("getdetailes")]
  101. public async Task<IActionResult> GetDetailsAsync(string id)
  102. {
  103. if (string.IsNullOrEmpty(id))
  104. return Error("参数错误");
  105. var model = await _app_applicationsVersionRepository.GetSingle(id);
  106. if (model != null)
  107. {
  108. var result = new
  109. {
  110. apptype = model.apptype,
  111. versionname = model.versionname,
  112. versioncode = model.versioncode,
  113. createby = model.createby,
  114. createtime = model.createby,
  115. updatetime = model.updatetime,
  116. ordernum = model.ordernum,
  117. downurl = model.downurl,
  118. httpurl = model.httpurl,
  119. note = model.note,
  120. };
  121. return Success("获取成功!", result);
  122. }
  123. return Error("获取失败");
  124. }
  125. /// <summary>
  126. /// 添加App版本信息
  127. /// </summary>
  128. /// <param name="apptype">应用分类(0android 1ios)</param>
  129. /// <param name="versionname">版本名称</param>
  130. /// <param name="versioncode">版本号</param>
  131. /// <param name="ordernum">排序</param>
  132. /// <param name="downurl">下载地址</param>
  133. /// <param name="note">备注</param>
  134. /// <returns></returns>
  135. [HttpPost("add")]
  136. public async Task<IActionResult> AddAsync(int apptype, string versionname, string versioncode, int ordernum, List<FileBaseModel> downurl,string httpurl, string note)
  137. {
  138. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  139. if (string.IsNullOrEmpty(versionname))
  140. return Error("版本名称不能为空");
  141. if (string.IsNullOrEmpty(versioncode))
  142. return Error("版本号不能为空");
  143. //if (string.IsNullOrEmpty(downurl))
  144. // return Error("版本下载地址不能为空");
  145. var model = new App_ApplicationsVersion();
  146. model.apptype = apptype;
  147. model.versionname = versionname;
  148. model.versioncode = versioncode;
  149. model.createby = usercode;
  150. model.createtime = DateTime.Now.ToLocalTime();
  151. model.ordernum = ordernum;
  152. model.downurl = downurl.ToList<FileBaseModel>();
  153. model.httpurl = httpurl;
  154. model.note = note;
  155. model.isdelete = 0;
  156. //var model = new App_ApplicationsVersion();
  157. //model.apptype = 0;
  158. //model.versionname = "2018App端版本v1.1";
  159. //model.versioncode = "1.1";
  160. //model.createby = "8000";
  161. //model.createtime = DateTime.Now.ToLocalTime();
  162. //model.ordernum = 1;
  163. //model.downurl = null;
  164. //model.httpurl = "http://www.iciba.com";
  165. //model.note = "";
  166. //model.isdelete = 0;
  167. bool b = await _app_applicationsVersionRepository.Add(model);
  168. if (b)
  169. return Success("添加成功");
  170. //日志
  171. _logger.LogError($"版本名称:{versionname},版本号:{versioncode}添加失败");
  172. return Error("添加失败");
  173. }
  174. /// <summary>
  175. /// 修改App版本信息
  176. /// </summary>
  177. /// <param name="id"></param>
  178. /// <param name="apptype">应用分类(0android 1ios)</param>
  179. /// <param name="versionname">版本名称</param>
  180. /// <param name="versioncode">版本号</param>
  181. /// <param name="ordernum">排序</param>
  182. /// <param name="downurl">下载地址</param>
  183. /// <param name="note">备注</param>
  184. /// <returns></returns>
  185. [HttpPost("update")]
  186. public async Task<IActionResult> UpdateAsync(string id, int apptype, string versionname, string versioncode, int ordernum, List<FileBaseModel> downurl,string httpurl, string note)
  187. {
  188. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  189. if (string.IsNullOrEmpty(id))
  190. return Error("参数错误");
  191. if (string.IsNullOrEmpty(versionname))
  192. return Error("版本名称不能为空");
  193. if (string.IsNullOrEmpty(versioncode))
  194. return Error("版本号不能为空");
  195. //if (string.IsNullOrEmpty(downurl))
  196. // return Error("版本下载地址不能为空");
  197. App_ApplicationsVersion model = new App_ApplicationsVersion();
  198. model = await _app_applicationsVersionRepository.GetSingle(id);
  199. if (model == null)
  200. return Error("操作失败");
  201. model.apptype = apptype;
  202. model.versionname = versionname;
  203. model.versioncode = versioncode;
  204. model.createby = usercode;
  205. //model.createtime = DateTime.Now.ToLocalTime();
  206. model.updatetime = DateTime.Now.ToLocalTime();
  207. model.ordernum = ordernum;
  208. model.downurl = downurl.ToList<FileBaseModel>();
  209. model.httpurl = httpurl;
  210. model.note = note;
  211. //model.isdelete = 0;
  212. bool b = await _app_applicationsVersionRepository.Update(model);
  213. if(b)
  214. return Success("保存成功");
  215. //日志
  216. _logger.LogError($"版本名称:{versionname},版本号:{versioncode}修改失败");
  217. return Error("保存失败");
  218. }
  219. /// <summary>
  220. /// 删除App版本信息 by ids
  221. /// </summary>
  222. /// <param name="ids">string[] id</param>
  223. /// <returns></returns>
  224. [HttpPost("delete")]
  225. public async Task<IActionResult> DeleteAsync(string[] ids)
  226. {
  227. int num = 0;
  228. if (ids == null && ids.Length <= 0)
  229. return Error("请选择要删除的记录");
  230. foreach (var item in ids)
  231. {
  232. var model = await _app_applicationsVersionRepository.GetSingle(item);
  233. model.isdelete = -1;
  234. if (await _app_applicationsVersionRepository.Update(model))
  235. num += 1;
  236. }
  237. if (num == ids.Length)
  238. return Success("删除成功");
  239. if (num > 0 && num < ids.Length)
  240. return Error("部分删除失败,请查看后重新操作");
  241. return Error("删除失败,请查看后重新操作");
  242. }
  243. #region App版本升级提示
  244. /// <summary>
  245. /// 获取最新App版本详情
  246. /// </summary>
  247. /// <returns></returns>
  248. [HttpGet("getnewdetailes")]
  249. public async Task<IActionResult> GetNewDetailsAsync(int apptype = 1)
  250. {
  251. string oldurl = _configuration["downurl:oldurls"].ToString().ToLower();
  252. string newurl = _configuration["downurl:newurls"].ToString().ToLower();
  253. //排序字段
  254. var sort = Builders<App_ApplicationsVersion>.Sort.Descending("createtime");
  255. var model = await _app_applicationsVersionRepository.GetSingle(x => x.isdelete == 0 && x.apptype == apptype, sort);
  256. //其他方法
  257. //Task<IEnumerable<App_ApplicationsVersion>> list = _app_applicationsVersionRepository.Get(null, null, sort);
  258. //var model = list.Result.SingleOrDefault();
  259. if (model != null)
  260. {
  261. if (model.downurl != null)
  262. {
  263. foreach (var item in model.downurl.ToArray())
  264. {
  265. if (item.fileurl.Contains(oldurl))
  266. {
  267. item.fileurl = item.fileurl.Replace(oldurl, newurl);
  268. }
  269. }
  270. }
  271. var result = new
  272. {
  273. id = model.id,
  274. apptype = model.apptype,
  275. versionname = model.versionname,
  276. versioncode = model.versioncode,
  277. createby = model.createby,
  278. createtime = model.createtime.ToLocalTime(),
  279. updatetime = model.updatetime.ToLocalTime(),
  280. ordernum = model.ordernum,
  281. downurl = model.downurl,
  282. httpurl = model.httpurl,
  283. note = model.note,
  284. };
  285. return Success("获取成功!", result);
  286. }
  287. return Success("获取成功!", null);
  288. }
  289. #endregion
  290. }
  291. }