LuckLotteryRecordServices.php 11 KB

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