CloudStockTwoLevel.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace addons\CloudStockTwo\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_cloud_stock_level".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城id
  11. * @property int $level 等级权重id
  12. * @property string $name 等级名称
  13. * @property string|null $checked_condition_values 升级条件
  14. * @property string|null $checked_condition_keys 升级条件选中的key集合
  15. * @property int $condition_type 0 未选择、1满足其一 2、满足所有
  16. * @property int $upgrade_type_condition 是否开启条件升级
  17. * @property int $is_over 超越奖励
  18. * @property int $is_equal 平级
  19. * @property int $is_fill 补货
  20. * @property string|null $give_quota json;赠送等级配置
  21. * @property string $explain 等级说明
  22. * @property int $status 状态[-1:删除;0:禁用;1启用]
  23. * @property int $created_at
  24. * @property int $updated_at
  25. * @property int $is_open_fill_min
  26. * @property int $fill_min
  27. */
  28. class CloudStockTwoLevel extends BaseActiveRecord
  29. {
  30. private static $levels = [];
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%addons_cloud_stock_two_level}}';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['mall_id', 'name'], 'required'],
  45. [['mall_id', 'level', 'condition_type', 'upgrade_type_condition', 'is_over', 'is_equal', 'is_fill', 'status', 'created_at', 'updated_at','is_open_fill_min','fill_min','is_open_equal_settle_type','equal_settle_type','is_recommender_reward','is_recommender_condition','recommender_condition_prize','recommender_settle_prize','recommender_settle_rate'], 'integer'],
  46. [['checked_condition_values', 'checked_condition_keys', 'give_quota','commission','score','agent_setting_info'], 'string'],
  47. [['name'], 'string', 'max' => 255],
  48. [['explain'], 'string', 'max' => 5000],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => 'ID',
  58. 'mall_id' => '商城id',
  59. 'level' => '等级权重id',
  60. 'name' => '等级名称',
  61. 'checked_condition_values' => '升级条件',
  62. 'checked_condition_keys' => '升级条件选中的key集合',
  63. 'condition_type' => '0 未选择、1满足其一 2、满足所有',
  64. 'upgrade_type_condition' => '是否开启条件升级',
  65. 'is_over' => '超越奖励',
  66. 'is_equal' => '平级',
  67. 'is_fill' => '补货',
  68. 'give_quota' => 'json;赠送等级配置',
  69. 'explain' => '等级说明',
  70. 'status' => '状态[-1:删除;0:禁用;1启用]',
  71. 'created_at' => 'Created At',
  72. 'updated_at' => 'Updated At',
  73. 'is_open_fill_min' => '代理进货数量限制',
  74. 'fill_min' => '最低补货数量',
  75. 'commission' => '团队佣金奖励',
  76. 'score' => '团队积分奖励',
  77. 'is_open_equal_settle_type' => '平级奖独立设置',
  78. 'equal_settle_type' => '平级奖结算方式',
  79. 'is_recommender_reward' => '直推下级代理奖励设置',
  80. 'agent_setting_info' => '直推下级代理奖励设置',
  81. 'is_recommender_condition' => '直推下级代理奖条件',
  82. 'recommender_condition_prize' => '直推下级代理奖条件',
  83. 'recommender_settle_prize' => '结算比例',
  84. 'recommender_settle_rate' => '结算比例',
  85. ];
  86. }
  87. public static function setData(array $params)
  88. {
  89. try {
  90. if(!empty($params['id'])){
  91. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  92. if (empty($model)) throw new \Exception('服务不存在或已删除');
  93. if($params['status']==StatusEnum::DELETE){
  94. $res = CloudStockTwoAgent::findOne(['mall_id'=> $params['mall_id'],'level'=>$model->level,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  95. if($res) throw new \Exception('该等级关联了代理商');
  96. }
  97. }else{
  98. $model = new self();
  99. $model->loadDefaultValues();
  100. }
  101. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  102. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  103. return $model;
  104. } catch (\Exception $e) {
  105. self::$static_error = $e->getMessage();
  106. return false;
  107. }
  108. }
  109. /**
  110. * @name:
  111. * @msg: 获取云库存代理商等级列表
  112. * @param {int} $mall_id 商城id
  113. * @param {int} $level 等级
  114. * @return array
  115. */
  116. public static function getLevelList(int $mall_id, int $level = null) : array
  117. {
  118. if (!empty(self::$levels)) return self::$levels;
  119. $levels = self::find()
  120. ->select('id,mall_id,level,name as level_name,give_quota,created_at')
  121. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
  122. ->orderBy('level asc')
  123. ->asArray()
  124. ->indexBy('level')
  125. ->all();
  126. if (empty($levels)) return [];
  127. self::$levels = $levels;
  128. if ($level !== null) {
  129. $levels = $levels[$level] ?? [];
  130. }
  131. return $levels;
  132. }
  133. /**
  134. * @name:
  135. * @msg: 获取特定等级设置代理商的名额配置
  136. * @param {int} $mall_id 商城 id
  137. * @param {int} $level 等级
  138. * @return int
  139. */
  140. public static function getGiveQuota(int $mall_id, int $level, int $user_id) : int
  141. {
  142. $give_quota = CloudStockTwoAgentQuota::find()
  143. ->select('id,mall_id,user_id,level,num,status')
  144. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'level' => $level, 'user_id' => $user_id])
  145. ->asArray()
  146. ->one();
  147. if (empty($give_quota)) {
  148. $number = 0;
  149. } else {
  150. $number = $give_quota['num'];
  151. }
  152. return $number;
  153. }
  154. }