足力健后端,使用.netcore版本,合并1个项目使用

SetMobileDataController.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Common;
  4. using System.Common.Helpers;
  5. using System.IRepositories;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Model;
  9. using System.Security.Claims;
  10. using System.Threading.Tasks;
  11. using Microsoft.AspNetCore.Authorization;
  12. using Microsoft.AspNetCore.Mvc;
  13. using SqlSugar;
  14. using TVShoppingCallCenter_ZLJ.Models.Inputs;
  15. namespace TVShoppingCallCenter_ZLJ.Controllers.Traffic
  16. {
  17. [Authorize]
  18. [Produces("application/json")]
  19. [Route("api/[controller]")]
  20. public class SetMobileDataController : BaseController
  21. {
  22. private readonly ISys_MobileDataRepository _sys_mobiledataRepository;
  23. public SetMobileDataController(ISys_MobileDataRepository sys_mobiledataRepository)
  24. {
  25. _sys_mobiledataRepository = sys_mobiledataRepository;
  26. }
  27. /// <summary>
  28. /// 获取列表
  29. /// </summary>
  30. /// <param name="filter"></param>
  31. /// <returns></returns>
  32. [HttpGet("getlistbypage")]
  33. public async Task<IActionResult> GetListsByPage(string key, int pageindex = 1, int pagesize = 20)
  34. {
  35. List<IConditionalModel> conModels = new List<IConditionalModel>();
  36. #region 条件筛选
  37. conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
  38. if (!string.IsNullOrEmpty(key))
  39. {
  40. conModels.Add(new ConditionalCollections()
  41. {
  42. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
  43. {
  44. new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_MobileNum", ConditionalType = ConditionalType.Like, FieldValue = key }),
  45. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_ZipCode", ConditionalType = ConditionalType.Like, FieldValue = key }),
  46. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or, new ConditionalModel() { FieldName = "F_CityDes", ConditionalType = ConditionalType.Like, FieldValue = key }),
  47. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or, new ConditionalModel() { FieldName = "F_CardDes", ConditionalType = ConditionalType.Like, FieldValue = key })
  48. }
  49. });
  50. }
  51. #endregion
  52. int recordCount = 0;
  53. var list = await _sys_mobiledataRepository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount }, "F_CreateOn desc");
  54. return Success("成功", list);
  55. }
  56. /// <summary>
  57. /// 获取实体
  58. /// </summary>
  59. [HttpGet("getdetails")]
  60. public async Task<IActionResult> GetDetails(int id)
  61. {
  62. if (id > 0)
  63. {
  64. var dModel = await _sys_mobiledataRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  65. if (dModel != null)
  66. {
  67. return Success("获取基础数据成功", dModel);
  68. }
  69. else
  70. {
  71. return Error("获取基础数据失败");
  72. }
  73. }
  74. else
  75. {
  76. return Error("获取参数失败");
  77. }
  78. }
  79. /// <summary>
  80. /// 添加基础数据
  81. /// </summary>
  82. [HttpPost("add")]
  83. public async Task<IActionResult> Add(MobileDataInput input)
  84. {
  85. #region 验证
  86. if (string.IsNullOrWhiteSpace(input.mobilenum))
  87. {
  88. return Error("号段不能为空");
  89. }
  90. if (string.IsNullOrWhiteSpace(input.zipcode))
  91. {
  92. return Error("区号不能为空");
  93. }
  94. if (await GetExistByCodeAsync(input.mobilenum, 0))
  95. return Error("已存在此号段的记录,请重新输入!");
  96. #endregion
  97. T_Sys_MobileData clmodel = new T_Sys_MobileData();
  98. clmodel.F_MobileNum = input.mobilenum.Trim();
  99. clmodel.F_ZipCode = input.zipcode.Trim();
  100. clmodel.F_CityDes = input.citydes.Trim();
  101. clmodel.F_CardDes = input.carddes.Trim();
  102. clmodel.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
  103. clmodel.F_CreateOn = DateTime.Now;
  104. clmodel.F_State = (int)EnumDelState.Enabled;
  105. var res = await _sys_mobiledataRepository.Add(clmodel);
  106. if (res > 0)
  107. return Success("保存成功");
  108. else
  109. {
  110. return Error("保存失败");
  111. }
  112. }
  113. /// <summary>
  114. /// 修改基础数据
  115. /// </summary>
  116. [HttpPost("update")]
  117. public async Task<IActionResult> Edit(MobileDataInput input)
  118. {
  119. #region
  120. if (input.id <= 0)
  121. return Error("请选择要编辑的数据");
  122. if (string.IsNullOrWhiteSpace(input.mobilenum))
  123. {
  124. return Error("号段不能为空");
  125. }
  126. if (string.IsNullOrWhiteSpace(input.zipcode))
  127. {
  128. return Error("区号不能为空");
  129. }
  130. if (await GetExistByCodeAsync(input.mobilenum, input.id))
  131. return Error("已存在此号段的记录,请重新输入!");
  132. #endregion
  133. var clmodel = await _sys_mobiledataRepository.GetSingle(x => x.F_Id == input.id && x.F_State == (int)EnumDelState.Enabled);
  134. if (clmodel == null)
  135. return Error("信息获取失败");
  136. clmodel.F_MobileNum = input.mobilenum.Trim();
  137. clmodel.F_ZipCode = input.zipcode.Trim();
  138. clmodel.F_CityDes = input.citydes.Trim();
  139. clmodel.F_CardDes = input.carddes.Trim();
  140. clmodel.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  141. clmodel.F_LastModifyOn = DateTime.Now;
  142. var res = await _sys_mobiledataRepository.Update(clmodel);
  143. if (res)
  144. return Success("基础数据保存成功");
  145. else
  146. {
  147. return Error("基础数据保存失败");
  148. }
  149. }
  150. [HttpPost("delete")]
  151. public async Task<IActionResult> Remove(int[] ids)
  152. {
  153. //使用逻辑删除
  154. //物理删除的数据无法恢复
  155. var res = 0;
  156. if (ids != null && ids.Length > 0)
  157. {
  158. foreach (var item in ids)
  159. {
  160. var ml = await _sys_mobiledataRepository.GetSingle(x => x.F_Id == item);
  161. ml.F_State = (int)EnumDelState.Delete;
  162. ml.F_DeleteBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  163. ml.F_DeleteOn = DateTime.Now;
  164. if (await _sys_mobiledataRepository.Update(ml))
  165. res += 1;
  166. }
  167. if (res == ids.Length)
  168. return Success("删除成功");
  169. else if (res > 0 && res < ids.Length)
  170. return Error("部分删除失败,请查看后重新操作");
  171. else
  172. return Error("删除失败,请查看后重新操作");
  173. }
  174. else
  175. return Error("请选择要删除的记录");
  176. }
  177. /// <summary>
  178. /// 导入号码段
  179. /// </summary>
  180. /// <param name="input"></param>
  181. /// <returns></returns>
  182. [HttpPost("importmobile")]
  183. public async Task<IActionResult> ImportMobile()
  184. {
  185. Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
  186. if (!_upfile.ContentType.Equals("application/vnd.ms-excel") && !_upfile.ContentType.Equals("application/x-xls") && !_upfile.ContentType.Equals("application/x-xlsx") && !_upfile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && !_upfile.ContentType.Equals("application/octet-stream"))
  187. return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
  188. //将数据导入数据库
  189. int headrow = 0;
  190. NPOIHelper npoi = new NPOIHelper();
  191. var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
  192. if (dtExcel.Rows.Count <= 0)
  193. return Error("请选择要导入的文件");
  194. IList<MobileDataInput> IMobileDatas = ModelConvertHelper<MobileDataInput>.ConvertToModel(dtExcel);
  195. List<MobileDataInput> MobileDatas = ModelConvertHelper<MobileDataInput>.ConvertIListToList(IMobileDatas);
  196. int num = MobileDatas.Count;
  197. List<MobileDataInput> inputlist = new List<MobileDataInput>();
  198. foreach (MobileDataInput input in MobileDatas)
  199. {
  200. Expression<Func<T_Sys_MobileData, bool>> eq = a => a.F_MobileNum == input.mobilenum;
  201. eq = eq.Or(b => b.F_ZipCode == input.zipcode);
  202. //eq = eq.Or(b => b.F_ZipCode == input.zipcode);
  203. //eq = eq.Or(b => b.F_CityDes == input.citydes);
  204. //eq = eq.Or(b => b.F_CardDes == input.carddes);
  205. if (await _sys_mobiledataRepository.GetCount(eq) > 0)
  206. {
  207. return Error("号段 有重复:" + input.mobilenum);
  208. }
  209. inputlist.Add(input);
  210. }
  211. if (await AddForInput(inputlist))
  212. {
  213. return Success("已导入数据");
  214. }
  215. else
  216. {
  217. return Error("导入失败");
  218. }
  219. }
  220. #region 私有方法
  221. /// <summary>
  222. /// 根据基础数据编号查询是否存在此基础数据编号
  223. /// </summary>
  224. /// <param name="code"></param>
  225. /// <returns></returns>
  226. private async Task<bool> GetExistByCodeAsync(string code, int id)
  227. {
  228. long c = 0;
  229. if (id > 0)
  230. c = await _sys_mobiledataRepository.GetCount(x => x.F_MobileNum.Equals(code) && x.F_Id != id && x.F_State == (int)EnumDelState.Enabled);
  231. else
  232. c = await _sys_mobiledataRepository.GetCount(x => x.F_MobileNum.Equals(code) && x.F_State == (int)EnumDelState.Enabled);
  233. return c > 0;
  234. }
  235. private async Task<bool> AddForInput(List<MobileDataInput> inputs)
  236. {
  237. List<T_Sys_MobileData> inputlist = new List<T_Sys_MobileData>();
  238. foreach (var input in inputs)
  239. {
  240. T_Sys_MobileData clmodel = new T_Sys_MobileData();
  241. clmodel.F_MobileNum = input.mobilenum.Trim();
  242. clmodel.F_ZipCode = input.zipcode.Trim();
  243. clmodel.F_CityDes = input.citydes.Trim();
  244. clmodel.F_CardDes = input.carddes.Trim();
  245. clmodel.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
  246. clmodel.F_CreateOn = DateTime.Now;
  247. clmodel.F_State = (int)EnumDelState.Enabled;
  248. inputlist.Add(clmodel);
  249. }
  250. if (await _sys_mobiledataRepository.AddMany(inputlist))
  251. {
  252. return true;
  253. }
  254. else
  255. {
  256. return false;
  257. }
  258. }
  259. #endregion
  260. }
  261. }