UserLevelUpgradeCondition.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace common\models\user;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use PHPUnit\Util\Exception;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_user_level_upgrade_condition".
  9. *
  10. *
  11. * @property int $id
  12. * @property int $mall_id
  13. * @property int $user_level_id 会员等级ID
  14. * @property int $is_auto_upgrade 是否开启自动升级
  15. * @property string $checked_condition_values 升级条件
  16. * @property string $upgrade_type_condition 条件升级
  17. * @property string $checked_condition_keys 升级条件选中的key集合
  18. * @property int $condition_type 0 未选择、1满足其一 2、满足所有
  19. * @property int $upgrade_type_goods 购买升级商品,0关闭,1开启
  20. * @property int $goods_type 商品升级类型,1- 任意商品 2- 指定商品
  21. * @property string $goods_ids 商品id集合
  22. * @property string $cate_ids 商品分类id集合
  23. * @property int $buy_cate_type 订单完成0;支付完成:1
  24. * @property string $goods_list 商品列表json
  25. * @property int $status 状态[-1:删除;0:禁用;1启用]
  26. * @property int $created_at
  27. * @property int $updated_at
  28. * @property int $store_upgrade_type_goods 购买门店升级商品,0关闭,1开启
  29. * @property int $store_buy_goods_type 门店订单完成0;支付完成:1
  30. * @property int $store_goods_type 门店商品升级类型,1- 任意商品 2- 指定商品
  31. * @property string $store_goods_ids 门店商品id集合
  32. * @property string|null $store_goods_list 门店商品列表json
  33. * @property int $enable_goods_protocol 商品协议开关[0关闭;1开启]
  34. * @property int $is_custom_condition_num 是否自定义升级商品条件数量
  35. */
  36. class UserLevelUpgradeCondition extends BaseActiveRecord
  37. {
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public static function tableName()
  42. {
  43. return '{{%user_level_upgrade_condition}}';
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function rules()
  49. {
  50. return [
  51. [['mall_id', 'user_level_id', 'checked_condition_values', 'condition_type', 'upgrade_type_goods'], 'required'],
  52. [['mall_id', 'user_level_id', 'is_auto_upgrade', 'upgrade_type_condition', 'condition_type', 'upgrade_type_goods', 'goods_type', 'buy_goods_type', 'buy_cate_type', 'status', 'created_at', 'updated_at',
  53. 'store_upgrade_type_goods', 'store_buy_goods_type', 'store_goods_type', 'enable_goods_protocol', 'is_custom_condition_num'], 'integer'],
  54. [['checked_condition_values', 'checked_condition_keys','store_goods_list'], 'string'],
  55. [['goods_ids', 'cate_ids','store_goods_ids'], 'string', 'max' => 3000],
  56. [['goods_list'], 'string', 'max' => 50000],
  57. ];
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'id' => 'ID',
  66. 'mall_id' => 'Mall ID',
  67. 'user_level_id' => '会员等级ID',
  68. 'is_auto_upgrade' => '是否开启自动升级',
  69. 'upgrade_type_condition' => '条件升级',
  70. 'checked_condition_keys' => '升级条件选中的key集合',
  71. 'checked_condition_values' => '升级条件',
  72. 'condition_type' => '0 未选择、1满足其一 2、满足所有',
  73. 'upgrade_type_goods' => '购买升级商品,0关闭,1开启',
  74. 'goods_type' => '商品升级类型,1- 任意商品 2- 指定商品',
  75. 'buy_goods_type' => '商品升级类型,1- 任意商品 2- 指定商品',
  76. 'goods_ids' => '商品id集合',
  77. 'goods_list' => '商品列表json',
  78. 'status' => '状态[-1:删除;0:禁用;1启用]',
  79. 'created_at' => 'Created At',
  80. 'updated_at' => 'Updated At',
  81. 'store_upgrade_type_goods' => '购买门店升级商品,0关闭,1开启',
  82. 'store_buy_goods_type' => '门店订单完成0;支付完成:1',
  83. 'store_goods_type' => '门店商品升级类型,1- 任意商品 2- 指定商品',
  84. 'store_goods_ids' => '门店商品id集合',
  85. 'store_goods_list' => '门店商品列表json',
  86. 'enable_goods_protocol' => '商品协议开关[0关闭;1开启]',
  87. 'is_custom_condition_num' => '是否自定义升级商品条件数量',
  88. ];
  89. }
  90. /**
  91. * 保存数据
  92. * @param array $params
  93. * @return bool|UserLevelUpgradeCondition
  94. */
  95. public static function setData(array $params)
  96. {
  97. try {
  98. $model = self::findOne(['mall_id' => $params['mall_id'], 'user_level_id' => $params['user_level_id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  99. if (empty($model)) {
  100. $model = new self();
  101. $model->loadDefaultValues();
  102. }
  103. if (!$model->load($params, '')) throw new Exception($model->getErrorMessage());
  104. if (empty($params['checked_condition_keys'])) $params['checked_condition_keys'] = [];
  105. if (empty($params['checked_condition_values'])) $params['checked_condition_values'] = [];
  106. if (empty($params['goods_ids'])) $params['goods_ids'] = [];
  107. if (empty($params['store_goods_ids'])) $params['store_goods_ids'] = [];
  108. if (empty($params['cate_ids'])) $params['cate_ids'] = [];
  109. if (empty($params['goods_list'])) $params['goods_list'] = [];
  110. if (empty($params['store_goods_list'])) $params['store_goods_list'] = [];
  111. $model->checked_condition_keys = json_encode($params['checked_condition_keys'], JSON_NUMERIC_CHECK);
  112. $model->checked_condition_values = json_encode($params['checked_condition_values'], JSON_NUMERIC_CHECK);
  113. $model->goods_ids = json_encode($params['goods_ids'], JSON_NUMERIC_CHECK);
  114. $model->store_goods_ids = json_encode($params['store_goods_ids'], JSON_NUMERIC_CHECK);
  115. $model->cate_ids = json_encode($params['cate_ids'], JSON_NUMERIC_CHECK);
  116. $model->goods_list = json_encode($params['goods_list'], JSON_NUMERIC_CHECK);
  117. $model->store_goods_list = json_encode($params['store_goods_list'], JSON_NUMERIC_CHECK);
  118. if (!$model->save()) throw new Exception($model->getErrorMessage());
  119. return $model;
  120. } catch (Exception $e) {
  121. self::$static_error = $e->getMessage();
  122. return false;
  123. }
  124. }
  125. /**
  126. * 获取升级条件模板数据
  127. * @param $obj
  128. * @param $mall_id
  129. * @param $rules
  130. * @return array
  131. */
  132. public static function getUpgradeConditionData($obj, $mall_id, $rules)
  133. {
  134. // $tpl = $obj->renderPartial('../upgrade-condition/tpl/tpl.php', ['rules' => $rules, 'mall_id' => $mall_id]);
  135. $tpl = $obj->renderPartial('@backend/modules/mall/views/upgrade-condition/tpl/tpl.php', ['rules' => $rules, 'mall_id' => $mall_id]);
  136. $source_list = [];
  137. $key_arr = [];
  138. foreach ($rules as $k => $item) {
  139. $row = [];
  140. $row['key'] = $item['id'];
  141. foreach ($item['setting'] as $value) {
  142. $row['value'][$value['name']] = '';
  143. if (!empty($value['source']['class']) && !empty($value['source']['method'])) {
  144. $class = $value['source']['class'];
  145. $method = $value['source']['method'];
  146. $rows = $class::$method(['mall_id' => $mall_id]);
  147. $source_list[$value['source']['key']] = $rows;
  148. }
  149. }
  150. $key_arr[] = $row;
  151. }
  152. $source_list = json_encode($source_list, JSON_NUMERIC_CHECK);
  153. $key_arr = json_encode($key_arr, JSON_NUMERIC_CHECK);
  154. return ['tpl' => $tpl, 'source_list' => $source_list, 'key_arr' => $key_arr];
  155. }
  156. /**
  157. * @param $where
  158. * @param $status
  159. * @return bool
  160. */
  161. public static function del($where, $status)
  162. {
  163. try {
  164. $query = self::find();
  165. self::paramPack(['where' => $where], $query);
  166. $model = $query->one();
  167. $model->status = $status;
  168. $res = $model->save();
  169. if ($res === false) throw new Exception(UserLevelUpgradeCondition::getStaticError());
  170. return true;
  171. } catch (\Exception $e) {
  172. self::$static_error = $e->getMessage();
  173. return false;
  174. }
  175. }
  176. }