CloudStockLevel.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace addons\CloudStock\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 CloudStockLevel extends BaseActiveRecord
  29. {
  30. private static $levels = [];
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%addons_cloud_stock_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_fill_min_once','fill_min_once','fill_max_once', 'upgrade_type_goods', 'upgrade_type_store_goods'], 'integer'],
  46. [['checked_condition_values', 'checked_condition_keys', 'give_quota', 'upgrade_goods_list', 'upgrade_store_goods_list'], 'string'],
  47. [['name'], 'string', 'max' => 255],
  48. [['explain'], 'string', 'max' => 5000],
  49. [['detail'], 'safe'],
  50. ];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'id' => 'ID',
  59. 'mall_id' => '商城id',
  60. 'level' => '等级权重id',
  61. 'name' => '等级名称',
  62. 'checked_condition_values' => '升级条件',
  63. 'checked_condition_keys' => '升级条件选中的key集合',
  64. 'condition_type' => '0 未选择、1满足其一 2、满足所有',
  65. 'upgrade_type_condition' => '是否开启条件升级',
  66. 'is_over' => '超越奖励',
  67. 'is_equal' => '平级',
  68. 'is_fill' => '补货',
  69. 'give_quota' => 'json;赠送等级配置',
  70. 'explain' => '等级说明',
  71. 'status' => '状态[-1:删除;0:禁用;1启用]',
  72. 'created_at' => 'Created At',
  73. 'updated_at' => 'Updated At',
  74. 'is_open_fill_min' => '代理进货数量限制',
  75. 'fill_min' => '最低补货数量',
  76. 'is_open_fill_min_once' => '代理一次性提货数量限制',
  77. 'fill_min_once' => '一次性提货最低数量',
  78. 'fill_max_once' => '一次性提货最高数量',
  79. 'upgrade_type_goods' => '是否开启购买商品升级',
  80. 'upgrade_goods_list' => '商品升级条件',
  81. 'upgrade_type_store_goods' => '是否开启购买门店商品升级',
  82. 'upgrade_store_goods_list' => '门店商品升级条件',
  83. ];
  84. }
  85. public static function setData(array $params)
  86. {
  87. try {
  88. if(!empty($params['id'])){
  89. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  90. if (empty($model)) throw new \Exception('服务不存在或已删除');
  91. if($params['status']==StatusEnum::DELETE){
  92. $res = CloudStockAgent::findOne(['mall_id'=> $params['mall_id'],'level'=>$model->level,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  93. if($res) throw new \Exception('该等级关联了代理商');
  94. }
  95. }else{
  96. $model = new self();
  97. $model->loadDefaultValues();
  98. }
  99. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  100. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  101. return $model;
  102. } catch (\Exception $e) {
  103. self::$static_error = $e->getMessage();
  104. return false;
  105. }
  106. }
  107. /**
  108. * @name:
  109. * @msg: 获取云库存代理商等级列表
  110. * @param {int} $mall_id 商城id
  111. * @param {int} $level 等级
  112. * @return array
  113. */
  114. public static function getLevelList(int $mall_id, int $level = null) : array
  115. {
  116. if (!empty(self::$levels)) return self::$levels;
  117. $levels = self::find()
  118. ->select('id,mall_id,level,name as level_name,give_quota,created_at')
  119. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
  120. ->orderBy('level asc')
  121. ->asArray()
  122. ->indexBy('level')
  123. ->all();
  124. if (empty($levels)) return [];
  125. self::$levels = $levels;
  126. if ($level !== null) {
  127. $levels = $levels[$level] ?? [];
  128. }
  129. return $levels;
  130. }
  131. /**
  132. * @name:
  133. * @msg: 获取特定等级设置代理商的名额配置
  134. * @param {int} $mall_id 商城 id
  135. * @param {int} $level 等级
  136. * @return int
  137. */
  138. public static function getGiveQuota(int $mall_id, int $level, int $user_id) : int
  139. {
  140. $give_quota = CloudStockAgentQuota::find()
  141. ->select('id,mall_id,user_id,level,num,status')
  142. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'level' => $level, 'user_id' => $user_id])
  143. ->asArray()
  144. ->one();
  145. if (empty($give_quota)) {
  146. $number = 0;
  147. } else {
  148. $number = $give_quota['num'];
  149. }
  150. return $number;
  151. }
  152. public static function getMap($mall_id)
  153. {
  154. $data = [];
  155. $list = self::find()->select('level,name')->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]])->orderBy('level asc')->asArray()->all();
  156. if ($list) {
  157. foreach ($list as $item) {
  158. $data[$item['level']] = $item['name'];
  159. }
  160. }
  161. return $data;
  162. }
  163. public static function getListByUpgrade($params)
  164. {
  165. $rows = [];
  166. if (!empty($params['mall_id'])) {
  167. $where = ['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED];
  168. $list = self::find()->where($where)->orderBy('level asc')->all();
  169. foreach ($list as $v) {
  170. $rows[] = ['level' => $v['level'], 'name' => $v['name']];
  171. }
  172. }
  173. return $rows;
  174. }
  175. }