UserAwardIntegralServices.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\dao\system\AwardLakeDao;
  14. use app\dao\user\UserIntegralDao;
  15. use app\model\user\UserIntegral;
  16. use app\services\BaseServices;
  17. use crmeb\traits\ServicesTrait;
  18. use app\webscoket\SocketPush;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. /**
  23. *
  24. * Class UserRechargeServices
  25. * @package app\services\user
  26. * @mixin UserIntegralDao
  27. */
  28. class UserAwardIntegralServices extends BaseServices
  29. {
  30. use ServicesTrait;
  31. private $lake_dao;
  32. /**
  33. * UserRechargeServices constructor.
  34. * @param UserIntegralDao $dao
  35. */
  36. public function __construct(UserIntegralDao $dao, AwardLakeDao $awardLakeDao)
  37. {
  38. $this->dao = $dao;
  39. $this->lake_dao = $awardLakeDao;
  40. }
  41. /**
  42. * 获取充值列表
  43. * @param array $where
  44. * @param string $field
  45. * @param int $limit
  46. * @return array
  47. */
  48. public function getIntegralList(array $where, string $field = '*', int $limit = 0, array $with = [])
  49. {
  50. $whereData = $where;
  51. if ($limit) {
  52. [$page] = $this->getPageValue();
  53. } else {
  54. [$page, $limit] = $this->getPageValue();
  55. }
  56. $list = $this->dao->getList($whereData, $field, $page, $limit, $with);
  57. $count = $this->dao->count($whereData);
  58. foreach ($list as &$item) {
  59. $item['_extract_time'] = $item['extract_time'] ? date('Y-m-d H:i:s', $item['extract_time']) : '暂无';
  60. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  61. }
  62. $price = $this->getPrice();
  63. return compact('list', 'count', 'price');
  64. }
  65. /**
  66. * 获取单条数据
  67. * @param int $id
  68. * @param array $field
  69. */
  70. public function getIntegral(int $id, array $field = [])
  71. {
  72. return $this->dao->get($id, $field);
  73. }
  74. /**
  75. * 获取单条数据
  76. * @param string|float $price
  77. * @param string $field
  78. * @return array
  79. * @throws DataNotFoundException
  80. * @throws DbException
  81. * @throws ModelNotFoundException
  82. */
  83. public function getIntegralsOverExtract($price, string $field = '*')
  84. {
  85. if (!$price) $price = $this->getPrice();
  86. return $this->dao->getList(['extract_price' => $price, 'status' => 0], $field);
  87. }
  88. /**
  89. * 获取单条数据
  90. * @return float
  91. */
  92. public function getIntegralSum($where)
  93. {
  94. return $this->dao->sum($where, 'num', true);
  95. }
  96. /**
  97. * 获取单条数据
  98. * @return float
  99. */
  100. public function getPaySum($uid)
  101. {
  102. $where = ['uid' => $uid, 'status' => 0, 'type' => 0];
  103. return $this->dao->sum($where, 'order_price', true);
  104. }
  105. /**
  106. * 获取单条数据
  107. * @return array|\think\Model|null
  108. */
  109. public function getFirstStaticIntegral($uid)
  110. {
  111. $where = ['uid' => $uid, 'status' => 0, 'type' => 0];
  112. return $this->dao->getOne($where);
  113. }
  114. /**
  115. * 获取单条数据
  116. * @return float
  117. */
  118. public function getHourExtractPaySum($uid, $HourLimit = 0)
  119. {
  120. $where = ['uid' => $uid, 'status' => 1, 'type' => 0];
  121. $where['extract_time_gt'] = time() - ($HourLimit * 3600);
  122. return $this->dao->sum($where, 'order_price', true);
  123. }
  124. /**
  125. * 获取用户业绩
  126. * @param $uid
  127. * @return float
  128. */
  129. public function getAchievement($uid)
  130. {
  131. $where = ['uid' => array_merge([$uid], get_group_user($uid)), 'type' => 0];
  132. return $this->dao->sum($where, 'order_price', true);
  133. }
  134. /**
  135. * 获取单条数据
  136. * @return float
  137. */
  138. public function getLake()
  139. {
  140. return $this->lake_dao->sum([], 'num');
  141. }
  142. /**
  143. * 获取单条数据
  144. * @param int $link_id
  145. * @return string|null
  146. * @throws DataNotFoundException
  147. * @throws DbException
  148. * @throws ModelNotFoundException
  149. */
  150. public function getPrice(int $link_id = 0)
  151. {
  152. $price = 0;
  153. if ($link_id > 0) {
  154. $info = $this->dao->getOne(['link_id' => $link_id]);
  155. if ($info) $price = (string)($info['price'] ?: 0);
  156. }
  157. if ($price <= 0) {
  158. $lake_sum = $this->getLake();
  159. $sum_integral = $this->getIntegralSum(['status' => 0]);
  160. return (string)($sum_integral > 0 ? bcdiv((string)$lake_sum, (string)$sum_integral, 8) : 1);
  161. }
  162. }
  163. /**
  164. * 加积分
  165. * @param int $uid 用户
  166. * @param string $price 价格
  167. * @param string $total 总额
  168. * @param float $order_price
  169. * @param int $type 类型
  170. * @param string $extract_sum
  171. * @param int $link_id 关联ID
  172. * @param string $mark
  173. * @return \crmeb\basic\BaseModel|\think\Model
  174. */
  175. public function incIntegral(int $uid, string $price, string $total, float $order_price, int $type, string $extract_sum, int $link_id = 0, string $mark = '', $add_time = 0)
  176. {
  177. $inc_integral = bcdiv($total, $price, 5);
  178. return $this->dao->save([
  179. 'uid' => $uid,
  180. 'type' => $type == 1 ? 1 : 0,
  181. 'num' => $inc_integral,
  182. 'price' => $price,
  183. 'sum_price' => $total,
  184. 'extract_sum' => $extract_sum,
  185. 'link_id' => $link_id,
  186. 'mark' => $mark,
  187. 'order_price' => $order_price,
  188. 'add_time' => $add_time ?: time(),
  189. 'status' => $inc_integral > 0 ? 0 : 1,
  190. ]);
  191. }
  192. public function incIntegralAdmin(int $uid, string $price, string $total, float $order_price, int $type, string $extract_sum, int $link_id = 0, string $mark = '', $add_time = 0)
  193. {
  194. $inc_integral = bcdiv($total, $price, 5);
  195. $old = UserIntegral::where('uid', $uid)->where('link_id', $link_id)->where('type', $type)->where('mark', $mark)->find();
  196. if ($old) {
  197. $dump = '用户' . $uid . $mark . '补' . bcsub($inc_integral, $old['num'], 5);
  198. } else {
  199. $dump = '用户' . $uid . $mark . '补' . $inc_integral;
  200. }
  201. @file_put_contents('add', $dump . PHP_EOL, FILE_APPEND);
  202. // return $this->dao->save([
  203. // 'uid' => $uid,
  204. // 'type' => $type == 1 ? 1 : 0,
  205. // 'num' => $inc_integral,
  206. // 'price' => $price,
  207. // 'sum_price' => $total,
  208. // 'extract_sum' => $extract_sum,
  209. // 'link_id' => $link_id,
  210. // 'mark' => $mark,
  211. // 'order_price' => $order_price,
  212. // 'add_time' => $add_time ?: time(),
  213. // 'status' => $inc_integral > 0 ? 0 : 1,
  214. // ]);
  215. return true;
  216. }
  217. public function incUpdateIntegral(int $id, string $price, string $total, string $mark)
  218. {
  219. $inc_integral = bcdiv($total, $price, 5);
  220. $origin = $this->get($id);
  221. return $this->dao->update($id, [
  222. 'num' => bcadd($origin['num'], $inc_integral, 5),
  223. 'mark' => $origin['mark'] . $mark . (float)$inc_integral,
  224. ]);
  225. }
  226. /**
  227. * 加奖池
  228. * @param string $total 总额
  229. * @param int $link_id 关联ID
  230. * @param string $mark
  231. * @return \crmeb\basic\BaseModel|\think\Model
  232. */
  233. public function addLake(string $total, int $link_id = 0, string $mark = '')
  234. {
  235. return $this->lake_dao->save([
  236. 'num' => $total,
  237. 'link_id' => $link_id,
  238. 'mark' => $mark,
  239. 'add_time' => time(),
  240. ]);
  241. }
  242. }