MoneyDetailServer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Servers;
  3. use App\Models\Member;
  4. use App\Models\MoneyDetail;
  5. use App\Models\MoneyRecord;
  6. use App\Models\ShopBean;
  7. use App\Models\ShopUser;
  8. /**
  9. * 订单管理
  10. * Class OrderServer
  11. * @package App\Servers
  12. */
  13. class MoneyDetailServer
  14. {
  15. private $type_arr = [
  16. 1 => '余额',
  17. ];
  18. private $type_key = [
  19. 1 => 'money',
  20. ];
  21. private $add_reduce_arr = [
  22. 1 => '增加',
  23. 0 => '减少',
  24. ];
  25. private $status_arr = [
  26. 1 => '代币分账',
  27. 2 => '公排出局',
  28. ];
  29. static private $server = null;
  30. private function __construct()
  31. {
  32. }
  33. /**
  34. * 创建对象
  35. * @return MoneyDetailServer
  36. */
  37. static function creatServer()
  38. {
  39. if (empty(self::$server)) self::$server = new MoneyDetailServer();
  40. return self::$server;
  41. }
  42. /**
  43. * @param $reduce
  44. * @return string
  45. */
  46. public function getAddReduceArr($reduce)
  47. {
  48. return empty($this->add_reduce_arr[$reduce]) ? '' : $this->add_reduce_arr[$reduce];
  49. }
  50. /**
  51. * @return string[]
  52. */
  53. public function getStatus()
  54. {
  55. return $this->status_arr;
  56. }
  57. /**
  58. * @param $status
  59. * @return array|string
  60. */
  61. public function getStatusArr($status)
  62. {
  63. return empty($this->status_arr[$status]) ? '' : $this->status_arr[$status];
  64. }
  65. /**
  66. * 获取订单类型
  67. * @param $type
  68. * @return array|string
  69. */
  70. public function getTypeArr($type)
  71. {
  72. return empty($this->type_arr[$type]) ? '' : $this->type_arr[$type];
  73. }
  74. /**
  75. * @return string[]
  76. */
  77. public function getType()
  78. {
  79. return $this->type_arr;
  80. }
  81. /**
  82. * 会员余额变动
  83. * @param $type
  84. * @param $status
  85. * @param $price
  86. * @param $addReduce
  87. * @param $m_id
  88. * @param $mark
  89. * @param int $fromID
  90. * @return MoneyDetail|false|\Illuminate\Database\Eloquent\Model
  91. */
  92. public function write($type, $status, $price, $addReduce, $m_id, $mark = '', $fromID = 0)
  93. {
  94. $money_mode = Member::where('id', $m_id)->select(['id', 'money'])->first();
  95. if (empty($money_mode)) {
  96. return false;
  97. }
  98. $remainder=$money_mode->{'money'};
  99. if($addReduce==1){
  100. $money_mode->increment('money', $price);
  101. }else{
  102. if($remainder<$price){
  103. return false;
  104. }
  105. $money_mode->decrement('money', $price);
  106. }
  107. $detail = MoneyDetail::create(
  108. [
  109. 'type' => $type, // 货币类型
  110. 'status' => $status, // 来源
  111. 'price' => $price, // 数目
  112. 'add_reduce' => $addReduce, // 增加 | 减少
  113. 'm_id' => $m_id, // 被操作会员ID
  114. 'mark' => $mark, // 备注
  115. 'source_id' => $fromID, // 操作员备注
  116. 'remainder' => sprintf("%.2f", ($addReduce == 0 ? $price * -1 : $price) + $remainder),// 剩余数目
  117. ]
  118. );
  119. MemberServer::creatServer()->delMemberCache($m_id);
  120. return $detail->{'id'};
  121. }
  122. }