HigoGoodsSetting.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace addons\HiGo\common\models;
  3. use common\enums\AddonsEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "{{%addons_higo_goods_setting}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property int $goods_id 商品id
  13. * @property int $source_id 来源id
  14. * @property string $source 来源标识; 主商城[Main], 插件对应插件标识
  15. * @property int|null $wallet_type 钱包类型[hi_bei:嗨贝,hi_value:嗨值]
  16. * @property int $is_enable 是否开启:0:否,1是
  17. * @property int $is_percent 是否百分比:0:否(固定金额),1是
  18. * @property float $give 赠送红包金额
  19. * @property int $deduct_type 商品抵扣类型[1:单件抵扣,2:多件抵扣]
  20. * @property int $settlement_method 结算方式[;0:支付完成后;1:订单完成后]
  21. * @property float $deduct_money 商品抵扣金额
  22. * @property int $status 状态[-1:删除;0:禁用;1启用]
  23. * @property int $created_at
  24. * @property int $updated_at
  25. */
  26. class HigoGoodsSetting extends \common\models\base\BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%addons_higo_goods_setting}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id'], 'required'],
  42. [['mall_id', 'goods_id', 'is_enable', 'is_percent', 'deduct_type', 'status', 'created_at', 'updated_at', 'settlement_method', 'source_id'], 'integer'],
  43. [['give', 'deduct_money'], 'number'],
  44. [['wallet_type', 'source'], 'string', 'max' => 20],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => '商城ID',
  55. 'goods_id' => '商品id',
  56. 'wallet_type' => '钱包类型[hi_bei:嗨贝,hi_value:嗨值]',
  57. 'is_enable' => '是否开启:0:否,1是',
  58. 'is_percent' => '是否百分比:0:否(固定金额),1是',
  59. 'give' => '赠送金额',
  60. 'deduct_type' => '商品抵扣类型[1:单件抵扣,2:多件抵扣]',
  61. 'deduct_money' => '商品抵扣金额',
  62. 'settlement_method' => '结算方式[;0:支付完成后;1:订单完成后]',
  63. 'status' => '状态[-1:删除;0:禁用;1启用]',
  64. 'source_id' => '来源id',
  65. 'source' => '来源标识; 主商城[Main], 插件对应插件标识',
  66. 'created_at' => 'Created At',
  67. 'updated_at' => 'Updated At',
  68. ];
  69. }
  70. /**
  71. * 保存设置
  72. * @Author: lun
  73. * @DateTime: 2022/3/23 0023 14:26
  74. * @Copyright: copyright (c) 2021 广东七件事集团
  75. * @param $mall_id
  76. * @param $goods_id
  77. * @param $list
  78. * @return bool
  79. */
  80. public static function setGoodsSetting($mall_id, $goods_id, $list)
  81. {
  82. try {
  83. if (isset($list['enable_map'])) {
  84. $enable_map = $list['enable_map'];
  85. $goods_mode = HigoStoreGoodsMode::setGoodsSetting($mall_id, $goods_id, $enable_map);
  86. if ($goods_mode === false) {
  87. throw new Exception(HigoStoreGoodsMode::getStaticError());
  88. }
  89. unset($list['enable_map']);
  90. }
  91. foreach ($list as $item) {
  92. $model = self::findOne([
  93. 'mall_id' => $mall_id,
  94. 'goods_id' => $goods_id,
  95. 'wallet_type' => $item['wallet_type'],
  96. 'status' => StatusEnum::ENABLED,
  97. 'source' => $item['source'],
  98. 'source_id' => $item['source_id'],
  99. ]);
  100. if (empty($model)) {
  101. $model = new self();
  102. $model->mall_id = $mall_id;
  103. $model->goods_id = $goods_id;
  104. $model->loadDefaultValues();
  105. }
  106. if (!$model->load($item, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  107. }
  108. return true;
  109. } catch (\Exception $e) {
  110. self::setStaticError('商品嗨购设置失败:' . $e->getMessage());
  111. return false;
  112. }
  113. }
  114. /**
  115. * 获取商品的优惠券设置
  116. * @Author: lun
  117. * @DateTime: 2022/5/12 0012 14:49
  118. * @Copyright: copyright (c) 2021 广东七件事集团
  119. * @param $params
  120. * @return bool|string[]
  121. */
  122. public static function getGoodSetting($params)
  123. {
  124. // 后台页面组件展示
  125. if ($params['type'] == 'show_component') {
  126. return [
  127. 'component' => 'com-higo',
  128. 'path' => Yii::$app->basePath . '/../addons/HiGo/backend/views/goods',
  129. ];
  130. } elseif ($params['type'] == 'show_store_component') {
  131. return [
  132. 'component' => 'com-higo',
  133. 'path' => Yii::$app->basePath . '/../addons/HiGo/backend/views/goods/addons/store',
  134. ];
  135. } elseif ($params['type'] == 'show_mch_component') {
  136. return [
  137. 'component' => 'com-higo',
  138. 'path' => Yii::$app->basePath . '/../addons/HiGo/backend/views/goods/addons/mch',
  139. ];
  140. } else {
  141. try {
  142. if (empty($params['setting'][AddonsEnum::ADDONS_NAME_HI_GO])) return true;
  143. $setting = $params['setting'][AddonsEnum::ADDONS_NAME_HI_GO];
  144. // 数据保存
  145. $res = self::setGoodsSetting($params['mall_id'], $params['goods_id'], $setting);
  146. if ($res === false) throw new Exception(self::getStaticError());
  147. return true;
  148. } catch (Exception $e) {
  149. self::setStaticError($e->getMessage());
  150. return false;
  151. }
  152. }
  153. }
  154. public function getGoodsModeCommssionType()
  155. {
  156. return [
  157. [
  158. 'name' => 'hi_bei',
  159. 'label' => '嗨贝'
  160. ],
  161. [
  162. 'name' => 'hi_value',
  163. 'label' => '嗨值'
  164. ],
  165. ];
  166. }
  167. /**
  168. * 获取商品 独立设置
  169. * @param $goods_id
  170. * @param $wallet_type
  171. * @param string $source
  172. * @return array
  173. */
  174. public static function getGoodsSetting($goods_id, $wallet_type, string $source = 'Main')
  175. {
  176. $setting = self::find()->where(['goods_id' => $goods_id, 'is_enable' => 1, 'status' => StatusEnum::ENABLED, 'wallet_type' => $wallet_type, 'source' => $source])->asArray()->one();
  177. return $setting ?: [];
  178. }
  179. }