ProfitReceiverForm.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\ProfitSharingReceiver;
  9. use app\utils\Wechat\WechatShare;
  10. use yii\base\Model;
  11. class ProfitReceiverForm extends Model
  12. {
  13. public $id;
  14. public $type;
  15. public $account;
  16. public $name;
  17. public $description;
  18. public $ratio;
  19. public $search_key;
  20. public $max_amount;
  21. public static $valid_type = [
  22. [
  23. 'type' => 1,
  24. 'type_flag' => WechatShare::RECEIVE_PERSONAL_OPENID,
  25. ],
  26. [
  27. 'type' => 2,
  28. 'type_flag' => WechatShare::RECEIVE_MERCHANT_ID,
  29. ],
  30. [
  31. 'type' => 3,
  32. 'type_flag' => WechatShare::RECEIVE_PERSONAL_SUB_OPENID,
  33. ]
  34. ];
  35. public function rules()
  36. {
  37. return [
  38. [['type'], 'integer'],
  39. [['name', 'description', 'account', 'id'], 'string', 'max' => 255],
  40. [['name', 'description', 'account'], 'trim'],
  41. ];
  42. }
  43. /**
  44. * @return array
  45. * @throws \yii\db\Exception
  46. */
  47. public function save()
  48. {
  49. if (!$this->validate()) {
  50. return [
  51. 'code' => 1,
  52. 'msg' => $this->getErrorSummary(false)[0]
  53. ];
  54. }
  55. if (!in_array($this->type, array_column(self::$valid_type, 'type'))) {
  56. return [
  57. 'code' => 1,
  58. 'msg' => '接收方类型错误'
  59. ];
  60. }
  61. $model = ProfitSharingReceiver::findOne($this->id);
  62. if (!$model) {
  63. $model = new ProfitSharingReceiver();
  64. $model->created_at = time();
  65. $model->store_id = get_store_id();
  66. }
  67. $model->type = $this->type;
  68. $model->account = $this->account;
  69. $model->name = $this->name;
  70. $model->max_amount = $this->max_amount;
  71. $model->ratio = $this->ratio;
  72. $model->description = $this->description;
  73. if (!$model->save()) {
  74. return [
  75. 'code' => 1,
  76. 'msg' => $model->errors[0]
  77. ];
  78. }
  79. return [
  80. 'code' => 0,
  81. 'msg' => '保存成功'
  82. ];
  83. }
  84. public function search()
  85. {
  86. $query = ProfitSharingReceiver::find()->where(['store_id' => get_store_id(), 'is_delete' => 0]);
  87. if (!empty($this->search_key)) {
  88. $query->andWhere(['like', 'name', $this->search_key]);
  89. }
  90. $list = pagination_make($query);
  91. foreach ($list['list'] as &$val) {
  92. $val['type'] = intval($val['type']);
  93. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  94. $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
  95. }
  96. return [
  97. 'code' => 0,
  98. 'msg' => 'success',
  99. 'data' => [
  100. 'data' => $list['list'],
  101. 'pageNo' => $list['pageNo'],
  102. 'totalCount' => $list['totalCount'],
  103. 'type' => self::$valid_type
  104. ]
  105. ];
  106. }
  107. }