LotteryMoneyConfirm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: scstf
  5. * Date: 2018/10/15
  6. * Time: 10:27
  7. */
  8. namespace Biz\Money;
  9. class LotteryMoneyConfirm
  10. {
  11. public $order_id;
  12. public $account_name;
  13. public $account_identity;
  14. public $money;
  15. public $money_cash;
  16. public $lottery_requestObj;
  17. public $operator;
  18. public $operation_time;
  19. public $rate;
  20. public $trade_type;
  21. public $money_type;
  22. /**
  23. * 根据用户ID和申请ID自动为符合条件的请求写入流水差额记录、资金记录和资金详情记录,有错返回5100开始的错误码
  24. * @param int $lottery_req_id
  25. * @param string $rechargeDetail
  26. * @param string $waterDetail
  27. * @return int
  28. */
  29. public function autoPass($lottery_req_id, $rechargeDetail = '', $waterDetail = '',$type='')
  30. {
  31. $this->operator = 'system_auto';
  32. $this->trade_type = 12;
  33. $this->operation_time = date('Y-m-d H:i:s', time());
  34. $this->lottery_requestObj = lm('lottery_money_log', 'api')->where('id', $lottery_req_id)->first();
  35. if (!$this->lottery_requestObj)
  36. return -30019;//彩金申请记录不存在
  37. if (!$this->lottery_requestObj->money)
  38. return -51013;//彩金申请金额为0
  39. if(empty($type)){
  40. if ($this->lottery_requestObj->status == 1)
  41. return -30023;//彩金申请已发放
  42. }
  43. if ($this->lottery_requestObj->status == 2)
  44. return -30024;//彩金申请已驳回
  45. $this->account_name = $this->lottery_requestObj->account_name;
  46. $this->account_identity = $this->lottery_requestObj->account_identity;
  47. $p_order_id = $this->lottery_requestObj->order_id;
  48. $this->order_id = $p_order_id ? 'CJ' . $p_order_id : OrderID();//要插入的资金订单号
  49. $this->money = $this->lottery_requestObj->money;
  50. $this->rate = 1;
  51. _beginTransaction();
  52. $ret = $this->changeReqStatus($lottery_req_id, 1);//通过申请
  53. if (!$ret) {
  54. _rollback ();
  55. return -51011;//更新审核状态记录失败
  56. }
  57. $content = json_decode ($this->lottery_requestObj->conent);
  58. if ($content['fr_rate']) $this->rate = $content['fr_rate'];
  59. $ret = $this->addMoneyRechargeRec($rechargeDetail);
  60. if ($ret < 1) {
  61. _rollback ();
  62. return -51011;//添加资金记录失败
  63. }
  64. $ret = $this->addMoneyDetailRec($waterDetail);
  65. if ($ret < 1) {
  66. _rollback ();
  67. return -51011;//添加资金详情记录失败
  68. }
  69. $ret = $this->updateNeedMoneyRec();
  70. if (!$ret) {
  71. _rollback ();
  72. return -51011;//更新资金流水差额记录失败
  73. }
  74. $ret = $this->addMoneyRechargeRemark();
  75. if ($ret < 1) {
  76. _rollback ();
  77. return -51011;//更新资金remark记录失败
  78. }
  79. $ret = $this->updateAccountDetailMoney();
  80. if (!$ret) {
  81. _rollback ();
  82. return -51011;//更新用户账户详情中资金记录失败
  83. }
  84. _commit ();
  85. $tpl='恭喜%s,您通过%s申请的彩金%s已到账,订单号为%s,请查收!';
  86. //$tpl = lang ('lottery_money_log','api')->get ('pass');
  87. $msg=sprintf ($tpl,$this->account_name,$this->lottery_requestObj->lottery_money_name??'彩金申请',$this->money,
  88. $this->order_id);
  89. lm ('Message', 'commons')->add ($title='申领彩金提醒', $msg, $from='系统', $to=$this->account_name, $type = 1, $addStatus = 2);
  90. return 1;
  91. }
  92. /**
  93. * 自动充值
  94. * @param string $account_identity
  95. * @param float $money
  96. * @param string $rechargeDetail
  97. * @param string $waterDetail
  98. * @return int
  99. */
  100. public function recharge(string $account_identity, float $money, string $rechargeDetail, string $waterDetail)
  101. {
  102. $this->account_identity = $account_identity;
  103. $this->operation_time = date ('Y-m-d H:i:s', time ());
  104. //$account = lm ('AccountDetail', 'api')->where ('account_identity', $account_identity)->first ();
  105. $account = lm ('Account', 'api')->where ('identity', $account_identity)->first ();
  106. if (!$account)
  107. return -51017;//用户不存在
  108. $this->account_name = $account->account;//用户账号
  109. $this->money = $money;//充值金额
  110. $this->order_id = OrderID ();//生成订单号
  111. $this->trade_type = 9;//充值;
  112. $this->rate = 1;//流水倍数
  113. $this->operator = 'system_auto';//审核人
  114. _beginTransaction();
  115. $ret = $this->addMoneyRechargeRec($rechargeDetail);
  116. if ($ret < 1) {
  117. _rollback ();
  118. return -51011;//添加资金记录失败
  119. }
  120. $ret = $this->addMoneyDetailRec($waterDetail);
  121. if ($ret < 1) {
  122. _rollback ();
  123. return -51011;//添加资金详情记录失败
  124. }
  125. $ret = $this->addMoneyRechargeRemark();
  126. if ($ret < 1) {
  127. _rollback ();
  128. return -51011;//更新资金remark记录失败
  129. }
  130. $ret = $this->updateAccountDetailMoney();
  131. if (!$ret) {
  132. _rollback ();
  133. return -51011;//更新用户账户详情中资金记录失败
  134. }
  135. _commit ();
  136. return 1;
  137. }
  138. /**
  139. * 审核彩金申请状态
  140. * @param $lottery_req_id
  141. * @param $status
  142. * @return mixed
  143. */
  144. protected function changeReqStatus($lottery_req_id, $status)
  145. {
  146. $lml = lm ('lottery_money_log', 'api')->where ('id', $lottery_req_id)->first ();
  147. $operator = 'system_auto';
  148. $operatorId = 0;
  149. $arr = json_decode ($lml->flow_record, 1);
  150. $arr['update_time'] = date ('Y-m-d H:i:s');
  151. $arr['operator'] = $operator;
  152. $remark = '';
  153. $datas = array(
  154. 'status' => $status,
  155. 'remark' => $remark,
  156. 'operator' => $operator,
  157. 'operator_id' => $operatorId,
  158. 'flow_record' => json_encode ($arr),
  159. 'update_time' => $arr['update_time']
  160. );
  161. $res = $lml->update ($datas);
  162. if (!$res) {
  163. return 0; //没有数据
  164. }
  165. return 1;
  166. }
  167. /**
  168. * 添加资金记录
  169. * @param string $remark
  170. * @param string $reason
  171. * @param string $recharge_type
  172. * @return int
  173. */
  174. public function addMoneyRechargeRec(string $remark, $reason = '用于活动', $recharge_type = '活动充值')
  175. {
  176. $money = $this->money;
  177. $moneyRechargeObj = lm('AccountDetail', 'api')->where('account_identity', $idd = $this->account_identity)->first();
  178. $money_cash = $moneyRechargeObj ? $moneyRechargeObj->cash : 0;
  179. $money_cash += $money; //当前余额
  180. $this->money_cash = $money_cash;
  181. $cj = [
  182. 'order_id' => $this->order_id,
  183. 'info_identity' => UUID(),
  184. 'account_identity' => $idd,
  185. 'account_name' => $this->account_name,
  186. 'money' => $money,
  187. 'reason' => $reason,
  188. 'money_cash' => $money_cash,
  189. 'status' => 1,
  190. 'apply_time' => $this->operation_time,
  191. 'complete_time' => $this->operation_time,
  192. 'recharge_type' => $recharge_type,
  193. 'sysetem_user' => $this->operator,
  194. ];
  195. $detail = parseTag($remark, $cj);
  196. $cj['remark'] = $detail;
  197. try {
  198. return lm ('MoneyRecharge', 'api')->insertGetId ($cj);
  199. } catch (\Exception $e) {
  200. //Render ($e->getMessage (), 0);
  201. return -50014;
  202. }
  203. }
  204. /**
  205. * 添加资金详情记录
  206. * @param string $detail
  207. * @param string $reason
  208. * @param int $return_type
  209. * @return mixed
  210. */
  211. public function addMoneyDetailRec(string $detail,string $reason='', int $return_type = 1)
  212. {
  213. $oid = UUID();
  214. $details['info_identity'] = $oid;
  215. $details['trade_id'] = $this->order_id;
  216. $details['account_name'] = $this->account_name;
  217. $details['account_identity'] = $this->account_identity;
  218. $details['money'] =number_format($this->money,2,'.','');
  219. $details['money_time'] = date('Y-m-d H:i:s');
  220. $details['money_type'] = $this->money_type ?? 1;//2=扣款
  221. $details['money_cash'] = $this->money_cash;
  222. $details['trade_type'] = $this->trade_type;
  223. $details['sysetem_user'] = $this->operator;
  224. $details['reason'] = $reason;
  225. $details['status'] = 1;
  226. $detail = parseTag($detail, $details);
  227. $details['trade_desc'] = $detail;
  228. try {
  229. $re = lm('MoneyDetails', 'api')->insertGetId($details);
  230. if ($return_type === 1)
  231. return $re;
  232. return $oid;
  233. } catch (\Exception $e) {
  234. // Render($e->getMessage(), 0);
  235. return -50014;
  236. }
  237. }
  238. /**
  239. * 更新流水差额
  240. * @return mixed
  241. */
  242. public function updateNeedMoneyRec()
  243. {
  244. $withdrawRuleObj = lm ('withdraw_rule', 'api');
  245. return $ret = $withdrawRuleObj->updateRule ($this->account_identity, $this->money, $this->rate);
  246. }
  247. /**
  248. * 添加资金remark
  249. * @return mixed
  250. */
  251. public function addMoneyRechargeRemark()
  252. {
  253. $cjd = [
  254. 'name' => $this->account_name,
  255. 'order_id' => $this->order_id,
  256. 'money' => $this->money,
  257. 'recharge_time' => $this->operation_time,
  258. ];
  259. try {
  260. return lm ('MoneyRechargeRemark', 'api')->insertGetId ($cjd);
  261. } catch (\Exception $e) {
  262. return -50014;
  263. }
  264. }
  265. /**
  266. * 更新用户账户详情表中余额信息(加款操作)
  267. * @return mixed
  268. */
  269. public function updateAccountDetailMoney()
  270. {
  271. $type = 1;
  272. S('DB')->select("select update_money('" . $this->account_identity . "'," . $this->money . "," . $type . ")");
  273. return 1;
  274. }
  275. public function incrAccountDetailMoney(string $account_identity, float $money):int
  276. {
  277. $type = 1;
  278. $accountDetail = lm('AccountDetail', 'api')->where('account_identity', $account_identity)->first();
  279. if (!$accountDetail) {
  280. return -1;//用户不存在
  281. }
  282. S('DB')->select("select update_money('" . $account_identity . "'," . $money . "," . $type . ")");
  283. return 1;
  284. }
  285. /**
  286. * 用户账户详情扣款操作
  287. * @param $account_identity
  288. * @param $money
  289. * @return int
  290. */
  291. public function decrAccountDetailMoney(string $account_identity, float $money): int
  292. {
  293. $accountDetail = lm('AccountDetail', 'api')->where('account_identity', $account_identity)->first();
  294. if (!$accountDetail) {
  295. return -1;//用户不存在
  296. }
  297. $money_cash = $accountDetail->cash ? $accountDetail->cash : 0;
  298. // dd($money_cash);
  299. $money_cash -= $money; //更新后当前余额
  300. if ($money_cash < 0) {
  301. return -2;
  302. }
  303. $type = 0;
  304. S('DB')->select("select update_money('" . $account_identity . "'," . $money . "," . $type . ")");
  305. return 1;
  306. }
  307. }