| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Goods;
- use app\models\OrderComment;
- use app\models\OrderDetail;
- use app\models\SaasUser;
- use app\models\User;
- use yii\base\Model;
- class OrderCommentForm extends Model
- {
- public $store_id;
- public $id;
- public $status;
- public $reply_content;
- public $pic_list;
- public $mch;
- public $mch_id;
- public $goods_id;
- public $score;
- public $content;
- public $is_hide;
- public $is_virtual;
- public $virtual_user;
- public $virtual_avatar;
- public $created_at;
- public $user_type;
- public $user_name;
- public $user_avatar;
- public $saas_user_id;
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['goods_id', 'is_hide', 'is_virtual', 'mch_id', 'score','saas_user_id', 'user_type'], 'integer'],
- [['content','virtual_avatar', 'user_name', 'user_avatar'], 'string', 'max' => 1000],
- [['virtual_user'], 'string', 'max' => 255],
- [['created_at'], 'integer', 'max' => 2000000000],
- [['mch', 'mch_id'], 'safe'],
- ];
- }
- /**
- * 获取评论列表
- * @return array
- */
- public function getComment() {
- $query = OrderComment::find()->alias('oc')->where(['oc.store_id' => $this->store_id,
- 'oc.is_delete' => OrderComment::IS_DELETE_FALSE]);
- $query
- ->leftJoin(['u' => User::tableName()], 'oc.user_id=u.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->leftJoin(['od' => OrderDetail::tableName()], 'oc.order_detail_id=od.id')
- ->select('oc.created_at, oc.user_id as uid,oc.is_virtual,oc.virtual_user,oc.id,u.nickname,u.platform,u.avatar_url,
- oc.score,oc.content,oc.pic_list,od.goods_name,oc.is_hide,oc.reply_content, od.pic goods_pic, oc.goods_id, , oc.order_detail_id,
- oc.user_type, oc.user_name, oc.user_avatar
- ')
- ->orderBy('oc.created_at DESC')->groupBy('oc.id');
- if (isset($this->mch) && $this->mch == 1) {
- if ($this->mch_id > 0) {
- $query->andWhere(['oc.mch_id' => $this->mch_id]);
- }else{
- $query->andWhere(['>', 'oc.mch_id', 0]);
- }
- }else{
- $query->andWhere(['oc.mch_id' => 0]);
- }
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach ($list as $key => &$value) {
- if (!$value['order_detail_id']) {
- $goods = Goods::findOne($value['goods_id']);
- $value['goods_name'] = $goods->name;
- $value['goods_pic'] = $goods->cover_pic;
- }
- $value['user_type'] = intval($value['user_type']);
- if ($value['user_type'] === OrderComment::USER_TYPE_CUSTOM) {
- $value['nickname'] = $value['user_name'];
- $value['avatar_url'] = $value['user_avatar'];
- }
- if ($value['is_virtual'] == 1) {
- $list[$key]['nickname'] = '(' . $value['virtual_user'] . ')';
- }
- $value['score'] = intval($value['score']);
- $value['pic_list'] = json_decode($value['pic_list'], true);
- $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
- // TODO: 其他数据
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- ];
- }
- /**
- * 回复消息
- * @return array
- */
- public function reply() {
- $query = OrderComment::find()->where(['id' => $this->id])->one();
- if (!$query || empty($this->reply_content)) {
- return [
- 'code' => 1,
- 'msg' => '参数错误',
- ];
- }
- $query->reply_content = $this->reply_content;
- if ($query->save()) {
- return [
- 'code' => 0,
- 'msg' => '回复成功',
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '回复失败',
- ];
- }
- }
- /**
- * 隐藏评论
- * @return array
- */
- public function hideStatus()
- {
- $order_comment = OrderComment::findOne([
- 'store_id' => $this->store_id,
- 'id' => $this->id,
- ]);
- if ($order_comment) {
- $order_comment->is_hide = $this->status;
- $order_comment->save();
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功',
- ];
- }
- /**
- * 删除评论
- * @return array
- */
- public function deleteStatus()
- {
- $order_comment = OrderComment::findOne([
- 'store_id' => $this->store_id,
- 'id' => $this->id,
- ]);
- if ($order_comment) {
- $order_comment->is_delete = $this->status;
- $order_comment->save();
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功',
- ];
- }
- /**
- * 增加评论
- * @return array
- */
- public function save()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $orderComment = new OrderComment();
- $orderComment->store_id = $this->store_id;
- $orderComment->user_id = 0;
- $orderComment->order_id = 0;
- $orderComment->order_detail_id = 0;
- $orderComment->user_id = 0;
- $orderComment->is_delete = 0;
- $orderComment->created_at = $this->created_at;
- $orderComment->is_virtual = 1;
- $orderComment->pic_list = $this->pic_list;
- $orderComment->score = $this->score;
- $orderComment->created_at = $this->created_at;
- $orderComment->mch_id = $this->mch_id;
- $orderComment->goods_id = $this->goods_id;
- $orderComment->is_hide = $this->is_hide;
- $orderComment->content = $this->content;
- $orderComment->virtual_user = $this->virtual_user;
- $orderComment->virtual_avatar = $this->virtual_avatar;
- if ($orderComment->save()) {
- return [
- 'code' => 0,
- 'msg' => '保存成功',
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- }
- /**
- * 增加评论后台添加
- * @return array
- */
- public function commentSave()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $saas_user = SaasUser::findOne($this->saas_user_id);
- if ($saas_user) {
- $user = User::findOne(['binding' => $saas_user->mobile]);
- }
- if (intval($this->user_type) === OrderComment::USER_TYPE_USER) {
- if (empty($user)) {
- return [
- 'code' => 1,
- 'msg' => '用户信息不存在',
- ];
- }
- }
- if (intval($this->user_type) === OrderComment::USER_TYPE_CUSTOM) {
- if (empty($this->user_name) || empty($this->user_avatar)) {
- return [
- 'code' => 1,
- 'msg' => '用户信息错误',
- ];
- }
- }
- $orderComment = new OrderComment();
- $orderComment->store_id = $this->store_id;
- $orderComment->user_id = $user->id ?? 0;
- $orderComment->saas_id = $this->saas_user_id;
- $orderComment->order_id = 0;
- $orderComment->order_detail_id = 0;
- $orderComment->is_delete = 0;
- $orderComment->created_at = $this->created_at;
- $orderComment->is_virtual = 0;
- $orderComment->pic_list = $this->pic_list;
- $orderComment->score = $this->score;
- $orderComment->created_at = $this->created_at;
- $orderComment->mch_id = 0;
- $orderComment->goods_id = $this->goods_id;
- $orderComment->is_hide = $this->is_hide;
- $orderComment->content = $this->content;
- $orderComment->virtual_user = $this->virtual_user;
- $orderComment->virtual_avatar = $this->virtual_avatar;
- $orderComment->user_type = $this->user_type;
- $orderComment->user_name = $this->user_name;
- $orderComment->user_avatar = $this->user_avatar;
- if ($orderComment->save()) {
- return [
- 'code' => 0,
- 'msg' => '保存成功',
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($orderComment->firstErrors)),
- ];
- }
- }
- }
|