StoreGoodsExtra.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. use yii\helpers\Json;
  7. /**
  8. * This is the model class for table "qimall_addons_store_goods_extra".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城id
  12. * @property int $store_id 门店id
  13. * @property int $goods_id 商品ID
  14. * @property float $full_reduce_money 单品满额减免金额
  15. * @property float $full_reduce_price_limit 单品满额金额阈值
  16. * @property string|null $score_give_setting 积分赠送设置
  17. * @property string|null $score_deduct_setting 积分抵扣设置
  18. * @property string|null $currency_deduct_setting 货币营销设置
  19. * @property int $is_member_price 是否参与会员价 [1是 0 否]
  20. * @property string|null $member_price_setting 会员价格设置
  21. * @property string|null $member_buy_limit_setting 会员限购设置
  22. * @property int is_repurchase 是否开启复购价[0=否, 1=是]
  23. * @property string|null repurchase_setting 复购价设置
  24. * @property int $status 状态[1正常 -1删除]
  25. * @property int $created_at 创建时间
  26. * @property int $updated_at 修改时间
  27. */
  28. class StoreGoodsExtra extends BaseActiveRecord
  29. {
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%addons_store_goods_extra}}';
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['mall_id', 'store_id', 'goods_id', 'is_member_price', 'status', 'created_at', 'updated_at', 'is_repurchase','is_alone_settle', 'is_hidden_member_price', 'member_price_switch', 'is_show_price'], 'integer'],
  44. [['full_reduce_money', 'full_reduce_price_limit','alone_settle_price'], 'number'],
  45. [['score_give_setting', 'score_deduct_setting', 'currency_deduct_setting', 'member_price_setting', 'member_buy_limit_setting', 'repurchase_setting', 'service_fee_setting'], 'string'],
  46. ];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => 'ID',
  55. 'mall_id' => '商城id',
  56. 'store_id' => '门店id',
  57. 'goods_id' => '商品ID',
  58. 'full_reduce_money' => '单品满额减免金额',
  59. 'full_reduce_price_limit' => '单品满额金额阈值',
  60. 'score_give_setting' => '积分赠送设置',
  61. 'score_deduct_setting' => '积分抵扣设置',
  62. 'currency_deduct_setting' => '货币营销设置',
  63. 'is_member_price' => '是否参与会员价 [1是 0 否]',
  64. 'member_price_setting' => '会员价格设置',
  65. 'member_buy_limit_setting' => '会员限购设置',
  66. 'is_repurchase' => '是否开启复购价[0=否, 1=是]',
  67. 'repurchase_setting' => '复购价设置',
  68. 'status' => '状态[1正常 -1删除]',
  69. 'created_at' => '创建时间',
  70. 'updated_at' => '修改时间',
  71. ];
  72. }
  73. /**
  74. * 保存数据
  75. * @param $params
  76. * @return bool
  77. */
  78. public static function setData($goods_id,$params)
  79. {
  80. try{
  81. // 判断参数中是否存在数组,存在则转为json字符串
  82. foreach ($params as $key => &$item) {
  83. $item = is_array($item) ? json_encode($item,JSON_UNESCAPED_UNICODE) : $item;
  84. }
  85. // 根据商品id获取商品信息
  86. $model = self::findOne(['goods_id' => $goods_id,'status'=>StatusEnum::ENABLED]);
  87. if (empty($model)) {
  88. // 初始化商品额外参数
  89. $model = new self();
  90. $model->loadDefaultValues();
  91. }
  92. $model->goods_id = $goods_id;
  93. if(!$model->load($params,'') || !$model->save(false)){
  94. throw new \Exception($model->getErrorMessage());
  95. }
  96. return true;
  97. }catch(\Exception $e){
  98. self::$static_error = $e->getMessage();
  99. return false;
  100. }
  101. }
  102. /**
  103. * 购买条件验证
  104. * @Author bing
  105. * @DateTime 2021-09-27 14:20:53
  106. * @copyright: Copyright (c) 2020 广东七件事集团
  107. * @param [type] $user
  108. * @param [type] $limit_setting
  109. * @return bool
  110. */
  111. public static function buyLimitValidate($user,&$limit_setting){
  112. try{
  113. if(!empty($limit_setting)){
  114. if(!in_array($user->level,$limit_setting['member_level'])){
  115. throw new \Exception('你的等级不支持购买该商品');
  116. }
  117. foreach($limit_setting as $key => $val){
  118. if($val == 0) continue;
  119. }
  120. }
  121. return true;
  122. }catch(\Exception $e){
  123. self::$static_error = $e->getMessage();
  124. return false;
  125. }
  126. }
  127. public static function updateServiceFeeSetting($params)
  128. {
  129. $max_platform_ratio = $params['max_platform_ratio'];
  130. $min_platform_ratio = $params['min_platform_ratio'];
  131. $query = self::find()
  132. ->where([
  133. 'mall_id' => $params['mall_id'],
  134. 'status' => StatusEnum::ENABLED,
  135. ])
  136. ->select(['id', 'service_fee_setting']);
  137. foreach ($query->each() as $row) {
  138. $setting = Json::decode($row['service_fee_setting']);
  139. if (isset($setting['service_fee_ratio']) && $setting['is_alone'] == 1) {
  140. $ratio = $setting['service_fee_ratio'];
  141. if ($ratio < $min_platform_ratio || $ratio > $max_platform_ratio) {
  142. $setting['service_fee_ratio'] = $max_platform_ratio;
  143. \Yii::$app->db->createCommand()
  144. ->update(self::tableName(), [
  145. 'service_fee_setting' => Json::encode($setting)
  146. ], ['id' => $row['id']])
  147. ->execute();
  148. }
  149. }
  150. }
  151. }
  152. }