OrderCommentForm.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Goods;
  9. use app\models\OrderComment;
  10. use app\models\OrderDetail;
  11. use app\models\SaasUser;
  12. use app\models\User;
  13. use yii\base\Model;
  14. class OrderCommentForm extends Model
  15. {
  16. public $store_id;
  17. public $id;
  18. public $status;
  19. public $reply_content;
  20. public $pic_list;
  21. public $mch;
  22. public $mch_id;
  23. public $goods_id;
  24. public $score;
  25. public $content;
  26. public $is_hide;
  27. public $is_virtual;
  28. public $virtual_user;
  29. public $virtual_avatar;
  30. public $created_at;
  31. public $user_type;
  32. public $user_name;
  33. public $user_avatar;
  34. public $saas_user_id;
  35. /**
  36. * @inheritdoc
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['goods_id', 'is_hide', 'is_virtual', 'mch_id', 'score','saas_user_id', 'user_type'], 'integer'],
  42. [['content','virtual_avatar', 'user_name', 'user_avatar'], 'string', 'max' => 1000],
  43. [['virtual_user'], 'string', 'max' => 255],
  44. [['created_at'], 'integer', 'max' => 2000000000],
  45. [['mch', 'mch_id'], 'safe'],
  46. ];
  47. }
  48. /**
  49. * 获取评论列表
  50. * @return array
  51. */
  52. public function getComment() {
  53. $query = OrderComment::find()->alias('oc')->where(['oc.store_id' => $this->store_id,
  54. 'oc.is_delete' => OrderComment::IS_DELETE_FALSE]);
  55. $query
  56. ->leftJoin(['u' => User::tableName()], 'oc.user_id=u.id')
  57. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  58. ->leftJoin(['od' => OrderDetail::tableName()], 'oc.order_detail_id=od.id')
  59. ->select('oc.created_at, oc.user_id as uid,oc.is_virtual,oc.virtual_user,oc.id,u.nickname,u.platform,u.avatar_url,
  60. 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,
  61. oc.user_type, oc.user_name, oc.user_avatar
  62. ')
  63. ->orderBy('oc.created_at DESC')->groupBy('oc.id');
  64. if (isset($this->mch) && $this->mch == 1) {
  65. if ($this->mch_id > 0) {
  66. $query->andWhere(['oc.mch_id' => $this->mch_id]);
  67. }else{
  68. $query->andWhere(['>', 'oc.mch_id', 0]);
  69. }
  70. }else{
  71. $query->andWhere(['oc.mch_id' => 0]);
  72. }
  73. $pagination = pagination_make($query);
  74. $list = $pagination['list'];
  75. foreach ($list as $key => &$value) {
  76. if (!$value['order_detail_id']) {
  77. $goods = Goods::findOne($value['goods_id']);
  78. $value['goods_name'] = $goods->name;
  79. $value['goods_pic'] = $goods->cover_pic;
  80. }
  81. $value['user_type'] = intval($value['user_type']);
  82. if ($value['user_type'] === OrderComment::USER_TYPE_CUSTOM) {
  83. $value['nickname'] = $value['user_name'];
  84. $value['avatar_url'] = $value['user_avatar'];
  85. }
  86. if ($value['is_virtual'] == 1) {
  87. $list[$key]['nickname'] = '(' . $value['virtual_user'] . ')';
  88. }
  89. $value['score'] = intval($value['score']);
  90. $value['pic_list'] = json_decode($value['pic_list'], true);
  91. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  92. // TODO: 其他数据
  93. }
  94. return [
  95. 'code' => 0,
  96. 'msg' => 'success',
  97. 'data' => [
  98. 'data' => $list,
  99. 'pageNo' => $pagination['pageNo'],
  100. 'totalCount' => $pagination['totalCount']
  101. ],
  102. ];
  103. }
  104. /**
  105. * 回复消息
  106. * @return array
  107. */
  108. public function reply() {
  109. $query = OrderComment::find()->where(['id' => $this->id])->one();
  110. if (!$query || empty($this->reply_content)) {
  111. return [
  112. 'code' => 1,
  113. 'msg' => '参数错误',
  114. ];
  115. }
  116. $query->reply_content = $this->reply_content;
  117. if ($query->save()) {
  118. return [
  119. 'code' => 0,
  120. 'msg' => '回复成功',
  121. ];
  122. } else {
  123. return [
  124. 'code' => 1,
  125. 'msg' => '回复失败',
  126. ];
  127. }
  128. }
  129. /**
  130. * 隐藏评论
  131. * @return array
  132. */
  133. public function hideStatus()
  134. {
  135. $order_comment = OrderComment::findOne([
  136. 'store_id' => $this->store_id,
  137. 'id' => $this->id,
  138. ]);
  139. if ($order_comment) {
  140. $order_comment->is_hide = $this->status;
  141. $order_comment->save();
  142. }
  143. return [
  144. 'code' => 0,
  145. 'msg' => '操作成功',
  146. ];
  147. }
  148. /**
  149. * 删除评论
  150. * @return array
  151. */
  152. public function deleteStatus()
  153. {
  154. $order_comment = OrderComment::findOne([
  155. 'store_id' => $this->store_id,
  156. 'id' => $this->id,
  157. ]);
  158. if ($order_comment) {
  159. $order_comment->is_delete = $this->status;
  160. $order_comment->save();
  161. }
  162. return [
  163. 'code' => 0,
  164. 'msg' => '操作成功',
  165. ];
  166. }
  167. /**
  168. * 增加评论
  169. * @return array
  170. */
  171. public function save()
  172. {
  173. if (!$this->validate()) {
  174. return [
  175. 'code' => 1,
  176. 'msg' => $this->getErrorSummary(false)[0],
  177. ];
  178. }
  179. $orderComment = new OrderComment();
  180. $orderComment->store_id = $this->store_id;
  181. $orderComment->user_id = 0;
  182. $orderComment->order_id = 0;
  183. $orderComment->order_detail_id = 0;
  184. $orderComment->user_id = 0;
  185. $orderComment->is_delete = 0;
  186. $orderComment->created_at = $this->created_at;
  187. $orderComment->is_virtual = 1;
  188. $orderComment->pic_list = $this->pic_list;
  189. $orderComment->score = $this->score;
  190. $orderComment->created_at = $this->created_at;
  191. $orderComment->mch_id = $this->mch_id;
  192. $orderComment->goods_id = $this->goods_id;
  193. $orderComment->is_hide = $this->is_hide;
  194. $orderComment->content = $this->content;
  195. $orderComment->virtual_user = $this->virtual_user;
  196. $orderComment->virtual_avatar = $this->virtual_avatar;
  197. if ($orderComment->save()) {
  198. return [
  199. 'code' => 0,
  200. 'msg' => '保存成功',
  201. ];
  202. } else {
  203. return [
  204. 'code' => 1,
  205. 'msg' => $this->getErrorSummary(false)[0],
  206. ];
  207. }
  208. }
  209. /**
  210. * 增加评论后台添加
  211. * @return array
  212. */
  213. public function commentSave()
  214. {
  215. if (!$this->validate()) {
  216. return [
  217. 'code' => 1,
  218. 'msg' => $this->getErrorSummary(false)[0],
  219. ];
  220. }
  221. $saas_user = SaasUser::findOne($this->saas_user_id);
  222. if ($saas_user) {
  223. $user = User::findOne(['binding' => $saas_user->mobile]);
  224. }
  225. if (intval($this->user_type) === OrderComment::USER_TYPE_USER) {
  226. if (empty($user)) {
  227. return [
  228. 'code' => 1,
  229. 'msg' => '用户信息不存在',
  230. ];
  231. }
  232. }
  233. if (intval($this->user_type) === OrderComment::USER_TYPE_CUSTOM) {
  234. if (empty($this->user_name) || empty($this->user_avatar)) {
  235. return [
  236. 'code' => 1,
  237. 'msg' => '用户信息错误',
  238. ];
  239. }
  240. }
  241. $orderComment = new OrderComment();
  242. $orderComment->store_id = $this->store_id;
  243. $orderComment->user_id = $user->id ?? 0;
  244. $orderComment->saas_id = $this->saas_user_id;
  245. $orderComment->order_id = 0;
  246. $orderComment->order_detail_id = 0;
  247. $orderComment->is_delete = 0;
  248. $orderComment->created_at = $this->created_at;
  249. $orderComment->is_virtual = 0;
  250. $orderComment->pic_list = $this->pic_list;
  251. $orderComment->score = $this->score;
  252. $orderComment->created_at = $this->created_at;
  253. $orderComment->mch_id = 0;
  254. $orderComment->goods_id = $this->goods_id;
  255. $orderComment->is_hide = $this->is_hide;
  256. $orderComment->content = $this->content;
  257. $orderComment->virtual_user = $this->virtual_user;
  258. $orderComment->virtual_avatar = $this->virtual_avatar;
  259. $orderComment->user_type = $this->user_type;
  260. $orderComment->user_name = $this->user_name;
  261. $orderComment->user_avatar = $this->user_avatar;
  262. if ($orderComment->save()) {
  263. return [
  264. 'code' => 0,
  265. 'msg' => '保存成功',
  266. ];
  267. } else {
  268. return [
  269. 'code' => 1,
  270. 'msg' => implode(';', array_values($orderComment->firstErrors)),
  271. ];
  272. }
  273. }
  274. }