UserAwardIntegralServices.php 9.4 KB

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