ShareLevelFrom.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\Share;
  4. use app\models\ShareLevel;
  5. use yii\base\Model;
  6. class ShareLevelFrom extends Model
  7. {
  8. public $id;
  9. public $name;
  10. public $level;
  11. public $is_auto_upgrade; //是否自动升级
  12. public $type; //升级条件
  13. public $value; //升级条件值
  14. public $profit_type; //分销佣金类型
  15. public $first_profit;
  16. public $second_profit;
  17. public $third_profit;
  18. public $status;
  19. public $desc;
  20. public $store_id;
  21. public function rules()
  22. {
  23. return [
  24. [['id', 'level', 'is_auto_upgrade', 'type', 'profit_type', 'status', 'store_id'], 'integer'],
  25. [['value', 'first_profit', 'second_profit', 'third_profit'], 'number'],
  26. [['name', 'desc'], 'string']
  27. ];
  28. }
  29. public function attributeLabels()
  30. {
  31. return [
  32. "id" => "ID",
  33. "name" => "名称",
  34. "level" => "等级",
  35. "is_auto_upgrade" => "是否自动升级",
  36. "type" => "升级条件类型",
  37. "value" => "升级条件值",
  38. "profit_type" => "分销佣金类型",
  39. "first_profit" => "一级佣金",
  40. "second_profit" => "二级佣金",
  41. "third_profit" => "三级佣金",
  42. "status" => "启用状态",
  43. "desc" => "等级说明",
  44. ];
  45. }
  46. /**
  47. * 保存
  48. */
  49. public function setShareLevel() {
  50. try {
  51. $id = $this->id;
  52. $store_id = $this->store_id;
  53. $type = intval($this->type);
  54. $status = intval($this->status);
  55. $profit_type = intval($this->profit_type);
  56. $is_auto_upgrade = intval($this->is_auto_upgrade);
  57. $share_level = ShareLevel::findOne($id);
  58. if (!$share_level) {
  59. $share_level = new ShareLevel();
  60. $share_level->store_id = $store_id;
  61. }
  62. $share_level->name = $this->name;
  63. $share_level->level = $this->level;
  64. $share_level->is_auto_upgrade = $is_auto_upgrade;
  65. $share_level->type = $type;
  66. $share_level->value = $this->value;
  67. $share_level->profit_type = $profit_type;
  68. $share_level->first_profit = (float)$this->first_profit;
  69. $share_level->second_profit = (float)$this->second_profit;
  70. $share_level->third_profit = (float)$this->third_profit;
  71. $share_level->status = $status;
  72. $share_level->desc = $this->desc;
  73. if (!$share_level->save()) {
  74. throw new \Exception(json_encode($share_level->errors, JSON_UNESCAPED_UNICODE));
  75. }
  76. return [
  77. 'code' => 0,
  78. 'msg' => '设置成功'
  79. ];
  80. } catch (\Exception $e) {
  81. return [
  82. 'code' => 1,
  83. 'msg' => $e->getMessage()
  84. ];
  85. }
  86. }
  87. /**
  88. * 列表
  89. */
  90. public function list() {
  91. try {
  92. $name = $this->name;
  93. $status = (int)$this->status;
  94. $store_id = $this->store_id;
  95. $query = ShareLevel::find()->where(['store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE]);
  96. $level_query = clone $query;
  97. if ($name) {
  98. $query->andWhere(['LIKE', 'name', $name]);
  99. }
  100. if ($status && in_array($status, ShareLevel::$valid_status_arr)) {
  101. $query->andWhere(['status' => $status]);
  102. }
  103. // $query->select('id, name, level, type, value, first_profit, second_profit, third_profit, status');
  104. $list = pagination_make($query);
  105. foreach ($list['list'] as &$item) {
  106. $item['type'] = (int)$item['type'];
  107. $item['status'] = (int)$item['status'];
  108. $item['profit_type'] = (int)$item['profit_type'];
  109. $item['is_auto_upgrade'] = (int)$item['is_auto_upgrade'];
  110. }
  111. $level = $level_query->select('level')->column();
  112. $level = array_values(array_diff(range('1', '99'), $level));
  113. return [
  114. 'code' => 0,
  115. 'msg' => 'success',
  116. 'data' => [
  117. 'data' => $list['list'],
  118. 'level' => $level,
  119. 'pageNo' => $list['pageNo'],
  120. 'totalCount' => $list['totalCount'],
  121. ]
  122. ];
  123. } catch (\Exception $e) {
  124. return [
  125. 'code' => 1,
  126. 'msg' => $e->getMessage()
  127. ];
  128. }
  129. }
  130. public function setStatus() {
  131. try {
  132. $id = $this->id;
  133. $status = (int)$this->status;
  134. $store_id = $this->store_id;
  135. $share_level = ShareLevel::findOne(['id' => $id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'store_id' => $store_id]);
  136. if (!$share_level) {
  137. throw new \Exception('查询失败');
  138. }
  139. if ($this->status === null) {
  140. throw new \Exception('状态错误');
  141. }
  142. if ($status == ShareLevel::STATUS_OFF) {
  143. $share = Share::findOne(['status' => 1, 'store_id' => $this->store_id, 'is_delete' => 0, 'level' => $share_level->level]);
  144. if ($share) {
  145. throw new \Exception('该等级下有分销商,不可禁用');
  146. }
  147. }
  148. if (in_array($status, ShareLevel::$valid_status_arr)) {
  149. $share_level->status = $status;
  150. } else {
  151. $share = Share::findOne(['status' => 1, 'store_id' => $this->store_id, 'is_delete' => 0, 'level' => $share_level->level]);
  152. if ($share) {
  153. throw new \Exception('该等级下有分销商,不可禁用');
  154. }
  155. $share_level->is_delete = ShareLevel::SHARE_IS_DELETE;
  156. }
  157. if (!$share_level->save()) {
  158. throw new \Exception('修改状态失败');
  159. }
  160. return [
  161. 'code' => 0,
  162. 'msg' => '修改成功'
  163. ];
  164. } catch (\Exception $e) {
  165. return [
  166. 'code' => 1,
  167. 'msg' => $e->getMessage()
  168. ];
  169. }
  170. }
  171. }