EvaluateForm.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace addons\TinyShop\common\forms;
  3. use Yii;
  4. use common\helpers\ArrayHelper;
  5. use addons\TinyShop\common\models\product\Evaluate;
  6. use addons\TinyShop\common\enums\ExplainStatusEnum;
  7. use addons\TinyShop\common\enums\ExplainTypeEnum;
  8. /**
  9. * Class EvaluateForm
  10. * @package addons\TinyShop\common\forms
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class EvaluateForm extends Evaluate
  14. {
  15. /**
  16. * @var
  17. */
  18. protected $product;
  19. public function rules()
  20. {
  21. return ArrayHelper::merge(parent::rules(), [
  22. [['order_product_id', 'scores'], 'required'],
  23. [['order_product_id'], 'verifyValid'],
  24. ]);
  25. }
  26. /**
  27. * @param $attribute
  28. */
  29. public function verifyValid($attribute)
  30. {
  31. $product = $this->product;
  32. if (!$product) {
  33. $product = Yii::$app->tinyShopService->orderProduct->findById($this->order_product_id);
  34. }
  35. if (!$product) {
  36. $this->addError($attribute, '找不到订单产品');
  37. return;
  38. }
  39. if ($product->is_evaluate != ExplainStatusEnum::DEAULT) {
  40. $this->addError($attribute, $product['product_name'] . '已经评价');
  41. return;
  42. }
  43. $this->merchant_id = $product['merchant_id'] ?? 0;
  44. $this->order_id = $product['order_id'];
  45. $this->order_sn = $product->order->order_sn;
  46. $this->product_id = $product['product_id'];
  47. $this->product_name = $product['product_name'];
  48. $this->product_picture = $product['product_picture'];
  49. $this->sku_name = $product['sku_name'];
  50. $this->product_price = $product['price'];
  51. $this->member_id = $product['buyer_id'];
  52. $this->member_nickname = $product->member->nickname;
  53. $this->member_head_portrait = $product->member->head_portrait;
  54. $this->explain_type = ExplainTypeEnum::scoresToType($this->scores);
  55. // 评价状态更新
  56. Yii::$app->tinyShopService->orderProduct->evaluate($product->id);
  57. }
  58. /**
  59. * @param $product
  60. */
  61. public function setProduct($product)
  62. {
  63. $this->product = $product;
  64. }
  65. /**
  66. * @param bool $insert
  67. * @return bool
  68. */
  69. public function beforeSave($insert)
  70. {
  71. $this->has_cover = !empty($this->covers) ? 1 : 0;
  72. $this->has_video = !empty($this->video) ? 1 : 0;
  73. $this->has_content = !empty($this->content) ? 1 : 0;
  74. // 默认内容
  75. empty($this->content) && $this->content = '此用户没有填写评价。';
  76. return parent::beforeSave($insert);
  77. }
  78. /**
  79. * @param bool $insert
  80. * @param array $changedAttributes
  81. */
  82. public function afterSave($insert, $changedAttributes)
  83. {
  84. if ($insert) {
  85. // 增加评价数量
  86. Yii::$app->tinyShopService->product->updateCommentNum($this->product_id, $this->scores, 1);
  87. // 校验是否全部评价并修改订单已评价
  88. Yii::$app->tinyShopService->order->evaluate($this->order_id);
  89. // 更新商品评价标签
  90. Yii::$app->tinyShopService->productEvaluateStat->updateNum(new EvaluateStatForm([
  91. 'has_cover' => !empty($this->covers),
  92. 'has_video' => !empty($this->video),
  93. 'has_good' => $this->explain_type == ExplainTypeEnum::GOOD,
  94. 'has_ordinary' => $this->explain_type == ExplainTypeEnum::ORDINARY,
  95. 'has_negative' => $this->explain_type == ExplainTypeEnum::NEGATIVE,
  96. ]), $this->product_id);
  97. }
  98. parent::afterSave($insert, $changedAttributes);
  99. }
  100. }
粤ICP备19079148号