| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\FreeQueue;
- use app\models\FreeQueueGoods;
- use app\models\Goods;
- use app\models\Order;
- use app\models\QueueAccountLog;
- use app\models\QueueLog;
- use app\models\User;
- use app\utils\OrderUtil;
- use yii\base\Model;
- class FreeQueueForm extends Model
- {
- public $store_id;
- public $id;
- public $ids;
- public $queue_id;
- public $name;
- public $mobile;
- public $order_no;
- public $activity_name;
- public $start_time;
- public $end_time;
- public $status;
- public $goods_ext;
- public $goods_ids;
- public $can_out;
- public $cashback_bili;
- public $single_bili;
- public $coupon_ids;
- public $goods_name;
- public $coupon_name;
- public $buy_limit;
- public $is_platform = 0;
- public $self_bili = 0;
- public $share_bili = 0;
- public $is_platform_audit = 0;
- public $store_name;
- public function rules()
- {
- return [
- [['status', 'id', 'is_platform', 'is_platform_audit', 'queue_id', 'mobile'], 'integer'],
- [['start_time', 'end_time', 'ids', 'name', 'store_name', 'activity_name', 'order_no'], 'string'],
- [['single_bili', 'cashback_bili', 'self_bili', 'share_bili'], 'number'],
- [['goods_name', 'coupon_name', 'store_id', 'goods_ids', 'goods_ext', 'coupon_ids', 'buy_limit', 'can_out'], 'safe'],
- ];
- }
- public function init()
- {
- parent::init();
- if (empty($this->store_id)) {
- $this->store_id = get_store_id();
- }
- }
- public function search()
- {
- try {
- $query = FreeQueue::find()->where(['is_delete' => 0, 'store_id' => get_store_id()]);
- if ((int)$this->status === 1) { //未开始
- $query->andWhere(['>', 'start_time', time()]);
- }
- if ((int)$this->status === 2) { //进行中
- $query->andWhere(['AND', ['<', 'start_time', time()], ['>', 'end_time', time()]]);
- }
- if ((int)$this->status === 3) { //已结束
- $query->andWhere(['<', 'end_time', time()]);
- }
- if (!empty($this->name)) { //名称
- $query->andWhere(['LIKE', 'name', $this->name]);
- }
- if (!empty($this->start_time)) {
- $query->andWhere(['>', 'end_time', strtotime($this->start_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<', 'start_time', strtotime($this->end_time)]);
- }
- $query->andWhere(['is_platform' => $this->is_platform])->orderBy('id desc');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['publish'] = $item['status'];
- //获取活动状态
- if ($item['start_time'] > time()) {
- $item['status'] = 1;
- }
- if ($item['start_time'] < time() && $item['end_time'] > time()) {
- $item['status'] = 2;
- }
- if ($item['end_time'] < time()) {
- $item['status'] = 3;
- }
- //格式化时间
- $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']);
- $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']);
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
- //参与人数
- $item['num'] = QueueLog::find()->where(['queue_id' => $item['id']])->andWhere(['!=', 'status', 2])->count() ?? 0;;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $pagination['list'],
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function searchActivity()
- {
- try {
- $query = FreeQueueGoods::find()
- ->alias('fg')
- ->leftJoin(['fq' => FreeQueue::tableName()], 'fq.id = fg.activity_id')
- ->leftJoin(['g' => Goods::tableName()], 'g.id = fg.goods_id')
- ->where(['fq.is_delete' => 0, 'fq.store_id' => get_store_id()]);
- if ((int)$this->status === 1) { //未开始
- $query->andWhere(['>', 'fq.start_time', time()]);
- }
- if ((int)$this->status === 2) { //进行中
- $query->andWhere(['AND', ['<', 'fq.start_time', time()], ['>', 'fq.end_time', time()]]);
- }
- if ((int)$this->status === 3) { //已结束
- $query->andWhere(['<', 'fq.end_time', time()]);
- }
- if (!empty($this->activity_name)) { //名称
- $query->andWhere(['LIKE', 'fq.name', $this->activity_name]);
- }
- if (!empty($this->name)) { //名称
- $query->andWhere(['LIKE', 'g.name', $this->name]);
- }
- if (!empty($this->start_time)) {
- $query->andWhere(['>', 'fq.end_time', strtotime($this->start_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<', 'fq.start_time', strtotime($this->end_time)]);
- }
- $query->andWhere(['fq.is_platform' => $this->is_platform]);
- $query->select('fg.*,g.name,fq.name as activity_name,g.cover_pic')->orderBy('fg.id DESC');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['publish'] = $item['status'];
- //获取活动状态
- if ($item['start_time'] > time()) {
- $item['status'] = 1;
- }
- if ($item['start_time'] < time() && $item['end_time'] > time()) {
- $item['status'] = 2;
- }
- if ($item['end_time'] < time()) {
- $item['status'] = 3;
- }
- //参与人数
- $item['num'] = QueueLog::find()->where(['store_id' => get_store_id(), 'goods_id' => $item['goods_id']])->andWhere(['!=', 'status', 2])->count() ?? 0;
- //格式化时间
- $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']);
- $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']);
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $pagination['list'],
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 活动详情
- * @return array
- */
- public function searchActivityDetail()
- {
- try {
- $query = QueueLog::find()
- ->alias('ql')
- ->leftJoin(['o' => Order::tableName()], 'ql.order_id = o.id')
- ->leftJoin(['fq' => FreeQueue::tableName()], 'fq.id = ql.queue_id')
- ->leftJoin(['fg' => FreeQueueGoods::tableName()], 'fg.activity_id = fq.id')
- ->leftJoin(['u' => User::tableName()], 'u.id = ql.user_id')
- ->where(['fq.is_delete' => 0, 'ql.store_id' => get_store_id()])
- ->andWhere(['!=', 'ql.status', 2]);
- if ($this->queue_id) {
- $query->andWhere(['fg.id' => $this->queue_id]);
- }
- if ((int)$this->status === 1) { //待返现
- $query->andWhere(['=', 'ql.status', 0]);
- }
- if ((int)$this->status === 2) { //进行中
- $query->andWhere(['=', 'ql.status', 3]);
- }
- if ((int)$this->status === 3) { //已完成
- $query->andWhere(['=', 'ql.status', 1]);
- }
- if (!empty($this->name)) { //用户名称
- $query->andWhere(['LIKE', 'u.nickname', $this->name]);
- }
- if (!empty($this->mobile)) { //手机号
- $query->andWhere(['LIKE', 'u.binding', $this->mobile]);
- }
- if (!empty($this->order_no)) { //订单号
- $query->andWhere(['LIKE', 'o.order_no', $this->order_no]);
- }
- $query->select('ql.*,o.order_no,u.nickname,u.binding,u.avatar_url');
- // print_r($query);die();
- $query->orderBy('ql.id desc');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['status'] = intval($item['status']);
- //未返金额
- $item['un_money'] = round(($item['total_money'] - $item['returned_money']), 2);
- //格式化时间
- $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']);
- $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']);
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
- $item['sort'] = OrderUtil::getQueueSort($item['queue_id'], $item['id'])['current_sort'];
-
-
- $from_queue_list_arr = (array)json_decode($item['order_detail_total_price_list'], true);
- foreach($from_queue_list_arr as &$que){
- $order = Order::findOne($que['order_id']);
- $que['order'] = $order;
- $que['user'] = User::findOne($order['user_id']);
- $que['saas_user'] = \app\models\SaasUser::findOne(['mobile' => $que['user']['binding']]);
- }
- $item['order_detail_total_price_list_arr'] = $from_queue_list_arr;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $pagination['list'],
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 参与活动记录
- * @return array
- */
- public function searchJoin()
- {
- try {
- $query = QueueLog::find()
- ->alias('ql')
- ->leftJoin(['o' => Order::tableName()], 'ql.order_id = o.id')
- ->leftJoin(['fq' => FreeQueue::tableName()], 'fq.id = ql.queue_id')
- ->leftJoin(['fg' => FreeQueueGoods::tableName()], 'fg.activity_id = fq.id')
- ->leftJoin(['u' => User::tableName()], 'u.id = ql.user_id')
- ->where(['fq.is_delete' => 0, 'ql.store_id' => get_store_id()])
- ->andWhere(['!=', 'ql.status', 2]);
- if ($this->queue_id) {
- $query->andWhere(['fg.id' => $this->queue_id]);
- }
- if ((int)$this->status === 1) { //待返现
- $query->andWhere(['=', 'ql.status', 0]);
- }
- if ((int)$this->status === 2) { //进行中
- $query->andWhere(['=', 'ql.status', 3]);
- }
- if ((int)$this->status === 3) { //已完成
- $query->andWhere(['=', 'ql.status', 1]);
- }
- if (!empty($this->name)) { //用户名称
- $query->andWhere(['LIKE', 'u.nickname', $this->name]);
- }
- if (!empty($this->mobile)) { //手机号
- $query->andWhere(['LIKE', 'u.binding', $this->mobile]);
- }
- if (!empty($this->order_no)) { //订单号
- $query->andWhere(['LIKE', 'o.order_no', $this->order_no]);
- }
- if (!empty($this->goods_name)) { //商品名称
- $query->andWhere(['LIKE', 'ql.goods_name', $this->goods_name]);
- }
- $query->select('ql.*,o.order_no,u.nickname,u.binding,u.avatar_url');
- // print_r($query);die();
- $query->orderBy('ql.id desc')->groupBy('ql.id');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['status'] = intval($item['status']);
- //未返金额
- $item['un_money'] = round(($item['total_money'] - $item['returned_money']), 2);
- //格式化时间
- $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']);
- $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']);
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
- $item['sort'] = OrderUtil::getQueueSort($item['queue_id'], $item['id'])['current_sort'];
-
-
- $from_queue_list_arr = (array)json_decode($item['order_detail_total_price_list'], true);
- foreach($from_queue_list_arr as &$que){
- $order = Order::findOne($que['order_id']);
- $que['order'] = $order;
- $que['user'] = User::findOne($order['user_id']);
- $que['saas_user'] = \app\models\SaasUser::findOne(['mobile' => $que['user']['binding']]);
- }
- $item['order_detail_total_price_list_arr'] = $from_queue_list_arr;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $pagination['list'],
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 参与记录详情
- * @return array
- */
- public function searchJoinDetail()
- {
- if ($this->id) {
- // 参与记录详情
- $queueDetail = QueueLog::find()
- ->alias('ql')
- ->leftJoin(['u' => User::tableName()], 'u.id = ql.user_id')
- ->where(['ql.id' => $this->id, 'ql.store_id' => $this->store_id])
- ->andWhere(['!=', 'ql.status', 2])
- ->select('ql.*,u.binding')
- ->asArray()
- ->one();
- $queueDetail['created_at'] = date('Y-m-d H:i:s', $queueDetail['created_at']);
- $queueDetail['un_money'] = round(($queueDetail['total_money'] - $queueDetail['returned_money']), 2);
- $queueDetail['sort'] = OrderUtil::getQueueSort($queueDetail['queue_id'], $queueDetail['id'])['current_sort'];
-
-
- $from_queue_list_arr = (array)json_decode($queueDetail['order_detail_total_price_list'], true);
- foreach($from_queue_list_arr as &$que){
- $order = Order::findOne($que['order_id']);
- $que['order'] = $order;
- $que['user'] = User::findOne($order['user_id']);
- $que['saas_user'] = \app\models\SaasUser::findOne(['mobile' => $que['user']['binding']]);
- }
- $queueDetail['order_detail_total_price_list_arr'] = $from_queue_list_arr;
- $goodsList = Goods::find()
- ->where(['in', 'id', [$queueDetail['goods_id']], 'store_id' => $this->store_id, 'is_delete' => 0])
- ->asArray()
- ->all();
- $goodsList[0]['num'] = $queueDetail['num'];
- $query = QueueAccountLog::find()->alias('qal')
- ->leftJoin(['ql' => QueueLog::tableName()], 'qal.from_queue_id = ql.id')
- ->leftJoin(['o' => Order::tableName()], 'o.id = ql.order_id')
- ->leftJoin(['u' => User::tableName()], 'u.id = ql.user_id')
- ->where(['qal.queue_id' => $this->id, 'qal.log_type' => 1]);
- $query->select(['qal.*', 'u.binding', 'u.avatar_url', 'u.nickname', 'o.order_no']);
- $list = pagination_make($query);
- foreach($list['list'] as &$item){
- $from_queue_list_arr = (array)json_decode($item['from_queue_list'], true);
- foreach($from_queue_list_arr as $i => &$que){
- if($i > 0 && $que['order_id'] == $from_queue_list_arr[$i-1]['order_id']){
- $que['money'] += $from_queue_list_arr[$i-1]['money'];
- unset($from_queue_list_arr[$i-1]);
- }
- }
- $from_queue_list_arr = array_values($from_queue_list_arr);
- foreach($from_queue_list_arr as &$que){
- $queue_log = QueueLog::findOne($que['from_queue_log_id']);
- $order = Order::findOne($queue_log['order_id']);
- $que['queue_log'] = $queue_log;
- $que['order'] = $order;
- $que['user'] = User::findOne($order['user_id']);
- $que['saas_user'] = \app\models\SaasUser::findOne(['mobile' => $que['user']['binding']]);
- }
- $item['from_queue_list_arr'] = $from_queue_list_arr;
- }
- $data = [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- 'queueDetail' => $queueDetail,
- 'goodsList' => $goodsList,
- ];
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '参数错误',
- ];
- }
- }
- public function getInfo()
- {
- try {
- $activity = FreeQueue::find()->where(['id' => $this->id])->one();
- if ($activity) {
- $activity_goods = Goods::find()->where(['in', 'id', explode(',', $activity->goods_ids)])->andWhere(['is_delete' => 0])->asArray()->all();
- $activity['start_time'] = date("Y-m-d H:i:s", $activity['start_time']);
- $activity['end_time'] = date("Y-m-d H:i:s", $activity['end_time']);
- $goods_ext = FreeQueueGoods::findAll(['activity_id' => $activity->id, 'is_delete' => 0]);
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'activity_goods' => $activity_goods ?? [],
- 'goods_ext' => $goods_ext ?? [],
- 'activity_coupons' => $activity_coupons ?? [],
- 'activity' => $activity ?: []
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
- ];
- }
- }
- public function save($params = [])
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!$this->name || !$this->start_time || !$this->end_time) {
- throw new \Exception("请将参数填充完整");
- }
- $activity = FreeQueue::findOne($this->id);
- if (empty($activity)) {
- $activity = new FreeQueue();
- $activity->store_id = $this->store_id;
- }
- $activity->name = $this->name;
- $activity->cashback_bili = $this->cashback_bili;
- $activity->single_bili = $this->single_bili;
- $activity->self_bili = $this->self_bili;
- $activity->share_bili = $this->share_bili;
- $activity->start_time = strtotime($this->start_time);
- $activity->end_time = strtotime($this->end_time);
- $activity->goods_ids = $this->goods_ids;
- $activity->can_out = $this->can_out;
- isset($params['cashback_time']) && $activity->cashback_time = (int)$params['cashback_time'];
- isset($params['cashback_type']) && $activity->cashback_type = (int)$params['cashback_type'];
- isset($params['cashback_money']) && $activity->cashback_money = (float)$params['cashback_money'];
- isset($params['cashback_money_bili']) && $activity->cashback_money_bili = (float)$params['cashback_money_bili'];
- $goods_ids = \explode(',', $this->goods_ids);
- foreach ($goods_ids as $goods_id) {
- $flag = FreeQueueGoods::find()->where(['goods_id' => $goods_id]);
- if ($this->id) {
- $flag->andWhere(['<>', 'activity_id', $this->id]);
- }
- if ($flag->one()) {
- throw new \Exception('该商品已参与其他活动');
- }
- }
- $activity->is_platform = (int)$this->is_platform;
- if (!$activity->save()) {
- throw new \Exception(json_encode($activity->errors));
- }
- if (!empty($this->goods_ext)) {
- foreach ($this->goods_ext as &$item) {
- $item['activity_id'] = $activity->id;
- $item['store_id'] = $this->store_id;
- }
- FreeQueueGoods::saveAll($this->goods_ext, $activity->id);
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function setStatus()
- {
- try {
- if ($this->ids) {
- $ids = explode(',', $this->ids);
- if (in_array($this->status, [0, 1])) {
- FreeQueue::updateAll(['status' => $this->status], ['and', ['in', 'id', $ids], ['store_id' => get_store_id()]]);
- }
- if ((int)$this->status === 2) {
- foreach ($ids as $id) {
- $is = QueueLog::find()->where('`queue_id` = :queue_id AND `is_meet` = 0 AND `status` != 2', [':queue_id' => $id])->one();
- if ($is) {
- return [
- 'code' => 0,
- 'msg' => '当前活动还有未完成的队列,暂时不能删除'
- ];
- }
- }
- FreeQueue::updateAll(['is_delete' => 1], ['in', 'id', $ids]);
- FreeQueueGoods::deleteAll(['in', 'activity_id', $ids]);
- }
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|