AddonsAlbumGoodsVisit.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace addons\Album\common\models;
  3. use common\models\goods\Goods;
  4. use Yii;
  5. use yii\db\Exception;
  6. /**
  7. * This is the model class for table "qimall_addons_album_goods_visit".
  8. *
  9. * @property int $id ID
  10. * @property int $mall_id 商城ID
  11. * @property int $album_id 内容 id
  12. * @property int $goods_id 商品 id
  13. * @property int $created_at 创建时间
  14. * @property int $updated_at 更新时间
  15. * @property int|null $visit_count 访问人数
  16. * @property int|null $pay_count 支付人数
  17. * @property float|null $amount 支付金额
  18. */
  19. class AddonsAlbumGoodsVisit extends \common\models\base\BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_album_goods_visit}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id', 'album_id', 'goods_id'], 'required'],
  35. [['mall_id', 'album_id', 'goods_id', 'created_at', 'updated_at', 'visit_count', 'pay_count'], 'integer'],
  36. [['amount'], 'number'],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => '商城ID',
  47. 'album_id' => '相册 id',
  48. 'goods_id' => '商品 id',
  49. 'created_at' => '创建时间',
  50. 'updated_at' => '更新时间',
  51. 'visit_count' => '访问人数',
  52. 'pay_count' => '支付人数',
  53. 'amount' => '支付金额',
  54. ];
  55. }
  56. //管理商品
  57. public function getGoods()
  58. {
  59. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select('id,goods_name,price,cover_pic,sales_num,virtual_sales');
  60. }
  61. /**
  62. * @Author: ltm
  63. * @DateTime: 2022/2/16 0016 9:17
  64. * @Copyright: copyright (c) 2021 广东七件事集团
  65. * @param $data
  66. * @return string
  67. */
  68. public static function setData(array $params){
  69. try{
  70. $model = self::findOne(['goods_id'=>$params['goods_id'],'mall_id'=>$params['mall_id'],'album_id'=>$params['album_id']]);
  71. if (!$model){
  72. $model = new self();
  73. $model->created_at = time();
  74. $model->loadDefaultValues();
  75. if(!$model->load($params,'')){
  76. throw new Exception($model->getErrorMessage());
  77. }
  78. }else{
  79. $model->updated_at = time();
  80. if($params['visit_count']) $model->visit_count += $params['visit_count'];
  81. if($params['pay_count']) $model->pay_count += $params['pay_count'];
  82. if($params['goods_amount']) $model->amount += $params['goods_amount'];
  83. unset($params['visit_count'],$params['pay_count'],$params['goods_amount']);
  84. }
  85. $model->mall_id = $params['mall_id'];
  86. if(!$model->save()){
  87. throw new Exception($model->getErrorMessage());
  88. }
  89. return $model;
  90. }catch(\Exception $e){
  91. var_dump($e->getMessage());
  92. self::$static_error = $e->getMessage();
  93. return false;
  94. }
  95. }
  96. }