OrderComments.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace common\models\order;
  3. use addons\Store\common\models\Store;
  4. use addons\Store\common\models\StoreGoods;
  5. use addons\Store\common\models\StoreOrderDetail;
  6. use common\enums\StatusEnum;
  7. use common\models\base\BaseActiveRecord;
  8. use common\models\goods\Goods;
  9. use common\models\user\User;
  10. use PHPUnit\Util\Exception;
  11. use Yii;
  12. use yii\helpers\Json;
  13. /**
  14. * This is the model class for table "qimall_order_comments".
  15. *
  16. *
  17. * @property int $id
  18. * @property int $mall_id
  19. * @property int $mch_id
  20. * @property int $store_id
  21. * @property int $order_id
  22. * @property int|null $order_detail_id 订单详情id
  23. * @property int $goods_id 商品ID
  24. * @property int $user_id 会员id
  25. * @property int $score 评分:1=差评,2,3=中评,4,5=好
  26. * @property string $content 评价内容
  27. * @property string $pic_url 评价图片
  28. * @property int $is_top 是否置顶0.否|1.是
  29. * @property string $reply_content 商家回复内容
  30. * @property int $status 状态[-1:删除;0:禁用;1启用]
  31. * @property int $created_at 创建时间
  32. * @property int $updated_at 修改时间
  33. * @property int $is_virtual 是否虚拟用户
  34. * @property string $virtual_user 虚拟用户名
  35. * @property string $virtual_avatar 虚拟头像
  36. * @property int $virtual_time 虚拟评价时间
  37. */
  38. class OrderComments extends BaseActiveRecord
  39. {
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function tableName()
  44. {
  45. return '{{%order_comments}}';
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['mall_id', 'mch_id','store_id', 'order_id', 'order_detail_id', 'goods_id', 'user_id', 'score', 'is_top',
  54. 'status', 'created_at', 'updated_at', 'is_virtual', 'virtual_time','goods_type'], 'integer'],
  55. [['score'], 'required'],
  56. [['content', 'pic_url'], 'string', 'max' => 5000],
  57. [['reply_content'], 'string', 'max' => 1000],
  58. [['virtual_user', 'virtual_avatar'], 'string', 'max' => 255],
  59. ];
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => 'ID',
  68. 'mall_id' => 'Mall ID',
  69. 'mch_id' => '多商户id',
  70. 'store_id' => '门店id',
  71. 'order_id' => '订单id',
  72. 'order_detail_id' => '订单详情id',
  73. 'goods_id' => '商品ID',
  74. 'user_id' => '会员id',
  75. 'score' => '评分:1=差评,2,3=中评,4,5=好',
  76. 'content' => '评价内容',
  77. 'pic_url' => '评价图片',
  78. 'is_top' => '是否置顶0.否|1.是',
  79. 'reply_content' => '商家回复内容',
  80. 'status' => '状态[-1:删除;0:禁用;1启用]',
  81. 'created_at' => '创建时间',
  82. 'updated_at' => '修改时间',
  83. 'is_virtual' => '是否虚拟用户',
  84. 'virtual_user' => '虚拟用户名',
  85. 'virtual_avatar' => '虚拟头像',
  86. 'virtual_time' => '虚拟评价时间',
  87. ];
  88. }
  89. public function getGoods()
  90. {
  91. return $this->hasOne(Goods::class, ['id' => 'goods_id']);
  92. }
  93. public function getUsers()
  94. {
  95. return $this->hasOne(User::class, ['id' => 'user_id']);
  96. }
  97. public function getReserveComment()
  98. {
  99. return $this->hasOne(OrderReserveComments::class, ['id' => 'id']);
  100. }
  101. public function getOrder()
  102. {
  103. return $this->hasOne(Order::class, ['id' => 'order_id']);
  104. }
  105. public function getOrderDetail()
  106. {
  107. return $this->hasOne(OrderDetail::class, ['id' => 'order_detail_id']);
  108. }
  109. /**
  110. * @Author: hua
  111. * @DateTime: 2021/10/8 9:41
  112. * @Copyright: copyright (c) 2021 广东七件事集团
  113. * @param array $params
  114. * @return bool|OrderComments
  115. * @throws \yii\db\Exception
  116. */
  117. public static function setData(array $params)
  118. {
  119. try {
  120. if (!empty($params['id'])) {
  121. $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  122. if (empty($model)) throw new Exception('服务不存在或已删除');
  123. } else {
  124. $model = new self();
  125. $model->loadDefaultValues();
  126. }
  127. if (isset($params['pic_url'])) $params['pic_url'] = Json::encode($params['pic_url']);//后台虚拟
  128. if (isset($params['pic_list'])) $params['pic_url'] = $params['pic_list'];//前端真实用户
  129. if (!empty($params['virtual_time'])) $params['virtual_time'] = strtotime($params['virtual_time']);
  130. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  131. $res = OrderDetail::setData(['id' => $params['order_detail_id'], 'mall_id' => $params['mall_id'], 'is_evaluate' => 1]);
  132. if (!$res) throw new Exception(OrderDetail::getStaticError());
  133. return $model;
  134. } catch (Exception $e) {
  135. self::$static_error = $e->getMessage();
  136. return false;
  137. }
  138. }
  139. /**
  140. *
  141. * @Author: hua
  142. * @DateTime: 2021/10/7 16:41
  143. * @Copyright: copyright (c) 2021 广东七件事集团
  144. * @param $params
  145. * @return bool|OrderComments
  146. * @throws \Exception
  147. */
  148. public static function updateData($params)
  149. {
  150. try {
  151. $res = self::updateAll($params['field'], ['in', 'id', $params['id']]);
  152. if (!$res) throw new Exception(self::getStaticError());
  153. return true;
  154. } catch (Exception $e) {
  155. self::$static_error = $e->getMessage();
  156. return false;
  157. }
  158. }
  159. public static function updateGoodsType()
  160. {
  161. $i = 1;
  162. $page = 500;
  163. while(true){
  164. $offset = ($i-1)*$page;
  165. $list = self::find()->where(['>','mall_id',0])->offset($offset)->limit($page)->all();
  166. if(empty($list)){
  167. break;
  168. }
  169. $goods_ids = $store_goods_ids = [];
  170. foreach ($list as $item){
  171. if(!empty($item['store_id'])){
  172. $store_goods_ids[]=$item['goods_id'];
  173. }else{
  174. $goods_ids[]=$item['goods_id'];
  175. }
  176. }
  177. if(!empty($store_goods_ids)){
  178. $store_goods_list = StoreGoods::lists([
  179. 'where'=>[
  180. ['id'=>$store_goods_ids]
  181. ],
  182. 'index'=>'id',
  183. 'select'=>'id,goods_type'
  184. ]);
  185. }
  186. if(!empty($store_goods_ids)){
  187. $goods_list = Goods::lists([
  188. 'where'=>[
  189. ['id'=>$goods_ids]
  190. ],
  191. 'index'=>'id',
  192. 'select'=>'id,goods_type'
  193. ]);
  194. }
  195. foreach ($list as $item){
  196. if(!empty($item['store_id'])){
  197. $item->goods_type = $store_goods_list[$item['goods_id']]['goods_type']??1;
  198. }else{
  199. $item->goods_type = $goods_list[$item['goods_id']]['goods_type']??1;
  200. }
  201. $item->save();
  202. }
  203. $i++;
  204. }
  205. }
  206. }