StoreOperationsForm.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\SaasUser;
  4. use app\models\StoreOperations;
  5. use yii\base\Model;
  6. class StoreOperationsForm extends Model
  7. {
  8. public $user_name;
  9. public $mobile;
  10. public $start_time;
  11. public $end_time;
  12. public $status;
  13. public $id;
  14. public $saas_id;
  15. public $remark;
  16. public function rules()
  17. {
  18. return [
  19. [['user_name', 'mobile', 'start_time', 'end_time', 'remark'], 'string'],
  20. [['status', 'saas_id'], 'integer'],
  21. [['id'], 'safe']
  22. ];
  23. }
  24. public function list() {
  25. $user_name = $this->user_name;
  26. $mobile = $this->mobile;
  27. $start_time = $this->start_time;
  28. $end_time = $this->end_time;
  29. $status = $this->status;
  30. $query = StoreOperations::find()->alias('so')
  31. ->leftJoin(['su' => SaasUser::tableName()], 'su.id = so.saas_id')
  32. ->where(['su.is_delete' => 0, 'so.is_delete' => 0]);
  33. if ($user_name) {
  34. $query->andWhere(['LIKE', 'su.name', $user_name]);
  35. }
  36. if ($mobile) {
  37. $query->andWhere(['LIKE', 'su.mobile', $mobile]);
  38. }
  39. if ($start_time) {
  40. $start_time = strtotime($start_time);
  41. $query->andWhere(['>=', 'so.created_at', $start_time]);
  42. }
  43. if ($end_time) {
  44. $end_time = strtotime($end_time);
  45. $query->andWhere(['<=', 'so.created_at', $end_time]);
  46. }
  47. if (isset($status) && $status >= 0) {
  48. $query->andWhere(['so.status' => $status]);
  49. }
  50. $query->select('so.id, su.name, su.mobile, su.avatar, so.status, so.created_at, so.remark')->orderBy('so.id desc');
  51. $list = pagination_make($query);
  52. foreach ($list['list'] as &$item) {
  53. $item['status'] = (int)$item['status'];
  54. if (!empty($item['created_at'])) {
  55. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  56. }
  57. }
  58. return [
  59. 'code' => 0,
  60. 'msg' => '获取成功',
  61. 'data' => $list
  62. ];
  63. }
  64. public function save() {
  65. try {
  66. $status = $this->status;
  67. $saas_id = $this->saas_id;
  68. $remark = $this->remark;
  69. $saas_user = SaasUser::findOne($saas_id);
  70. if (!$saas_user) {
  71. throw new \Exception('联盟用户查询失败');
  72. }
  73. $storeOperations = StoreOperations::findOne(['saas_id' => $saas_id, 'is_delete' => 0]);
  74. if ($storeOperations) {
  75. throw new \Exception('运营人员已存在');
  76. }
  77. $storeOperations = new StoreOperations();
  78. $storeOperations->saas_id = $saas_id;
  79. $storeOperations->remark = $remark;
  80. $storeOperations->status = $status;
  81. if (!$storeOperations->save()) {
  82. throw new \Exception(json_encode($storeOperations->errors, JSON_UNESCAPED_UNICODE));
  83. }
  84. return [
  85. 'code' => 0,
  86. 'msg' => '添加成功'
  87. ];
  88. } catch (\Exception $e) {
  89. return [
  90. 'code' => 1,
  91. 'msg' => $e->getMessage()
  92. ];
  93. }
  94. }
  95. public function setStatus() {
  96. try {
  97. $id = explode(',', $this->id);
  98. $status = $this->status;
  99. if (empty($id)) {
  100. throw new \Exception('参数错误');
  101. }
  102. //修改状态
  103. if (in_array($status, [0, 1])) {
  104. StoreOperations::updateAll(['status' => $status], ['id' => $id]);
  105. } else {
  106. //删除
  107. StoreOperations::updateAll(['is_delete' => 1], ['id' => $id]);
  108. }
  109. return [
  110. 'code' => 0,
  111. 'msg' => '操作成功'
  112. ];
  113. } catch (\Exception $e) {
  114. return [
  115. 'code' => 1,
  116. 'msg' => $e->getMessage()
  117. ];
  118. }
  119. }
  120. }