EvaluateService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace addons\TinyShop\services\product;
  3. use Yii;
  4. use yii\web\UnprocessableEntityHttpException;
  5. use common\components\Service;
  6. use common\enums\StatusEnum;
  7. use addons\TinyShop\common\models\product\Evaluate;
  8. use addons\TinyShop\common\forms\EvaluateForm;
  9. use addons\TinyShop\common\enums\ExplainStatusEnum;
  10. use addons\TinyShop\common\forms\SettingForm;
  11. /**
  12. * Class EvaluateService
  13. * @package addons\TinyShop\services\product
  14. * @author jianyan74 <751393839@qq.com>
  15. */
  16. class EvaluateService extends Service
  17. {
  18. /**
  19. * 自动评价
  20. *
  21. * @throws UnprocessableEntityHttpException
  22. */
  23. public function autoEvaluate()
  24. {
  25. $data = Yii::$app->tinyShopService->order->findEvaluateData();
  26. foreach ($data as $datum) {
  27. /** @var SettingForm $setting */
  28. $setting = Yii::$app->tinyShopService->config->setting();
  29. foreach ($datum['product'] as $item) {
  30. if ($item->is_evaluate == ExplainStatusEnum::DEAULT) {
  31. $model = new EvaluateForm();
  32. $model = $model->loadDefaultValues();
  33. $model->setProduct($item);
  34. $model->order_product_id = $item['id'];
  35. $model->scores = 5;
  36. $model->is_auto = StatusEnum::ENABLED;
  37. $model->content = $setting->order_evaluate;
  38. if (!$model->save()) {
  39. throw new UnprocessableEntityHttpException($this->getError($model));
  40. }
  41. }
  42. }
  43. }
  44. }
  45. /**
  46. * @param $order_product_id
  47. * @return array|\yii\db\ActiveRecord|null
  48. */
  49. public function findByOrderProductId($order_product_id)
  50. {
  51. return Evaluate::find()
  52. ->where(['order_product_id' => $order_product_id])
  53. ->andWhere(['status' => StatusEnum::ENABLED])
  54. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  55. ->one();
  56. }
  57. /**
  58. * 获取评价的头像
  59. *
  60. * @param $order_product_id
  61. * @return array|\yii\db\ActiveRecord|null
  62. */
  63. public function findHeadPortraitByProductId($product_id, $limit = 3)
  64. {
  65. return Evaluate::find()
  66. ->select(['member_head_portrait'])
  67. ->where(['product_id' => $product_id])
  68. ->andWhere(['status' => StatusEnum::ENABLED])
  69. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  70. ->limit($limit)
  71. ->column();
  72. }
  73. /**
  74. * @return int|string
  75. */
  76. public function getCount()
  77. {
  78. return Evaluate::find()
  79. ->select('id')
  80. ->andWhere(['>', 'status', StatusEnum::DISABLED])
  81. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  82. ->count();
  83. }
  84. }
粤ICP备19079148号