UserAwardIntegralServices.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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\services\FormBuilder;
  18. use crmeb\traits\ServicesTrait;
  19. use app\webscoket\SocketPush;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\facade\Route;
  24. /**
  25. *
  26. * Class UserRechargeServices
  27. * @package app\services\user
  28. * @mixin UserIntegralDao
  29. */
  30. class UserAwardIntegralServices extends BaseServices
  31. {
  32. use ServicesTrait;
  33. private $lake_dao;
  34. /**
  35. * UserRechargeServices constructor.
  36. * @param UserIntegralDao $dao
  37. */
  38. public function __construct(UserIntegralDao $dao, AwardLakeDao $awardLakeDao)
  39. {
  40. $this->dao = $dao;
  41. $this->lake_dao = $awardLakeDao;
  42. }
  43. /**
  44. * 获取充值列表
  45. * @param array $where
  46. * @param string $field
  47. * @param int $limit
  48. * @return array
  49. */
  50. public function getIntegralList(array $where, string $field = '*', int $limit = 0, array $with = [])
  51. {
  52. $whereData = $where;
  53. if ($limit) {
  54. [$page] = $this->getPageValue();
  55. } else {
  56. [$page, $limit] = $this->getPageValue();
  57. }
  58. $list = $this->dao->getList($whereData, $field, $page, $limit, $with);
  59. $count = $this->dao->count($whereData);
  60. foreach ($list as &$item) {
  61. $item['_extract_time'] = $item['extract_time'] ? date('Y-m-d H:i:s', $item['extract_time']) : '暂无';
  62. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  63. }
  64. $price = $this->getPrice();
  65. return compact('list', 'count', 'price');
  66. }
  67. /**
  68. * 获取单条数据
  69. * @param int $id
  70. * @param array $field
  71. */
  72. public function getIntegral(int $id, array $field = [])
  73. {
  74. return $this->dao->get($id, $field);
  75. }
  76. /**
  77. * 获取单条数据
  78. * @param string|float $price
  79. * @param string $field
  80. * @return array
  81. * @throws DataNotFoundException
  82. * @throws DbException
  83. * @throws ModelNotFoundException
  84. */
  85. public function getIntegralsOverExtract($price, string $field = '*')
  86. {
  87. if (!$price) $price = $this->getPrice();
  88. return $this->dao->getList(['extract_price' => $price, 'status' => 0], $field);
  89. }
  90. /**
  91. * 获取单条数据
  92. * @return float
  93. */
  94. public function getIntegralSum($where)
  95. {
  96. return $this->dao->sum($where, 'num', true);
  97. }
  98. /**
  99. * 获取单条数据
  100. * @return float
  101. */
  102. public function getPaySum($uid)
  103. {
  104. $where = ['uid' => $uid, 'status' => 0, 'type' => 0];
  105. return $this->dao->sum($where, 'order_price', true);
  106. }
  107. /**
  108. * 获取单条数据
  109. * @return array|\think\Model|null
  110. */
  111. public function getFirstStaticIntegral($uid)
  112. {
  113. $where = ['uid' => $uid, 'status' => 0, 'type' => 0];
  114. return $this->dao->getOne($where);
  115. }
  116. /**
  117. * 获取单条数据
  118. * @return array|\think\Model|null
  119. */
  120. public function getTopStaticIntegral($id)
  121. {
  122. $where = ['status' => 0, 'type' => 0];
  123. return $this->dao->search($where)->where('id', '>', $id)->order('id', 'asc')->find();
  124. }
  125. /**
  126. * 获取单条数据
  127. * @return float
  128. */
  129. public function getHourExtractPaySum($uid, $HourLimit = 0)
  130. {
  131. $where = ['uid' => $uid, 'status' => 1, 'type' => 0];
  132. $where['extract_time_gt'] = time() - ($HourLimit * 3600);
  133. return $this->dao->sum($where, 'order_price', true);
  134. }
  135. /**
  136. * 获取用户业绩
  137. * @param $uid
  138. * @return float
  139. */
  140. public function getAchievement($uid)
  141. {
  142. $where = ['uid' => array_merge([$uid], get_group_user($uid)), 'type' => 0];
  143. return $this->dao->sum($where, 'order_price', true);
  144. }
  145. /**
  146. * 获取单条数据
  147. * @return float
  148. */
  149. public function getLake()
  150. {
  151. return $this->lake_dao->sum([], 'num');
  152. }
  153. /**
  154. * 获取单条数据
  155. * @param int $link_id
  156. * @return string|null
  157. * @throws DataNotFoundException
  158. * @throws DbException
  159. * @throws ModelNotFoundException
  160. */
  161. public function getPrice(int $link_id = 0)
  162. {
  163. $price = 0;
  164. if ($link_id > 0) {
  165. $info = $this->dao->getOne(['link_id' => $link_id]);
  166. if ($info) $price = (string)($info['price'] ?: 0);
  167. }
  168. if ($price <= 0) {
  169. $lake_sum = $this->getLake();
  170. $sum_integral = $this->getIntegralSum(['status' => 0]);
  171. return (string)($sum_integral > 0 ? bcdiv((string)$lake_sum, (string)$sum_integral, 8) : 1);
  172. }
  173. }
  174. /**
  175. * 加积分
  176. * @param int $uid 用户
  177. * @param string $price 价格
  178. * @param string $total 总额
  179. * @param float $order_price
  180. * @param int $type 类型
  181. * @param string $extract_sum
  182. * @param int $link_id 关联ID
  183. * @param string $mark
  184. * @return \crmeb\basic\BaseModel|\think\Model
  185. */
  186. 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)
  187. {
  188. $inc_integral = bcdiv($total, $price, 5);
  189. return $this->dao->save([
  190. 'uid' => $uid,
  191. 'type' => $type == 1 ? 1 : 0,
  192. 'num' => $inc_integral,
  193. 'price' => $price,
  194. 'sum_price' => $total,
  195. 'extract_sum' => $extract_sum,
  196. 'link_id' => $link_id,
  197. 'mark' => $mark,
  198. 'order_price' => $order_price,
  199. 'add_time' => $add_time ?: time(),
  200. 'status' => $inc_integral > 0 ? 0 : 1,
  201. ]);
  202. }
  203. 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)
  204. {
  205. $inc_integral = bcdiv($total, $price, 5);
  206. $old = UserIntegral::where('uid', $uid)->where('link_id', $link_id)->where('type', $type)->whereLike('mark', '%' . $mark . '%')->find();
  207. if ($old) {
  208. $dump = '用户' . $uid . $mark . '补' . bcsub($inc_integral, $old['num'], 5);
  209. } else {
  210. $dump = '用户' . $uid . $mark . '补' . $inc_integral;
  211. }
  212. @file_put_contents('add', $dump . PHP_EOL, FILE_APPEND);
  213. if ($old) {
  214. UserIntegral::where('id', $old['id'])->delete();
  215. }
  216. return UserIntegral::create([
  217. 'uid' => $uid,
  218. 'type' => $type == 1 ? 1 : 0,
  219. 'num' => $inc_integral,
  220. 'price' => $price,
  221. 'sum_price' => $total,
  222. 'extract_sum' => $extract_sum,
  223. 'link_id' => $link_id,
  224. 'mark' => $mark,
  225. 'order_price' => $order_price,
  226. 'add_time' => $add_time,
  227. 'status' => $inc_integral > 0 ? 0 : 1,
  228. ]);
  229. // return true;
  230. }
  231. public function incUpdateIntegral(int $id, string $price, string $total, string $mark)
  232. {
  233. if ($price > 0) {
  234. $inc_integral = bcdiv($total, $price, 5);
  235. } else {
  236. $inc_integral = $total;
  237. }
  238. $origin = $this->get($id);
  239. return $this->dao->update($id, [
  240. 'num' => bcadd($origin['num'], $inc_integral, 5),
  241. 'mark' => $origin['mark'] . $mark . (float)$inc_integral,
  242. ]);
  243. }
  244. /**
  245. * 加奖池
  246. * @param string $total 总额
  247. * @param int $link_id 关联ID
  248. * @param string $mark
  249. * @return \crmeb\basic\BaseModel|\think\Model
  250. */
  251. public function addLake(string $total, int $link_id = 0, string $mark = '')
  252. {
  253. return $this->lake_dao->save([
  254. 'num' => $total,
  255. 'link_id' => $link_id,
  256. 'mark' => $mark,
  257. 'add_time' => time(),
  258. ]);
  259. }
  260. /**
  261. * 获取修改页面数据
  262. * @param int $id
  263. * @return array
  264. * @throws \FormBuilder\Exception\FormBuilderException
  265. */
  266. public function editLack()
  267. {
  268. $f = array();
  269. $f[] = FormBuilder::number('sum', '资金池', $this->getLake())->disabled(true);
  270. $f[] = FormBuilder::number('num', '操作数量', 0)->step(0.01)->min(0.01)->max($this->getLake())->required();
  271. $f[] = FormBuilder::radio('pm', '操作类型', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 0, 'label' => '减少']]);
  272. $f[] = FormBuilder::textarea('mark', '操作原因');
  273. return create_form('编辑', $f, Route::buildUrl('/finance/lake'), 'POST');
  274. }
  275. }