StoreAssistanceActive.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. <?php
  2. namespace app\models\store;
  3. use app\models\routine\RoutineFormId;
  4. use app\models\routine\RoutineTemplate;
  5. use app\models\user\User;
  6. use app\models\user\WechatUser;
  7. use app\models\wechat\WechatTemplate;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\WechatTemplateService;
  10. use crmeb\services\MiniProgramService;
  11. use crmeb\traits\ModelTrait;
  12. use PDOStatement;
  13. use think\db\exception\DataNotFoundException;
  14. use think\db\exception\DbException;
  15. use think\db\exception\ModelNotFoundException;
  16. use think\facade\Log;
  17. use think\facade\Route;
  18. /**
  19. * TODO 助力Model
  20. * Class StoreAssistanceActive
  21. * @package app\models\store
  22. */
  23. class StoreAssistanceActive extends BaseModel
  24. {
  25. /**
  26. * 数据表主键
  27. * @var string
  28. */
  29. protected $pk = 'id';
  30. /**
  31. * 模型名称
  32. * @var string
  33. */
  34. protected $name = 'store_assistance_active';
  35. use ModelTrait;
  36. public static function merSet($mer_id, $alias = '')
  37. {
  38. return $mer_id ? self::where($alias ? $alias . '.mer_id' : 'mer_id', $mer_id) : new self;
  39. }
  40. /*
  41. * 获取助力完成的用户
  42. * @param int $uid 用户id
  43. * @return array
  44. * */
  45. public static function getAssistanceOkList($uid)
  46. {
  47. $list = self::alias('a')->where('a.status', 2)->where('a.is_refund', 0)->where('a.uid', '<>', $uid)->join('User u', 'u.uid=a.uid', 'right')->column('nickname', 'id');
  48. $msg = [];
  49. foreach ($list as &$item) {
  50. $msg[] = $item .= '助力成功';
  51. }
  52. return $msg;
  53. }
  54. /*
  55. * 获取助力完成的商品总件数
  56. * */
  57. public static function getAssistanceOkSumTotalNum($id)
  58. {
  59. return self::where('status', 2)->where('is_refund', 0)->sum('total_num');
  60. }
  61. /**
  62. * 获取一条助力数据
  63. * @param $id
  64. * @param bool $mer_id
  65. * @return mixed
  66. */
  67. public static function getAssistanceUserOne($id, $mer_id = false)
  68. {
  69. $model = self::merSet($mer_id, 'p');
  70. $model = $model->alias('p');
  71. $model = $model->field('p.*,u.nickname,u.avatar');
  72. $model = $model->where('id', $id);
  73. $model = $model->join('user u', 'u.uid = p.uid');
  74. return $model->find();
  75. }
  76. /**
  77. * 获取助力的团员
  78. * @param $id
  79. * @return mixed
  80. */
  81. public static function getAssistanceMember($id)
  82. {
  83. $model = new self();
  84. $model = $model->alias('p');
  85. $model = $model->field('p.*,u.nickname,u.avatar');
  86. $model = $model->where('k_id', $id);
  87. $model = $model->where('is_refund', 0);
  88. $model = $model->join('user u', 'u.uid = p.uid');
  89. $model = $model->order('id asc');
  90. return $model->select();
  91. }
  92. /**
  93. * 设置结束时间
  94. * @param $idAll
  95. * @return $this
  96. */
  97. public static function setAssistanceStopTime($idAll)
  98. {
  99. $model = new self();
  100. $model = $model->where('id', 'IN', $idAll);
  101. return $model->update(['stop_time' => time(), 'status' => 2]);
  102. }
  103. /**
  104. * 获取正在助力的数据 发起人
  105. * @param int $cid 商品id
  106. * @param int $isAll 是否查找所有助力
  107. * @return array
  108. */
  109. public static function getAssistanceAll($cid, $isAll = false)
  110. {
  111. $model = new self();
  112. $model = $model->alias('p');
  113. $model = $model->field('p.id,p.uid,p.people,p.price,p.stop_time,u.nickname,u.avatar');
  114. $model = $model->where('stop_time', '>', time());
  115. $model = $model->where('p.status', 1);
  116. $model = $model->where('p.cid', $cid);
  117. $model = $model->where('p.k_id', 0);
  118. $model = $model->where('p.is_refund', 0);
  119. $model = $model->order('p.add_time desc');
  120. $model = $model->join('user u', 'u.uid = p.uid');
  121. $list = $model->select();
  122. $list = count($list) ? $list->toArray() : [];
  123. if ($isAll) {
  124. $assistanceAll = [];
  125. foreach ($list as &$v) {
  126. $v['count'] = self::getAssistancePeople($v['id'], $v['people']);
  127. $v['h'] = date('H', $v['stop_time']);
  128. $v['i'] = date('i', $v['stop_time']);
  129. $v['s'] = date('s', $v['stop_time']);
  130. $assistanceAll[] = $v['id'];//助力发起人ID
  131. $v['stop_time'] = (int)$v['stop_time'];
  132. }
  133. return [$list, $assistanceAll];
  134. }
  135. return $list;
  136. }
  137. /**
  138. * 获取还差几人
  139. */
  140. public static function getAssistancePeople($kid, $people)
  141. {
  142. $model = new self();
  143. $model = $model->where('k_id', $kid)->where('is_refund', 0);
  144. $count = $model->count();
  145. return bcsub($people, $count, 0);
  146. }
  147. /**
  148. * 判断是否发送模板消息 0 未发送 1已发送
  149. * @param $uidAll
  150. * @return int|string
  151. */
  152. public static function isTpl($uidAll, $pid)
  153. {
  154. if (is_array($uidAll))
  155. $count = self::where('uid', 'IN', implode(',', $uidAll))->where('is_tpl', 0)->where('id|k_id', $pid)->count();
  156. else
  157. $count = self::where('uid', $uidAll)->where('is_tpl', 0)->where('k_id|id', $pid)->count();
  158. return $count;
  159. }
  160. /**
  161. * 助力成功提示模板消息
  162. * @param $uidAll
  163. * @param $pid
  164. */
  165. public static function orderAssistanceAfter($uidAll, $pid)
  166. {
  167. $assistanceInfo = self::where('p.id|p.k_id', $pid)->alias('p')->field(['p.people', 't.title', 'p.add_time', 'p.order_id', 'u.nickname'])->join('user u', 'u.uid = p.uid')->join('store_assistance t', 'p.cid = t.id')->find();
  168. if (!$assistanceInfo) return false;
  169. foreach ($uidAll as $key => &$item) {
  170. $openid = WechatUser::uidToOpenid($item, 'openid');
  171. $routineOpenid = WechatUser::uidToOpenid($item, 'routine_openid');
  172. $nickname = WechatUser::uidToOpenid(self::where('id', $pid)->value('uid'), 'nickname');
  173. if ($openid) { //公众号模板消息
  174. // $firstWeChat = '亲,您的助力已经完成了';
  175. $keyword1WeChat = self::where('id|k_id', $pid)->where('uid', $item)->value('order_id');
  176. $keyword2WeChat = self::alias('p')->where('p.id|p.k_id', $pid)->where('p.uid', $item)->join('store_assistance c', 'c.id=p.cid')->value('c.title');
  177. // $remarkWeChat = '点击查看订单详情';
  178. // $urlWeChat = Route::buildUrl('order/detail/' . $keyword1WeChat)->suffix('')->domain(true)->build();
  179. // WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_SUCCESS, [
  180. // 'first' => $firstWeChat,
  181. // 'keyword1' => $keyword1WeChat,
  182. // 'keyword2' => $keyword2WeChat,
  183. // 'remark' => $remarkWeChat
  184. // ], $urlWeChat);
  185. $wechatTemplate = new WechatTemplate();
  186. $wechatTemplate->sendOrderAssistanceUseSuccess($item, $keyword1WeChat, $keyword2WeChat, $pid);
  187. } else if ($routineOpenid) {// 小程序模板消息
  188. RoutineTemplate::sendAssistanceSuccess($item, $assistanceInfo['title'], $assistanceInfo['nickname'] ?? '', $assistanceInfo['add_time'], $assistanceInfo['people'], '/pages/order_details/index?order_id=' . $assistanceInfo['order_id']);
  189. }
  190. }
  191. self::beginTrans();
  192. $res1 = self::where('uid', 'IN', implode(',', $uidAll))->where('id|k_id', $pid)->update(['is_tpl' => 1]);
  193. self::checkTrans($res1);
  194. }
  195. /**
  196. * 助力失败发送的模板消息
  197. * @param $uid
  198. * @param $pid
  199. */
  200. public static function orderAssistanceAfterNo($uid, $pid, $fillTilt = '', $isRemove = false)
  201. {
  202. $store = self::alias('p')->where('p.id|p.k_id', $pid)->field('c.*')->where('p.uid', $uid)->join('store_assistance c', 'c.id=p.cid')->find();
  203. $assistance = self::where('id|k_id', $pid)->where('uid', $uid)->find();
  204. $openid = WechatUser::uidToOpenid($uid, 'openid');
  205. $routineOpenid = WechatUser::uidToOpenid($uid, 'routine_openid');
  206. $wechatTemplate = new WechatTemplate();
  207. if ($isRemove) {
  208. if ($openid) {//公众号发送模板消息
  209. // $urlWeChat = Route::buildUrl('order/detail/' . $assistance->order_id)->suffix('')->domain(true)->build();
  210. // WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_LOSE, [
  211. // 'first' => '亲,您的助力取消',
  212. // 'keyword1' => $store->title,
  213. // 'keyword2' => $assistance->price,
  214. // 'keyword3' => $assistance->price,
  215. // 'remark' => '点击查看订单详情'
  216. // ], $urlWeChat);
  217. $wechatTemplate->sendOrderAssistanceClone($uid, $assistance, $store->title);
  218. } else if ($routineOpenid) {//小程序发送模板消息
  219. RoutineTemplate::sendAssistanceFail($uid, $store->title, $assistance->people, '亲,您的助力取消,点击查看订单详情', '/pages/order_details/index?order_id=' . $assistance->order_id);
  220. }
  221. } else {
  222. if ($openid) {//公众号发送模板消息
  223. // $urlWeChat = Route::buildUrl('order/detail/' . $assistance->order_id)->suffix('')->domain(true)->build();
  224. // WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_LOSE, [
  225. // 'first' => '亲,您的助力失败',
  226. // 'keyword1' => $store->title,
  227. // 'keyword2' => $assistance->price,
  228. // 'keyword3' => $assistance->price,
  229. // 'remark' => '点击查看订单详情'
  230. // ], $urlWeChat);
  231. $wechatTemplate->sendOrderAssistanceClone($uid, $assistance, $store->title);
  232. } else if ($routineOpenid) {//小程序发送模板消息
  233. RoutineTemplate::sendAssistanceFail(
  234. $uid,
  235. $store->title,
  236. $assistance->people,
  237. '亲,您助力失败,自动为您申请退款,退款金额为:' . $assistance->price,
  238. '/pages/order_details/index?order_id=' . $assistance->order_id
  239. );
  240. }
  241. }
  242. self::where('id', $pid)->update(['status' => 3, 'stop_time' => time()]);
  243. self::where('k_id', $pid)->update(['status' => 3, 'stop_time' => time()]);
  244. }
  245. /**
  246. * 获取当前助力数据返回订单编号
  247. * @param $id
  248. * @param $uid
  249. * @return array|false|PDOStatement|string|\think\Model
  250. * @throws DataNotFoundException
  251. * @throws DbException
  252. * @throws ModelNotFoundException
  253. */
  254. public static function getCurrentAssistance($id, $uid)
  255. {
  256. $assistance = self::where('id', $id)->where('uid', $uid)->find();
  257. if (!$assistance) $assistance = self::where('k_id', $id)->where('uid', $uid)->find();
  258. return StoreOrder::where('id', $assistance['order_id_key'])->value('order_id');
  259. }
  260. public static function systemPage($where)
  261. {
  262. $model = new self;
  263. $model = $model->alias('p');
  264. $model = $model->field('p.*,c.title,u.nickname,u.avatar');
  265. $model = self::getModelTime($where, $model, "p.add_time");
  266. if ($where['status']) $model = $model->where('p.status', $where['status']);
  267. if ($where['mer_id']) $model = $model->where('p.mer_id', $where['mer_id']);
  268. $model = $model->where('p.k_id', 0);
  269. $model = $model->order('p.id desc');
  270. $model = $model->join('StoreAssistance c', 'c.id=p.cid');
  271. $model = $model->join('user u', 'u.uid = p.uid');
  272. $count = $model->count();
  273. $list = $model->page((int)$where['page'], (int)$where['limit'])
  274. ->select()
  275. ->each(function ($item) {
  276. $item['count_people'] = self::where('k_id', $item['id'])->count();
  277. });
  278. return compact('count', 'list');
  279. }
  280. public static function isAssistanceBe($data, $id)
  281. {
  282. $data['id'] = $id;
  283. $count = self::where($data)->count();
  284. if ($count) return $count;
  285. $data['k_id'] = $id;
  286. $count = self::where($data)->count();
  287. if ($count) return $count;
  288. else return 0;
  289. }
  290. /**
  291. * 创建助力
  292. * @param $order
  293. * @return mixed
  294. * @throws DataNotFoundException
  295. * @throws DbException
  296. * @throws ModelNotFoundException
  297. */
  298. public static function createAssistanceActive($order)
  299. {
  300. $order = StoreOrder::tidyOrder($order, true, false, $order['mer_id'])->toArray();
  301. $openid = WechatUser::uidToOpenid($order['uid'], 'openid');
  302. $routineOpenid = WechatUser::uidToOpenid($order['uid'], 'routine_openid');
  303. $product = StoreAssistance::where('id', $order['assistance_id'])->field('effective_time,title')->find();
  304. if ($product) {
  305. if (!$order['assistance_active_id']) {//助力不存在
  306. $res = false;
  307. $assistance['uid'] = $order['uid'];//用户id
  308. $assistance['order_id'] = $order['order_id'];//订单id 生成
  309. $assistance['order_id_key'] = $order['id'];//订单id 数据库id
  310. $assistance['total_num'] = $order['total_num'];//购买个数
  311. $assistance['total_price'] = $order['pay_price'];//总金额
  312. $assistance['k_id'] = 0;//助力id
  313. foreach ($order['cartInfo'] as $v) {
  314. $assistance['cid'] = $v['assistance_id'];//助力商品id
  315. $assistance['pid'] = $v['product_id'];//商品id
  316. $assistance['people'] = StoreAssistance::where('id', $v['assistance_id'])->value('people');//几人助力
  317. $assistance['price'] = $v['productInfo']['price'];//单价
  318. $assistance['stop_time'] = bcadd(time(), bcmul($product->effective_time, 3600, 0), 0);//结束时间
  319. $assistance['add_time'] = time();//开团时间
  320. $assistance['mer_id'] = $order['mer_id'];//所属商户
  321. $res1 = self::create($assistance)->toArray();
  322. $res2 = StoreOrder::where('id', $order['id'])->update(['assistance_active_id' => $res1['id']]);
  323. $res = $res1 && $res2;
  324. $assistance['id'] = $res1['id'];
  325. }
  326. // 开团成功发送模板消息
  327. if ($openid && $order['is_channel'] != 1) { //公众号模板消息
  328. // $urlWeChat = Route::buildUrl('/order/detail/' . $assistance['order_id'])->suffix('')->domain(true)->build();
  329. // WechatTemplateService::sendTemplate($openid, WechatTemplateService::OPEN_PINK_SUCCESS, [
  330. // 'first' => '您好,您已成功开团!赶紧与小伙伴们分享吧!!!',
  331. // 'keyword1' => $product->title,
  332. // 'keyword2' => $assistance['total_price'],
  333. // 'keyword3' => $assistance['people'],
  334. // 'remark' => '点击查看订单详情'
  335. // ], $urlWeChat);
  336. $wechatTemplate = new WechatTemplate();
  337. $wechatTemplate->sendOrderAssistanceUseSuccess($order['uid'], $assistance, $product->title);
  338. } else if ($routineOpenid && $order['is_channel'] == 1) {
  339. $nickname = User::where('uid', $order['uid'])->value('nickname');
  340. RoutineTemplate::sendAssistanceSuccess(
  341. $order['uid'], $product->title,
  342. $nickname,
  343. $assistance['add_time'],
  344. $assistance['people'],
  345. '/pages/order_details/index?order_id=' . $assistance['order_id']
  346. );
  347. }
  348. if ($res) return true;
  349. else return false;
  350. }
  351. } else {
  352. Log::error('助力支付成功读取商品数据失败订单号:' . $order['order_id']);
  353. return false;
  354. }
  355. }
  356. /**
  357. * 助力人数完成时,判断全部人都是未退款状态
  358. * @param $assistanceIds
  359. * @return bool
  360. */
  361. public static function setAssistanceStatus($assistanceIds)
  362. {
  363. $orderAssistance = self::where('id', 'IN', $assistanceIds)->where('is_refund', 1)->count();
  364. if (!$orderAssistance) return true;
  365. else return false;
  366. }
  367. /*
  368. * 助力完成更改数据写入内容
  369. * @param array $uidAll 当前助力uid
  370. * @param array $idAll 当前助力assistance_active_id
  371. * @param array $assistanceT 团长信息
  372. * @return int
  373. * */
  374. public static function AssistanceComplete($uidAll, $idAll, $uid, $assistanceT)
  375. {
  376. $assistanceBool = 6;
  377. try {
  378. if (self::setAssistanceStatus($idAll)) {
  379. self::setAssistanceStopTime($idAll);
  380. if (in_array($uid, $uidAll)) {
  381. if (self::isTpl($uidAll, $assistanceT['id'])) self::orderAssistanceAfter($uidAll, $assistanceT['id']);
  382. $assistanceBool = 1;
  383. } else $assistanceBool = 3;
  384. }
  385. return $assistanceBool;
  386. } catch (\Exception $e) {
  387. self::setErrorInfo($e->getMessage());
  388. return $assistanceBool;
  389. }
  390. }
  391. /**
  392. * 更改助力订单状态
  393. */
  394. public static function changeAssistanceActiveStatus($uidAll, $idAll, $uid, $assistanceT)
  395. {
  396. if (self::setAssistanceStatus($idAll)) {
  397. self::setAssistanceStopTime($idAll);
  398. }
  399. }
  400. /*
  401. * 助力失败 退款
  402. * @param array $assistanceAll 助力数据,不包括团长
  403. * @param array $assistanceT 团长数据
  404. * @param int $assistanceBool
  405. * @param boolen $isRunErr 是否返回错误信息
  406. * @param boolen $isIds 是否返回记录所有助力id
  407. * @return int| boolen
  408. * */
  409. public static function AssistanceFail($assistanceAll, $assistanceT, $assistanceBool, $isRunErr = true, $isIds = false)
  410. {
  411. self::startTrans();
  412. $assistanceIds = [];
  413. try {
  414. if ($assistanceT['stop_time'] < time()) {//助力时间超时 退款
  415. $assistanceBool = -1;
  416. array_push($assistanceAll, $assistanceT);
  417. foreach ($assistanceAll as $v) {
  418. if (StoreOrder::orderApplyRefund(StoreOrder::getAssistanceOrderId($v['order_id_key']), $v['uid'], '助力时间超时') && self::isTpl($v['uid'], $assistanceT['id'])) {
  419. if ($isIds) array_push($assistanceIds, $v['id']);
  420. self::orderAssistanceAfterNo($assistanceT['uid'], $assistanceT['id']);
  421. } else {
  422. if ($isRunErr) return $assistanceBool;
  423. }
  424. }
  425. }
  426. self::commit();
  427. if ($isIds) return $assistanceIds;
  428. return $assistanceBool;
  429. } catch (\Exception $e) {
  430. self::rollback();
  431. return $assistanceBool;
  432. }
  433. }
  434. /*
  435. * 获取助力人和发起人和助力总人数
  436. * @param array $assistanceActive
  437. * @return array
  438. * */
  439. public static function getAssistanceMemberAndAssistanceK($assistanceActive)
  440. {
  441. //查找助力团员和团长
  442. if ($assistanceActive['k_id']) {
  443. $assistanceAll = self::getAssistanceMember($assistanceActive['k_id']);
  444. $assistanceT = self::getAssistanceUserOne($assistanceActive['k_id']);
  445. } else {
  446. $assistanceAll = self::getAssistanceMember($assistanceActive['id']);
  447. $assistanceT = $assistanceActive;
  448. }
  449. $assistanceT = $assistanceT->hidden(['order_id', 'total_price', 'cid', 'pid', 'add_time', 'k_id', 'is_tpl', 'is_refund'])->toArray();
  450. $assistanceAll = $assistanceAll->hidden(['total_price', 'cid', 'pid', 'add_time', 'k_id', 'is_tpl', 'is_refund'])->toArray();
  451. $count = count($assistanceAll);
  452. $count = (int)bcsub($assistanceT['people'], $count, 0);
  453. $idAll = [];
  454. $uidAll = [];
  455. //收集助力用户id和助力id
  456. foreach ($assistanceAll as $k => $v) {
  457. $idAll[$k] = $v['id'];
  458. $uidAll[$k] = $v['uid'];
  459. }
  460. $idAll[] = $assistanceT['id'];
  461. $uidAll[] = $assistanceT['uid'];
  462. return [$assistanceAll, $assistanceT, $count, $idAll, $uidAll];
  463. }
  464. /*
  465. * 取消助力
  466. * @param int $uid 用户id
  467. * @param int $assistance_active_id 团长id
  468. * @return boolean
  469. * */
  470. public static function removeAssistance($uid, $cid, $assistance_active_id, $nextAssistanceT = null)
  471. {
  472. $assistanceT = self::where('uid', $uid)
  473. ->where('id', $assistance_active_id)
  474. ->where('cid', $cid)
  475. ->where('k_id', 0)
  476. ->where('is_refund', 0)
  477. ->where('status', 1)
  478. ->where('stop_time', '>', time())
  479. ->find();
  480. if (!$assistanceT) return self::setErrorInfo('未查到助力信息,无法取消');
  481. self::startTrans();
  482. try {
  483. list($assistanceAll, $assistanceT, $count, $idAll, $uidAll) = self::getAssistanceMemberAndAssistanceK($assistanceT);
  484. if (count($assistanceAll)) {
  485. if (self::getAssistancePeople($assistance_active_id, $assistanceT['people'])) {
  486. //助力未完成,助力有成员取消开团取 紧跟团长后助力的人
  487. if (isset($assistanceAll[0])) $nextAssistanceT = $assistanceAll[0];
  488. } else {
  489. //助力完成
  490. self::AssistanceComplete($uidAll, $idAll, $uid, $assistanceT);
  491. return self::setErrorInfo(['status' => 200, 'msg' => '助力已完成,无法取消']);
  492. }
  493. }
  494. //取消开团
  495. if (StoreOrder::orderApplyRefund(StoreOrder::getAssistanceOrderId($assistanceT['order_id_key']), $assistanceT['uid'], '助力取消开团') && self::isTpl($assistanceT['uid'], $assistanceT['id'])) {
  496. self::orderAssistanceAfterNo($assistanceT['uid'], $assistanceT['id'], '助力取消开团', true);
  497. } else
  498. return self::setErrorInfo(['status' => 200, 'msg' => StoreOrder::getErrorInfo()], true);
  499. //当前团有人的时候
  500. if (is_array($nextAssistanceT)) {
  501. self::where('id', $nextAssistanceT['id'])->update(['k_id' => 0, 'status' => 1, 'stop_time' => $assistanceT['stop_time']]);
  502. self::where('k_id', $assistanceT['id'])->update(['k_id' => $nextAssistanceT['id']]);
  503. StoreOrder::where('order_id', $nextAssistanceT['order_id'])->update(['assistance_active_id' => $nextAssistanceT['id']]);
  504. }
  505. self::commitTrans();
  506. return true;
  507. } catch (\Exception $e) {
  508. return self::setErrorInfo($e->getLine() . ':' . $e->getMessage() . ':' . $e->getFile(), true);
  509. }
  510. }
  511. /**
  512. * 获取当前商品参与的人数
  513. * @param int $assistanceId
  514. * @return int|string
  515. */
  516. public static function getCountPeopleAll($assistanceId = 0, $mer_id = '')
  517. {
  518. if (!$assistanceId) return self::merSet($mer_id)->where('status', 2)->count();
  519. return self::merSet($mer_id)->where('cid', $assistanceId)->where('status', 2)->count();
  520. }
  521. /**
  522. * 获取当前商品参与的团数
  523. * @param int $assistanceId
  524. * @return int|string
  525. */
  526. public static function getCountPeopleAssistance($assistanceId = 0, $mer_id = '')
  527. {
  528. if (!$assistanceId) return self::merSet($mer_id)->where('k_id', 0)->where('status', 2)->count();
  529. return self::merSet($mer_id)->where('cid', $assistanceId)->where('k_id', 0)->where('status', 2)->count();
  530. }
  531. /**
  532. * 参与助力
  533. */
  534. public static function attendAssistance($uid, $parent_assistance_id)
  535. {
  536. $assistanceActive = self::get($parent_assistance_id);
  537. $product = StoreAssistance::where('id', $assistanceActive['cid'])->field('effective_time,title')->find();
  538. if ($product) {
  539. $res = false;
  540. $assistance['uid'] = $uid;
  541. if (self::isAssistanceBe($assistance, $parent_assistance_id)) return false;
  542. $assistance['order_id'] = $assistanceActive['order_id'];
  543. $assistance['order_id_key'] = $assistanceActive['order_id_key'];
  544. $assistance['total_num'] = $assistanceActive['total_num'];
  545. $assistance['total_price'] = $assistanceActive['total_price'];
  546. $assistance['cid'] = $assistanceActive['cid'];
  547. $assistance['pid'] = $assistanceActive['pid'];
  548. $assistance['people'] = $assistanceActive['people'];
  549. $assistance['price'] = $assistanceActive['price'];
  550. $assistance['add_time'] = time();
  551. $assistance['stop_time'] = 0;
  552. $assistance['k_id'] = $parent_assistance_id;
  553. $assistance['is_tpl'] = 0;
  554. $assistance['is_refund'] = 0;
  555. $assistance['is_separate_account'] = 0;
  556. $assistance['status'] = 1;
  557. $assistance['mer_id'] = $assistanceActive['mer_id'];//所属商户
  558. $res = self::create($assistance)->toArray();
  559. } else {
  560. Log::error('助力读取商品数据失败订单号:' . $order['order_id']);
  561. }
  562. if ($res) return true;
  563. else return false;
  564. }
  565. /**
  566. * 处理奖励
  567. */
  568. public static function treatmentAward($order_id, $mer_id = '')
  569. {
  570. $order = StoreOrder::get($order_id);
  571. $parent = self::where('mer_id', $mer_id)->where('order_id_key', $order_id)->where('k_id', 0)->find();
  572. $childs = self::where('mer_id', $mer_id)->where('order_id_key', $order_id)->where('k_id', '<>', 0)->select();
  573. $assistance = StoreAssistance::get($order['assistance_id']);
  574. // 选择发起人的订单作为分账的账单
  575. $transaction_id = $order['transaction_id']; // 微信订单号
  576. $out_order_no = $order['order_id'];
  577. // 计算发起人和参与者的分账金额
  578. $price = $order['pay_price'];
  579. $p_separate_account = $assistance['p_separate_account'];
  580. $c_separate_account = $assistance['c_separate_account'];
  581. $p_amount = bcmul($price, $p_separate_account / 100, 2); //bcmul:2个任意精度数字乘法计算
  582. $c_amount = bcmul($price, $c_separate_account / 100, 2); //bcmul:2个任意精度数字乘法计算
  583. $p_separate_account_amount = floor($p_amount * 100); // 发起人分账金额,以分为单位
  584. $c_separate_account_amount = floor($c_amount * 100); // 参与者分账金额,以分为单位
  585. $p_user_info = WechatUser::get($parent['uid']);
  586. $receivers[] = array(
  587. 'type' => 'PERSONAL_OPENID',
  588. 'account' => $p_user_info['routine_openid'],
  589. 'amount' => $p_separate_account_amount,
  590. 'description' => '您发起的助力人数已满,给您发了一笔现金奖励' . ($p_amount) . '元'
  591. );
  592. // 记录发起人分账开始的信息
  593. $p_separate_account_record = new StoreSeparateAccountRecord;
  594. $p_separate_account_record['product_id'] = $assistance['product_id'];
  595. $p_separate_account_record['active_product_id'] = $assistance['id'];
  596. $p_separate_account_record['type'] = 4;
  597. $p_separate_account_record['money'] = $p_amount;
  598. $p_separate_account_record['active_id'] = $parent['id'];
  599. $p_separate_account_record['order_id'] = $order_id; // OK
  600. $p_separate_account_record['uid'] = $parent['uid'];
  601. $p_separate_account_record['state'] = 0; // 默认为失败
  602. $p_separate_account_record['notice'] = "分账失败";
  603. $p_separate_account_record['mer_id'] = $parent['mer_id'];
  604. $p_separate_account_record['start_time'] = time();
  605. $p_separate_account_record['end_time'] = 0;
  606. $p_separate_account_record->save();
  607. // 添加发起人为分账接受方
  608. $res1 = MiniProgramService::addReceiver($p_user_info['routine_openid'], $mer_id); // 添加发起人为分账接受方
  609. // 处理参与者(添加分账人及加入分账列表)
  610. foreach ($childs as $key => $value) {
  611. $uid = $value['uid'];
  612. $c_user_info = WechatUser::get($uid);
  613. $res2 = MiniProgramService::addReceiver($c_user_info['routine_openid'], $mer_id); // 添加分账接收方
  614. // 记录参与者分账开始的信息
  615. $c_separate_account_record = new StoreSeparateAccountRecord;
  616. $c_separate_account_record['product_id'] = $assistance['product_id'];
  617. $c_separate_account_record['active_product_id'] = $assistance['id'];
  618. $c_separate_account_record['type'] = 4;
  619. $c_separate_account_record['money'] = $c_amount;
  620. $c_separate_account_record['active_id'] = $value['id'];
  621. $c_separate_account_record['order_id'] = $order_id;
  622. $c_separate_account_record['uid'] = $value['uid'];
  623. $c_separate_account_record['status'] = 0; // 默认为失败
  624. $c_separate_account_record['notice'] = "分账失败";
  625. $c_separate_account_record['mer_id'] = $value['mer_id'];
  626. $c_separate_account_record['start_time'] = time();
  627. $c_separate_account_record['end_time'] = 0;
  628. $c_separate_account_record->save();
  629. $receivers[] = array(
  630. 'type' => 'PERSONAL_OPENID',
  631. 'account' => $c_user_info['routine_openid'],
  632. 'amount' => $c_separate_account_amount,
  633. 'description' => '您参与的助力人数已满,给您发了一笔现金奖励' . ($c_amount) . '元'
  634. );
  635. }
  636. if($transaction_id){
  637. $res = MiniProgramService::multi_profit_sharing($transaction_id, $out_order_no, $receivers, $mer_id);
  638. $res = json_decode($res, true);
  639. $separateAccount = StoreSeparateAccountRecord::where('order_id', $order_id)->select();
  640. if ($res['return_code'] == 'SUCCESS') {
  641. foreach ($separateAccount as $key => $value) {
  642. $value['state'] = 1;
  643. $value['notice'] = "分账成功";
  644. $value['end_time'] = time();
  645. $value->save();
  646. }
  647. MiniProgramService::profit_sharing_finish($order['transaction_id'], $order['order_id'], $order['mer_id']);
  648. }
  649. }
  650. }
  651. /**
  652. * 记录本次助力分账奖励
  653. */
  654. public static function recordAssistanceMoney($order_id)
  655. {
  656. $assistance_active = self::where('order_id_key', $order_id)->find();
  657. $assistance = StoreAssistance::get($assistance_active['cid']);
  658. $order = StoreOrder::get($order_id);
  659. $price = $order['pay_price'];
  660. $assistance->assistance_money += bcmul($price, $assistance['p_separate_account'] / 100, 2) + $assistance['people'] * bcmul($price, $assistance['c_separate_account'] / 100, 2);
  661. $assistance->save();
  662. }
  663. }