StaffClass.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace bizHd\staff\classes;
  3. use bizHd\admin\classes\AdminClass;
  4. use bizHd\admin\classes\AdminRoleClass;
  5. use bizHd\admin\models\Admin;
  6. use bizHd\shop\classes\ShopClass;
  7. use bizHd\staff\models\Staff;
  8. use common\components\dict;
  9. use common\components\util;
  10. use bizHd\base\classes\BaseClass;
  11. class StaffClass extends BaseClass
  12. {
  13. public static $baseFile = '\bizHd\staff\models\Staff';
  14. //删除员工 ssh 2021.5.3
  15. public static function deleteStaff($relation, $thisShopAdminId, $mainId)
  16. {
  17. $id = $relation->id ?? 0;
  18. if ($relation->founder == 2) {
  19. util::fail("超管不能删除");
  20. }
  21. if ($id == $thisShopAdminId) {
  22. util::fail("不能删自己");
  23. }
  24. self::valid($relation, $mainId);
  25. $adminId = $relation->adminId ?? 0;
  26. $currentShopId = $relation->shopId ?? 0;
  27. $admin = AdminClass::getById($adminId, true);
  28. if (empty($admin)) {
  29. util::fail('没有找到员工信息');
  30. }
  31. $admin->currentShopId = 0;
  32. $admin->save();
  33. $relation->delStatus = 1;
  34. $relation->save();
  35. //删除员工关系后,再找出这个员工还有没其它门店员工身份补上
  36. if (!empty($admin) && isset($admin->currentShopId) && $admin->currentShopId == $currentShopId) {
  37. $has = self::getByCondition(['adminId' => $adminId, 'delStatus' => 0, 'status' => 1], true);
  38. $hasCurrentShopId = $has->shopId ?? 0;
  39. $admin->currentShopId = $hasCurrentShopId;
  40. $admin->save();
  41. }
  42. // 员工数-1
  43. AdminRoleClass::counters(['num' => -1], ['id' => $relation->roleId]);
  44. }
  45. public static function valid($relation, $mainId)
  46. {
  47. if (isset($relation->mainId) == false || $relation->mainId != $mainId) {
  48. util::fail('没有权限');
  49. }
  50. }
  51. //创建员工 ssh 2021.5.9
  52. public static function appGenerateStaff($data, $admin)
  53. {
  54. $mobile = $data['mobile'] ?? '';
  55. $shopId = $data['shopId'] ?? 0;
  56. $name = $data['name'] ?? '未命名';
  57. $remark = isset($data['remark']) ? $data['remark'] : '';
  58. $status = 1;
  59. $super = $data['super'] ?? 0;
  60. $remind = isset($data['remind']) ? $data['remind'] : 0;
  61. $roleId = $data['roleId'] ?? 0;
  62. $role = AdminRoleClass::getById($roleId);
  63. if (empty($role)) {
  64. util::fail('没有找到角色!');
  65. }
  66. $shop = ShopClass::getById($shopId);
  67. if (empty($shop)) {
  68. util::fail('没有找到门店57');
  69. }
  70. $avatar = $admin['avatar'] ?? '';
  71. $adminId = $admin['id'] ?? 0;
  72. $mainId = $shop['mainId'] ?? 0;
  73. $shopAdmin = self::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  74. if (!empty($shopAdmin)) {
  75. //已删除员工补回
  76. if (isset($shopAdmin->delStatus) && $shopAdmin->delStatus == 1) {
  77. $shopAdmin->delStatus = 0;
  78. $shopAdmin->roleId = $roleId;
  79. $shopAdmin->remind = $remind;
  80. $shopAdmin->status = $status;
  81. $shopAdmin->remark = $remark;
  82. $shopAdmin->name = $name;
  83. $shopAdmin->avatar = $avatar;
  84. $shopAdmin->super = $super;
  85. $shopAdmin->save();
  86. AdminRoleClass::counters(['num' => 1], ['id' => $roleId]);
  87. }
  88. } else {
  89. $addData = [
  90. 'adminId' => $adminId,
  91. 'roleId' => $roleId,
  92. 'remind' => $remind,
  93. 'status' => $status,
  94. 'name' => $name,
  95. 'mobile' => $mobile,
  96. 'avatar' => $avatar,
  97. 'super' => $super,
  98. 'mainId' => $mainId
  99. ];
  100. $shopAdmin = self::add($addData, true);
  101. AdminRoleClass::counters(['num' => 1], ['id' => $roleId]);
  102. }
  103. $update = ['currentShopId' => $shopId];
  104. $shop = ShopClass::getById($shopId, true);
  105. $pfShopId = $shop->pfShopId ?? 0;
  106. $update['currentGhsShopId'] = $pfShopId;
  107. AdminClass::updateById($adminId, $update);
  108. return $shopAdmin;
  109. }
  110. /**
  111. * 执行员工离职操作
  112. * @param Admin $admin 管理员模型
  113. * @param Staff $staff 员工模型
  114. */
  115. public static function executeStaffResignation($admin, $staff)
  116. {
  117. // 标记员工为已删除
  118. $staff->delStatus = 1;
  119. $staff->save();
  120. $mobile = $admin->mobile;
  121. $hasShop = 0;
  122. // 检查管理员是否有自己的门店
  123. $shopList = ShopClass::getAllByCondition(['mobile' => $mobile], null, 'id,shopName,mainId,pfShopId,lsShopId,adminId,ptStyle', null, true);
  124. if (empty($shopList)) {
  125. // 情况1:自己没有店,在别人那边当员工,直接退出
  126. $admin->currentShopId = 0;
  127. $admin->currentGhsShopId = 0;
  128. $admin->save();
  129. } else {
  130. // 情况2:自己有店,自己可能开批零店,也可能只开零售店,也在其他店当员工
  131. $hdMainId = 0;
  132. $ghsMainId = 0;
  133. if (count($shopList) == 1) {
  134. foreach ($shopList as $shop) {
  135. if ($shop->ptStyle == 1) {
  136. $admin->currentShopId = $shop->id;
  137. $admin->currentGhsShopId = 0;
  138. $admin->save();
  139. } else {
  140. util::fail('账号异常,请联系管理员,编号266389');
  141. }
  142. }
  143. } else {
  144. if (count($shopList) != 2) {
  145. util::fail('账号异常,请联系管理员,编号873380');
  146. }
  147. foreach ($shopList as $shop) {
  148. if ($shop->ptStyle == 2) {
  149. $ghsMainId = $shop->mainId;
  150. $admin->currentGhsShopId = $shop->id;
  151. $admin->save();
  152. }
  153. if ($shop->ptStyle == 1) {
  154. $hdMainId = $shop->mainId;
  155. $admin->currentShopId = $shop->id;
  156. $admin->save();
  157. }
  158. }
  159. if ($ghsMainId != $hdMainId) {
  160. util::fail('账号异常,请联系管理,编号0068');
  161. }
  162. if ($ghsMainId == 0) {
  163. util::fail('账号异常,请联系管理,编号006835');
  164. }
  165. }
  166. $adminId = $admin->id;
  167. $staffList = StaffClass::getAllByCondition(['mainId' => $hdMainId, 'adminId' => $adminId], null, '*', null, true);
  168. if (empty($staffList)) {
  169. $role = AdminRoleClass::getByCondition(['mainId' => $hdMainId], true);
  170. $roleId = $role->id;
  171. $avatar = $admin->avatar;
  172. $name = $admin->name;
  173. $staffData = [
  174. 'adminId' => $adminId,
  175. 'mainId' => $hdMainId,
  176. 'remind' => 0,
  177. 'roleId' => $roleId,
  178. 'avatar' => $avatar,
  179. 'name' => $name,
  180. 'super' => 1,
  181. 'finance' => 0,
  182. 'switchShop' => 0,
  183. 'founder' => 1,
  184. 'mobile' => $mobile,
  185. ];
  186. self::add($staffData);
  187. } else {
  188. foreach ($staffList as $myStaff) {
  189. $myStaff->delStatus = 0;
  190. $myStaff->save();
  191. }
  192. }
  193. if (count($staffList) > 1) {
  194. util::fail('账号异常,请联系管理员56988');
  195. }
  196. $hasShop = 1;
  197. }
  198. return ['hasShop' => $hasShop];
  199. }
  200. }