| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace app\modules\admin\models;
- use app\models\SaasUser;
- use app\models\StoreOperations;
- use yii\base\Model;
- class StoreOperationsForm extends Model
- {
- public $user_name;
- public $mobile;
- public $start_time;
- public $end_time;
- public $status;
- public $id;
- public $saas_id;
- public $remark;
- public function rules()
- {
- return [
- [['user_name', 'mobile', 'start_time', 'end_time', 'remark'], 'string'],
- [['status', 'saas_id'], 'integer'],
- [['id'], 'safe']
- ];
- }
- public function list() {
- $user_name = $this->user_name;
- $mobile = $this->mobile;
- $start_time = $this->start_time;
- $end_time = $this->end_time;
- $status = $this->status;
- $query = StoreOperations::find()->alias('so')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.id = so.saas_id')
- ->where(['su.is_delete' => 0, 'so.is_delete' => 0]);
- if ($user_name) {
- $query->andWhere(['LIKE', 'su.name', $user_name]);
- }
- if ($mobile) {
- $query->andWhere(['LIKE', 'su.mobile', $mobile]);
- }
- if ($start_time) {
- $start_time = strtotime($start_time);
- $query->andWhere(['>=', 'so.created_at', $start_time]);
- }
- if ($end_time) {
- $end_time = strtotime($end_time);
- $query->andWhere(['<=', 'so.created_at', $end_time]);
- }
- if (isset($status) && $status >= 0) {
- $query->andWhere(['so.status' => $status]);
- }
- $query->select('so.id, su.name, su.mobile, su.avatar, so.status, so.created_at, so.remark')->orderBy('so.id desc');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['status'] = (int)$item['status'];
- if (!empty($item['created_at'])) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- }
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $list
- ];
- }
- public function save() {
- try {
- $status = $this->status;
- $saas_id = $this->saas_id;
- $remark = $this->remark;
- $saas_user = SaasUser::findOne($saas_id);
- if (!$saas_user) {
- throw new \Exception('联盟用户查询失败');
- }
- $storeOperations = StoreOperations::findOne(['saas_id' => $saas_id, 'is_delete' => 0]);
- if ($storeOperations) {
- throw new \Exception('运营人员已存在');
- }
- $storeOperations = new StoreOperations();
- $storeOperations->saas_id = $saas_id;
- $storeOperations->remark = $remark;
- $storeOperations->status = $status;
- if (!$storeOperations->save()) {
- throw new \Exception(json_encode($storeOperations->errors, JSON_UNESCAPED_UNICODE));
- }
- return [
- 'code' => 0,
- 'msg' => '添加成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function setStatus() {
- try {
- $id = explode(',', $this->id);
- $status = $this->status;
- if (empty($id)) {
- throw new \Exception('参数错误');
- }
- //修改状态
- if (in_array($status, [0, 1])) {
- StoreOperations::updateAll(['status' => $status], ['id' => $id]);
- } else {
- //删除
- StoreOperations::updateAll(['is_delete' => 1], ['id' => $id]);
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|