ShopIdentity.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\store\model;
  4. use app\common\model\ShopIdentity as ShopIdentityModel;
  5. use think\facade\Db;
  6. class ShopIdentity extends ShopIdentityModel
  7. {
  8. /**
  9. * 写入门店身份用户
  10. */
  11. public function addIdentityUser($shopId, $data)
  12. {
  13. Db::startTrans();
  14. try {
  15. // 写入门店身份用户
  16. if (!empty($data['identity_user'])) {
  17. // 判断门店身份用户是否重复
  18. if (count($data['identity_user']) != count(array_unique(array_column($data['identity_user'], 'mobile')))) {
  19. $this->error = "同一手机号码用户不能有多重身份";
  20. return false;
  21. }
  22. foreach ($data['identity_user'] as $item) {
  23. // 判断是否已添加过
  24. if (User::where('mobile', $item['mobile'])->where('shop_id', $shopId)->where('role', $item['role_id'])->find()) {
  25. $this->error = "该用户已存在,请勿重复添加";
  26. return false;
  27. }
  28. // 厨师不能同时在其他门店担任职务
  29. if (User::where('mobile', $item['mobile'])->where('role', '>', User::NORMAL_USER)->find()) {
  30. $this->error = "同一手机号码用户不能有多重身份";
  31. return false;
  32. }
  33. User::createUserByMobile(make_nickname($item['mobile']), $item['mobile'], $shopId, $item['role_id'], $shopId);
  34. }
  35. }
  36. Db::commit();
  37. return true;
  38. } catch (\Exception $e) {
  39. Db::rollback();
  40. $this->error = $e->getMessage();
  41. return false;
  42. }
  43. }
  44. /**
  45. * 写入门店身份配置
  46. */
  47. public function addShopIdentity($shopId, $data)
  48. {
  49. Db::startTrans();
  50. try {
  51. $shopModel = Shops::find($shopId);
  52. $shopModel->staff_percent = $data['staff_percent'] ?? 0;
  53. $shopModel->manager_percent = $data['manager_percent'] ?? 0;
  54. $shopModel->boss_percent = $data['boss_percent'] ?? 0;
  55. $shopModel->save();
  56. // 写入门店身份
  57. if (!empty($data['shop_identity'])) {
  58. foreach ($data['shop_identity'] as $item) {
  59. // 判断门店身份是否存在
  60. $shopIdentity = ShopIdentity::where('shop_id', $shopId)->where('role_id', $item['role_id'])->find();
  61. if (empty($shopIdentity)) {
  62. ShopIdentity::create([
  63. 'shop_id' => $shopId,
  64. 'role_id' => $item['role_id'],
  65. 'fc_percent' => $item['fc_percent'],
  66. ]);
  67. } else {
  68. $shopIdentity->fc_percent = $item['fc_percent'];
  69. $shopIdentity->save();
  70. }
  71. }
  72. }
  73. Db::commit();
  74. return true;
  75. } catch (\Exception $e) {
  76. Db::rollback();
  77. $this->error = $e->getMessage();
  78. return false;
  79. }
  80. }
  81. }