LuckLotteryRecordServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\activity\lottery;
  13. use app\services\BaseServices;
  14. use app\dao\activity\lottery\LuckLotteryRecordDao;
  15. use app\services\activity\coupon\StoreCouponIssueServices;
  16. use app\services\order\StoreOrderCreateServices;
  17. use app\services\statistic\CapitalFlowServices;
  18. use app\services\user\UserBillServices;
  19. use app\services\user\UserMoneyServices;
  20. use app\services\user\UserServices;
  21. use app\services\wechat\WechatUserServices;
  22. use crmeb\exceptions\ApiException;
  23. use crmeb\services\app\WechatService;
  24. use think\facade\Log;
  25. use crmeb\services\pay\Pay;
  26. /**
  27. * 抽奖记录
  28. * Class LuckLotteryRecordServices
  29. * @package app\services\activity\lottery
  30. */
  31. class LuckLotteryRecordServices extends BaseServices
  32. {
  33. /**
  34. * LuckLotteryRecordServices constructor.
  35. * @param LuckLotteryRecordDao $dao
  36. */
  37. public function __construct(LuckLotteryRecordDao $dao)
  38. {
  39. $this->dao = $dao;
  40. }
  41. /**
  42. * 获取抽奖记录列表
  43. * @param array $where
  44. * @return array
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function getList(array $where)
  50. {
  51. [$page, $limit] = $this->getPageValue();
  52. /** @var LuckLotteryServices $luckServices */
  53. $luckServices = app()->make(LuckLotteryServices::class);
  54. $where['lottery_id'] = $where['factor'] == '' ? '' : $luckServices->value(['factor' => $where['factor']], 'id');
  55. unset($where['factor']);
  56. $list = $this->dao->getList($where, '*', ['lottery', 'prize', 'user'], $page, $limit);
  57. foreach ($list as &$item) {
  58. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  59. }
  60. $count = $this->dao->count($where);
  61. return compact('list', 'count');
  62. }
  63. /**
  64. * 获取中奖记录
  65. * @param array $where
  66. * @param int $limit
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function getWinList(array $where, int $limit = 20)
  73. {
  74. $where = $where + ['not_type' => 1];
  75. $list = $this->dao->getList($where, 'id,uid,prize_id,lottery_id,receive_time,add_time', ['user', 'prize'], 0, $limit);
  76. foreach ($list as &$item) {
  77. $item['receive_time'] = $item['receive_time'] ? date('Y-m-d H:i:s', $item['receive_time']) : '';
  78. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', $item['add_time']) : '';
  79. }
  80. return $list;
  81. }
  82. /**
  83. * 参与抽奖数据统计
  84. * @param int $lottery_id
  85. * @return int[]
  86. */
  87. public function getLotteryRecordData(int $lottery_id)
  88. {
  89. $data = ['all' => 0, 'people' => 0, 'win' => 0];
  90. if ($lottery_id) {
  91. $where = [['lottery_id', '=', $lottery_id]];
  92. $data['all'] = $this->dao->getCount($where);
  93. $data['people'] = $this->dao->getCount($where, 'uid');
  94. $data['win'] = $this->dao->getCount($where + [['type', '>', 1]], 'uid');
  95. }
  96. return $data;
  97. }
  98. /**
  99. * 写入中奖纪录
  100. * @param int $uid
  101. * @param array $prize
  102. * @param array $userInfo
  103. * @return mixed
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\DbException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. */
  108. public function insertPrizeRecord(int $uid, array $prize, array $userInfo = [])
  109. {
  110. if (!$userInfo) {
  111. /** @var UserServices $userServices */
  112. $userServices = app()->make(UserServices::class);
  113. $userInfo = $userServices->getUserInfo($uid);
  114. }
  115. if (!$userInfo) {
  116. throw new ApiException(410032);
  117. }
  118. if (!$prize) {
  119. throw new ApiException(410048);
  120. }
  121. $data = [];
  122. $data['uid'] = $uid;
  123. $data['lottery_id'] = $prize['lottery_id'];
  124. $data['prize_id'] = $prize['id'];
  125. $data['type'] = $prize['type'];
  126. $data['add_time'] = time();
  127. if (!$res = $this->dao->save($data)) {
  128. throw new ApiException(400439);
  129. }
  130. return $res;
  131. }
  132. /**
  133. * 领取奖品
  134. * @param int $uid
  135. * @param int $lottery_record_id
  136. * @param string $receive_info
  137. * @return bool
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\DbException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. */
  142. public function receivePrize(int $uid, int $lottery_record_id, array $receive_info = [])
  143. {
  144. /** @var UserServices $userServices */
  145. $userServices = app()->make(UserServices::class);
  146. $userInfo = $userServices->getUserInfo($uid);
  147. if (!$userInfo) {
  148. throw new ApiException(410032);
  149. }
  150. $lotteryRecord = $this->dao->get($lottery_record_id, ['*'], ['prize']);
  151. if (!$lotteryRecord || !isset($lotteryRecord['prize'])) {
  152. throw new ApiException(410050);
  153. }
  154. if ($lotteryRecord['is_receive'] == 1) {
  155. throw new ApiException(410051);
  156. }
  157. $data = ['is_receive' => 1, 'receive_time' => time(), 'receive_info' => $receive_info];
  158. $prize = $lotteryRecord['prize'];
  159. $this->transaction(function () use ($uid, $userInfo, $lottery_record_id, $data, $prize, $userServices, $receive_info) {
  160. //奖品类型1:未中奖2:积分3:余额4:红包5:优惠券6:站内商品7:等级经验8:用户等级 9:svip天数
  161. switch ($prize['type']) {
  162. case 1:
  163. break;
  164. case 2:
  165. /** @var UserBillServices $userBillServices */
  166. $userBillServices = app()->make(UserBillServices::class);
  167. $userBillServices->income('lottery_give_integral', $uid, $prize['num'], $userInfo['integral'] + $prize['num'], $prize['id']);
  168. $userServices->update($uid, ['integral' => bcadd((string)$userInfo['integral'], (string)$prize['num'], 0)], 'uid');
  169. break;
  170. case 3:
  171. /** @var UserMoneyServices $userMoneyServices */
  172. $userMoneyServices = app()->make(UserMoneyServices::class);
  173. $now_money = bcadd((string)$userInfo['now_money'], (string)$prize['num'], 2);
  174. $userMoneyServices->income('lottery_give_money', $uid, $prize['num'], $now_money, $prize['id']);
  175. $userServices->update($uid, ['now_money' => $now_money], 'uid');
  176. break;
  177. case 4:
  178. /** @var WechatUserServices $wechatServices */
  179. $wechatServices = app()->make(WechatUserServices::class);
  180. $openid = $wechatServices->uidToOpenid((int)$uid, 'wechat');
  181. $type = 'JSAPI';
  182. if (!$openid) {
  183. $openid = $wechatServices->uidToOpenid((int)$uid, 'routine');
  184. $type = 'mini';
  185. }
  186. if (!$openid) {
  187. $openid = $wechatServices->uidToOpenid((int)$uid, 'app');
  188. $type = 'APP';
  189. }
  190. if ($openid) {
  191. /** @var StoreOrderCreateServices $services */
  192. $services = app()->make(StoreOrderCreateServices::class);
  193. $wechat_order_id = $services->getNewOrderId();
  194. /** @var CapitalFlowServices $capitalFlowServices */
  195. $capitalFlowServices = app()->make(CapitalFlowServices::class);
  196. $capitalFlowServices->setFlow([
  197. 'order_id' => $wechat_order_id,
  198. 'uid' => $uid,
  199. 'price' => bcmul('-1', (string)$prize['num'], 2),
  200. 'pay_type' => 'weixin',
  201. 'nickname' => $userInfo['nickname'],
  202. 'phone' => $userInfo['phone']
  203. ], 'luck');
  204. if (sys_config('pay_wechat_type')) {
  205. $pay = new Pay('v3_wechat_pay');
  206. $pay->merchantPay($openid, $wechat_order_id, $prize['num'], [
  207. 'type' => $type,
  208. 'batch_name' => '抽奖中奖红包',
  209. 'batch_remark' => '您于' . date('Y-m-d H:i:s') . '中奖.' . $prize['num'] . '元'
  210. ]);
  211. } else {
  212. WechatService::merchantPay($openid, $wechat_order_id, $prize['num'], '抽奖中奖红包');
  213. }
  214. }
  215. break;
  216. case 5:
  217. /** @var StoreCouponIssueServices $couponIssueService */
  218. $couponIssueService = app()->make(StoreCouponIssueServices::class);
  219. try {
  220. $couponIssueService->issueUserCoupon($prize['coupon_id'], $userInfo);
  221. } catch (\Throwable $e) {
  222. Log::error('抽奖领取优惠券失败,原因:' . $e->getMessage());
  223. }
  224. break;
  225. case 6:
  226. if (!$receive_info['name'] || !$receive_info['phone'] || !$receive_info['address']) {
  227. throw new ApiException(410052);
  228. }
  229. if (!check_phone($receive_info['phone'])) {
  230. throw new ApiException(410053);
  231. }
  232. break;
  233. }
  234. $this->dao->update($lottery_record_id, $data, 'id');
  235. });
  236. return true;
  237. }
  238. /**
  239. * 发货、备注
  240. * @param int $lottery_record_id
  241. * @param array $data
  242. * @return bool
  243. * @throws \think\db\exception\DataNotFoundException
  244. * @throws \think\db\exception\DbException
  245. * @throws \think\db\exception\ModelNotFoundException
  246. */
  247. public function setDeliver(int $lottery_record_id, array $data)
  248. {
  249. $lotteryRecord = $this->dao->get($lottery_record_id);
  250. if (!$lotteryRecord) {
  251. throw new ApiException(410054);
  252. }
  253. $deliver_info = $lotteryRecord['deliver_info'];
  254. $edit = [];
  255. //备注
  256. if ($data['deliver_name'] && $data['deliver_number']) {
  257. if ($lotteryRecord['type'] != 6 && ($data['deliver_name'] || $data['deliver_number'])) {
  258. throw new ApiException(410055);
  259. }
  260. if ($lotteryRecord['type'] == 6 && (!$data['deliver_name'] || !$data['deliver_number'])) {
  261. throw new ApiException(410056);
  262. }
  263. $deliver_info['deliver_name'] = $data['deliver_name'];
  264. $deliver_info['deliver_number'] = $data['deliver_number'];
  265. $edit['is_deliver'] = 1;
  266. $edit['deliver_time'] = time();
  267. }
  268. $deliver_info['mark'] = $data['mark'];
  269. $edit['deliver_info'] = $deliver_info;
  270. if (!$this->dao->update($lottery_record_id, $edit, 'id')) {
  271. throw new ApiException(100005);
  272. }
  273. return true;
  274. }
  275. /**
  276. * 获取中奖记录
  277. * @param int $uid
  278. * @param array $where
  279. * @return array
  280. * @throws \think\db\exception\DataNotFoundException
  281. * @throws \think\db\exception\DbException
  282. * @throws \think\db\exception\ModelNotFoundException
  283. */
  284. public function getRecord(int $uid, $where = [])
  285. {
  286. if (!$where) {
  287. $where['uid'] = $uid;
  288. $where['not_type'] = 1;
  289. }
  290. [$page, $limit] = $this->getPageValue();
  291. $list = $this->dao->getList($where, '*', ['prize'], $page, $limit);
  292. foreach ($list as &$item) {
  293. $item['deliver_time'] = $item['deliver_time'] ? date('Y-m-d H:i:s', $item['deliver_time']) : '';
  294. $item['receive_time'] = $item['receive_time'] ? date('Y-m-d H:i:s', $item['receive_time']) : '';
  295. }
  296. return $list;
  297. }
  298. }