Divideticket.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\divideticket\model;
  11. use app\model\BaseModel;
  12. use app\model\system\Config as ConfigModel;
  13. use app\model\system\Cron;
  14. use extend\Poster as PosterExtend;
  15. use app\model\upload\Upload;
  16. /**
  17. * 好友瓜分券活动表
  18. * Class Divideticket
  19. * @package addon\divideticket\model
  20. */
  21. class Divideticket extends BaseModel
  22. {
  23. private $status = [
  24. 0 => '未开始',
  25. 1 => '进行中',
  26. 2 => '已结束',
  27. -1 => '已关闭'
  28. ];
  29. /**
  30. * 获取预售活动状态
  31. * @return array
  32. */
  33. public function getDivideticketStatus()
  34. {
  35. return $this->status;
  36. }
  37. /**
  38. * 获取分页列表
  39. * @param array $condition
  40. * @param number $page
  41. * @param string $page_size
  42. * @param string $order
  43. * @param string $field
  44. */
  45. public function getDivideticketPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc', $field = '*')
  46. {
  47. $list = model('promotion_friends_coupon')->pageList($condition, $field, $order, $page, $page_size);
  48. return $this->success($list);
  49. }
  50. /**
  51. * 获取优惠券活动信息
  52. * @param array $condition
  53. * @param string $field
  54. * @param string $alias
  55. * @param unknown $join
  56. * @param unknown $data
  57. * @return array
  58. */
  59. public function getDivideticketInfo($condition = [], $field = true, $alias = 'a', $join = null, $data = null)
  60. {
  61. $res = model('promotion_friends_coupon')->getInfo($condition, $field, $alias, $join, $data);
  62. if ($res) {
  63. if (isset($res[ 'goods_type' ]) && $res[ 'goods_type' ] == 2) {
  64. $field = 'goods_id,goods_name,FLOOR(goods_stock),price,sort,goods_image';
  65. $goods_ids = substr($res[ 'goods_ids' ], '1', '-1');
  66. $goods_list = model('goods')->getList([ [ 'goods_id', 'in', $goods_ids ] ], $field);
  67. $res[ 'goods_list' ] = $goods_list ?? [];
  68. } else {
  69. $res[ 'goods_list' ] = [];
  70. }
  71. $res[ 'goods_list_count' ] = count($res[ 'goods_list' ]);
  72. }
  73. return $this->success($res);
  74. }
  75. /**
  76. * 新增瓜分优惠券活动
  77. * @param $data
  78. * @return array
  79. */
  80. public function addDivideticket($data)
  81. {
  82. if ($data[ 'start_time' ] > time()) {
  83. $data[ 'status' ] = 0;
  84. $data[ 'status_name' ] = $this->status[ 0 ];
  85. } else {
  86. $data[ 'status' ] = 1;
  87. $data[ 'status_name' ] = $this->status[ 1 ];
  88. }
  89. //获取商品id
  90. if ($data[ 'goods_type' ] == 1) {//全部商品参与
  91. $data[ 'goods_ids' ] = '';
  92. }
  93. $data[ 'goods_ids' ] = ',' . $data[ 'goods_ids' ] . ',';
  94. model('promotion_friends_coupon')->startTrans();
  95. try {
  96. $coupon_type_data = [
  97. 'promotion_type' => 1,
  98. 'promotion_name' => 'divideticket',
  99. 'at_least' => $data[ 'at_least' ],
  100. 'is_limit' => $data[ 'is_limit' ],
  101. 'goods_type' => $data[ 'goods_type' ],
  102. 'goods_ids' => $data[ 'goods_ids' ]
  103. ];
  104. $coupon_type_id = model('promotion_coupon_type')->add($coupon_type_data);
  105. $data[ 'coupon_type_id' ] = $coupon_type_id;
  106. $res = model("promotion_friends_coupon")->add($data);
  107. $cron = new Cron();
  108. //增加定时更改活动状态自动事件
  109. if (!empty($data[ 'start_time' ])) {
  110. $cron->addCron(1, 0, "瓜分优惠券变更活动状态", "CronChangeDivideticketStatus", $data[ 'start_time' ], $res);
  111. }
  112. if (!empty($data[ 'end_time' ])) {
  113. $cron->addCron(1, 0, "瓜分优惠券变更活动状态", "CronChangeDivideticketStatus", $data[ 'end_time' ], $res);
  114. }
  115. model('promotion_friends_coupon')->commit();
  116. return $this->success($res);
  117. } catch (\Exception $e) {
  118. model('promotion_friends_coupon')->rollback();
  119. return $this->error($e->getMessage());
  120. }
  121. }
  122. public function editDivideticket($data)
  123. {
  124. $data[ 'update_time' ] = time();
  125. if ($data[ 'start_time' ] > time()) {
  126. $data[ 'status' ] = 0;
  127. $data[ 'status_name' ] = $this->status[ 0 ];
  128. } else {
  129. $data[ 'status' ] = 1;
  130. $data[ 'status_name' ] = $this->status[ 1 ];
  131. }
  132. //获取商品id
  133. if ($data[ 'goods_type' ] == 1) {//全部商品参与
  134. $data[ 'goods_ids' ] = '';
  135. }
  136. $data[ 'goods_ids' ] = ',' . $data[ 'goods_ids' ] . ',';
  137. $coupon_id = $data[ 'coupon_id' ];
  138. unset($data[ 'coupon_id' ]);
  139. #(修改发放量与库存)
  140. $old_info = model("promotion_friends_coupon")->getInfo([ [ 'coupon_id', '=', $coupon_id ] ], 'inventory,count,coupon_type_id,image');
  141. $data[ 'count' ] = $old_info[ 'count' ] + $data[ 'inventory' ] - $old_info[ 'inventory' ];
  142. if (!empty($old_info[ 'image' ]) && !empty($data[ 'image' ]) && $old_info[ 'image' ] != $data[ 'image' ]) {
  143. $upload_model = new Upload();
  144. $upload_model->deletePic($old_info[ 'image' ], $data[ 'site_id' ]);
  145. }
  146. model('promotion_coupon_type')->update([ 'goods_type' => $data[ 'goods_type' ], 'goods_ids' => $data[ 'goods_ids' ] ], [ [ 'coupon_type_id', '=', $old_info[ 'coupon_type_id' ] ] ]);
  147. $res = model("promotion_friends_coupon")->update($data, [ [ 'coupon_id', '=', $coupon_id ] ]);
  148. $cron = new Cron();
  149. $cron->deleteCron([ [ 'event', '=', 'CronChangeDivideticketStatus' ], [ 'relate_id', '=', $coupon_id ] ]);
  150. if ($data[ 'status' ] == 0) {
  151. $cron->addCron(1, 0, "变更活动状态", "CronChangeDivideticketStatus", $data[ 'start_time' ], $coupon_id);
  152. $cron->addCron(1, 0, "变更活动状态", "CronChangeDivideticketStatus", $data[ 'end_time' ], $coupon_id);
  153. } else if ($data[ 'status' ] == 1) {
  154. $cron->addCron(1, 0, "变更活动状态", "CronChangeDivideticketStatus", $data[ 'end_time' ], $coupon_id);
  155. }
  156. return $this->success($res);
  157. }
  158. /**
  159. * 更改活动状态
  160. * @param $coupon_id
  161. * @return array
  162. */
  163. public function changeDivideticketStatus($coupon_id)
  164. {
  165. $info = model('promotion_friends_coupon')->getInfo([ [ 'coupon_id', '=', $coupon_id ] ]);
  166. if (empty($info)) $this->success();
  167. if ($info[ 'end_time' ] <= time()) {
  168. $status = 2;
  169. $status_name = $this->status[ 2 ];
  170. model('promotion_friends_coupon_group')->update([ 'is_look' => 1 ], [ [ 'promotion_id', '=', $coupon_id ], [ 'status', '=', 2 ] ]);
  171. } else if ($info[ 'start_time' ] <= time() && $info[ 'end_time' ] > time()) {
  172. $status = 1;
  173. $status_name = $this->status[ 1 ];
  174. } else {
  175. $status = 0;
  176. $status_name = $this->status[ 0 ];
  177. }
  178. $res = model('promotion_friends_coupon')->update([ 'status' => $status, 'status_name' => $status_name ], [ [ 'coupon_id', '=', $coupon_id ] ]);
  179. return $this->success($res);
  180. }
  181. /**
  182. * 关闭活动
  183. * @param $data
  184. * @return array
  185. */
  186. public function closeDividetocket($data)
  187. {
  188. $coupon_id = $data[ 'coupon_id' ];
  189. $site_id = $data[ 'site_id' ];
  190. $condition = [
  191. [ 'coupon_id', '=', $coupon_id ],
  192. [ 'site_id', '=', $site_id ],
  193. ];
  194. model('promotion_friends_coupon')->startTrans();
  195. try {
  196. $res = model('promotion_friends_coupon')->update([ 'status' => -1, 'status_name' => '已关闭' ], $condition);
  197. model('promotion_friends_coupon_group')->update([ 'status' => 2 ], [ [ 'promotion_id', '=', $coupon_id ], [ 'status', '=', 0 ] ]);
  198. model('promotion_friends_coupon_group')->update([ 'is_look' => 1 ], [ [ 'promotion_id', '=', $coupon_id ], [ 'status', '=', 2 ] ]);
  199. $cron = new Cron();
  200. $cron->deleteCron([ [ 'event', '=', 'CronChangeDivideticketStatus' ], [ 'relate_id', '=', $coupon_id ] ]);
  201. model('promotion_friends_coupon')->commit();
  202. return $this->success($res);
  203. } catch (\Exception $e) {
  204. model('promotion_friends_coupon')->rollback();
  205. return $this->error($e->getMessage());
  206. }
  207. }
  208. /**
  209. * 删除活动
  210. * @param $data
  211. * @return array
  212. */
  213. public function deleteDividetocket($data)
  214. {
  215. $coupon_id = $data[ 'coupon_id' ];
  216. $site_id = $data[ 'site_id' ];
  217. model('promotion_friends_coupon')->startTrans();
  218. try {
  219. $condition = [
  220. [ 'coupon_id', '=', $coupon_id ],
  221. [ 'site_id', '=', $site_id ],
  222. ];
  223. $old_info = model("promotion_friends_coupon")->getInfo($condition);
  224. if (!empty($old_info[ 'image' ])) {
  225. $upload_model = new Upload();
  226. $upload_model->deletePic($old_info[ 'image' ], $site_id);
  227. }
  228. $res = model("promotion_friends_coupon")->delete($condition);
  229. #删除活动所建分组
  230. model('promotion_friends_coupon_group')->delete([ [ 'promotion_id', '=', $coupon_id ] ]);
  231. $cron = new Cron();
  232. $cron->deleteCron([ [ 'event', '=', 'CronChangeDivideticketStatus' ], [ 'relate_id', '=', $coupon_id ] ]);
  233. model('promotion_friends_coupon')->commit();
  234. return $this->success($res);
  235. } catch (\Exception $e) {
  236. model('promotion_friends_coupon')->rollback();
  237. return $this->error($e->getMessage());
  238. }
  239. }
  240. /**
  241. * 发起瓜分
  242. */
  243. public function launch($coupon_id, $member_id, $site_id)
  244. {
  245. $divideticket_info = model('promotion_friends_coupon')->getInfo(
  246. [
  247. [ 'coupon_id', '=', $coupon_id ],
  248. [ 'site_id', '=', $site_id ],
  249. [ 'status', '=', 1 ],
  250. ]
  251. );
  252. if (empty($divideticket_info)) return $this->error('', '未查到瓜分活动信息');
  253. if ($divideticket_info[ 'inventory' ] <= 0) return $this->error('', '优惠券已被抢完了,下次再来吧');
  254. $launch_info = model('promotion_friends_coupon_group')->getInfo([ [ 'promotion_id', '=', $divideticket_info[ 'coupon_id' ] ], [ 'header_id', '=', $member_id ] ], 'group_id,status');
  255. if (!empty($launch_info) && $launch_info[ 'status' ] == 0) return $this->error('', '该商品正在瓜分中');
  256. if (!empty($launch_info) && $launch_info[ 'status' ] == 1) return $this->error('', '已参与过此活动,无法重复发起瓜分');
  257. $member_info = model('member')->getInfo([ [ 'site_id', '=', $site_id ], [ 'member_id', '=', $member_id ] ], 'nickname,headimg');
  258. if (empty($member_info)) return $this->error('', '未获取到会员信息');
  259. model('promotion_friends_coupon_group')->startTrans();
  260. $end_time = time() + ( $divideticket_info[ 'divide_time' ] * 3600 );
  261. try {
  262. $data = [
  263. 'promotion_id' => $divideticket_info[ 'coupon_id' ],
  264. 'coupon_type_id' => $divideticket_info[ 'coupon_type_id' ],
  265. 'start_time' => time(),
  266. 'header_id' => $member_id,
  267. 'status' => 0,
  268. 'num' => $divideticket_info[ 'divide_num' ],
  269. 'site_id' => $site_id,
  270. 'group_member_ids' => $member_id,
  271. ];
  272. if ($divideticket_info[ 'end_time' ] < $end_time) {
  273. $data[ 'end_time' ] = $divideticket_info[ 'end_time' ];
  274. } else {
  275. $data[ 'end_time' ] = $end_time;
  276. }
  277. $launch_id = model('promotion_friends_coupon_group')->add($data);
  278. #同一活动瓜分失败的改为去查看
  279. model('promotion_friends_coupon_group')->update([ 'is_look' => 1 ], [ [ 'promotion_id', '=', $divideticket_info[ 'coupon_id' ] ], [ 'header_id', '=', $member_id ], [ 'status', '=', 2 ] ]);
  280. model('promotion_friends_coupon')->setDec([ [ 'coupon_id', '=', $coupon_id ], [ 'site_id', '=', $site_id ] ], 'inventory');
  281. $cron = new Cron();
  282. #是否模拟好友 1 是 2 否
  283. if ($divideticket_info[ 'is_simulation' ] == 1) {
  284. #加个 瓜分时间到期自动补齐
  285. $cron->addCron(1, 0, '未成团自动模拟好友瓜分', 'DivideticketSimulation', $data[ 'end_time' ], $launch_id);
  286. } else {
  287. $cron->addCron(1, 0, '瓜分发起自动关闭', 'DivideticketLaunchClose', $data[ 'end_time' ], $launch_id);
  288. }
  289. model('promotion_friends_coupon_group')->commit();
  290. return $this->success($launch_id);
  291. } catch (\Exception $e) {
  292. model('promotion_friends_coupon_group')->rollback();
  293. return $this->error($e->getMessage());
  294. }
  295. }
  296. /**
  297. * 帮瓜分
  298. * @param $launch_id
  299. * @param $member_id
  300. * @param $site_id
  301. */
  302. public function divideticket($launch_id, $member_id, $site_id)
  303. {
  304. $divideticket_group = model('promotion_friends_coupon_group')->getInfo(
  305. [
  306. [ 'group_id', '=', $launch_id ],
  307. [ 'site_id', '=', $site_id ],
  308. ]
  309. );
  310. if (empty($divideticket_group)) return $this->error('', '未查到好友瓜分券参与活动组信息');
  311. if ($divideticket_group[ 'status' ] == 1) return $this->error('', '已经被瓜分完了');
  312. if ($divideticket_group[ 'status' ] == 2) return $this->error('', '瓜分过期请重新发起瓜分');
  313. $member_info = model('member')->getInfo([ [ 'site_id', '=', $site_id ], [ 'member_id', '=', $member_id ] ], 'nickname,headimg');
  314. if (empty($member_info)) return $this->error('', '未获取到会员信息');
  315. model('promotion_friends_coupon_group')->startTrans();
  316. try {
  317. $divideticket_info = model('promotion_friends_coupon')->getInfo(
  318. [
  319. [ 'coupon_id', '=', $divideticket_group[ 'promotion_id' ] ],
  320. [ 'site_id', '=', $site_id ],
  321. [ 'status', '=', 1 ],
  322. ]
  323. );
  324. if (empty($divideticket_info)) return $this->error('', '未查到瓜分活动信息');
  325. #判断此用户是否是新人
  326. $divideticket_member_group = model('promotion_friends_coupon_group')->getList([ [ 'promotion_id', '=', $divideticket_info[ 'coupon_id' ] ] ], 'member_ids');
  327. $is_new = 0;
  328. if (!empty($divideticket_member_group)) {
  329. foreach ($divideticket_member_group as $k => $v) {
  330. if (in_array($member_id, explode(",", $v[ 'member_ids' ]))) {
  331. $is_new = $is_new + 1;
  332. }
  333. }
  334. }
  335. #活动限制仅新人可瓜
  336. if ($divideticket_info[ 'is_new' ] == 1) {
  337. if ($is_new > 0) return $this->error('', '您已参加过此活动了,此活动只可参与一次');
  338. }
  339. $member_arr = [];#帮瓜分用户
  340. $group_member_arr = [];#瓜分组用户
  341. if (!empty($divideticket_group[ 'group_member_ids' ])) {
  342. $group_member_arr = explode(",", $divideticket_group[ 'group_member_ids' ]);
  343. }
  344. if (!empty($divideticket_group[ 'member_ids' ])) {
  345. $member_arr = explode(",", $divideticket_group[ 'member_ids' ]);
  346. }
  347. if (in_array($member_id, $member_arr)) {
  348. return $this->error('', '已经帮助瓜分过啦');
  349. }
  350. #插入瓜分的用户组
  351. array_push($member_arr, $member_id);
  352. array_push($group_member_arr, $member_id);
  353. #达到人数(瓜分成功)
  354. if (count($group_member_arr) == $divideticket_group[ 'num' ]) {
  355. model('promotion_friends_coupon')->setInc([ [ 'coupon_id', '=', $divideticket_group[ 'promotion_id' ] ], [ 'site_id', '=', $site_id ] ], 'success_count');
  356. model('promotion_friends_coupon_group')->update([ 'status' => 1, 'is_look' => 1, 'member_ids' => implode(',', $member_arr), 'group_member_ids' => implode(',', $group_member_arr) ], [ [ 'group_id', '=', $launch_id ], [ 'site_id', '=', $site_id ] ]);
  357. $coupon_data = [
  358. 'type' => 'divideticket',
  359. 'at_least' => $divideticket_info[ 'at_least' ],
  360. 'coupon_name' => $divideticket_info[ 'name' ],
  361. 'coupon_type_id' => $divideticket_info[ 'coupon_type_id' ],
  362. 'site_id' => $site_id,
  363. 'coupon_code' => random_keys(8),
  364. 'goods_type' => $divideticket_info[ 'goods_type' ],
  365. 'goods_ids' => $divideticket_info[ 'goods_ids' ],
  366. 'state' => 1,
  367. 'get_type' => 3,
  368. 'fetch_time' => time(),
  369. 'start_time' => time(),
  370. ];
  371. if ($divideticket_info[ 'validity_type' ] == 0) {
  372. $coupon_data[ 'end_time' ] = $divideticket_info[ 'validity_end_time' ];
  373. } else if ($divideticket_info[ 'validity_type' ] == 1) {
  374. $coupon_data[ 'end_time' ] = time() + $divideticket_info[ 'fixed_term' ] * 86400;
  375. }
  376. #固定的瓜分金额
  377. if ($divideticket_info[ 'divide_type' ] == 0) {
  378. $discount_coupon_money = round($divideticket_info[ 'money' ] / $divideticket_info[ 'divide_num' ], 2);
  379. $coupon_data[ 'money' ] = $discount_coupon_money;
  380. foreach ($group_member_arr as $k => $v) {
  381. $coupon_data[ 'member_id' ] = $v;
  382. $this->addCoupon($coupon_data, $launch_id);
  383. }
  384. } else {
  385. #新人组
  386. $couple_group = [];
  387. #旧人组
  388. $old_group = [];
  389. #判断参与活动的用户是否是新人
  390. if (!empty($divideticket_member_group)) {
  391. foreach ($group_member_arr as $key => $value) {
  392. $fresh_num = 0;
  393. foreach ($divideticket_member_group as $k => $v) {
  394. if (in_array($value, explode(",", $v[ 'member_ids' ]))) {
  395. $fresh_num = $fresh_num + 1;
  396. }
  397. }
  398. if ($fresh_num == 0) array_push($couple_group, $value);
  399. }
  400. $old_group = array_diff($group_member_arr, $couple_group);
  401. } else {
  402. $couple_group = $group_member_arr;
  403. }
  404. #随机获取比例(整数)
  405. $proportion = $this->rand_bouns($divideticket_info[ 'divide_num' ], $divideticket_info[ 'money' ]);
  406. #比例从大到小排序
  407. arsort($proportion);
  408. $proportion = array_values($proportion);
  409. #有新人
  410. if (!empty($couple_group)) {
  411. #打乱新人组排序
  412. shuffle($couple_group);
  413. $couple_group = array_values($couple_group);
  414. foreach ($couple_group as $k => $v) {
  415. $coupon_data[ 'money' ] = round($proportion[ $k ], 2);
  416. $coupon_data[ 'member_id' ] = $v;
  417. $this->addCoupon($coupon_data, $launch_id);
  418. unset($proportion[ $k ]);
  419. }
  420. if (!empty($old_group)) {
  421. shuffle($old_group);
  422. $old_group = array_values($old_group);
  423. $proportion = array_values($proportion);
  424. foreach ($old_group as $k => $v) {
  425. $coupon_data[ 'money' ] = round($proportion[ $k ], 2);
  426. $coupon_data[ 'member_id' ] = $v;
  427. $this->addCoupon($coupon_data, $launch_id);
  428. }
  429. }
  430. } else {
  431. #打乱旧人组排序
  432. shuffle($old_group);
  433. $old_group = array_values($old_group);
  434. foreach ($old_group as $k => $v) {
  435. $coupon_data[ 'money' ] = round($proportion[ $k ], 2);
  436. $coupon_data[ 'member_id' ] = $v;
  437. $this->addCoupon($coupon_data, $launch_id);
  438. }
  439. }
  440. #修改 人组顺序
  441. $new_group_member_ids = array_merge($couple_group, $old_group);
  442. model('promotion_friends_coupon_group')->update([ 'group_member_ids' => implode(',', $new_group_member_ids) ], [ [ 'group_id', '=', $launch_id ], [ 'site_id', '=', $site_id ] ]);
  443. }
  444. } else {
  445. #增加参与人
  446. model('promotion_friends_coupon_group')->update([ 'member_ids' => implode(',', $member_arr), 'group_member_ids' => implode(',', $group_member_arr) ], [ [ 'group_id', '=', $launch_id ], [ 'site_id', '=', $site_id ] ]);
  447. }
  448. model('promotion_friends_coupon_group')->commit();
  449. return $this->success();
  450. } catch (\Exception $e) {
  451. model('promotion_friends_coupon_group')->rollback();
  452. return $this->error($e->getMessage());
  453. }
  454. }
  455. #新增优惠券
  456. public function addCoupon($data, $launch_id)
  457. {
  458. model('promotion_coupon')->startTrans();
  459. try {
  460. $res = model('promotion_coupon')->add($data);
  461. $divideticket_group = model('promotion_friends_coupon_group')->getInfo(
  462. [
  463. [ 'group_id', '=', $launch_id ],
  464. ]
  465. );
  466. if (empty($divideticket_group)) return $this->error('', '未查到好友瓜分券参与活动组信息');
  467. $coupon_ids_arr = [];
  468. if (!empty($divideticket_group[ 'coupon_type_id' ])) {
  469. $coupon_ids_arr = explode(",", $divideticket_group[ 'coupon_ids' ]);
  470. }
  471. array_push($coupon_ids_arr, $res);
  472. model('promotion_friends_coupon_group')->update([ 'coupon_ids' => implode(',', $coupon_ids_arr) ], [ [ 'group_id', '=', $launch_id ] ]);
  473. $cron = new Cron();
  474. #删除瓜分组定时结束
  475. $cron->deleteCron([ [ 'event', '=', 'DivideticketLaunchClose' ], [ 'relate_id', '=', $launch_id ] ]);
  476. model('promotion_coupon')->commit();
  477. return $res;
  478. } catch (\Exception $e) {
  479. model('promotion_coupon')->rollback();
  480. return $this->error($e->getMessage());
  481. }
  482. }
  483. /**
  484. * @param $person 人数
  485. * @param $percent 金额
  486. * @return array
  487. */
  488. public static function rand_bouns($person, $percent)
  489. {
  490. //百分比
  491. $now_person = $person;
  492. $bouns = array ();
  493. for ($i = 0; $i <= $person - 1; $i++) {
  494. $bouns[ $i ] = self::get_bouns($now_person, $percent);
  495. $percent = $percent - $bouns[ $i ];
  496. $now_person = $now_person - 1;
  497. }
  498. return $bouns;
  499. }
  500. public static function get_bouns($person, $percent)
  501. {
  502. if ($person == 1) return $percent;
  503. $max = 30;
  504. if ($percent < $max) $max = $percent;
  505. $min = $percent - $max * ( $person - 1 ) <= 0 ? 1 : $percent - $max * ( $person - 1 );
  506. $max = $max - ( $person ) <= 0 ? 1 : $max - ( $person );
  507. return rand($min, $max);
  508. }
  509. /**
  510. * 海报
  511. */
  512. public function poster($arr, $app_type, $site_id, $member_id)
  513. {
  514. try {
  515. $qrcode_info = $this->getQrcode($arr, $app_type, $site_id);
  516. if ($qrcode_info[ 'code' ] < 0) return $qrcode_info;
  517. $member_info = $this->getMemberInfo($member_id);
  518. if (empty($member_info)) return $this->error('未获取到会员信息');
  519. $poster = new PosterExtend(740, 1250);
  520. $option = [
  521. [
  522. 'action' => 'imageCopy', // 背景图
  523. 'data' => [
  524. 'public/uniapp/divideticket/poster_two.png',
  525. 0,
  526. 0,
  527. 740,
  528. 1250,
  529. 'square',
  530. 0,
  531. 1
  532. ]
  533. ],
  534. [
  535. 'action' => 'imageCopy', // 二维码
  536. 'data' => [
  537. $qrcode_info[ 'data' ][ 'path' ],
  538. 505,
  539. 980,
  540. 205,
  541. 205,
  542. 'square',
  543. 0,
  544. 1
  545. ]
  546. ],
  547. [
  548. 'action' => 'imageCircularCopy', // 写入用户头像
  549. 'data' => [
  550. !empty($member_info[ 'headimg' ]) ? $member_info[ 'headimg' ] : 'public/static/img/default_img/head.png',
  551. 82,
  552. 852,
  553. 112,
  554. 112
  555. ]
  556. ],
  557. [
  558. 'action' => 'imageText', // 写入分享人昵称
  559. 'data' => [
  560. $member_info[ 'nickname' ],
  561. 22,
  562. [ 255, 129, 61 ],
  563. 40,
  564. 1030,
  565. 440,
  566. 1,
  567. true,
  568. 1
  569. ]
  570. ]
  571. ];
  572. $option_res = $poster->create($option);
  573. if (is_array($option_res)) return $option_res;
  574. $res = $option_res->jpeg('upload/poster/divideticket', 'coupon_id_' . $arr[ 'cid' ] . 'group_id_' . $arr[ 'gid' ] . '_' . $app_type);
  575. return $res;
  576. } catch (\Exception $e) {
  577. return $this->error($e->getMessage());
  578. }
  579. }
  580. /**
  581. * 获取用户信息
  582. * @param unknown $member_id
  583. */
  584. private function getMemberInfo($member_id)
  585. {
  586. $info = model('member')->getInfo([ 'member_id' => $member_id ], 'nickname,headimg');
  587. return $info;
  588. }
  589. /**
  590. * 生成优惠券二维码
  591. * @param $coupon_id
  592. * @param string $app_type all为全部
  593. * @param string $type 类型 create创建 get获取
  594. * @return mixed|array
  595. */
  596. // public function poster($arr, $app_type, $site_id, $type = 'create')
  597. // {
  598. // $res = event('Qrcode', [
  599. // 'site_id' => $site_id,
  600. // 'app_type' => $app_type,
  601. // 'type' => $type,
  602. // 'data' => $arr,
  603. // 'page' => '/promotionpages/guafen/guafen/index',
  604. // 'qrcode_path' => 'upload/qrcode/divideticket',
  605. // 'qrcode_name' => 'coupon_id_' . $arr['coupon_id'] . 'inviter_id_' . $arr['inviter_id'] . 'group_id_' . $arr['group_id'] . '_' . $site_id,
  606. // ], true);
  607. // return $res;
  608. // }
  609. public function getQrcode($arr, $app_type, $site_id, $type = 'create')
  610. {
  611. $res = event('Qrcode', [
  612. 'site_id' => $site_id,
  613. 'app_type' => $app_type,
  614. 'type' => $type,
  615. 'data' => $arr,
  616. 'page' => '/pages_promotion/divideticket/index',
  617. 'qrcode_path' => 'upload/qrcode/divideticket',
  618. 'qrcode_name' => 'coupon_id_' . $arr[ 'cid' ] . 'inviter_id_' . $arr[ 'id' ] . 'group_id_' . $arr[ 'gid' ] . '_' . $site_id,
  619. ], true);
  620. return $res;
  621. }
  622. /**
  623. * 到时模拟瓜分
  624. */
  625. public function cronDivideticketSimulation($launch_id)
  626. {
  627. $launch_info = model('promotion_friends_coupon_group')->getInfo([ [ 'group_id', '=', $launch_id ] ]);
  628. $divideticket_info = model('promotion_friends_coupon')->getInfo([ [ 'coupon_id', '=', $launch_info[ 'promotion_id' ] ] ]);
  629. model('promotion_friends_coupon')->setInc([ [ 'coupon_id', '=', $launch_info[ 'promotion_id' ] ] ], 'success_count');
  630. model('promotion_friends_coupon_group')->update([ 'status' => 1 ], [ [ 'group_id', '=', $launch_id ] ]);
  631. $coupon_data = [
  632. 'type' => 'divideticket',
  633. 'coupon_name' => $divideticket_info[ 'name' ],
  634. 'at_least' => $divideticket_info[ 'at_least' ],
  635. 'coupon_type_id' => $divideticket_info[ 'coupon_type_id' ],
  636. 'site_id' => $divideticket_info[ 'site_id' ],
  637. 'coupon_code' => random_keys(8),
  638. 'goods_type' => $divideticket_info[ 'goods_type' ],
  639. 'goods_ids' => $divideticket_info[ 'goods_ids' ],
  640. 'state' => 1,
  641. 'get_type' => 3,
  642. 'fetch_time' => time(),
  643. 'start_time' => time(),
  644. ];
  645. if ($divideticket_info[ 'validity_type' ] == 0) {
  646. $coupon_data[ 'end_time' ] = $divideticket_info[ 'validity_end_time' ];
  647. } else if ($divideticket_info[ 'validity_type' ] == 1) {
  648. $coupon_data[ 'end_time' ] = time() + $divideticket_info[ 'fixed_term' ] * 86400;
  649. }
  650. $group_member_arr = [];#瓜分组用户
  651. if (!empty($launch_info[ 'group_member_ids' ])) {
  652. $group_member_arr = explode(",", $launch_info[ 'group_member_ids' ]);
  653. }
  654. #固定的瓜分金额
  655. if ($divideticket_info[ 'divide_type' ] == 0) {
  656. $discount_coupon_money = round($divideticket_info[ 'money' ] / $divideticket_info[ 'divide_num' ], 2);
  657. $coupon_data[ 'money' ] = $discount_coupon_money;
  658. foreach ($group_member_arr as $k => $v) {
  659. $coupon_data[ 'member_id' ] = $v;
  660. $this->addCoupon($coupon_data, $launch_id);
  661. }
  662. } else {
  663. #新人组
  664. $couple_group = [];
  665. #旧人组
  666. $old_group = [];
  667. #判断参与活动的用户是否是新人
  668. if (!empty($divideticket_member_group)) {
  669. foreach ($group_member_arr as $key => $value) {
  670. $fresh_num = 0;
  671. foreach ($divideticket_member_group as $k => $v) {
  672. if (in_array($value, explode(",", $v[ 'member_ids' ]))) {
  673. $fresh_num = $fresh_num + 1;
  674. }
  675. }
  676. if ($fresh_num == 0) array_push($couple_group, $value);
  677. }
  678. $old_group = array_diff($group_member_arr, $couple_group);
  679. } else {
  680. $couple_group = $group_member_arr;
  681. }
  682. #随机获取比例(整数)
  683. $proportion = $this->rand_bouns($divideticket_info[ 'divide_num' ], $divideticket_info[ 'money' ]);
  684. #比例从大到小排序
  685. arsort($proportion);
  686. $proportion = array_values($proportion);
  687. #有新人
  688. if (!empty($couple_group)) {
  689. #打乱新人组排序
  690. shuffle($couple_group);
  691. $couple_group = array_values($couple_group);
  692. foreach ($couple_group as $k => $v) {
  693. $coupon_data[ 'money' ] = round($proportion[ $k ], 2);
  694. $coupon_data[ 'member_id' ] = $v;
  695. $this->addCoupon($coupon_data, $launch_id);
  696. unset($proportion[ $k ]);
  697. }
  698. if (!empty($old_group)) {
  699. shuffle($old_group);
  700. $old_group = array_values($old_group);
  701. $proportion = array_values($proportion);
  702. foreach ($old_group as $k => $v) {
  703. $coupon_data[ 'money' ] = round($proportion[ $k ], 2);
  704. $coupon_data[ 'member_id' ] = $v;
  705. $this->addCoupon($coupon_data, $launch_id);
  706. }
  707. }
  708. } else {
  709. #打乱旧人组排序
  710. shuffle($old_group);
  711. $old_group = array_values($old_group);
  712. foreach ($old_group as $k => $v) {
  713. $coupon_data[ 'money' ] = round($proportion[ $k ], 2);
  714. $coupon_data[ 'member_id' ] = $v;
  715. $this->addCoupon($coupon_data, $launch_id);
  716. }
  717. }
  718. #修改 人组顺序
  719. $new_group_member_ids = array_merge($couple_group, $old_group);
  720. model('promotion_friends_coupon_group')->update([ 'group_member_ids' => implode(',', $new_group_member_ids) ], [ [ 'group_id', '=', $launch_id ], [ 'site_id', '=', $launch_info[ 'site_id' ] ] ]);
  721. }
  722. }
  723. /**
  724. * @param $coupon_id
  725. * @param $name
  726. * @param $site_id
  727. * @param string $type
  728. * @return array
  729. * shop端推广
  730. */
  731. public function spread($coupon_id, $name, $site_id, $type = "create")
  732. {
  733. $data = [
  734. 'site_id' => $site_id,
  735. 'app_type' => "all", // all为全部
  736. 'type' => $type, // 类型 create创建 get获取
  737. 'data' => [
  738. "coupon_id" => $coupon_id
  739. ],
  740. 'page' => '/pages_promotion/divideticket/index',
  741. 'qrcode_path' => 'upload/qrcode/devideticket',
  742. 'qrcode_name' => 'coupon_id_' . $coupon_id,
  743. ];
  744. event('Qrcode', $data, true);
  745. $app_type_list = config('app_type');
  746. $path = [];
  747. foreach ($app_type_list as $k => $v) {
  748. switch ( $k ) {
  749. case 'h5':
  750. $wap_domain = getH5Domain();
  751. $path[ $k ][ 'status' ] = 1;
  752. $path[ $k ][ 'url' ] = $wap_domain . $data[ 'page' ] . '?coupon_id=' . $coupon_id;
  753. $path[ $k ][ 'img' ] = "upload/qrcode/devideticket/coupon_id_" . $coupon_id . "_" . $k . ".png";
  754. break;
  755. case 'weapp' :
  756. $config = new ConfigModel();
  757. $res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WEAPP_CONFIG' ] ]);
  758. if (!empty($res[ 'data' ])) {
  759. if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
  760. $path[ $k ][ 'status' ] = 2;
  761. $path[ $k ][ 'message' ] = '未配置微信小程序';
  762. } else {
  763. $path[ $k ][ 'status' ] = 1;
  764. $path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
  765. }
  766. } else {
  767. $path[ $k ][ 'status' ] = 2;
  768. $path[ $k ][ 'message' ] = '未配置微信小程序';
  769. }
  770. break;
  771. case 'wechat' :
  772. $config = new ConfigModel();
  773. $res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_CONFIG' ] ]);
  774. if (!empty($res[ 'data' ])) {
  775. if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
  776. $path[ $k ][ 'status' ] = 2;
  777. $path[ $k ][ 'message' ] = '未配置微信公众号';
  778. } else {
  779. $path[ $k ][ 'status' ] = 1;
  780. $path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
  781. }
  782. } else {
  783. $path[ $k ][ 'status' ] = 2;
  784. $path[ $k ][ 'message' ] = '未配置微信公众号';
  785. }
  786. break;
  787. }
  788. }
  789. $return = [
  790. 'path' => $path,
  791. 'name' => $name,
  792. ];
  793. return $this->success($return);
  794. }
  795. public function urlQrcode($page, $qrcode_param, $promotion_type, $site_id)
  796. {
  797. $params = [
  798. 'site_id' => $site_id,
  799. 'data' => $qrcode_param,
  800. 'page' => $page,
  801. 'promotion_type' => $promotion_type,
  802. 'h5_path' => $page . '?cid=' . $qrcode_param[ 'cid' ],
  803. 'qrcode_path' => 'upload/qrcode/devideticket',
  804. 'qrcode_name' =>'coupon_id_' . $promotion_type . '_' . $qrcode_param[ 'cid' ] . '_' . $site_id,
  805. ];
  806. $solitaire = event('ExtensionInformation', $params, true);
  807. return $this->success($solitaire);
  808. }
  809. }