LuckLotteryRecordServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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\activity\lottery;
  13. use app\jobs\system\CapitalFlowJob;
  14. use app\services\BaseServices;
  15. use app\dao\activity\lottery\LuckLotteryRecordDao;
  16. use app\services\activity\coupon\StoreCouponIssueServices;
  17. use app\services\order\StoreOrderCreateServices;
  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\services\wechat\Payment;
  23. use think\exception\ValidateException;
  24. use think\facade\Log;
  25. /**
  26. * 抽奖记录
  27. * Class LuckLotteryRecordServices
  28. * @package app\services\activity\lottery
  29. * @mixin LuckLotteryRecordDao
  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. */
  46. public function getList(array $where)
  47. {
  48. [$page, $limit] = $this->getPageValue();
  49. $where['not_type'] = 1;
  50. if (isset($where['factor']) && $where['factor']) {
  51. /** @var LuckLotteryServices $luckServices */
  52. $luckServices = app()->make(LuckLotteryServices::class);
  53. $where['lottery_id'] = $luckServices->value(['factor' => $where['factor'], 'status' => 1], 'id');
  54. if (!$where['lottery_id']) {
  55. $list = [];
  56. $count = 0;
  57. return compact('list', 'count');
  58. }
  59. unset($where['factor']);
  60. }
  61. $list = $this->dao->getList($where, '*', ['lottery', 'prize', 'user'], $page, $limit);
  62. foreach ($list as &$item) {
  63. if (isset($item['prize_info']) && $item['prize_info']) {
  64. $prize = json_decode($item['prize_info'], true);
  65. } else {
  66. $prize = $item['prize'];
  67. }
  68. $item['prize'] = $prize;
  69. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  70. }
  71. $count = $this->dao->count($where);
  72. return compact('list', 'count');
  73. }
  74. /**
  75. * 获取中奖记录
  76. * @param array $where
  77. * @param int $limit
  78. * @return array
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function getWinList(array $where, int $limit = 20)
  84. {
  85. $where = $where + ['not_type' => 1];
  86. $list = $this->dao->getList($where, 'id,uid,prize_id,lottery_id,receive_time,prize_info,add_time', ['user', 'prize'], 0, $limit);
  87. foreach ($list as &$item) {
  88. if (isset($item['prize_info']) && $item['prize_info']) {
  89. $prize = json_decode($item['prize_info'], true);
  90. } else {
  91. $prize = $item['prize'];
  92. }
  93. $item['prize'] = $prize;
  94. $item['receive_time'] = $item['receive_time'] ? date('Y-m-d H:i:s', $item['receive_time']) : '';
  95. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', $item['add_time']) : '';
  96. }
  97. return $list;
  98. }
  99. /**
  100. * 参与抽奖数据统计
  101. * @param int $lottery_id
  102. * @return int[]
  103. */
  104. public function getLotteryRecordData(int $lottery_id)
  105. {
  106. $data = ['all' => 0, 'people' => 0, 'win' => 0];
  107. if ($lottery_id) {
  108. $where = [['lottery_id', '=', $lottery_id]];
  109. $data['all'] = $this->dao->getCount($where);
  110. $data['people'] = $this->dao->getCount($where, 'uid');
  111. $data['win'] = $this->dao->getCount($where + [['type', '>', 1]], 'uid');
  112. }
  113. return $data;
  114. }
  115. /**
  116. * 写入中奖纪录
  117. * @param int $uid
  118. * @param array $prize
  119. * @return mixed
  120. */
  121. public function insertPrizeRecord(int $uid, array $prize, array $userInfo = [])
  122. {
  123. if (!$userInfo) {
  124. /** @var UserServices $userServices */
  125. $userServices = app()->make(UserServices::class);
  126. $userInfo = $userServices->getUserInfo($uid);
  127. }
  128. if (!$userInfo) {
  129. throw new ValidateException('用户不存在');
  130. }
  131. if (!$prize) {
  132. throw new ValidateException('奖品不存在');
  133. }
  134. $data = [];
  135. $data['uid'] = $uid;
  136. $data['lottery_id'] = $prize['lottery_id'];
  137. $data['prize_id'] = $prize['id'];
  138. $data['type'] = $prize['type'];
  139. $data['prize_info'] = json_encode($prize);
  140. $data['add_time'] = time();
  141. if (!$res = $this->dao->save($data)) {
  142. throw new ValidateException('写入中奖记录失败');
  143. }
  144. return $res;
  145. }
  146. /**
  147. * 领取奖品
  148. * @param int $uid
  149. * @param int $lottery_record_id
  150. * @param string $receive_info
  151. * @return bool
  152. * @throws \think\db\exception\DataNotFoundException
  153. * @throws \think\db\exception\DbException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. */
  156. public function receivePrize(int $uid, int $lottery_record_id, array $receive_info = [])
  157. {
  158. /** @var UserServices $userServices */
  159. $userServices = app()->make(UserServices::class);
  160. $userInfo = $userServices->getUserInfo($uid);
  161. if (!$userInfo) {
  162. throw new ValidateException('用户不存在');
  163. }
  164. $lotteryRecord = $this->dao->get($lottery_record_id, ['*'], ['prize']);
  165. if (!$lotteryRecord || !isset($lotteryRecord['prize'])) {
  166. throw new ValidateException('请继续参与活动抽奖');
  167. }
  168. if ($lotteryRecord['is_receive'] == 1) {
  169. throw new ValidateException('已经领取成功');
  170. }
  171. $data = ['is_receive' => 1, 'receive_time' => time(), 'receive_info' => $receive_info];
  172. if (isset($lotteryRecord['prize_info']) && $lotteryRecord['prize_info']) {
  173. $prize = json_decode($lotteryRecord['prize_info'], true);
  174. } else {
  175. $prize = $lotteryRecord['prize'];
  176. }
  177. $this->transaction(function () use ($uid, $userInfo, $lottery_record_id, $data, $prize, $userServices, $receive_info) {
  178. //奖品类型1:未中奖2:积分3:余额4:红包5:优惠券6:站内商品7:等级经验8:用户等级 9:svip天数
  179. switch ($prize['type']) {
  180. case 1:
  181. break;
  182. case 2:
  183. /** @var UserBillServices $userBillServices */
  184. $userBillServices = app()->make(UserBillServices::class);
  185. $integral = bcadd((string)$userInfo['integral'], (string)$prize['num'], 0);
  186. $userBillServices->income('lottery_give_integral', $uid, $prize['num'], (int)$integral, $prize['id']);
  187. $userServices->update($uid, ['integral' => $integral], 'uid');
  188. break;
  189. case 3:
  190. /** @var UserMoneyServices $userMoneyServices */
  191. $userMoneyServices = app()->make(UserMoneyServices::class);
  192. $now_money = bcadd((string)$userInfo['now_money'], (string)$prize['num'], 2);
  193. $userMoneyServices->income('lottery_give_money', $uid, $prize['num'], $now_money, $prize['id']);
  194. $userServices->update($uid, ['now_money' => $now_money], 'uid');
  195. break;
  196. case 4:
  197. /** @var WechatUserServices $wechatServices */
  198. $wechatServices = app()->make(WechatUserServices::class);
  199. $openid = $wechatServices->getWechatOpenid($uid, 'wechat');
  200. if ($openid) {//公众号用户
  201. $type = Payment::WEB;
  202. } else {//小程序用户
  203. $openid = $wechatServices->getWechatOpenid($uid, 'routine');
  204. $type = Payment::MINI;
  205. }
  206. //app微信用户
  207. if (!$openid) {
  208. $openid = $wechatServices->getWechatOpenid($uid, 'app');
  209. $type = Payment::APP;
  210. }
  211. if ($openid) {
  212. /** @var StoreOrderCreateServices $services */
  213. $services = app()->make(StoreOrderCreateServices::class);
  214. $wechat_order_id = $services->getNewOrderId();
  215. //记录资金流水
  216. CapitalFlowJob::dispatch([['order_id' => $wechat_order_id, 'store_id' => 0, 'uid' => $uid, 'price' => $prize['num'], 'pay_type' => 'weixin', 'nickname' => $userInfo['nickname'], 'phone' => $userInfo['phone']], 'luck']);
  217. Payment::merchantPay($openid, $wechat_order_id, (string)$prize['num'], '抽奖中奖红包', $type);
  218. }
  219. break;
  220. case 5:
  221. /** @var StoreCouponIssueServices $couponIssueService */
  222. $couponIssueService = app()->make(StoreCouponIssueServices::class);
  223. try {
  224. $couponIssueService->issueUserCoupon($prize['coupon_id'], $userInfo, true, 'luck_lottery');
  225. } catch (\Throwable $e) {
  226. Log::error('抽奖领取优惠券失败,原因:' . $e->getMessage());
  227. }
  228. break;
  229. case 6:
  230. if (!$receive_info['name'] || !$receive_info['phone'] || !$receive_info['address']) {
  231. throw new ValidateException('请输入收货人信息');
  232. }
  233. if (!check_phone($receive_info['phone'])) {
  234. throw new ValidateException('请输入正确的收货人电话');
  235. }
  236. break;
  237. case 7:
  238. break;
  239. case 8:
  240. break;
  241. case 9:
  242. break;
  243. }
  244. $this->dao->update($lottery_record_id, $data, 'id');
  245. });
  246. return true;
  247. }
  248. /**
  249. * 发货、备注
  250. * @param int $lottery_record_id
  251. * @param array $data
  252. * @return bool
  253. * @throws \think\db\exception\DataNotFoundException
  254. * @throws \think\db\exception\DbException
  255. * @throws \think\db\exception\ModelNotFoundException
  256. */
  257. public function setDeliver(int $lottery_record_id, array $data)
  258. {
  259. $lotteryRecord = $this->dao->get($lottery_record_id);
  260. if (!$lotteryRecord) {
  261. throw new ValidateException('抽奖记录不存在');
  262. }
  263. $deliver_info = $lotteryRecord['deliver_info'];
  264. $edit = [];
  265. //备注
  266. if (!$data['deliver_name'] && !$data['deliver_number']) {
  267. $deliver_info['mark'] = $data['mark'];
  268. } else {
  269. if ($lotteryRecord['type'] != 6 && ($data['deliver_name'] || $data['deliver_number'])) {
  270. throw new ValidateException('该奖品不需要发货');
  271. }
  272. if ($lotteryRecord['type'] == 6 && (!$data['deliver_name'] || !$data['deliver_number'])) {
  273. throw new ValidateException('请选择快递公司或输入快递单号');
  274. }
  275. $deliver_info['deliver_name'] = $data['deliver_name'];
  276. $deliver_info['deliver_number'] = $data['deliver_number'];
  277. $edit['is_deliver'] = 1;
  278. $edit['deliver_time'] = time();
  279. }
  280. $edit['deliver_info'] = $deliver_info;
  281. if (!$this->dao->update($lottery_record_id, $edit, 'id')) {
  282. throw new ValidateException('处理失败');
  283. }
  284. return true;
  285. }
  286. /**
  287. * 获取中奖记录
  288. * @param int $uid
  289. * @return array
  290. */
  291. public function getRecord(int $uid, $where = [])
  292. {
  293. if (!$where) {
  294. $where['uid'] = $uid;
  295. $where['not_type'] = 1;
  296. }
  297. [$page, $limit] = $this->getPageValue();
  298. $list = $this->dao->getList($where, '*', ['prize'], $page, $limit);
  299. foreach ($list as &$item) {
  300. if (isset($item['prize_info']) && $item['prize_info']) {
  301. $prize = $item['prize_info'] = json_decode($item['prize_info'], true);
  302. } else {
  303. $prize = $item['prize'];
  304. }
  305. $item['prize'] = $prize;
  306. $item['deliver_time'] = $item['deliver_time'] ? date('Y-m-d H:i:s', $item['deliver_time']) : '';
  307. $item['receive_time'] = $item['receive_time'] ? date('Y-m-d H:i:s', $item['receive_time']) : '';
  308. }
  309. return $list;
  310. }
  311. /**获取抽奖次数
  312. * @param int $uid
  313. * @param int $lottery_id
  314. * @return array
  315. */
  316. public function getLotteryNum(int $uid, int $lottery_id)
  317. {
  318. $where['uid'] = $uid;
  319. $where['lottery_id'] = $lottery_id;
  320. $now_day = strtotime(date('Y-m-d'));//今日
  321. $todayCount = $this->dao->getCount($where + ['add_time'=> $now_day]);
  322. $totalCount = $this->dao->getCount($where);
  323. return compact('todayCount','totalCount');
  324. }
  325. }