MdForm.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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\Admin;
  9. use app\models\District;
  10. use app\models\Md;
  11. use app\models\MdCategory;
  12. use app\models\MdProfit;
  13. use app\models\MdStaff;
  14. use app\models\SaasUser;
  15. use app\models\User;
  16. use yii\base\BaseObject;
  17. use yii\base\Model;
  18. use yii\helpers\Json;
  19. class MdForm extends Model
  20. {
  21. public $id;
  22. public $store_id;
  23. public $user_id;
  24. public $saas_id;
  25. public $name;
  26. public $mobile;
  27. public $address;
  28. public $is_delete;
  29. public $longitude;
  30. public $latitude;
  31. public $cover_url;
  32. public $pic_url;
  33. public $shop_time_type;
  34. public $is_time_forbid;
  35. public $open_status;
  36. public $delivery_type;
  37. public $self_delivery_type;
  38. public $rate;
  39. public $start_time;
  40. public $end_time;
  41. public $contact;
  42. public $province;
  43. public $city;
  44. public $district;
  45. public $shop_audit;
  46. public $manager_name;
  47. public $is_single;
  48. public $delivery;
  49. public $clerk_rate;
  50. public $is_set_distance;
  51. public $distance;
  52. public $user_name;//门店账户
  53. public $password;//门店密码
  54. public $refuse_desc;
  55. //搜索
  56. public $search_key;
  57. public $shop_audit_status;
  58. public $status = -1;
  59. public $cat_id;
  60. const SCENARIO_ADD = 'add';
  61. const SCENARIO_EDIT = 'edit';
  62. const SCENARIO_DEL = 'del';
  63. const SCENARIO_AUDIT = 'audit';
  64. const SCENARIO_LIST = 'list';
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function rules()
  69. {
  70. return [
  71. ['delivery_type', function ($attr, $params) {
  72. $data = $this->delivery_type;
  73. if (empty($data)) {
  74. $data = ['express', 'shop'];
  75. }
  76. $this->delivery_type = Json::encode($data);
  77. }],
  78. ['self_delivery_type', function ($attr, $params) {
  79. $data = $this->self_delivery_type;
  80. if (empty($data)) {
  81. $data = ['express', 'shop'];
  82. }
  83. $this->self_delivery_type = Json::encode($data);
  84. }],
  85. [['is_set_distance', 'is_single', 'store_id', 'is_delete', 'province', 'city', 'district', 'user_id',
  86. 'shop_audit', 'shop_time_type', 'is_time_forbid', 'open_status','status', 'cat_id'], 'integer'],
  87. [['longitude', 'latitude', 'cover_url', 'pic_url', 'contact', 'manager_name', 'delivery', 'user_name', 'password'], 'string'],
  88. [['name', 'mobile', 'address', 'refuse_desc', 'start_time', 'end_time'], 'string', 'max' => 255],
  89. ['is_delete', 'default', 'value' => 0],
  90. [['rate', 'clerk_rate', 'distance'], 'number'],
  91. ['shop_audit', 'default', 'value' => 1],
  92. ['rate', 'default', 'value' => 0],
  93. // 操作
  94. [['name', 'contact', 'mobile', 'longitude', 'latitude'], 'required', 'on' => [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
  95. [['id'], 'required', 'on' => [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
  96. ['id', 'string', 'on' => self::SCENARIO_DEL],
  97. [['id', 'shop_audit'], 'required', 'on' => self::SCENARIO_AUDIT],
  98. [['search_key', 'shop_audit_status'], 'string', 'on' => self::SCENARIO_LIST],
  99. [['delivery_type', 'self_delivery_type', 'saas_id'], 'safe']
  100. ];
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function attributeLabels()
  106. {
  107. return [
  108. 'id' => 'ID',
  109. 'store_id' => 'Store ID',
  110. 'user_id' => '用户id',
  111. 'name' => 'Name',
  112. 'mobile' => 'Mobile',
  113. 'address' => 'Address',
  114. 'is_delete' => 'Is Delete',
  115. 'longitude' => 'Longitude',
  116. 'latitude' => 'Latitude',
  117. 'cover_url' => '自提点大图',
  118. 'pic_url' => '自提点小图',
  119. 'shop_time' => '营业时间',
  120. 'province' => '省份',
  121. 'city' => '城市',
  122. 'district' => '区县',
  123. 'shop_audit' => '审核状态(-1未通过,0待审核,1通过)',
  124. 'refuse_desc' => '拒接原因',
  125. ];
  126. }
  127. public function scenarios()
  128. {
  129. $scenarios = parent::scenarios();
  130. return $scenarios;
  131. }
  132. public function saveMd()
  133. {
  134. if ($this->validate()) {
  135. $t = \Yii::$app->db->beginTransaction();
  136. $user_id = 0;
  137. $audit_status = 0;
  138. $refuse_desc = null;
  139. $is_new = false;
  140. if ($this->scenario == self::SCENARIO_EDIT) {
  141. $model = Md::findOne(['id' => $this->id]);
  142. $user_id = $model->user_id;
  143. $audit_status = $model->shop_audit;
  144. $refuse_desc = $model->refuse_desc;
  145. } else {
  146. $model = new Md();
  147. $is_new = true;
  148. $user_id = $this->saas_id;
  149. $admin = Admin::find()->where(['username' => $this->user_name, 'is_delete' => 0])->one();
  150. $md = Md::find()->where(['store_id' => $this->store_id, 'user_name' => $this->user_name, 'is_delete' => 0])->one();
  151. if (!empty($admin) || !empty($md)) {
  152. return [
  153. 'code' => 1,
  154. 'msg' => '用户名已经存在,请更换'
  155. ];
  156. }
  157. $md_staff = MdStaff::findOne(['saas_user_id' => $this->saas_id, 'is_delete' => 0, 'store_id' => $this->store_id]);
  158. if ($md_staff) {
  159. return [
  160. 'code' => 1,
  161. 'msg' => '由于您属于其他门店员工,暂时无法创建门店'
  162. ];
  163. }
  164. $admin = Md::find()->where(['store_id' => $this->store_id, 'mobile' => $this->mobile, 'is_delete' => 0])->andWhere(['in', 'shop_audit', [0, 1]])->one();
  165. if ($admin) {
  166. return [
  167. 'code' => 1,
  168. 'msg' => '当前注册手机号店铺已存在或存在待审核记录'
  169. ];
  170. }
  171. $this->rate = 0;
  172. $this->clerk_rate = 0;
  173. $this->distance = 0;
  174. $audit_status = Md::SHOP_AUDIT_YES;
  175. $model->manager = $user_id;
  176. }
  177. $this->user_id = $user_id;
  178. $this->refuse_desc = $refuse_desc;
  179. $this->shop_audit = $audit_status;
  180. $user_name = $this->user_name;
  181. $password = $this->password;
  182. unset($this->attributes['password']);
  183. unset($this->attributes['user_name']);
  184. $model->attributes = $this->attributes;
  185. if ($is_new && empty($this->self_delivery_type)) {
  186. $model->self_delivery_type = Json::encode(['express', 'shop']);
  187. }
  188. if (!$is_new && !empty($this->delivery_type)) {
  189. $model->delivery_type = $this->delivery_type;
  190. }
  191. if (!$is_new && !empty($this->self_delivery_type)) {
  192. $model->self_delivery_type = $this->self_delivery_type;
  193. }
  194. if (isset($this->cat_id) && $this->cat_id > 0) {
  195. $mdCategory = MdCategory::getList($model->store_id, $this->cat_id);
  196. if (!$mdCategory) {
  197. return [
  198. 'code' => 1,
  199. 'msg' => '门店分类不存在'
  200. ];
  201. }
  202. $model->cat_id = $this->cat_id;
  203. }
  204. if (!$model->save()) {
  205. $t->rollBack();
  206. return [
  207. 'code' => 1,
  208. 'msg' => $model->errors[0]
  209. ];
  210. }
  211. if ($this->scenario == self::SCENARIO_EDIT) {
  212. $user_admin = Admin::find()->where(['username' => $user_name, 'is_delete' => 0])->andWhere(['NOT', ['type' => 'md', 'type_id' => $this->id]])->one();
  213. } else {
  214. $user_admin = Admin::find()->where(['username' => $user_name, 'is_delete' => 0])->one();
  215. }
  216. if ($user_admin) {
  217. $t->rollBack();
  218. return [
  219. 'code' => 1,
  220. 'msg' => '登录账户已存在,请更换账户名'
  221. ];
  222. }
  223. $admin = Admin::find()->where(['type' => 'md', 'type_id' => $model->id, 'is_delete' => 0])->one();
  224. if ($is_new || empty($admin)) {
  225. // 自动创建admin账号
  226. $admin = new Admin();
  227. $admin->store_id = get_store_id();
  228. $admin->access_token = \Yii::$app->security->generateRandomString();
  229. $admin->mobile = $model->mobile;
  230. $admin->avatar = $model->cover_url;
  231. $admin->name = $model->name;
  232. $admin->type = 'md';
  233. $admin->type_id = $model->id;
  234. }
  235. $admin->username = $user_name ?: $model->mobile;
  236. $admin->password = \Yii::$app->security->generatePasswordHash(empty($password) ? $model->mobile : $password);
  237. if ($admin->save()) {
  238. \Yii::warning(['创建admin账号成功', $admin->id]);
  239. } else {
  240. \Yii::warning(['创建admin账号错误', $admin->errors[0]]);
  241. }
  242. //如果为新增就增加店长
  243. if ($is_new) {
  244. $md_staff = new MdStaff();
  245. $md_staff->store_id = $this->store_id;
  246. $md_staff->md_id = $model->id;
  247. $md_staff->saas_user_id = $user_id;
  248. $md_staff->name = $this->contact;
  249. $md_staff->mobile = $model->mobile;
  250. $md_staff->is_manager = 1;
  251. if (!$md_staff->save()) {
  252. return $this->asJson([
  253. 'code' => 1,
  254. 'msg' => $md_staff->errors[0]
  255. ]);
  256. }
  257. }
  258. $t->commit();
  259. return [
  260. 'code' => 0,
  261. 'msg' => '保存成功'
  262. ];
  263. } else {
  264. // 验证失败:$errors 是一个包含错误信息的数组
  265. return [
  266. 'code' => 1,
  267. 'msg' => $this->getErrorSummary(false)[0]
  268. ];
  269. }
  270. }
  271. public function searchMd()
  272. {
  273. $query = Md::find()->alias('m')->leftJoin(['su' => SaasUser::tableName()], 'su.id=m.manager');
  274. $query->where(['m.store_id' => get_store_id(), 'm.is_delete' => Md::IS_DELETE_NO])->orderBy("m.created_at desc,m.updated_at desc");
  275. if ($this->name) {
  276. $query->andWhere(['like', 'm.name', $this->name]);
  277. }
  278. if ($this->manager_name) {
  279. $query->andWhere(['like', 'su.name', $this->manager_name]);
  280. }
  281. if ($this->delivery) {
  282. $query->andWhere(['like', 'm.delivery_type', $this->delivery]);
  283. }
  284. if ($this->cat_id) {
  285. $query->andWhere(['m.cat_id' => $this->cat_id]);
  286. }
  287. if ($this->mobile) {
  288. $query->andWhere(['LIKE', 'm.mobile', $this->mobile]);
  289. }
  290. if (in_array($this->is_single, [0, 1]) && !is_null($this->is_single)) {
  291. $query->andWhere(['m.is_single' => $this->is_single]);
  292. }
  293. // HKG 修改查询方式
  294. switch ($this->status){
  295. case 1:
  296. $query->andWhere(['m.shop_audit'=>0]);
  297. break;
  298. case 2:
  299. $query->andWhere(['m.shop_audit'=>2]);
  300. break;
  301. case 3:
  302. $query->andWhere(['m.open_status'=>1]);
  303. break;
  304. case 4:
  305. $query->andWhere(['m.open_status'=>0]);
  306. break;
  307. default:
  308. break;
  309. }
  310. // if (in_array($this->open_status, [0, 1]) && !is_null($this->open_status)) {
  311. // $query->andWhere(['m.open_status' => $this->open_status]);
  312. // }
  313. // HKG 时间筛选
  314. if(!empty($this->start_time)){
  315. $query->andWhere(['>=','m.created_at',strtotime($this->start_time)]);
  316. }
  317. if(!empty($this->end_time)){
  318. $query->andWhere(['<=','m.created_at',strtotime($this->end_time)]);
  319. }
  320. $list = pagination_make($query);
  321. foreach ($list['list'] as &$val) {
  322. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  323. $saasUser = SaasUser::findOne($val['manager']);
  324. $user = User::find()->where(['store_id' => get_store_id(),'binding' => $saasUser->mobile])->one();
  325. $val['manager_name'] = $user ? $user->nickname : '-';
  326. $val['manager_avatar_url'] = $user ? $user->avatar_url : '-';
  327. $val['manager_mobile'] = $user ? $user->binding : '-';
  328. $val['detail_address'] = $val['address'];
  329. $province = District::findOne($val['province']);
  330. $city = District::findOne($val['city']);
  331. $district = District::findOne($val['district']);
  332. if ($province && $city && $district) {
  333. $val['detail_address'] = $province->name . $city->name . $district->name . $val['detail_address'];
  334. }
  335. $val['sale_profit'] = MdProfit::find()->where(['md_id' => $val['id'], 'is_send' => 1])->sum('sale_profit') ?: 0;
  336. $val['cat_name'] = '';
  337. $mdCategory = MdCategory::getList($val['store_id'], $val['cat_id'] ?: -1);
  338. if ($mdCategory) {
  339. $val['cat_name'] = $mdCategory[0]['name'] ?: '';
  340. }
  341. }
  342. return [
  343. 'code' => 0,
  344. 'msg' => 'success',
  345. 'data' => [
  346. 'data' => $list['list'],
  347. 'pageNo' => $list['pageNo'],
  348. 'totalCount' => $list['totalCount']
  349. ]
  350. ];
  351. }
  352. public function searchAudit()
  353. {
  354. $query = Md::find()->alias('m');
  355. $query->where(['store_id' => get_store_id(), 'is_delete' => 0])->orderBy("created_at desc,updated_at desc");
  356. if ($this->name) {
  357. $query->andWhere(['like', 'name', $this->name]);
  358. }
  359. if ($this->mobile) {
  360. $query->andWhere(['mobile' => $this->mobile]);
  361. }
  362. if (in_array($this->shop_audit, [0, 1, 2]) && !is_null($this->shop_audit)) {
  363. $query->andWhere(['shop_audit' => $this->shop_audit]);
  364. }
  365. $list = pagination_make($query);
  366. foreach ($list['list'] as &$val) {
  367. $val['detail_address'] = $val['address'];
  368. $province = District::findOne($val['province']);
  369. $city = District::findOne($val['city']);
  370. $district = District::findOne($val['district']);
  371. if ($province && $city && $district) {
  372. $val['detail_address'] = $province->name . $city->name . $district->name . $val['detail_address'];
  373. }
  374. }
  375. return [
  376. 'code' => 0,
  377. 'msg' => 'success',
  378. 'data' => [
  379. 'data' => $list['list'],
  380. 'pageNo' => $list['pageNo'],
  381. 'totalCount' => $list['totalCount']
  382. ]
  383. ];
  384. }
  385. public function delMd()
  386. {
  387. if ($this->validate()) {
  388. $t = \Yii::$app->db->beginTransaction();
  389. if ($this->scenario !== self::SCENARIO_DEL) {
  390. return [
  391. 'code' => 1,
  392. 'msg' => '删除失败'
  393. ];
  394. }
  395. $model = Md::findOne(['id' => $this->id]);
  396. if($model->is_single){
  397. $model->is_single = 0;
  398. if (!$model->save()) {
  399. $t->rollBack();
  400. return [
  401. 'code' => 1,
  402. 'msg' => array_shift($model->getFirstErrors()),
  403. ];
  404. }
  405. $t->commit();
  406. return [
  407. 'code' => 0,
  408. 'msg' => '操作成功'
  409. ];
  410. }
  411. $model->is_delete = Md::IS_DELETE_YES;
  412. if (!$model || !$model->save()) {
  413. $t->rollBack();
  414. return [
  415. 'code' => 1,
  416. 'msg' => $model->getErrorSummary(false)[0]
  417. ];
  418. }
  419. // 删除门店员工
  420. $staff = MdStaff::find()->where(['md_id' => $this->id,'is_delete'=>0])->asArray()->all();
  421. if (!empty($staff)) {
  422. $update_num = MdStaff::updateAll(['is_delete' => 1], ['in', 'id', array_column($staff, 'id')]);
  423. if ($update_num != count($staff)) {
  424. $t->rollBack();
  425. return [
  426. 'code' => 1,
  427. 'msg' => '门店员工数据删除失败'
  428. ];
  429. }
  430. }
  431. // Admin
  432. $admin = Admin::findOne(['type_id' => $model->id, 'type' => 'md', 'is_delete' => 0]);
  433. if ($admin) {
  434. $admin->is_delete = 1;
  435. if (!$admin->save()) {
  436. $t->rollBack();
  437. return [
  438. 'code' => 1,
  439. 'msg' => '请求错误'
  440. ];
  441. }
  442. }
  443. $t->commit();
  444. return [
  445. 'code' => 0,
  446. 'msg' => '删除成功'
  447. ];
  448. // 所有输入数据都有效 all inputs are valid
  449. } else {
  450. // 验证失败:$errors 是一个包含错误信息的数组
  451. return [
  452. 'code' => 1,
  453. "msg" => $this->getErrorSummary(false)[0]
  454. ];
  455. }
  456. }
  457. public function auditMd()
  458. {
  459. if ($this->validate()) {
  460. $t = \Yii::$app->db->beginTransaction();
  461. if ($this->scenario !== self::SCENARIO_AUDIT) {
  462. return [
  463. 'code' => 1,
  464. 'msg' => '操作失败'
  465. ];
  466. }
  467. $model = Md::findOne(['id' => $this->id]);
  468. if (!$model || !in_array($this->shop_audit, [$model::SHOP_AUDIT_NO, $model::SHOP_AUDIT_YES])) {
  469. return [
  470. 'code' => 1,
  471. 'msg' => '操作失败'
  472. ];
  473. }
  474. $model->shop_audit = $this->shop_audit;
  475. if (!$model->save()) {
  476. $t->rollBack();
  477. return [
  478. 'code' => 1,
  479. 'msg' => $model->errors[0]
  480. ];
  481. }
  482. $t->commit();
  483. return [
  484. 'code' => 0,
  485. 'msg' => $this->id
  486. ];
  487. // 所有输入数据都有效 all inputs are valid
  488. } else {
  489. // 验证失败:$errors 是一个包含错误信息的数组
  490. return [
  491. 'code' => 1,
  492. 'msg' => $this->getErrorSummary(false)[0]
  493. ];
  494. }
  495. }
  496. /**
  497. * Undocumented function
  498. *
  499. * @Author LGL 24963@qq.com
  500. * @DateTime 2021-01-15
  501. * @desc: 获取自提点列表
  502. */
  503. public static function getMdList($store_id = 0, $field = '', $filter_md_id = [])
  504. {
  505. $field = $field ?: '*';
  506. $query = Md::find()->where(['store_id' => $store_id ?: get_store_id(), 'is_delete' => Md::IS_DELETE_NO]);
  507. if (!empty($filter_md_id)) {
  508. $query->andWhere(['NOT IN', 'id', $filter_md_id]);
  509. }
  510. return $query->orderBy('id desc')->select($field)->asArray()->all();
  511. }
  512. }