LevelForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Level;
  9. use app\models\User;
  10. use yii\base\Model;
  11. use yii\helpers\Json;
  12. class LevelForm extends Model
  13. {
  14. public $store_id;
  15. public $keyword;
  16. public $model;
  17. public $level;
  18. public $name;
  19. public $money;
  20. public $status;
  21. public $promotion_status;
  22. public $discount;
  23. public $content;
  24. public $price;
  25. public $image;
  26. public $buy_prompt;
  27. public $detail;
  28. public $synopsis;
  29. public $fybili;
  30. public $firstProfit;
  31. public $secondProfit;
  32. public $thirdProfit;
  33. public $expires_type;
  34. public $expires_in;
  35. public $pages_css;
  36. public $is_forever;
  37. public $upgrade_conditions;
  38. public $growth_value;
  39. public $growth_refund_value;
  40. public $downgrade_status;
  41. public $integral_enlarge;
  42. public $rules_text;
  43. public $id;
  44. public function rules()
  45. {
  46. return [
  47. [['name', 'money', 'synopsis', 'keyword'], 'trim'],
  48. [['name', 'image', 'keyword'], 'string'],
  49. [['level', 'name', 'status', 'pages_css', 'is_forever', 'upgrade_conditions','discount', 'integral_enlarge', 'promotion_status', 'rules_text'], 'required', 'on'=>'edit'],
  50. [['status','is_forever','expires_type', 'promotion_status', 'downgrade_status'], 'in', 'range'=>[0, 1]],
  51. [['upgrade_conditions'], 'safe'],
  52. [['discount'], 'number', 'min' => 0.1, 'max' => 10],
  53. [['money', 'price', 'fybili', 'growth_value', 'growth_refund_value'], 'number', 'min' => 0, 'max' => 99999999],
  54. [['level'], 'integer', 'min' => 0, 'max' => 100],
  55. [['content'], 'required', 'on' => 'content'],
  56. [['name', 'detail', 'buy_prompt'], 'string', 'max' => 255],
  57. [['firstProfit', 'secondProfit', 'thirdProfit'], 'number'],
  58. [['expires_in'], 'integer'],
  59. ['integral_enlarge','default','value'=>1]
  60. ];
  61. }
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'level'=>'会员等级',
  66. 'name'=>'等级名称',
  67. 'money'=>'升级条件',
  68. 'status'=>'状态',
  69. 'discount'=>'折扣',
  70. 'content'=>'会员等级说明',
  71. 'price'=>'升级所需价格',
  72. 'image'=>'会员图片',
  73. 'buy_prompt'=>'购买前显示',
  74. 'detail'=>'会员说明',
  75. 'synopsis' => '小标题标题',
  76. 'fybili' => '返佣加速比例',
  77. 'firstProfit' => '一级分销金额',
  78. 'secondProfit' => '二级分销金额',
  79. 'thirdProfit' => '三级分销金额',
  80. 'expires_type' => '到期类型',
  81. 'expires_in' => '会员有效期时间'
  82. ];
  83. }
  84. public function search()
  85. {
  86. if (!$this->validate()) {
  87. return [
  88. 'code' => 1,
  89. 'msg' => '参数错误'
  90. ];
  91. }
  92. $query = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => Level::NOT_DELETE]);
  93. if ($this->name) {
  94. $query->andWhere(['like', 'name', $this->name]);
  95. }
  96. $query->select(['id','level','discount','money','price','growth_value','upgrade_conditions','status','is_student','growth_value','name','created_at']);
  97. $pagination = pagination_make($query,true, 'level DESC');
  98. $list = $pagination['list'];
  99. foreach ($list as &$value) {
  100. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  101. $value['upgrade_conditions'] = json_decode($value['upgrade_conditions'],2);
  102. $value['upgrade_conditions_text'] = [];
  103. if(in_array(Level::UPGRADE_CONDITIONS_MONEY,$value['upgrade_conditions']) && !empty($value['money'])){
  104. $value['upgrade_conditions_text'][] = Level::UPGRADE_CONDITIONS_MAP[Level::UPGRADE_CONDITIONS_MONEY]. ':' .$value['money'];
  105. }
  106. if(in_array(Level::UPGRADE_CONDITIONS_GROWTH,$value['upgrade_conditions']) && !empty($value['growth_value'])){
  107. $value['upgrade_conditions_text'][] = Level::UPGRADE_CONDITIONS_MAP[Level::UPGRADE_CONDITIONS_GROWTH]. ':' .$value['growth_value'];
  108. }
  109. if(in_array(Level::UPGRADE_CONDITIONS_PRICE,$value['upgrade_conditions']) && !empty($value['price'])){
  110. $value['upgrade_conditions_text'][] = Level::UPGRADE_CONDITIONS_MAP[Level::UPGRADE_CONDITIONS_PRICE]. ':' .$value['price'];
  111. }
  112. }
  113. return [
  114. 'code' => 0,
  115. 'msg' => 'success',
  116. 'data' => [
  117. 'data' => $list,
  118. 'pageNo' => $pagination['pageNo'],
  119. 'totalCount' => $pagination['totalCount']
  120. ],
  121. ];
  122. }
  123. /**
  124. * 获取单条会员等级数据
  125. * @author: hankaige
  126. * @Time: 2025/4/16 15:48
  127. */
  128. public function getLevelItem(){
  129. if(empty($this->id)){
  130. return ['code' => 1,'msg' => 'ID 不能为空'];
  131. }
  132. $level = Level::findOne($this->id);
  133. if(empty($level)){
  134. return ['code' => 1,'msg' => '会员等级不存在'];
  135. }
  136. $level->pages_css = json_decode($level->pages_css,true);
  137. $level->upgrade_conditions = json_decode($level->upgrade_conditions,true);
  138. // 兼容一下老数据 之前老数据没有设置购买金额的时候 取消选中购买升级的条件
  139. if(empty($level->price) && in_array(Level::UPGRADE_CONDITIONS_PRICE,$level->upgrade_conditions)){
  140. $level->upgrade_conditions = array_diff($level->upgrade_conditions,[Level::UPGRADE_CONDITIONS_PRICE]);
  141. }
  142. return [
  143. 'code' => 0,
  144. 'msg' => 'success',
  145. 'data' => $level
  146. ];
  147. }
  148. public function getAllLevel()
  149. {
  150. $list = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => Level::NOT_DELETE])
  151. ->select('id, level, name, discount')
  152. ->orderBy('level')
  153. ->asArray()->all();
  154. return $list;
  155. }
  156. public function getAllLevelList()
  157. {
  158. $list = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => Level::NOT_DELETE])
  159. ->select('id, level, name, discount')
  160. ->orderBy('level')
  161. ->asArray()->all();
  162. return [
  163. 'code' => 0,
  164. 'msg' => 'success',
  165. 'data' => $list,
  166. ];
  167. }
  168. /**
  169. * @return array
  170. */
  171. public function save()
  172. {
  173. if (!$this->validate()) {
  174. return [
  175. 'code' => 1,
  176. 'msg' => '参数错误',
  177. 'data' => $this->getErrors()
  178. ];
  179. }
  180. $this->upgrade_conditions = json_decode($this->upgrade_conditions);
  181. if ($this->model->isNewRecord) {
  182. $this->model->is_delete = Level::NOT_DELETE;
  183. $this->model->created_at = time();
  184. }
  185. if ($this->level != $this->model->level) {
  186. $exit = Level::find()->where(['level' => $this->level, 'store_id' => $this->store_id, 'is_delete' => Level::NOT_DELETE])->exists();
  187. if ($exit) {
  188. return [
  189. 'code' => 1,
  190. 'msg' => '会员等级已存在'
  191. ];
  192. }
  193. }
  194. if ($this->name != $this->model->name) {
  195. $exit_0 = Level::find()->where(['name' => $this->name, 'store_id' => $this->store_id,
  196. 'is_delete' => Level::NOT_DELETE])->exists();
  197. if ($exit_0) {
  198. return [
  199. 'code' => 1,
  200. 'msg' => '等级名称重复'
  201. ];
  202. }
  203. }
  204. if($this->model->id && $this->model->level != $this->level) {
  205. $count = User::find()->where(['store_id' => $this->store_id, 'level' => $this->model->level])->count();
  206. if($count > 0) {
  207. return [
  208. 'code' => 1,
  209. 'msg' => '当前会员等级下有会员,禁止修改会员等级'
  210. ];
  211. }
  212. }
  213. if(in_array(Level::UPGRADE_CONDITIONS_MONEY,$this->upgrade_conditions) && $this->money <= 0){
  214. return [
  215. 'code' => 1,
  216. 'msg' => '累计消费金额升级条件不能小于0'
  217. ];
  218. }
  219. if(in_array(Level::UPGRADE_CONDITIONS_GROWTH,$this->upgrade_conditions) && $this->growth_value <= 0){
  220. return [
  221. 'code' => 1,
  222. 'msg' => '成长值升级条件不能小于0'
  223. ];
  224. }
  225. if (in_array(Level::UPGRADE_CONDITIONS_PRICE,$this->upgrade_conditions) && ($this->firstProfit > 0 || $this->secondProfit > 0 || $this->thirdProfit > 0) && floatval($this->price) <= 0) {
  226. return [
  227. 'code' => 1,
  228. 'msg' => '需要输入会员购买价格'
  229. ];
  230. }
  231. if(in_array(Level::UPGRADE_CONDITIONS_GROWTH,$this->upgrade_conditions) && $this->downgrade_status == 1 && $this->growth_refund_value <= 0){
  232. return [
  233. 'code' => 1,
  234. 'msg' => '开启会员会退后需设置每日消耗成长值数量'
  235. ];
  236. }
  237. if($this->is_forever == 0 && $this->expires_in == 0){
  238. return [
  239. 'code' => 1,
  240. 'msg' => '有效时间不能为空'
  241. ];
  242. }
  243. $this->model->store_id = $this->store_id;
  244. $this->model->level = $this->level;
  245. $this->model->name = $this->name;
  246. $this->model->pages_css = $this->pages_css;
  247. $this->model->status = $this->status;
  248. $this->model->is_forever = $this->is_forever;
  249. $this->model->expires_type = $this->expires_type;
  250. // 如果设置的是永久 将有效期设置为0 会员等级变动时候 会员有效期计算代码就不用做修改了
  251. $this->model->expires_in = $this->is_forever == 1 ? 0 : $this->expires_in;
  252. $this->model->upgrade_conditions = json_encode($this->upgrade_conditions);// 这个字段废弃不用了
  253. $this->model->money = $this->money;
  254. $this->model->growth_value = $this->growth_value;
  255. $this->model->downgrade_status = $this->downgrade_status;
  256. $this->model->growth_refund_value = $this->downgrade_status == 1 ? $this->growth_refund_value : 0;
  257. $this->model->price = $this->price;
  258. $this->model->discount = $this->discount;
  259. $this->model->integral_enlarge = $this->integral_enlarge ?? 1;// 默认为1倍
  260. $this->model->promotion_status = $this->promotion_status ?? 0;
  261. $this->model->firstProfit = $this->firstProfit;
  262. $this->model->secondProfit = $this->secondProfit;
  263. $this->model->thirdProfit = $this->thirdProfit;
  264. $this->model->synopsis = $this->synopsis ? $this->synopsis : '';
  265. $this->model->rules_text = $this->rules_text ? $this->rules_text : '';
  266. // $this->model->image = $this->image;
  267. // $this->model->buy_prompt = $this->buy_prompt;
  268. // $this->model->detail = $this->detail;
  269. // $this->model->fybili = $this->fybili;
  270. if ($this->model->save()) {
  271. return [
  272. 'code' => 0,
  273. 'msg' => '成功'
  274. ];
  275. } else {
  276. return [
  277. 'code' => 1,
  278. 'msg' => '创建失败',
  279. 'data' => $this->model->getErrors()
  280. ];
  281. }
  282. }
  283. // /**
  284. // * 会员等级说明
  285. // * @return array|mixed
  286. // */
  287. // public function saveContent()
  288. // {
  289. // if (!$this->validate()) {
  290. // return $this->errorResponse;
  291. // }
  292. //
  293. // $store = Store::findOne(['id'=>$this->store_id]);
  294. // $store->member_content = $this->content;
  295. //
  296. // if ($store->save()) {
  297. // return [
  298. // 'code' => 0,
  299. // 'msg' => '成功'
  300. // ];
  301. // } else {
  302. // return $this->getErrorResponse($store);
  303. // }
  304. // }
  305. }