ReservationServiceLevel.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace addons\ReservationService\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%addons_reservation_service_level}}".
  7. *
  8. * @property int $id ID
  9. * @property int $mall_id 商城ID
  10. * @property string $name 名称
  11. * @property int $level 等级
  12. * @property float $item_commission 项目佣金
  13. * @property int $item_commission_type 0百分比;1固定值
  14. * @property int $enable_distribution_commission 享受分销提成奖励; 0关闭,1开启
  15. * @property float $level1_commission 一级佣金
  16. * @property int $level1_commission_type 0百分比;1固定值
  17. * @property float $level2_commission 一级佣金
  18. * @property int $level2_commission_type 0百分比;1固定值
  19. * @property int $status 状态;1正常,0禁用,-1删除
  20. * @property int $created_at 创建时间
  21. * @property int $updated_at 更新时间
  22. */
  23. class ReservationServiceLevel extends \common\models\base\BaseActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%addons_reservation_service_level}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id', 'level', 'item_commission_type', 'enable_distribution_commission', 'level1_commission_type', 'level2_commission_type', 'status', 'created_at', 'updated_at'], 'integer'],
  39. [['item_commission', 'level1_commission', 'level2_commission'], 'number'],
  40. [['name'], 'string', 'max' => 64],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => '商城ID',
  51. 'name' => '名称',
  52. 'level' => '等级',
  53. 'item_commission' => '项目佣金',
  54. 'item_commission_type' => '0百分比;1固定值',
  55. 'enable_distribution_commission' => '享受分销提成奖励; 0关闭,1开启',
  56. 'level1_commission' => '一级佣金',
  57. 'level1_commission_type' => '0百分比;1固定值',
  58. 'level2_commission' => '一级佣金',
  59. 'level2_commission_type' => '0百分比;1固定值',
  60. 'status' => '状态;1正常,0禁用,-1删除',
  61. 'created_at' => '创建时间',
  62. 'updated_at' => '更新时间',
  63. ];
  64. }
  65. public static function getLevelWeights($mall_id)
  66. {
  67. $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
  68. $newList = [];
  69. for ($i = 1; $i <= 10; $i++) {
  70. $newList[] = [
  71. 'name' => '等级' . $i,
  72. 'level' => $i,
  73. 'disabled' => in_array($i, $list),
  74. ];
  75. }
  76. return $newList;
  77. }
  78. public static function setData(array $params)
  79. {
  80. try {
  81. if (!empty($params['id'])) {
  82. if (!empty(self::getOne([['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]],
  83. ['level' => $params['level']], ['mall_id' => $params['mall_id']], ['<>', 'id', $params['id']]]))) throw new \Exception("当前等级已经存在");
  84. $model = self::findOne(['and', ['id' => $params['id'], 'mall_id' => $params['mall_id']], ['<>', 'status', StatusEnum::DELETE]]);
  85. if (empty($model)) throw new \Exception('等级不存在或已删除');
  86. // 判断等级是否有被修改
  87. if ($model->level != $params['level']) {
  88. // 判断该等级下是否有用户存在
  89. $boss = ReservationServiceUser::find()->where(['mall_id' => $model->mall_id, 'level' => $model->level, 'status' => StatusEnum::ENABLED])->asArray()->one();
  90. if ($boss) throw new \Exception('该等级下存在技师,不能被修改');
  91. }
  92. } else {
  93. // 判断当前等级是否已经存在
  94. if (!empty(self::getOne([['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]], ['mall_id' => $params['mall_id']], ['level' => $params['level']]]))) throw new \Exception("当前等级已经存在");
  95. $model = new self();
  96. $model->loadDefaultValues();
  97. $model->mall_id = $params['mall_id'];
  98. }
  99. if (!$model->load($params, '') || !$model->save()) {
  100. throw new \Exception($model->getErrorMessage());
  101. }
  102. return $model;
  103. } catch (\Exception $e) {
  104. self::$static_error = $e->getMessage();
  105. return false;
  106. }
  107. }
  108. }