123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872 |
- <?php
- namespace app\models\store;
- use app\models\routine\RoutineFormId;
- use app\models\routine\RoutineTemplate;
- use app\models\user\User;
- use app\models\user\WechatUser;
- use crmeb\basic\BaseModel;
- use crmeb\services\WechatTemplateService;
- use crmeb\traits\ModelTrait;
- use think\facade\Log;
- use think\facade\Route;
- class StorePink extends BaseModel
- {
-
- protected $pk = 'id';
-
- protected $name = 'store_pink';
- use ModelTrait;
-
- public static function getPinkOkList($uid)
- {
- $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');
- $msg = [];
- foreach ($list as &$item) {
- $msg[] = $item .= '拼团成功';
- }
- return $msg;
- }
-
- public static function getPinkOkSumTotalNum($id)
- {
- return self::where('status', 2)->where('is_refund', 0)->sum('total_num');
- }
-
- public static function getPinkUserOne($id)
- {
- $model = new self();
- $model = $model->alias('p');
- $model = $model->field('p.*,u.nickname,u.avatar');
- $model = $model->where('id', $id);
- $model = $model->join('user u', 'u.uid = p.uid');
- return $model->find();
- }
-
- public static function getPinkMember($id)
- {
- $model = new self();
- $model = $model->alias('p');
- $model = $model->field('p.*,u.nickname,u.avatar');
- $model = $model->where('k_id', $id);
- $model = $model->where('is_refund', 0);
- $model = $model->join('user u', 'u.uid = p.uid');
- $model = $model->order('id asc');
- return $model->select();
- }
-
- public static function setPinkStopTime($idAll)
- {
- $model = new self();
- $model = $model->where('id', 'IN', $idAll);
- return $model->update(['stop_time' => time(), 'status' => 2]);
- }
-
- public static function getPinkAll($cid, $isAll = false)
- {
- $model = new self();
- $model = $model->alias('p');
- $model = $model->field('p.id,p.uid,p.people,p.price,p.stop_time,u.nickname,u.avatar');
- $model = $model->where('stop_time', '>', time());
- $model = $model->where('p.cid', $cid);
- $model = $model->where('p.k_id', 0);
- $model = $model->where('p.is_refund', 0);
- $model = $model->order('p.add_time desc');
- $model = $model->join('user u', 'u.uid = p.uid');
- $list = $model->select();
- $list = count($list) ? $list->toArray() : [];
- if ($isAll) {
- $pindAll = [];
- foreach ($list as &$v) {
- $v['count'] = self::getPinkPeople($v['id'], $v['people']);
- $v['h'] = date('H', $v['stop_time']);
- $v['i'] = date('i', $v['stop_time']);
- $v['s'] = date('s', $v['stop_time']);
- $pindAll[] = $v['id'];
- $v['stop_time'] = (int)$v['stop_time'];
- }
- return [$list, $pindAll];
- }
- return $list;
- }
-
- public static function getPinkPeople($kid, $people)
- {
- $model = new self();
- $model = $model->where('k_id', $kid)->where('is_refund', 0);
- $count = bcadd($model->count(), 1, 0);
- return bcsub($people, $count, 0);
- }
-
- public static function getOrderIdAndPink($orderId, $kid)
- {
- $model = new self();
- $pink = $model->where('k_id|id', $kid)->column('order_id');
- if (in_array($orderId, $pink)) return true;
- else return false;
- }
-
- public static function getIsPinkUid($id = 0, $uid = 0)
- {
- $pink = self::where('k_id|id', $id)->where('uid', $uid)->where('is_refund', 0)->count();
- if ($pink) return true;
- else return false;
- }
-
- public static function isTpl($uidAll, $pid)
- {
- if (is_array($uidAll))
- $count = self::where('uid', 'IN', implode(',', $uidAll))->where('is_tpl', 0)->where('id|k_id', $pid)->count();
- else
- $count = self::where('uid', $uidAll)->where('is_tpl', 0)->where('k_id|id', $pid)->count();
- return $count;
- }
-
- public static function orderPinkAfter($uidAll, $pid)
- {
- $pinkInfo = 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_combination t', 'p.cid = t.id')->find();
- if (!$pinkInfo) return false;
- foreach ($uidAll as $key => &$item) {
- $openid = WechatUser::uidToOpenid($item, 'openid');
- $routineOpenid = WechatUser::uidToOpenid($item, 'routine_openid');
- if ($openid) {
- $firstWeChat = '亲,您的拼团已经完成了';
- $keyword1WeChat = self::where('id|k_id', $pid)->where('uid', $item)->value('order_id');
- $keyword2WeChat = self::alias('p')->where('p.id|p.k_id', $pid)->where('p.uid', $item)->join('store_combination c', 'c.id=p.cid')->value('c.title');
- $remarkWeChat = '点击查看订单详情';
- $urlWeChat = Route::buildUrl('order/detail/' . $keyword1WeChat)->suffix('')->domain(true)->build();
- WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_SUCCESS, [
- 'first' => $firstWeChat,
- 'keyword1' => $keyword1WeChat,
- 'keyword2' => $keyword2WeChat,
- 'remark' => $remarkWeChat
- ], $urlWeChat);
- } else if ($routineOpenid) {
- RoutineTemplate::sendPinkSuccess($item, $pinkInfo['title'], $pinkInfo['nickname'] ?? '', $pinkInfo['add_time'], $pinkInfo['people'], '/pages/order_details/index?order_id=' . $pinkInfo['order_id']);
- }
- }
- self::beginTrans();
- $res1 = self::where('uid', 'IN', implode(',', $uidAll))->where('id|k_id', $pid)->update(['is_tpl' => 1]);
- self::checkTrans($res1);
- }
-
- public static function orderPinkAfterNo($uid, $pid, $formId = '', $fillTilt = '', $isRemove = false)
- {
- $store = self::alias('p')->where('p.id|p.k_id', $pid)->field('c.*')->where('p.uid', $uid)->join('store_combination c', 'c.id=p.cid')->find();
- $pink = self::where('id|k_id', $pid)->where('uid', $uid)->find();
- $openid = WechatUser::uidToOpenid($uid, 'openid');
- $routineOpenid = WechatUser::uidToOpenid($uid, 'routine_openid');
- if ($isRemove) {
- if ($openid) {
- $urlWeChat = Route::buildUrl('order/detail/' . $pink->order_id)->suffix('')->domain(true)->build();
- WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_LOSE, [
- 'first' => '亲,您的拼团取消',
- 'keyword1' => $store->title,
- 'keyword2' => $pink->price,
- 'keyword3' => $pink->price,
- 'remark' => '点击查看订单详情'
- ], $urlWeChat);
- } else if ($routineOpenid) {
- RoutineTemplate::sendPinkFail($uid, $store->title, $pink->people, '亲,您的拼团取消,点击查看订单详情', '/pages/order_details/index?order_id=' . $pink->order_id);
- }
- } else {
- if ($openid) {
- $urlWeChat = Route::buildUrl('order/detail/' . $pink->order_id)->suffix('')->domain(true)->build();
- WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_LOSE, [
- 'first' => '亲,您的拼团失败',
- 'keyword1' => $store->title,
- 'keyword2' => $pink->price,
- 'keyword3' => $pink->price,
- 'remark' => '点击查看订单详情'
- ], $urlWeChat);
- } else if ($routineOpenid) {
- RoutineTemplate::sendPinkFail(
- $uid,
- $store->title,
- $pink->people,
- '亲,您拼团失败,自动为您申请退款,退款金额为:' . $pink->price,
- '/pages/order_details/index?order_id=' . $pink->order_id
- );
- }
- }
- self::where('id', $pid)->update(['status' => 3, 'stop_time' => time()]);
- self::where('k_id', $pid)->update(['status' => 3, 'stop_time' => time()]);
- }
-
- public static function getCurrentPink($id, $uid)
- {
- $pink = self::where('id', $id)->where('uid', $uid)->find();
- if (!$pink) $pink = self::where('k_id', $id)->where('uid', $uid)->find();
- return StoreOrder::where('id', $pink['order_id_key'])->value('order_id');
- }
- public static function systemPage($where)
- {
- $model = new self;
- $model = $model->alias('p');
- $model = $model->field('p.*,c.title');
- if ($where['data'] !== '') {
- list($startTime, $endTime) = explode(' - ', $where['data']);
- $model = $model->where('p.add_time', '>', strtotime($startTime));
- $model = $model->where('p.add_time', '<', strtotime($endTime));
- }
- if ($where['status']) $model = $model->where('p.status', $where['status']);
- $model = $model->where('p.k_id', 0);
- $model = $model->order('p.id desc');
- $model = $model->join('StoreCombination c', 'c.id=p.cid');
- return self::page($model, function ($item) use ($where) {
- $item['count_people'] = bcadd(self::where('k_id', $item['id'])->count(), 1, 0);
- }, $where);
- }
- public static function isPinkBe($data, $id)
- {
- $data['id'] = $id;
- $count = self::where($data)->count();
- if ($count) return $count;
- $data['k_id'] = $id;
- $count = self::where($data)->count();
- if ($count) return $count;
- else return 0;
- }
- public static function isPinkStatus($pinkId)
- {
- if (!$pinkId) return false;
- $stopTime = self::where('id', $pinkId)->value('stop_time');
- if ($stopTime < time()) return true;
- else return false;
- }
-
- public static function isSetPinkOver($pinkId)
- {
- $people = self::where('id', $pinkId)->value('people');
- $stopTime = self::where('id', $pinkId)->value('stop_time');
- if ($stopTime < time()) {
- $countNum = self::getPinkPeople($pinkId, $people);
- if ($countNum) return false;
- else return true;
- } else return true;
- }
-
- public static function setRefundPink($oid)
- {
- $res = true;
- $order = StoreOrder::where('id', $oid)->find();
- if ($order['pink_id']) $id = $order['pink_id'];
- else return $res;
- $count = self::where('id', $id)->where('uid', $order['uid'])->find();
- $countY = self::where('k_id', $id)->where('uid', $order['uid'])->find();
- if (!$count && !$countY) return $res;
- if ($count) {
-
- $kCount = self::where('k_id', $id)->order('add_time asc')->find();
- if ($kCount) {
- $res11 = self::where('k_id', $id)->update(['k_id' => $kCount['id']]);
- $res12 = self::where('id', $kCount['id'])->update(['stop_time' => $count['add_time'] + 86400, 'k_id' => 0]);
- $res1 = $res11 && $res12;
- $res2 = self::where('id', $id)->update(['stop_time' => time() - 1, 'k_id' => 0, 'is_refund' => $kCount['id'], 'status' => 3]);
- } else {
- $res1 = true;
- $res2 = self::where('id', $id)->update(['stop_time' => time() - 1, 'k_id' => 0, 'is_refund' => $id, 'status' => 3]);
- }
-
- $res = $res1 && $res2;
- } else if ($countY) {
- $res = self::where('id', $countY['id'])->update(['stop_time' => time() - 1, 'k_id' => 0, 'is_refund' => $id, 'status' => 3]);
- }
- return $res;
- }
-
- public static function setPinkStatus($pinkIds)
- {
- $orderPink = self::where('id', 'IN', $pinkIds)->where('is_refund', 1)->count();
- if (!$orderPink) return true;
- else return false;
- }
-
- public static function createPink($order)
- {
- $order = StoreOrder::tidyOrder($order, true)->toArray();
- $openid = WechatUser::uidToOpenid($order['uid'], 'openid');
- $routineOpenid = WechatUser::uidToOpenid($order['uid'], 'routine_openid');
- $product = StoreCombination::where('id', $order['combination_id'])->field('effective_time,title')->find();
- if ($product) {
- if ($order['pink_id']) {
- $res = false;
- $pink['uid'] = $order['uid'];
- if (self::isPinkBe($pink, $order['pink_id'])) return false;
- $pink['order_id'] = $order['order_id'];
- $pink['order_id_key'] = $order['id'];
- $pink['total_num'] = $order['total_num'];
- $pink['total_price'] = $order['pay_price'];
- $pink['k_id'] = $order['pink_id'];
- foreach ($order['cartInfo'] as $v) {
- $pink['cid'] = $v['combination_id'];
- $pink['pid'] = $v['product_id'];
- $pink['people'] = StoreCombination::where('id', $v['combination_id'])->value('people');
- $pink['price'] = $v['productInfo']['price'];
- $pink['stop_time'] = 0;
- $pink['add_time'] = time();
- $res = self::create($pink)->toArray();
- }
- if ($openid) {
- $urlWeChat = Route::buildUrl('order/detail/' . $order['order_id'])->suffix('')->domain(true)->build();
- WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_SUCCESS, [
- 'first' => '亲,您已成功参与拼团',
- 'keyword1' => $order['order_id'],
- 'keyword2' => $product->title,
- 'remark' => '点击查看订单详情'
- ], $urlWeChat);
- } else if ($routineOpenid) {
- $nickname = User::where('uid', self::where('id', $pink['k_id'])->value('uid'))->value('nickname');
- RoutineTemplate::sendPinkSuccess(
- $order['uid'], $product->title,
- $nickname,
- $pink['add_time'],
- $pink['people'],
- '/pages/order_details/index?order_id=' . $pink['order_id']
- );
- }
-
- list($pinkAll, $pinkT, $count, $idAll, $uidAll) = self::getPinkMemberAndPinkK($pink);
- if ($pinkT['status'] == 1) {
- if (!$count)
- self::PinkComplete($uidAll, $idAll, $pink['uid'], $pinkT);
- else
- self::PinkFail($pinkAll, $pinkT, 0);
- }
- if ($res) return true;
- else return false;
- } else {
- $res = false;
- $pink['uid'] = $order['uid'];
- $pink['order_id'] = $order['order_id'];
- $pink['order_id_key'] = $order['id'];
- $pink['total_num'] = $order['total_num'];
- $pink['total_price'] = $order['pay_price'];
- $pink['k_id'] = 0;
- foreach ($order['cartInfo'] as $v) {
- $pink['cid'] = $v['combination_id'];
- $pink['pid'] = $v['product_id'];
- $pink['people'] = StoreCombination::where('id', $v['combination_id'])->value('people');
- $pink['price'] = $v['productInfo']['price'];
- $pink['stop_time'] = bcadd(time(), bcmul($product->effective_time, 3600, 0), 0);
- $pink['add_time'] = time();
- $res1 = self::create($pink)->toArray();
- $res2 = StoreOrder::where('id', $order['id'])->update(['pink_id' => $res1['id']]);
- $res = $res1 && $res2;
- }
-
- if ($openid && $order['is_channel'] != 1) {
- $urlWeChat = Route::buildUrl('/order/detail/' . $pink['order_id'])->suffix('')->domain(true)->build();
- WechatTemplateService::sendTemplate($openid, WechatTemplateService::OPEN_PINK_SUCCESS, [
- 'first' => '您好,您已成功开团!赶紧与小伙伴们分享吧!!!',
- 'keyword1' => $product->title,
- 'keyword2' => $pink['total_price'],
- 'keyword3' => $pink['people'],
- 'remark' => '点击查看订单详情'
- ], $urlWeChat);
- } else if ($routineOpenid && $order['is_channel'] == 1) {
- $nickname = User::where('uid', $order['uid'])->value('nickname');
- RoutineTemplate::sendPinkSuccess(
- $order['uid'], $product->title,
- $nickname,
- $pink['add_time'],
- $pink['people'],
- '/pages/order_details/index?order_id=' . $pink['order_id']
- );
- }
- if ($res) return true;
- else return false;
- }
- } else {
- Log::error('拼团支付成功读取产品数据失败订单号:' . $order['order_id']);
- }
- }
-
- public static function getPinkSecondOne()
- {
- $addTime = mt_rand(time() - 30000, time());
- return self::where('p.add_time', '>', $addTime)->alias('p')->where('p.status', 1)->join('User u', 'u.uid=p.uid')->field('u.nickname,u.avatar as src')->find();
- }
-
-
- public static function PinkComplete($uidAll, $idAll, $uid, $pinkT)
- {
- $pinkBool = 6;
- try {
- if (self::setPinkStatus($idAll)) {
- self::setPinkStopTime($idAll);
- if (in_array($uid, $uidAll)) {
- if (self::isTpl($uidAll, $pinkT['id'])) self::orderPinkAfter($uidAll, $pinkT['id']);
- $pinkBool = 1;
- } else $pinkBool = 3;
- }
- return $pinkBool;
- } catch (\Exception $e) {
- self::setErrorInfo($e->getMessage());
- return $pinkBool;
- }
- }
-
- public static function PinkFail($pinkAll, $pinkT, $pinkBool, $isRunErr = true, $isIds = false)
- {
- self::startTrans();
- $pinkIds = [];
- try {
- if ($pinkT['stop_time'] < time()) {
- $pinkBool = -1;
- array_push($pinkAll, $pinkT);
- foreach ($pinkAll as $v) {
- if (StoreOrder::orderApplyRefund(StoreOrder::getPinkOrderId($v['order_id_key']), $v['uid'], '拼团时间超时') && self::isTpl($v['uid'], $pinkT['id'])) {
- if ($isIds) array_push($pinkIds, $v['id']);
- self::orderPinkAfterNo($pinkT['uid'], $pinkT['id']);
- } else {
- if ($isRunErr) return $pinkBool;
- }
- }
- }
- self::commit();
- if ($isIds) return $pinkIds;
- return $pinkBool;
- } catch (\Exception $e) {
- self::rollback();
- return $pinkBool;
- }
- }
-
- public static function getPinkMemberAndPinkK($pink)
- {
-
- if ($pink['k_id']) {
- $pinkAll = self::getPinkMember($pink['k_id']);
- $pinkT = self::getPinkUserOne($pink['k_id']);
- } else {
- $pinkAll = self::getPinkMember($pink['id']);
- $pinkT = $pink;
- }
- $pinkT = $pinkT->hidden(['order_id', 'total_price', 'cid', 'pid', 'add_time', 'k_id', 'is_tpl', 'is_refund'])->toArray();
- $pinkAll = $pinkAll->hidden(['total_price', 'cid', 'pid', 'add_time', 'k_id', 'is_tpl', 'is_refund'])->toArray();
- $count = (int)bcadd(count($pinkAll), 1, 0);
- $count = (int)bcsub($pinkT['people'], $count, 0);
- $idAll = [];
- $uidAll = [];
-
- foreach ($pinkAll as $k => $v) {
- $idAll[$k] = $v['id'];
- $uidAll[$k] = $v['uid'];
- }
- $idAll[] = $pinkT['id'];
- $uidAll[] = $pinkT['uid'];
- return [$pinkAll, $pinkT, $count, $idAll, $uidAll];
- }
-
- public static function removePink($uid, $cid, $pink_id, $nextPinkT = null)
- {
- $pinkT = self::where('uid', $uid)
- ->where('id', $pink_id)
- ->where('cid', $cid)
- ->where('k_id', 0)
- ->where('is_refund', 0)
- ->where('status', 1)
- ->where('stop_time', '>', time())
- ->find();
- if (!$pinkT) return self::setErrorInfo('未查到拼团信息,无法取消');
- self::startTrans();
- try {
- list($pinkAll, $pinkT, $count, $idAll, $uidAll) = self::getPinkMemberAndPinkK($pinkT);
- if (count($pinkAll)) {
- if (self::getPinkPeople($pink_id, $pinkT['people'])) {
-
- if (isset($pinkAll[0])) $nextPinkT = $pinkAll[0];
- } else {
-
- self::PinkComplete($uidAll, $idAll, $uid, $pinkT);
- return self::setErrorInfo(['status' => 200, 'msg' => '拼团已完成,无法取消']);
- }
- }
-
- if (StoreOrder::orderApplyRefund(StoreOrder::getPinkOrderId($pinkT['order_id_key']), $pinkT['uid'], '拼团取消开团') && self::isTpl($pinkT['uid'], $pinkT['id'])) {
- $formId = RoutineFormId::getFormIdOne($uid);
- if ($formId) RoutineFormId::delFormIdOne($formId);
- self::orderPinkAfterNo($pinkT['uid'], $pinkT['id'], $formId, '拼团取消开团', true);
- } else
- return self::setErrorInfo(['status' => 200, 'msg' => StoreOrder::getErrorInfo()], true);
-
- if (is_array($nextPinkT)) {
- self::where('id', $nextPinkT['id'])->update(['k_id' => 0, 'status' => 1, 'stop_time' => $pinkT['stop_time']]);
- self::where('k_id', $pinkT['id'])->update(['k_id' => $nextPinkT['id']]);
- StoreOrder::where('order_id', $nextPinkT['order_id'])->update(['pink_id' => $nextPinkT['id']]);
- }
- self::commitTrans();
- return true;
- } catch (\Exception $e) {
- return self::setErrorInfo($e->getLine() . ':' . $e->getMessage() . ':' . $e->getFile(), true);
- }
- }
-
- public static function pinkListEnd()
- {
- $model = new self;
- $model = $model->field('id,people');
- $model = $model->where('stop_time', '<=', time());
- $model = $model->where('status', 1);
- $model = $model->where('k_id', 0);
- $model = $model->where('is_refund', 0);
- return $model->select();
- }
-
- public static function successPinkEdit(array $pinkRegimental)
- {
- if (!count($pinkRegimental)) return true;
- foreach ($pinkRegimental as $key => &$item) {
- $pinkList = self::where('k_id', $item)->column('id', 'id');
- $pinkList[] = $item;
- $pinkList = implode(',', $pinkList);
- self::setPinkStatus($pinkList);
- self::setPinkStopTime($pinkList);
- $pinkUidList = self::isTplPink($pinkList);
- if (count($pinkUidList)) self::sendPinkTemplateMessageSuccess($pinkUidList, $item);
- }
- return true;
- }
-
- public static function failPinkEdit(array $pinkRegimental)
- {
- if (!count($pinkRegimental)) return true;
- foreach ($pinkRegimental as $key => &$item) {
- $pinkList = self::where('k_id', $item)->column('id', 'id');
- $pinkList[] = $item;
- $pinkList = implode(',', $pinkList);
- self::refundPink($pinkList);
- self::pinkStopStatus($pinkList);
- $pinkUidList = self::isTplPink($pinkList);
- if (count($pinkUidList)) self::sendPinkTemplateMessageError($pinkUidList, $item);
- }
- return true;
- }
-
- public static function sendPinkTemplateMessageError(array $pinkUidList, $pink)
- {
- foreach ($pinkUidList as $key => &$item) {
- $openid = WechatUser::uidToOpenid($item, 'openid');
- $routineOpenid = WechatUser::uidToOpenid($item, 'routine_openid');
- $store = self::alias('p')->where('p.id|p.k_id', $pink)->field('c.*')->where('p.uid', $item)->join('store_combination c', 'c.id = p.cid')->find();
- $pink = self::where('id|k_id', $pink)->where('uid', $item)->find();
- if ($openid) {
-
- $urlWeChat = Route::buildUrl('order/detail/' . $pink->order_id)->suffix('')->domain(true)->build();
- WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_LOSE, [
- 'first' => '亲,您的拼团失败',
- 'keyword1' => $store->title,
- 'keyword2' => $pink->price,
- 'keyword3' => $pink->price,
- 'remark' => '点击查看订单详情'
- ], $urlWeChat);
- } else if ($routineOpenid) {
-
- RoutineTemplate::sendPinkFail(
- $item,
- $store->title,
- $pink->people,
- '亲,您拼团失败,自动为您申请退款,退款金额为:' . $pink->price,
- '/pages/order_details/index?order_id=' . $pink->order_id
- );
- }
- }
- self::where('uid', 'IN', implode(',', $pinkUidList))->where('id|k_id', $pink)->update(['is_tpl' => 1]);
- }
-
- public static function refundPink($pinkList)
- {
- $refundPinkList = self::where('id', 'IN', $pinkList)->column('order_id,uid', 'id');
- if (!count($refundPinkList)) return true;
- foreach ($refundPinkList as $key => &$item) {
- StoreOrder::orderApplyRefund($item['order_id'], $item['uid'], '拼团时间超时');
- }
- }
-
- public static function pinkStopStatus($pinkList)
- {
- return self::where('id', 'IN', $pinkList)->update(['status' => 3]);
- }
-
- public static function isTplPink($pinkList)
- {
- return self::where('id', 'IN', $pinkList)->where('is_tpl', 0)->column('uid', 'uid');
- }
-
- public static function sendPinkTemplateMessageSuccess(array $pinkUidList, $pink)
- {
- foreach ($pinkUidList as $key => &$item) {
- $openid = WechatUser::uidToOpenid($item, 'openid');
- $routineOpenid = WechatUser::uidToOpenid($item, 'routine_openid');
- $nickname = WechatUser::uidToOpenid(self::where('id', $pink)->value('uid'), 'nickname');
- if ($openid) {
-
- $firstWeChat = '亲,您的拼团已经完成了';
- $keyword1WeChat = self::where('id|k_id', $pink)->where('uid', $item)->value('order_id');
- $keyword2WeChat = self::alias('p')->where('p.id|p.k_id', $pink)->where('p.uid', $item)->join('store_combination c', 'c.id=p.cid')->value('c.title');
- $remarkWeChat = '点击查看订单详情';
- $urlWeChat = Route::buildUrl('order/detail/' . $keyword1WeChat)->suffix('')->domain(true)->build();
- WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_SUCCESS, [
- 'first' => $firstWeChat,
- 'keyword1' => $keyword1WeChat,
- 'keyword2' => $keyword2WeChat,
- 'remark' => $remarkWeChat
- ], $urlWeChat);
- } else if ($routineOpenid) {
-
- $pinkInfo = self::where('k.id|k.k_id', $pink)->alias('k')->where('k.uid', $item)
- ->field(['k.order_id', 'k.people', 'k.add_time', 'c.title'])
- ->join('store_combination c', 'c.id = k.cid')->find();
- RoutineTemplate::sendPinkSuccess(
- $item, $pinkInfo['title'] ?? '',
- $nickname,
- $pinkInfo['add_time'] ?? 0,
- $pinkInfo['people'] ?? 0,
- '/pages/order_details/index?order_id=' . $pinkInfo['order_id'] ?? ''
- );
- }
- }
- self::where('uid', 'IN', implode(',', $pinkUidList))->where('id|k_id', $pink)->update(['is_tpl' => 1]);
- }
-
- public static function statusPink()
- {
- $pinkListEnd = self::pinkListEnd();
- if (!$pinkListEnd) return true;
- $pinkListEnd = $pinkListEnd->toArray();
- $failPinkList = [];
- $successPinkList = [];
- foreach ($pinkListEnd as $key => &$value) {
- $countPeople = (int)bcadd(self::where('k_id', $value['id'])->count(), 1, 0);
- if ($countPeople == $value['people'])
- $successPinkList[] = $value['id'];
- else
- $failPinkList[] = $value['id'];
- }
- $success = self::successPinkEdit($successPinkList);
- $error = self::failPinkEdit($failPinkList);
- $res = $success && $error;
- if (!$res)
- throw new \Exception('拼团订单取消失败!');
- }
- }
|