UserCashRecordController.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Options;
  8. using Microsoft.AspNetCore.Authorization;
  9. using System.Web;
  10. using MySystem.MainModels;
  11. using LitJson;
  12. using Library;
  13. namespace MySystem.Areas.Api.Controllers.v1
  14. {
  15. [Area("Api")]
  16. [Route("Api/v1/[controller]/[action]")]
  17. public class UserCashRecordController : BaseController
  18. {
  19. public UserCashRecordController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 创客-我的-提现-提交提现申请
  23. [Authorize]
  24. public JsonResult PostWithdraw(string value)
  25. {
  26. value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. AppResultJson result = PostWithdrawDo(value);
  29. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  30. }
  31. public AppResultJson PostWithdrawDo(string value)
  32. {
  33. JsonData data = JsonMapper.ToObject(value);
  34. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  35. decimal TradeAmount = decimal.Parse(function.CheckNum(data["TradeAmount"].ToString())); //交易金额
  36. var amount = 0;//手续费
  37. if (TradeAmount < 100)
  38. {
  39. return new AppResultJson() { Status = "-1", Info = "100元起提" };
  40. }
  41. if (TradeAmount < 500)
  42. {
  43. amount = 3;
  44. }
  45. UserAccount account = maindb.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new UserAccount();
  46. if (account.AccountStatus == 1)
  47. {
  48. return new AppResultJson() { Status = "-1", Info = "参数错误,请联系客服" };
  49. }
  50. if (account.BalanceAmount <= 0 || account.BalanceAmount - account.ToChargeAmount <= 0)
  51. {
  52. return new AppResultJson() { Status = "-1", Info = "余额不足" };
  53. }
  54. if (account.BalanceAmount - account.ToChargeAmount < TradeAmount)
  55. {
  56. TradeAmount = account.BalanceAmount - account.ToChargeAmount;
  57. }
  58. Dictionary<string, object> Obj = new Dictionary<string, object>();
  59. string CashOrderNo = "BC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  60. Users user = UsersDbconn.Instance.Get(UserId) ?? new Users();
  61. decimal ManageFee = TradeAmount * 0.08M;
  62. decimal ActualTradeAmount = TradeAmount - ManageFee;
  63. decimal TradeFee = 0; // 服务费
  64. if(TradeAmount < 500) //单次提现500元以下的订单收取3元提现服务费/笔
  65. {
  66. TradeFee = 3;
  67. ActualTradeAmount -= TradeFee;
  68. }
  69. UserCashRecord cash = maindb.UserCashRecord.Add(new UserCashRecord()
  70. {
  71. CreateDate = DateTime.Now,
  72. CashOrderNo = CashOrderNo, //申请提现单号
  73. UserId = UserId, //创客
  74. IdCardNo = user.CertId, //身份证号
  75. SettleBankCardNo = user.SettleBankCardNo, //提现卡号
  76. SettleBankName = user.SettleBankName, //银行名称
  77. TradeType = 2, //交易类型
  78. TradeAmount = TradeAmount, //交易金额
  79. ActualTradeAmount = ActualTradeAmount,
  80. ManageFee = ManageFee,
  81. TradeFee = TradeFee,
  82. }).Entity;
  83. maindb.SaveChanges();
  84. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  85. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  86. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  87. account.BalanceAmount -= TradeAmount;
  88. account.FreezeAmount += TradeAmount;
  89. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  90. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  91. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  92. maindb.UserAccountRecord.Add(new UserAccountRecord()
  93. {
  94. CreateDate = DateTime.Now,
  95. UpdateDate = DateTime.Now,
  96. UserId = user.Id, //创客
  97. ChangeType = 2, //变动类型
  98. ProductType = 99, //产品类型
  99. ChangeAmount = TradeAmount, //变更金额
  100. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  101. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  102. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  103. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  104. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  105. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  106. });
  107. maindb.SaveChanges();
  108. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  109. }
  110. #endregion
  111. #region 创客-我的-提现-提现记录
  112. [Authorize]
  113. public JsonResult List(string value)
  114. {
  115. value = DesDecrypt(value);
  116. JsonData data = JsonMapper.ToObject(value);
  117. List<Dictionary<string, object>> dataList = ListDo(value);
  118. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  119. }
  120. public List<Dictionary<string, object>> ListDo(string value)
  121. {
  122. JsonData data = JsonMapper.ToObject(value);
  123. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  124. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  125. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  126. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  127. // List<int> query = UserCashRecordDbconn.Instance.GetList(UserId, PageNum, PageSize);
  128. IQueryable<UserCashRecord> query = maindb.UserCashRecord.Where(m => m.UserId == UserId && m.TradeType < 3).OrderByDescending(m => m.Id);
  129. if (PageNum == 1)
  130. {
  131. query = query.Take(PageSize);
  132. }
  133. else
  134. {
  135. int skipNum = PageSize * (PageNum - 1);
  136. query = query.Skip(skipNum).Take(PageSize);
  137. }
  138. foreach (UserCashRecord subdata in query.ToList())
  139. {
  140. // UserCashRecord subdata = UserCashRecordDbconn.Instance.Get(CashRecordId) ?? new UserCashRecord();
  141. Dictionary<string, object> curData = new Dictionary<string, object>();
  142. curData.Add("TradeAmount", subdata.TradeAmount); //交易金额
  143. curData.Add("Remark", "提现到" + subdata.SettleBankName + "(" + subdata.SettleBankCardNo.Substring(subdata.SettleBankCardNo.Length - 4) + ")"); //备注
  144. curData.Add("Status", subdata.Status); //状态,0-处理中,1-交易成功,2-提现失败,3-挂账,-1-状态异常
  145. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
  146. curData.Add("Id", subdata.Id); //Id
  147. dataList.Add(curData);
  148. }
  149. return dataList;
  150. }
  151. #endregion
  152. #region 创客-盟主可提现金额提现-提交提现申请
  153. [Authorize]
  154. public JsonResult LeaderPostWithdraw(string value)
  155. {
  156. value = DesDecrypt(value);
  157. JsonData data = JsonMapper.ToObject(value);
  158. AppResultJson result = LeaderPostWithdrawDo(value);
  159. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  160. }
  161. public AppResultJson LeaderPostWithdrawDo(string value)
  162. {
  163. JsonData data = JsonMapper.ToObject(value);
  164. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  165. decimal TradeAmount = decimal.Parse(function.CheckNum(data["TradeAmount"].ToString())); //交易金额
  166. if (TradeAmount < 100)
  167. {
  168. return new AppResultJson() { Status = "-1", Info = "100元起提" };
  169. }
  170. UserAccount account = maindb.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new UserAccount();
  171. if (account.AccountStatus == 1)
  172. {
  173. return new AppResultJson() { Status = "-1", Info = "参数错误,请联系客服" };
  174. }
  175. if (account.LeaderBalanceAmount <= 0)
  176. {
  177. return new AppResultJson() { Status = "-1", Info = "余额不足" };
  178. }
  179. if (account.LeaderBalanceAmount < TradeAmount)
  180. {
  181. TradeAmount = account.LeaderBalanceAmount;
  182. }
  183. Dictionary<string, object> Obj = new Dictionary<string, object>();
  184. string CashOrderNo = "MZTX" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  185. Users user = UsersDbconn.Instance.Get(UserId) ?? new Users();
  186. decimal ActualTradeAmount = TradeAmount;
  187. decimal ManageFee = TradeAmount - ActualTradeAmount;
  188. UserCashRecord cash = maindb.UserCashRecord.Add(new UserCashRecord()
  189. {
  190. CreateDate = DateTime.Now,
  191. CashOrderNo = CashOrderNo, //申请提现单号
  192. UserId = UserId, //创客
  193. IdCardNo = user.CertId, //身份证号
  194. SettleBankCardNo = user.SettleBankCardNo, //提现卡号
  195. SettleBankName = user.SettleBankName, //银行名称
  196. TradeType = 3, //交易类型(3 盟主可提现金额提现)
  197. TradeAmount = TradeAmount, //交易金额
  198. ActualTradeAmount = TradeAmount,
  199. ManageFee = ManageFee,
  200. }).Entity;
  201. maindb.SaveChanges();
  202. decimal BeforeBalanceAmount = account.LeaderBalanceAmount; //变更前余额
  203. account.LeaderBalanceAmount -= TradeAmount;
  204. decimal AfterBalanceAmount = account.LeaderBalanceAmount; //变更后余额
  205. maindb.LeaderAccountRecord.Add(new LeaderAccountRecord()
  206. {
  207. CreateDate = DateTime.Now,
  208. Sort = cash.Id,//提现详情Id
  209. TransRecordNo = CashOrderNo,//提现单号
  210. SeoDescription = "提现到" + user.SettleBankName + "(" + user.SettleBankCardNo.Substring(user.SettleBankCardNo.Length - 4) + ")",//详情
  211. UserId = user.Id, //创客
  212. ChangeType = 2, //变动类型
  213. ProductType = 99, //产品类型
  214. ChangeAmount = TradeAmount, //变更金额
  215. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  216. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  217. });
  218. maindb.SaveChanges();
  219. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  220. }
  221. #endregion
  222. #region 创客-盟主可提现金额提现-提现记录
  223. [Authorize]
  224. public JsonResult LeaderList(string value)
  225. {
  226. value = DesDecrypt(value);
  227. JsonData data = JsonMapper.ToObject(value);
  228. List<Dictionary<string, object>> dataList = LeaderListDo(value);
  229. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  230. }
  231. public List<Dictionary<string, object>> LeaderListDo(string value)
  232. {
  233. JsonData data = JsonMapper.ToObject(value);
  234. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  235. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  236. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  237. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  238. IQueryable<UserCashRecord> query = maindb.UserCashRecord.Where(m => m.UserId == UserId && m.TradeType == 3).OrderByDescending(m => m.Id);
  239. if (PageNum == 1)
  240. {
  241. query = query.Take(PageSize);
  242. }
  243. else
  244. {
  245. int skipNum = PageSize * (PageNum - 1);
  246. query = query.Skip(skipNum).Take(PageSize);
  247. }
  248. foreach (UserCashRecord subdata in query.ToList())
  249. {
  250. Dictionary<string, object> curData = new Dictionary<string, object>();
  251. curData.Add("TradeAmount", subdata.ActualTradeAmount); //交易金额
  252. curData.Add("Remark", "提现到" + subdata.SettleBankName + "(" + subdata.SettleBankCardNo.Substring(subdata.SettleBankCardNo.Length - 4) + ")"); //备注
  253. curData.Add("Status", subdata.Status); //状态,0-处理中,1-交易成功,2-提现失败,3-挂账,-1-状态异常
  254. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
  255. curData.Add("Id", subdata.Id); //Id
  256. curData.Add("DetailId", subdata.Id); //详情Id
  257. dataList.Add(curData);
  258. }
  259. return dataList;
  260. }
  261. #endregion
  262. #region 创客-我的-提现-提现记录详情
  263. [Authorize]
  264. public JsonResult Detail(string value)
  265. {
  266. value = DesDecrypt(value);
  267. JsonData data = JsonMapper.ToObject(value);
  268. Dictionary<string, object> Obj = DetailDo(value);
  269. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  270. }
  271. public Dictionary<string, object> DetailDo(string value)
  272. {
  273. JsonData data = JsonMapper.ToObject(value);
  274. Dictionary<string, object> Obj = new Dictionary<string, object>();
  275. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  276. UserCashRecord query = UserCashRecordDbconn.Instance.Get(Id) ?? new UserCashRecord();
  277. Obj.Add("CashOrderNo", query.CashOrderNo); //申请提现单号
  278. Obj.Add("TradeAmount", query.TradeAmount); //交易金额
  279. Obj.Add("ActualTradeAmount", query.ActualTradeAmount); //实际交易金额
  280. Obj.Add("TradeFee", query.TradeFee); //交易手续费
  281. Obj.Add("ManageFee", query.ManageFee); //扣税金额
  282. Obj.Add("PaymentDate", query.PaymentDate == null ? "" : query.PaymentDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //支付时间
  283. Obj.Add("Remark", query.Remark); //备注
  284. Obj.Add("Id", query.Id); //Id
  285. Obj.Add("CreateDate", query.CreateDate); //创建时间
  286. Obj.Add("Status", query.Status); //状态,0-处理中,1-交易成功,2-提现失败,3-挂账,-1-状态异常
  287. return Obj;
  288. }
  289. #endregion
  290. #region 创客-我的-盟主可提现金额提现-提现记录详情
  291. [Authorize]
  292. public JsonResult LeaderDetail(string value)
  293. {
  294. value = DesDecrypt(value);
  295. JsonData data = JsonMapper.ToObject(value);
  296. Dictionary<string, object> Obj = LeaderDetailDo(value);
  297. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  298. }
  299. public Dictionary<string, object> LeaderDetailDo(string value)
  300. {
  301. JsonData data = JsonMapper.ToObject(value);
  302. Dictionary<string, object> Obj = new Dictionary<string, object>();
  303. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  304. UserCashRecord query = maindb.UserCashRecord.FirstOrDefault(m => m.Id == Id) ?? new UserCashRecord();
  305. Obj.Add("CashOrderNo", query.CashOrderNo); //申请提现单号
  306. Obj.Add("TradeAmount", query.TradeAmount); //交易金额
  307. Obj.Add("ActualTradeAmount", query.ActualTradeAmount); //实际交易金额
  308. Obj.Add("TradeFee", query.TradeFee); //交易手续费
  309. Obj.Add("ManageFee", query.ManageFee); //扣税金额
  310. Obj.Add("PaymentDate", query.PaymentDate == null ? "" : query.PaymentDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //支付时间
  311. Obj.Add("Remark", query.Remark); //备注
  312. Obj.Add("Id", query.Id); //Id
  313. Obj.Add("CreateDate", query.CreateDate); //创建时间
  314. Obj.Add("Status", query.Status); //状态,0-处理中,1-交易成功,2-提现失败,3-挂账,-1-状态异常
  315. return Obj;
  316. }
  317. #endregion
  318. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  319. /// <summary>
  320. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  321. /// </summary>
  322. /// <param name="value">请求的参数(json字符串)</param>
  323. /// <param name="signField">要签名的字段</param>
  324. /// <returns></returns>
  325. private string CheckSign(string value, string[] signField)
  326. {
  327. JsonData json = JsonMapper.ToObject(value);
  328. Dictionary<string, string> dic = new Dictionary<string, string>();
  329. for (int i = 0; i < signField.Length; i++)
  330. {
  331. dic.Add(signField[i], json[signField[i]].ToString());
  332. }
  333. string sign = json["sign"].ToString(); //客户端签名字符串
  334. return new Sign().sign(dic, sign);
  335. }
  336. #endregion
  337. }
  338. }