| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Admin;
- use app\models\District;
- use app\models\Md;
- use app\models\MdCategory;
- use app\models\MdProfit;
- use app\models\MdStaff;
- use app\models\SaasUser;
- use app\models\User;
- use yii\base\BaseObject;
- use yii\base\Model;
- use yii\helpers\Json;
- class MdForm extends Model
- {
- public $id;
- public $store_id;
- public $user_id;
- public $saas_id;
- public $name;
- public $mobile;
- public $address;
- public $is_delete;
- public $longitude;
- public $latitude;
- public $cover_url;
- public $pic_url;
- public $shop_time_type;
- public $is_time_forbid;
- public $open_status;
- public $delivery_type;
- public $self_delivery_type;
- public $rate;
- public $start_time;
- public $end_time;
- public $contact;
- public $province;
- public $city;
- public $district;
- public $shop_audit;
- public $manager_name;
- public $is_single;
- public $delivery;
- public $clerk_rate;
- public $is_set_distance;
- public $distance;
- public $user_name;//门店账户
- public $password;//门店密码
- public $refuse_desc;
- //搜索
- public $search_key;
- public $shop_audit_status;
- public $status = -1;
- public $cat_id;
- const SCENARIO_ADD = 'add';
- const SCENARIO_EDIT = 'edit';
- const SCENARIO_DEL = 'del';
- const SCENARIO_AUDIT = 'audit';
- const SCENARIO_LIST = 'list';
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- ['delivery_type', function ($attr, $params) {
- $data = $this->delivery_type;
- if (empty($data)) {
- $data = ['express', 'shop'];
- }
- $this->delivery_type = Json::encode($data);
- }],
- ['self_delivery_type', function ($attr, $params) {
- $data = $this->self_delivery_type;
- if (empty($data)) {
- $data = ['express', 'shop'];
- }
- $this->self_delivery_type = Json::encode($data);
- }],
- [['is_set_distance', 'is_single', 'store_id', 'is_delete', 'province', 'city', 'district', 'user_id',
- 'shop_audit', 'shop_time_type', 'is_time_forbid', 'open_status','status', 'cat_id'], 'integer'],
- [['longitude', 'latitude', 'cover_url', 'pic_url', 'contact', 'manager_name', 'delivery', 'user_name', 'password'], 'string'],
- [['name', 'mobile', 'address', 'refuse_desc', 'start_time', 'end_time'], 'string', 'max' => 255],
- ['is_delete', 'default', 'value' => 0],
- [['rate', 'clerk_rate', 'distance'], 'number'],
- ['shop_audit', 'default', 'value' => 1],
- ['rate', 'default', 'value' => 0],
- // 操作
- [['name', 'contact', 'mobile', 'longitude', 'latitude'], 'required', 'on' => [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
- [['id'], 'required', 'on' => [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
- ['id', 'string', 'on' => self::SCENARIO_DEL],
- [['id', 'shop_audit'], 'required', 'on' => self::SCENARIO_AUDIT],
- [['search_key', 'shop_audit_status'], 'string', 'on' => self::SCENARIO_LIST],
- [['delivery_type', 'self_delivery_type', 'saas_id'], 'safe']
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'user_id' => '用户id',
- 'name' => 'Name',
- 'mobile' => 'Mobile',
- 'address' => 'Address',
- 'is_delete' => 'Is Delete',
- 'longitude' => 'Longitude',
- 'latitude' => 'Latitude',
- 'cover_url' => '自提点大图',
- 'pic_url' => '自提点小图',
- 'shop_time' => '营业时间',
- 'province' => '省份',
- 'city' => '城市',
- 'district' => '区县',
- 'shop_audit' => '审核状态(-1未通过,0待审核,1通过)',
- 'refuse_desc' => '拒接原因',
- ];
- }
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- return $scenarios;
- }
- public function saveMd()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- $user_id = 0;
- $audit_status = 0;
- $refuse_desc = null;
- $is_new = false;
- if ($this->scenario == self::SCENARIO_EDIT) {
- $model = Md::findOne(['id' => $this->id]);
- $user_id = $model->user_id;
- $audit_status = $model->shop_audit;
- $refuse_desc = $model->refuse_desc;
- } else {
- $model = new Md();
- $is_new = true;
- $user_id = $this->saas_id;
- $admin = Admin::find()->where(['username' => $this->user_name, 'is_delete' => 0])->one();
- $md = Md::find()->where(['store_id' => $this->store_id, 'user_name' => $this->user_name, 'is_delete' => 0])->one();
- if (!empty($admin) || !empty($md)) {
- return [
- 'code' => 1,
- 'msg' => '用户名已经存在,请更换'
- ];
- }
- $md_staff = MdStaff::findOne(['saas_user_id' => $this->saas_id, 'is_delete' => 0, 'store_id' => $this->store_id]);
- if ($md_staff) {
- return [
- 'code' => 1,
- 'msg' => '由于您属于其他门店员工,暂时无法创建门店'
- ];
- }
- $admin = Md::find()->where(['store_id' => $this->store_id, 'mobile' => $this->mobile, 'is_delete' => 0])->andWhere(['in', 'shop_audit', [0, 1]])->one();
- if ($admin) {
- return [
- 'code' => 1,
- 'msg' => '当前注册手机号店铺已存在或存在待审核记录'
- ];
- }
- $this->rate = 0;
- $this->clerk_rate = 0;
- $this->distance = 0;
- $audit_status = Md::SHOP_AUDIT_YES;
- $model->manager = $user_id;
- }
- $this->user_id = $user_id;
- $this->refuse_desc = $refuse_desc;
- $this->shop_audit = $audit_status;
- $user_name = $this->user_name;
- $password = $this->password;
- unset($this->attributes['password']);
- unset($this->attributes['user_name']);
- $model->attributes = $this->attributes;
- if ($is_new && empty($this->self_delivery_type)) {
- $model->self_delivery_type = Json::encode(['express', 'shop']);
- }
- if (!$is_new && !empty($this->delivery_type)) {
- $model->delivery_type = $this->delivery_type;
- }
- if (!$is_new && !empty($this->self_delivery_type)) {
- $model->self_delivery_type = $this->self_delivery_type;
- }
- if (isset($this->cat_id) && $this->cat_id > 0) {
- $mdCategory = MdCategory::getList($model->store_id, $this->cat_id);
- if (!$mdCategory) {
- return [
- 'code' => 1,
- 'msg' => '门店分类不存在'
- ];
- }
- $model->cat_id = $this->cat_id;
- }
- if (!$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->errors[0]
- ];
- }
- if ($this->scenario == self::SCENARIO_EDIT) {
- $user_admin = Admin::find()->where(['username' => $user_name, 'is_delete' => 0])->andWhere(['NOT', ['type' => 'md', 'type_id' => $this->id]])->one();
- } else {
- $user_admin = Admin::find()->where(['username' => $user_name, 'is_delete' => 0])->one();
- }
- if ($user_admin) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '登录账户已存在,请更换账户名'
- ];
- }
- $admin = Admin::find()->where(['type' => 'md', 'type_id' => $model->id, 'is_delete' => 0])->one();
- if ($is_new || empty($admin)) {
- // 自动创建admin账号
- $admin = new Admin();
- $admin->store_id = get_store_id();
- $admin->access_token = \Yii::$app->security->generateRandomString();
- $admin->mobile = $model->mobile;
- $admin->avatar = $model->cover_url;
- $admin->name = $model->name;
- $admin->type = 'md';
- $admin->type_id = $model->id;
- }
- $admin->username = $user_name ?: $model->mobile;
- $admin->password = \Yii::$app->security->generatePasswordHash(empty($password) ? $model->mobile : $password);
- if ($admin->save()) {
- \Yii::warning(['创建admin账号成功', $admin->id]);
- } else {
- \Yii::warning(['创建admin账号错误', $admin->errors[0]]);
- }
- //如果为新增就增加店长
- if ($is_new) {
- $md_staff = new MdStaff();
- $md_staff->store_id = $this->store_id;
- $md_staff->md_id = $model->id;
- $md_staff->saas_user_id = $user_id;
- $md_staff->name = $this->contact;
- $md_staff->mobile = $model->mobile;
- $md_staff->is_manager = 1;
- if (!$md_staff->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $md_staff->errors[0]
- ]);
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- }
- public function searchMd()
- {
- $query = Md::find()->alias('m')->leftJoin(['su' => SaasUser::tableName()], 'su.id=m.manager');
- $query->where(['m.store_id' => get_store_id(), 'm.is_delete' => Md::IS_DELETE_NO])->orderBy("m.created_at desc,m.updated_at desc");
- if ($this->name) {
- $query->andWhere(['like', 'm.name', $this->name]);
- }
- if ($this->manager_name) {
- $query->andWhere(['like', 'su.name', $this->manager_name]);
- }
- if ($this->delivery) {
- $query->andWhere(['like', 'm.delivery_type', $this->delivery]);
- }
- if ($this->cat_id) {
- $query->andWhere(['m.cat_id' => $this->cat_id]);
- }
- if ($this->mobile) {
- $query->andWhere(['LIKE', 'm.mobile', $this->mobile]);
- }
- if (in_array($this->is_single, [0, 1]) && !is_null($this->is_single)) {
- $query->andWhere(['m.is_single' => $this->is_single]);
- }
- // HKG 修改查询方式
- switch ($this->status){
- case 1:
- $query->andWhere(['m.shop_audit'=>0]);
- break;
- case 2:
- $query->andWhere(['m.shop_audit'=>2]);
- break;
- case 3:
- $query->andWhere(['m.open_status'=>1]);
- break;
- case 4:
- $query->andWhere(['m.open_status'=>0]);
- break;
- default:
- break;
- }
- // if (in_array($this->open_status, [0, 1]) && !is_null($this->open_status)) {
- // $query->andWhere(['m.open_status' => $this->open_status]);
- // }
- // HKG 时间筛选
- if(!empty($this->start_time)){
- $query->andWhere(['>=','m.created_at',strtotime($this->start_time)]);
- }
- if(!empty($this->end_time)){
- $query->andWhere(['<=','m.created_at',strtotime($this->end_time)]);
- }
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $saasUser = SaasUser::findOne($val['manager']);
- $user = User::find()->where(['store_id' => get_store_id(),'binding' => $saasUser->mobile])->one();
- $val['manager_name'] = $user ? $user->nickname : '-';
- $val['manager_avatar_url'] = $user ? $user->avatar_url : '-';
- $val['manager_mobile'] = $user ? $user->binding : '-';
- $val['detail_address'] = $val['address'];
- $province = District::findOne($val['province']);
- $city = District::findOne($val['city']);
- $district = District::findOne($val['district']);
- if ($province && $city && $district) {
- $val['detail_address'] = $province->name . $city->name . $district->name . $val['detail_address'];
- }
- $val['sale_profit'] = MdProfit::find()->where(['md_id' => $val['id'], 'is_send' => 1])->sum('sale_profit') ?: 0;
- $val['cat_name'] = '';
- $mdCategory = MdCategory::getList($val['store_id'], $val['cat_id'] ?: -1);
- if ($mdCategory) {
- $val['cat_name'] = $mdCategory[0]['name'] ?: '';
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ];
- }
- public function searchAudit()
- {
- $query = Md::find()->alias('m');
- $query->where(['store_id' => get_store_id(), 'is_delete' => 0])->orderBy("created_at desc,updated_at desc");
- if ($this->name) {
- $query->andWhere(['like', 'name', $this->name]);
- }
- if ($this->mobile) {
- $query->andWhere(['mobile' => $this->mobile]);
- }
- if (in_array($this->shop_audit, [0, 1, 2]) && !is_null($this->shop_audit)) {
- $query->andWhere(['shop_audit' => $this->shop_audit]);
- }
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $val['detail_address'] = $val['address'];
- $province = District::findOne($val['province']);
- $city = District::findOne($val['city']);
- $district = District::findOne($val['district']);
- if ($province && $city && $district) {
- $val['detail_address'] = $province->name . $city->name . $district->name . $val['detail_address'];
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ];
- }
- public function delMd()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario !== self::SCENARIO_DEL) {
- return [
- 'code' => 1,
- 'msg' => '删除失败'
- ];
- }
- $model = Md::findOne(['id' => $this->id]);
- if($model->is_single){
- $model->is_single = 0;
- if (!$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => array_shift($model->getFirstErrors()),
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- }
- $model->is_delete = Md::IS_DELETE_YES;
- if (!$model || !$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- // 删除门店员工
- $staff = MdStaff::find()->where(['md_id' => $this->id,'is_delete'=>0])->asArray()->all();
- if (!empty($staff)) {
- $update_num = MdStaff::updateAll(['is_delete' => 1], ['in', 'id', array_column($staff, 'id')]);
- if ($update_num != count($staff)) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '门店员工数据删除失败'
- ];
- }
- }
- // Admin
- $admin = Admin::findOne(['type_id' => $model->id, 'type' => 'md', 'is_delete' => 0]);
- if ($admin) {
- $admin->is_delete = 1;
- if (!$admin->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '请求错误'
- ];
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- // 所有输入数据都有效 all inputs are valid
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- public function auditMd()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario !== self::SCENARIO_AUDIT) {
- return [
- 'code' => 1,
- 'msg' => '操作失败'
- ];
- }
- $model = Md::findOne(['id' => $this->id]);
- if (!$model || !in_array($this->shop_audit, [$model::SHOP_AUDIT_NO, $model::SHOP_AUDIT_YES])) {
- return [
- 'code' => 1,
- 'msg' => '操作失败'
- ];
- }
- $model->shop_audit = $this->shop_audit;
- if (!$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->errors[0]
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => $this->id
- ];
- // 所有输入数据都有效 all inputs are valid
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-15
- * @desc: 获取自提点列表
- */
- public static function getMdList($store_id = 0, $field = '', $filter_md_id = [])
- {
- $field = $field ?: '*';
- $query = Md::find()->where(['store_id' => $store_id ?: get_store_id(), 'is_delete' => Md::IS_DELETE_NO]);
- if (!empty($filter_md_id)) {
- $query->andWhere(['NOT IN', 'id', $filter_md_id]);
- }
- return $query->orderBy('id desc')->select($field)->asArray()->all();
- }
- }
|