AddonsXhsGoodsStatistics.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace addons\Xhs\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_xhs_goods_statistics".
  8. *
  9. * @property int $id ID
  10. * @property int $mall_id 商城ID
  11. * @property int $goods_id 商品 id
  12. * @property int $order_num 订单数量
  13. * @property float $total_amount 商品总金额
  14. * @property int|null $goods_views_count 商品访问量
  15. * @property int $created_at 更新时间
  16. * @property int $updated_at 创建时间
  17. * @property int $bought_count 购买量
  18. */
  19. class AddonsXhsGoodsStatistics extends \common\models\base\BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_xhs_goods_statistics}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id', 'goods_id', 'total_amount'], 'required'],
  35. [['mall_id', 'goods_id', 'order_num', 'goods_views_count', 'created_at', 'updated_at', 'bought_count'], 'integer'],
  36. [['total_amount'], 'number'],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => '商城ID',
  47. 'goods_id' => '商品 id',
  48. 'order_num' => '订单数量',
  49. 'total_amount' => '商品总金额',
  50. 'goods_views_count' => '商品访问量',
  51. 'created_at' => '更新时间',
  52. 'updated_at' => '创建时间',
  53. 'bought_count' => '购买量',
  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. * 保存数据
  63. * @Author: ltm
  64. * @DateTime: 2021/01/10 0022 17:13
  65. * @Copyright: copyright (c) 2021 广东七件事集团
  66. * @param array $params
  67. * @return AddonsMaterial|bool
  68. */
  69. public static function setData(array $params){
  70. try{
  71. $model = self::findOne(['mall_id' => $params['mall_id'],'goods_id' => $params['goods_id']]);
  72. if (!$model) {
  73. $model = new self();
  74. $model->loadDefaultValues();
  75. $model->mall_id = $params['mall_id'];
  76. $model->created_at = time();
  77. }else{
  78. if($params['order_num']) {
  79. $model->order_num = $model->order_num + $params['order_num'];
  80. unset($params['order_num']);
  81. }
  82. if($params['total_amount']) {
  83. $model->total_amount = $model->total_amount + $params['total_amount'];
  84. unset($params['total_amount']);
  85. }
  86. if($params['bought_count']) {
  87. $model->bought_count = $model->bought_count + $params['bought_count'];
  88. unset($params['bought_count']);
  89. }
  90. if($params['goods_views_count']) {
  91. $model->goods_views_count = $model->goods_views_count + $params['goods_views_count'];
  92. unset($params['goods_views_count']);
  93. }
  94. $model->updated_at = time();
  95. }
  96. if(!$model->load($params,'') || !$model->save()){
  97. throw new Exception($model->getErrorMessage());
  98. }
  99. return $model;
  100. }catch(\Exception $e){
  101. self::$static_error = $e->getMessage();
  102. return false;
  103. }
  104. }
  105. }