LuckLotteryRecordServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 crmeb\services\pay\Pay;
  25. use think\facade\Log;
  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. $list = $this->dao->getList($where, '*', ['lottery', 'prize', 'user'], $page, $limit);
  53. foreach ($list as &$item) {
  54. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  55. }
  56. $count = $this->dao->count($where);
  57. return compact('list', 'count');
  58. }
  59. /**
  60. * 获取中奖记录
  61. * @param array $where
  62. * @param int $limit
  63. * @return array
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function getWinList(array $where, int $limit = 20)
  69. {
  70. $where = $where + ['not_type' => 1];
  71. $list = $this->dao->getList($where, 'id,uid,prize_id,lottery_id,receive_time,add_time', ['user', 'prize'], 0, $limit);
  72. foreach ($list as &$item) {
  73. $item['receive_time'] = $item['receive_time'] ? date('Y-m-d H:i:s', $item['receive_time']) : '';
  74. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', $item['add_time']) : '';
  75. }
  76. return $list;
  77. }
  78. /**
  79. * 参与抽奖数据统计
  80. * @param int $lottery_id
  81. * @return int[]
  82. */
  83. public function getLotteryRecordData(int $lottery_id)
  84. {
  85. $data = ['all' => 0, 'people' => 0, 'win' => 0];
  86. if ($lottery_id) {
  87. $where = [['lottery_id', '=', $lottery_id]];
  88. $data['all'] = $this->dao->getCount($where);
  89. $data['people'] = $this->dao->getCount($where, 'uid');
  90. $data['win'] = $this->dao->getCount($where + [['type', '>', 1]], 'uid');
  91. }
  92. return $data;
  93. }
  94. /**
  95. * 写入中奖纪录
  96. * @param int $uid
  97. * @param array $prize
  98. * @param array $userInfo
  99. * @return mixed
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. */
  104. public function insertPrizeRecord(int $uid, array $prize, array $userInfo = [], $channel_type)
  105. {
  106. if (!$userInfo) {
  107. /** @var UserServices $userServices */
  108. $userServices = app()->make(UserServices::class);
  109. $userInfo = $userServices->getUserInfo($uid);
  110. }
  111. if (!$userInfo) {
  112. throw new ApiException(410032);
  113. }
  114. if (!$prize) {
  115. throw new ApiException(410048);
  116. }
  117. $data = [];
  118. $data['uid'] = $uid;
  119. $data['lottery_id'] = $prize['lottery_id'];
  120. $data['prize_id'] = $prize['id'];
  121. $data['type'] = $prize['type'];
  122. $data['num'] = $prize['num'];
  123. $data['channel_type'] = $channel_type;
  124. $data['add_time'] = time();
  125. if (!$res = $this->dao->save($data)) {
  126. throw new ApiException(400439);
  127. }
  128. return $res;
  129. }
  130. /**
  131. * 领取奖品
  132. * @param int $uid
  133. * @param int $lottery_record_id
  134. * @param string $receive_info
  135. * @return bool
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. */
  140. public function receivePrize(int $uid, int $lottery_record_id, array $receive_info = [])
  141. {
  142. /** @var UserServices $userServices */
  143. $userServices = app()->make(UserServices::class);
  144. $userInfo = $userServices->getUserInfo($uid);
  145. if (!$userInfo) {
  146. throw new ApiException(410032);
  147. }
  148. $lotteryRecord = $this->dao->get($lottery_record_id, ['*'], ['prize']);
  149. if (!$lotteryRecord || !isset($lotteryRecord['prize'])) {
  150. throw new ApiException(410050);
  151. }
  152. if ($lotteryRecord['is_receive'] == 1) {
  153. throw new ApiException(410051);
  154. }
  155. $data = ['is_receive' => 1, 'receive_time' => time(), 'receive_info' => $receive_info];
  156. $prize = $lotteryRecord['prize'];
  157. $this->transaction(function () use ($uid, $userInfo, $lottery_record_id, $data, $prize, $userServices, $receive_info, $lotteryRecord) {
  158. //奖品类型1:未中奖2:积分3:余额4:红包5:优惠券6:站内商品7:等级经验8:用户等级 9:svip天数
  159. switch ($prize['type']) {
  160. case 1:
  161. break;
  162. case 2:
  163. /** @var UserBillServices $userBillServices */
  164. $userBillServices = app()->make(UserBillServices::class);
  165. $userBillServices->income('lottery_give_integral', $uid, $prize['num'], $userInfo['integral'] + $prize['num'], $prize['id']);
  166. $userServices->update($uid, ['integral' => bcadd((string)$userInfo['integral'], (string)$prize['num'], 0)], 'uid');
  167. break;
  168. case 3:
  169. /** @var UserMoneyServices $userMoneyServices */
  170. $userMoneyServices = app()->make(UserMoneyServices::class);
  171. $now_money = bcadd((string)$userInfo['now_money'], (string)$prize['num'], 2);
  172. $userMoneyServices->income('lottery_give_money', $uid, $prize['num'], $now_money, $prize['id']);
  173. $userServices->update($uid, ['now_money' => $now_money], 'uid');
  174. break;
  175. case 4:
  176. /** @var WechatUserServices $wechatServices */
  177. $wechatServices = app()->make(WechatUserServices::class);
  178. $type = '';
  179. $openid = $wechatServices->uidToOpenid((int)$uid, $lotteryRecord['channel_type']);
  180. if ($lotteryRecord['channel_type'] == 'wechat') {
  181. $type = 'JSAPI';
  182. } elseif ($lotteryRecord['channel_type'] == 'routine') {
  183. $type = 'mini';
  184. } elseif ($lotteryRecord['channel_type'] == 'app') {
  185. $type = 'APP';
  186. }
  187. if ($openid) {
  188. /** @var StoreOrderCreateServices $services */
  189. $services = app()->make(StoreOrderCreateServices::class);
  190. $wechat_order_id = $services->getNewOrderId('hb');
  191. /** @var CapitalFlowServices $capitalFlowServices */
  192. $capitalFlowServices = app()->make(CapitalFlowServices::class);
  193. $capitalFlowServices->setFlow([
  194. 'order_id' => $wechat_order_id,
  195. 'uid' => $uid,
  196. 'price' => bcmul('-1', (string)$prize['num'], 2),
  197. 'pay_type' => 'weixin',
  198. 'nickname' => $userInfo['nickname'],
  199. 'phone' => $userInfo['phone']
  200. ], 'luck');
  201. if (sys_config('pay_wechat_type')) {
  202. $pay = new Pay('v3_wechat_pay');
  203. $res = $pay->merchantPayNew(
  204. $type,
  205. $wechat_order_id,
  206. sys_config('v3_transfer_scene_id', '1000'),
  207. $openid,
  208. '',
  209. bcmul($prize['num'], '100', 0),
  210. '抽奖活动红包中奖',
  211. sys_config('site_url') . '/api/transfer/notify/' . $type,
  212. '劳务报酬',
  213. [
  214. [
  215. 'info_type' => '岗位类型',
  216. 'info_content' => '抽奖'
  217. ],
  218. [
  219. 'info_type' => '报酬说明',
  220. 'info_content' => '抽奖活动红包中奖'
  221. ],
  222. ]
  223. );
  224. $this->dao->update($lottery_record_id, [
  225. 'wechat_order_id' => $wechat_order_id,
  226. 'out_bill_no' => $res['out_bill_no'] ?? '',
  227. 'package_info' => $res['package_info'] ?? '',
  228. 'state' => $res['state'] ?? '',
  229. 'transfer_bill_no' => $res['transfer_bill_no'] ?? '',
  230. 'fail_reason' => $res['fail_reason'] ?? ''
  231. ]);
  232. event('NoticeListener', [['uid' => $uid, 'order_id' => $wechat_order_id, 'extractNumber' => $prize['num'], 'type' => 2], 'revenue_received']);
  233. } else {
  234. WechatService::merchantPay($openid, $wechat_order_id, (string)$prize['num'], '抽奖中奖红包');
  235. }
  236. }
  237. break;
  238. case 5:
  239. /** @var StoreCouponIssueServices $couponIssueService */
  240. $couponIssueService = app()->make(StoreCouponIssueServices::class);
  241. try {
  242. $couponIssueService->issueUserCoupon($prize['coupon_id'], $userInfo);
  243. } catch (\Throwable $e) {
  244. Log::error('抽奖领取优惠券失败,原因:' . $e->getMessage());
  245. }
  246. break;
  247. case 6:
  248. if (!$receive_info['name'] || !$receive_info['phone'] || !$receive_info['address']) {
  249. throw new ApiException(410052);
  250. }
  251. if (!check_phone($receive_info['phone'])) {
  252. throw new ApiException(410053);
  253. }
  254. break;
  255. }
  256. $this->dao->update($lottery_record_id, $data, 'id');
  257. });
  258. return true;
  259. }
  260. /**
  261. * 发货、备注
  262. * @param int $lottery_record_id
  263. * @param array $data
  264. * @return bool
  265. * @throws \think\db\exception\DataNotFoundException
  266. * @throws \think\db\exception\DbException
  267. * @throws \think\db\exception\ModelNotFoundException
  268. */
  269. public function setDeliver(int $lottery_record_id, array $data)
  270. {
  271. $lotteryRecord = $this->dao->get($lottery_record_id);
  272. if (!$lotteryRecord) {
  273. throw new ApiException(410054);
  274. }
  275. $deliver_info = $lotteryRecord['deliver_info'];
  276. $edit = [];
  277. //备注
  278. if ($data['deliver_name'] && $data['deliver_number']) {
  279. if ($lotteryRecord['type'] != 6 && ($data['deliver_name'] || $data['deliver_number'])) {
  280. throw new ApiException(410055);
  281. }
  282. if ($lotteryRecord['type'] == 6 && (!$data['deliver_name'] || !$data['deliver_number'])) {
  283. throw new ApiException(410056);
  284. }
  285. $deliver_info['deliver_name'] = $data['deliver_name'];
  286. $deliver_info['deliver_number'] = $data['deliver_number'];
  287. $edit['is_deliver'] = 1;
  288. $edit['deliver_time'] = time();
  289. }
  290. $deliver_info['mark'] = $data['mark'];
  291. $edit['deliver_info'] = $deliver_info;
  292. if (!$this->dao->update($lottery_record_id, $edit, 'id')) {
  293. throw new ApiException(100005);
  294. }
  295. return true;
  296. }
  297. /**
  298. * 获取中奖记录
  299. * @param int $uid
  300. * @param array $where
  301. * @return array
  302. * @throws \think\db\exception\DataNotFoundException
  303. * @throws \think\db\exception\DbException
  304. * @throws \think\db\exception\ModelNotFoundException
  305. */
  306. public function getRecord(int $uid, $where = [])
  307. {
  308. if (!$where) {
  309. $where['uid'] = $uid;
  310. $where['not_type'] = 1;
  311. }
  312. [$page, $limit] = $this->getPageValue();
  313. $list = $this->dao->getList($where, '*', ['prize'], $page, $limit);
  314. foreach ($list as &$item) {
  315. $item['deliver_time'] = $item['deliver_time'] ? date('Y-m-d H:i:s', $item['deliver_time']) : '';
  316. $item['receive_time'] = $item['receive_time'] ? date('Y-m-d H:i:s', $item['receive_time']) : '';
  317. }
  318. return $list;
  319. }
  320. }