StaffClass.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. $admin = AdminClass::getById($adminId, true);
  27. if (empty($admin)) {
  28. util::fail('没有找到员工信息');
  29. }
  30. //员工离职,多处使用,关键词 staff_lz
  31. StaffClass::executeStaffResignation($admin, $relation);
  32. }
  33. public static function valid($relation, $mainId)
  34. {
  35. if (!isset($relation->mainId) || $relation->mainId != $mainId) {
  36. util::fail('没有权限');
  37. }
  38. }
  39. //创建员工 ssh 2021.5.9
  40. public static function appGenerateStaff($data, $admin)
  41. {
  42. $mobile = $data['mobile'] ?? '';
  43. $shopId = $data['shopId'] ?? 0;
  44. $name = $data['name'] ?? '未命名';
  45. $remark = isset($data['remark']) ? $data['remark'] : '';
  46. $status = 1;
  47. $super = $data['super'] ?? 0;
  48. $remind = isset($data['remind']) ? $data['remind'] : 0;
  49. $roleId = $data['roleId'] ?? 0;
  50. $role = AdminRoleClass::getById($roleId);
  51. if (empty($role)) {
  52. util::fail('没有找到角色!');
  53. }
  54. $shop = ShopClass::getById($shopId);
  55. if (empty($shop)) {
  56. util::fail('没有找到门店57');
  57. }
  58. $avatar = $admin['avatar'] ?? '';
  59. $adminId = $admin['id'] ?? 0;
  60. $mainId = $shop['mainId'] ?? 0;
  61. $shopAdmin = self::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  62. if (!empty($shopAdmin)) {
  63. //已删除员工补回
  64. if (isset($shopAdmin->delStatus) && $shopAdmin->delStatus == 1) {
  65. $shopAdmin->delStatus = 0;
  66. $shopAdmin->roleId = $roleId;
  67. $shopAdmin->remind = $remind;
  68. $shopAdmin->status = $status;
  69. $shopAdmin->remark = $remark;
  70. $shopAdmin->name = $name;
  71. $shopAdmin->avatar = $avatar;
  72. $shopAdmin->super = $super;
  73. $shopAdmin->save();
  74. AdminRoleClass::counters(['num' => 1], ['id' => $roleId]);
  75. }
  76. } else {
  77. $addData = [
  78. 'adminId' => $adminId,
  79. 'roleId' => $roleId,
  80. 'remind' => $remind,
  81. 'status' => $status,
  82. 'name' => $name,
  83. 'mobile' => $mobile,
  84. 'avatar' => $avatar,
  85. 'super' => $super,
  86. 'mainId' => $mainId
  87. ];
  88. $shopAdmin = self::add($addData, true);
  89. AdminRoleClass::counters(['num' => 1], ['id' => $roleId]);
  90. }
  91. $update = ['currentShopId' => $shopId];
  92. $shop = ShopClass::getById($shopId, true);
  93. $pfShopId = $shop->pfShopId ?? 0;
  94. $update['currentGhsShopId'] = $pfShopId;
  95. AdminClass::updateById($adminId, $update);
  96. return $shopAdmin;
  97. }
  98. /**
  99. * 执行员工离职操作
  100. * @param Admin $admin 管理员模型
  101. * @param Staff $staff 员工模型
  102. */
  103. public static function executeStaffResignation($admin, $staff)
  104. {
  105. // 标记员工为已删除
  106. $staff->delStatus = 1;
  107. $staff->save();
  108. $mobile = $admin->mobile;
  109. $hasShop = 0;
  110. // 检查管理员是否有自己的门店
  111. $shopList = ShopClass::getAllByCondition(['mobile' => $mobile], null, 'id,shopName,mainId,pfShopId,lsShopId,adminId,ptStyle', null, true);
  112. if (empty($shopList)) {
  113. // 情况1:自己没有店,在别人那边当员工,直接退出
  114. $admin->currentShopId = 0;
  115. $admin->currentGhsShopId = 0;
  116. $admin->save();
  117. } else {
  118. // 情况2:自己有店,自己可能开批零店,也可能只开零售店,也在其他店当员工
  119. $hdMainId = 0;
  120. $ghsMainId = 0;
  121. if (count($shopList) == 1) {
  122. foreach ($shopList as $shop) {
  123. if ($shop->ptStyle == 1) {
  124. $admin->currentShopId = $shop->id;
  125. $admin->currentGhsShopId = 0;
  126. $admin->save();
  127. } else {
  128. util::fail('账号异常,请联系管理员,编号266389');
  129. }
  130. }
  131. } else {
  132. if (count($shopList) != 2) {
  133. util::fail('账号异常,请联系管理员,编号873380');
  134. }
  135. foreach ($shopList as $shop) {
  136. if ($shop->ptStyle == 2) {
  137. $ghsMainId = $shop->mainId;
  138. $admin->currentGhsShopId = $shop->id;
  139. $admin->save();
  140. }
  141. if ($shop->ptStyle == 1) {
  142. $hdMainId = $shop->mainId;
  143. $admin->currentShopId = $shop->id;
  144. $admin->save();
  145. }
  146. }
  147. if ($ghsMainId != $hdMainId) {
  148. util::fail('账号异常,请联系管理,编号0068');
  149. }
  150. if ($ghsMainId == 0) {
  151. util::fail('账号异常,请联系管理,编号006835');
  152. }
  153. }
  154. $adminId = $admin->id;
  155. $staffList = StaffClass::getAllByCondition(['mainId' => $hdMainId, 'adminId' => $adminId], null, '*', null, true);
  156. if (empty($staffList)) {
  157. $role = AdminRoleClass::getByCondition(['mainId' => $hdMainId], true);
  158. $roleId = $role->id;
  159. $avatar = $admin->avatar;
  160. $name = $admin->name;
  161. $staffData = [
  162. 'adminId' => $adminId,
  163. 'mainId' => $hdMainId,
  164. 'remind' => 0,
  165. 'roleId' => $roleId,
  166. 'avatar' => $avatar,
  167. 'name' => $name,
  168. 'super' => 1,
  169. 'finance' => 0,
  170. 'switchShop' => 0,
  171. 'founder' => 1,
  172. 'mobile' => $mobile,
  173. ];
  174. self::add($staffData);
  175. } else {
  176. foreach ($staffList as $myStaff) {
  177. $myStaff->delStatus = 0;
  178. $myStaff->save();
  179. }
  180. }
  181. if (count($staffList) > 1) {
  182. util::fail('账号异常,请联系管理员,异常编号56988');
  183. }
  184. $hasShop = 1;
  185. }
  186. //把 xhHdDevice 与 xhGhsDevice 表下的数据更新为退出(删除)
  187. $ghsDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['adminId'=>$admin->id, 'mainId'=>$staff->mainId], null, 'id,status,clientId', null, true);
  188. foreach ($ghsDevices as $ghsDevice) {
  189. $ghsDevice->status = 0;
  190. $ghsDevice->clientId = $ghsDevice->clientId . '_delete_' . date('Y-m-d H:i:s');
  191. $ghsDevice->save();
  192. }
  193. $hdDevices = \bizHd\device\classes\HdDeviceClass::getAllByCondition(['adminId'=>$admin->id, 'mainId'=>$staff->mainId], null, 'id,status,clientId',null, true);
  194. foreach ($hdDevices as $hdDevice) {
  195. $hdDevice->status = 0;
  196. $hdDevice->clientId = $hdDevice->clientId . '_delete_' . date('Y-m-d H:i:s');
  197. $hdDevice->save();
  198. }
  199. return ['hasShop' => $hasShop];
  200. }
  201. }