UserAwardIntegralServices.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. * @return float
  139. */
  140. public function reOrder($uid, $exceptId = 0)
  141. {
  142. $where = ['uid' => $uid, 'type' => 0, 'order_id_not' => $exceptId];
  143. return $this->dao->search($where)->count() > 0;
  144. }
  145. /**
  146. * 获取用户业绩
  147. * @param $uid
  148. * @return float
  149. */
  150. public function getAchievement($uid, $status = '')
  151. {
  152. $where = ['uid' => array_merge([$uid], get_group_user($uid)), 'type' => 0];
  153. if ($status != '') $where['status'] = $status;
  154. return $this->dao->sum($where, 'order_price', true);
  155. }
  156. /**
  157. * 获取单条数据
  158. * @return float
  159. */
  160. public function getLake($time = '')
  161. {
  162. if ($time == '') {
  163. return $this->lake_dao->sum([], 'num');
  164. } else {
  165. return AwardLake::whereTime('add_time', $time)->sum('num');
  166. }
  167. }
  168. /**
  169. * 获取单条数据
  170. * @param int $link_id
  171. * @return string|null
  172. * @throws DataNotFoundException
  173. * @throws DbException
  174. * @throws ModelNotFoundException
  175. */
  176. public function getPrice(int $link_id = 0)
  177. {
  178. $price = 0;
  179. if ($link_id > 0) {
  180. $info = $this->dao->getOne(['link_id' => $link_id]);
  181. if ($info) $price = (string)($info['price'] ?: 0);
  182. }
  183. if ($price <= 0) {
  184. $lake_sum = $this->getLake();
  185. $sum_integral = $this->getIntegralSum(['status' => 0]);
  186. return (string)($sum_integral > 0 ? bcdiv((string)$lake_sum, (string)$sum_integral, 8) : 1);
  187. }
  188. return $price;
  189. }
  190. /**
  191. * 加积分
  192. * @param int $uid 用户
  193. * @param string $price 价格
  194. * @param string $total 总额
  195. * @param float $order_price
  196. * @param int $type 类型
  197. * @param string $extract_sum
  198. * @param int $link_id 关联ID
  199. * @param string $mark
  200. * @return \crmeb\basic\BaseModel|\think\Model
  201. */
  202. 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)
  203. {
  204. if ($price > 0) $inc_integral = bcdiv($total, $price, 5);
  205. else $inc_integral = 0;
  206. return $this->dao->save([
  207. 'uid' => $uid,
  208. 'type' => $type == 1 ? 1 : 0,
  209. 'num' => $inc_integral,
  210. 'price' => $price,
  211. 'sum_price' => $total,
  212. 'extract_sum' => $extract_sum,
  213. 'link_id' => $link_id,
  214. 'mark' => $mark,
  215. 'order_price' => $order_price,
  216. 'add_time' => $add_time ?: time(),
  217. 'status' => $inc_integral > 0 ? 0 : 1,
  218. ]);
  219. }
  220. 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)
  221. {
  222. $inc_integral = bcdiv($total, $price, 5);
  223. $old = UserIntegral::where('uid', $uid)->where('link_id', $link_id)->where('type', $type)->whereLike('mark', '%' . $mark . '%')->find();
  224. if ($old) {
  225. $dump = '用户' . $uid . $mark . '补' . bcsub($inc_integral, $old['num'], 5);
  226. } else {
  227. $dump = '用户' . $uid . $mark . '补' . $inc_integral;
  228. }
  229. @file_put_contents('add', $dump . PHP_EOL, FILE_APPEND);
  230. if ($old) {
  231. UserIntegral::where('id', $old['id'])->delete();
  232. }
  233. return UserIntegral::create([
  234. 'uid' => $uid,
  235. 'type' => $type == 1 ? 1 : 0,
  236. 'num' => $inc_integral,
  237. 'price' => $price,
  238. 'sum_price' => $total,
  239. 'extract_sum' => $extract_sum,
  240. 'link_id' => $link_id,
  241. 'mark' => $mark,
  242. 'order_price' => $order_price,
  243. 'add_time' => $add_time,
  244. 'status' => $inc_integral > 0 ? 0 : 1,
  245. ]);
  246. // return true;
  247. }
  248. public function incUpdateIntegral(int $id, string $price, string $total, string $mark)
  249. {
  250. if ($price > 0) {
  251. $inc_integral = bcdiv($total, $price, 5);
  252. } else {
  253. $inc_integral = $total;
  254. }
  255. $origin = $this->get($id);
  256. return $this->dao->update($id, [
  257. 'num' => bcadd($origin['num'], $inc_integral, 5),
  258. 'mark' => $origin['mark'] . $mark . (float)$inc_integral,
  259. ]);
  260. }
  261. /**
  262. * 加奖池
  263. * @param string $total 总额
  264. * @param int $link_id 关联ID
  265. * @param string $mark
  266. * @return \crmeb\basic\BaseModel|\think\Model
  267. */
  268. public function addLake(string $total, int $link_id = 0, string $mark = '')
  269. {
  270. return $this->lake_dao->save([
  271. 'num' => $total,
  272. 'link_id' => $link_id,
  273. 'mark' => $mark,
  274. 'add_time' => time(),
  275. ]);
  276. }
  277. /**
  278. * 获取修改页面数据
  279. * @param int $id
  280. * @return array
  281. * @throws \FormBuilder\Exception\FormBuilderException
  282. */
  283. public function editLack()
  284. {
  285. $f = array();
  286. $f[] = FormBuilder::number('sum', '资金池', $this->getLake())->disabled(true);
  287. $f[] = FormBuilder::number('num', '操作数量', 0)->step(0.01)->min(0.01)->max($this->getLake())->required();
  288. $f[] = FormBuilder::radio('pm', '操作类型', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 0, 'label' => '减少']]);
  289. $f[] = FormBuilder::textarea('mark', '操作原因');
  290. return create_form('编辑', $f, Route::buildUrl('/finance/lake'), 'POST');
  291. }
  292. }