Staff.Class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. /**
  3. * 职工管理Controller
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/10/31
  7. * Time: 18:00
  8. */
  9. namespace JinDouYun\Controller\Department;
  10. use JinDouYun\Dao\Department\DStaffReflect;
  11. use JinDouYun\Model\Department\MDepartment;
  12. use JinDouYun\Model\System\MBasicSetup;
  13. use Mall\Framework\Core\ErrorCode;
  14. use Mall\Framework\Core\StatusCode;
  15. use JinDouYun\Cache\EnterpriseCache;
  16. use JinDouYun\Cache\RoleAclCache;
  17. use JinDouYun\Controller\BaseController;
  18. use JinDouYun\Model\Department\MStaff;
  19. class Staff extends BaseController
  20. {
  21. private $objMStaff;
  22. private $objRoleAclCache;
  23. private $objEnterpriseCache;
  24. private $objMDepartment;
  25. private $basic;
  26. public function __construct($isCheckAcl = true, $isMustLogin = true)
  27. {
  28. parent::__construct($isCheckAcl, $isMustLogin);
  29. $this->objMStaff = new MStaff($this->onlineEnterpriseId, $this->onlineUserId);
  30. $this->objRoleAclCache = new RoleAclCache();
  31. $this->objEnterpriseCache = new EnterpriseCache();
  32. $this->objMDepartment = new MDepartment($this->onlineEnterpriseId);
  33. $this->basic = new MBasicSetup($this->onlineEnterpriseId);
  34. }
  35. /**
  36. * 获取参数
  37. *
  38. * @return array
  39. */
  40. public function commonFieldFilter()
  41. {
  42. $params = $this->request->getRawJson();
  43. if (empty($params)) {
  44. $this->sendOutput('参数为空', ErrorCode::$paramError);
  45. }
  46. $returnData = [
  47. "departmentId" => isset($params['departmentId']) ? $params['departmentId'] : 0, //int(10) DEFAULT NULL COMMENT '部门id',
  48. "staffName" => isset($params['staffName']) ? $params['staffName'] : '', //varchar(30) DEFAULT NULL COMMENT '员工姓名',
  49. "roleId" => isset($params['roleId']) ? $params['roleId'] : 0, //varchar(255) DEFAULT NULL COMMENT '角色id',
  50. "mobile" => isset($params['mobile']) ? $params['mobile'] : '', //varchar(11) DEFAULT NULL COMMENT '手机号',
  51. "dataField" => isset($params['dataField']) ? json_encode($params['dataField']) : '',
  52. ];
  53. //必填项
  54. foreach ($returnData as $key => $value) {
  55. if (empty($value) && $value !== 0) {
  56. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  57. }
  58. }
  59. //选填项
  60. $returnData['extend'] = [];
  61. isset($params['departmentPidPath']) && $returnData['extend']['departmentPidPath'] = $params['departmentPidPath'];
  62. isset($params['rolePidPath']) && $returnData['extend']['rolePidPath'] = $params['rolePidPath'];
  63. $returnData['email'] = isset($params['email']) ? $params['email'] : '';
  64. $returnData['sex'] = isset($params['sex']) ? $params['sex'] : '';
  65. $returnData['age'] = isset($params['age']) ? $params['age'] : '';
  66. $returnData['education'] = isset($params['education']) ? $params['education'] : '';
  67. $returnData['info'] = isset($params['info']) ? $params['info'] : '';
  68. $returnData['stock_right'] = isset($params['stock_right']) ? $params['stock_right'] : '';
  69. $returnData['evaluate'] = isset($params['evaluate']) ? $params['evaluate'] : '';
  70. $returnData['avatar'] = isset($params['avatar']) ? $params['avatar'] : '';
  71. $returnData['spread_uid'] = isset($params['spread_uid']) ? $params['spread_uid'] : '';
  72. $returnData['is_clock'] = isset($params['is_clock']) ? $params['is_clock'] : 0;
  73. $returnData['is_technician'] = isset($params['is_technician']) ? $params['is_technician'] : 0;
  74. $returnData['extend'] = json_encode($returnData['extend']);
  75. $returnData['updateTime'] = time();
  76. if (isset($params['isSetRule']) && !empty($params['isSetRule'])){
  77. $returnData['isSetRule'] = $params['isSetRule'];
  78. }
  79. if (isset($params['rule']) && !empty($params['rule'])){
  80. $returnData['rule'] = json_encode($params['rule']);
  81. }
  82. return $returnData;
  83. }
  84. /**
  85. * 职工添加
  86. */
  87. public function addStaff()
  88. {
  89. $addStaffData = $this->commonFieldFilter();
  90. $addStaffData['shopId'] = $this->shopId;
  91. // 拉去用户钉钉userID
  92. $data = $this->basic->getBasicSetup($this->onlineEnterpriseId)->getData();
  93. if (isset($data['basicData']['appKey']) || isset($data['basicData']['appSecret'])){
  94. $url = 'https://oapi.dingtalk.com/topapi/v2/user/getbymobile?access_token='.$this->voucher();
  95. $json = [
  96. 'mobile' => $addStaffData['mobile'],
  97. ];
  98. $data = $this->post_json($url, json_encode($json));
  99. $data = json_decode($data);
  100. if ($data->errcode > 0){
  101. $userid = '';
  102. }else{
  103. $userid = $data->result->userid;
  104. }
  105. $addStaffData['userId'] = $userid;
  106. }
  107. $result = $this->objMStaff->addStaff($addStaffData);
  108. if ($result->isSuccess()) {
  109. $resultData = $result->getData();
  110. $userCenterId = $resultData['userCenterId'];
  111. //缓存职工的userCenterId和拥有的数据域
  112. $this->objRoleAclCache->cacheStaffUidAndDataField(
  113. $this->onlineEnterpriseId,
  114. $userCenterId,
  115. json_decode($addStaffData['dataField'], true)
  116. );
  117. //缓存职工和角色组id的关系
  118. $extend = json_decode($addStaffData['extend'], true);
  119. if(isset($extend['rolePidPath'])) {
  120. $rolePidPath = explode(',', $extend['rolePidPath']);
  121. $this->objRoleAclCache->cacheStaffAndRole(
  122. $this->onlineEnterpriseId,
  123. $userCenterId,
  124. $rolePidPath[0]
  125. );
  126. }
  127. //缓存职工的userCenterId和当前企业的关系
  128. $this->objEnterpriseCache->cacheUserCenterIdAndEnterpriseId(
  129. $userCenterId,
  130. StatusCode::$roleType['staff'],
  131. $this->onlineEnterpriseId
  132. );
  133. parent::sendOutput($resultData['title']);
  134. } else {
  135. parent::sendOutput($result->getData(), $result->getErrorCode());
  136. }
  137. }
  138. /**
  139. * 职工删除
  140. */
  141. public function deleteStaff()
  142. {
  143. $params = $this->request->getRawJson();
  144. if (empty($params)) {
  145. $this->sendOutput('参数为空', ErrorCode::$paramError);
  146. }
  147. $deleteData = [
  148. 'id' => isset($params['id']) ? $params['id'] : [],
  149. ];
  150. foreach($deleteData as $key => $value){
  151. if(empty($value)){
  152. parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
  153. }
  154. }
  155. $result = $this->objMStaff->deleteStaff($deleteData);
  156. if ($result->isSuccess()) {
  157. $staffResult = $this->objMStaff->getStaffInfo($deleteData);
  158. $staffInfo = $staffResult->getData();
  159. //删除职工和角色id的关系
  160. $this->objRoleAclCache->deleteStaffRelationRole($this->onlineEnterpriseId, $staffInfo['userCenterId']);
  161. //删除redis里的企业id和员工id的关联
  162. $this->objEnterpriseCache->deleteEnterprise($staffInfo['userCenterId'], $this->onlineEnterpriseId);
  163. parent::sendOutput($result->getData());
  164. } else {
  165. parent::sendOutput($result->getData(), $result->getErrorCode());
  166. }
  167. }
  168. /**
  169. * 职工修改
  170. */
  171. public function updateStaff()
  172. {
  173. $id['id'] = $this->request->param('request_id');
  174. if (empty($id['id'])) {
  175. $this->sendOutput('参数为空', ErrorCode::$paramError);
  176. }
  177. $params = $this->commonFieldFilter();
  178. $result = $this->objMStaff->updateStaff($params, $id);
  179. if ($result->isSuccess()) {
  180. $staffResult = $this->objMStaff->getStaffInfo($id);
  181. $staffInfo = $staffResult->getData();
  182. //缓存职工的userCenterId和拥有的数据域
  183. $test = $this->objRoleAclCache->cacheStaffUidAndDataField(
  184. $this->onlineEnterpriseId,
  185. $staffInfo['userCenterId'],
  186. $staffInfo['dataField']
  187. );
  188. //缓存职工和角色id的关系
  189. if(isset($staffInfo['extend']) && isset($staffInfo['extend']['rolePidPath'])) {
  190. $rolePidPath = explode(',',$staffInfo['extend']['rolePidPath']);
  191. $this->objRoleAclCache->cacheStaffAndRole(
  192. $this->onlineEnterpriseId,
  193. $staffInfo['userCenterId'],
  194. $rolePidPath[0]
  195. );
  196. }
  197. parent::sendOutput($result->getData());
  198. } else {
  199. parent::sendOutput($result->getData(), $result->getErrorCode());
  200. }
  201. }
  202. /**
  203. * 职工列表
  204. */
  205. public function getAllStaff()
  206. {
  207. $params = $this->request->getRawJson();
  208. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  209. $selectParams['limit'] = $pageParams['limit'];
  210. $selectParams['offset'] = $pageParams['offset'];
  211. //部门id
  212. if(isset($params['departmentId']) && !empty($params['departmentId'])){
  213. $selectParams['departmentId'] = $params['departmentId'];
  214. }
  215. //角色id
  216. if(isset($params['roleId']) && !empty($params['roleId'])){
  217. $selectParams['roleId'] = $params['roleId'];
  218. }
  219. //员工姓名
  220. if(isset($params['keyword']) && !empty($params['keyword'])){
  221. $selectParams['staffName'] = $params['keyword'];
  222. }
  223. if (isset($params['signId']) && !empty($params['signId'])){
  224. $selectParams['signId'] = $params['signId'];
  225. }
  226. if (isset($params['shopId']) && !empty($params['shopId'])){
  227. $selectParams['shopId'] = $params['shopId'];
  228. }else{
  229. if($this->shopId){
  230. $selectParams['shopId'] = $this->shopId;
  231. }
  232. }
  233. $result = $this->objMStaff->getAllStaff($selectParams);
  234. if ($result->isSuccess()) {
  235. $returnData = $result->getData();
  236. $pageData = [
  237. 'pageIndex' => $params['page'],
  238. 'pageSize' => $params['pageSize'],
  239. 'pageTotal' => $returnData['total'],
  240. ];
  241. parent::sendOutput($returnData['data'], 0, $pageData);
  242. } else {
  243. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  244. }
  245. }
  246. /**
  247. * 职工详情
  248. */
  249. public function getStaffInfo()
  250. {
  251. $where = [];
  252. $id = $this->request->param('request_id');
  253. if(!empty($id)){
  254. $where['id'] = $id;
  255. }
  256. $params = $this->request->getRawJson();
  257. if(empty($id) && isset($params['userCenterId']) && !empty($params['userCenterId'])){
  258. $where['userCenterId'] = $params['userCenterId'];
  259. }
  260. $result = $this->objMStaff->getStaffInfo($where);
  261. if ($result->isSuccess()) {
  262. parent::sendOutput($result->getData());
  263. } else {
  264. parent::sendOutput($result->getData(), $result->getErrorCode());
  265. }
  266. }
  267. /**
  268. * 根据userCenterId 获取职工详情
  269. * @throws \Exception
  270. */
  271. public function getStaffDataByUserCenterId()
  272. {
  273. $modelResult = $this->objMStaff->getStaffDataByUserCenterId($this->onlineUserId);
  274. if($modelResult->isSuccess()){
  275. parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
  276. }
  277. parent::sendOutput($modelResult->getData());
  278. }
  279. /**
  280. * 获取商铺下的员工
  281. */
  282. public function getStaffByShopId() {
  283. $params = $this->request->getRawJson();
  284. if( empty($params) ){
  285. $this->sendOutput('参数为空', ErrorCode::$paramError );
  286. }
  287. $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
  288. $selectParams['limit'] = $pageParams['limit'];
  289. $selectParams['offset'] = $pageParams['offset'];
  290. if(isset($params['shopId']) && !empty($params['shopId'])){
  291. $selectParams['shopId'] = $params['shopId'];
  292. }else{
  293. parent::sendOutput('shopId参数错误', ErrorCode::$paramError);
  294. }
  295. $result = $this->objMStaff->getStaffByShopId($selectParams);
  296. if(!$result->isSuccess()){
  297. parent::sendOutput($result->getData(), $result->getErrorCode());
  298. }
  299. $returnData = $result->getData();
  300. $pageData = [
  301. 'pageIndex' => $params['page'],
  302. 'pageSize' => $params['pageSize'],
  303. 'pageTotal' => $returnData['total'],
  304. ];
  305. parent::sendOutput($returnData['data'], 0, $pageData);
  306. }
  307. /**
  308. * 提成流水
  309. */
  310. public function getAllStaffRewardDesc()
  311. {
  312. $params = $this->request->getRawJson();
  313. if (empty($params)) {
  314. parent::sendOutput('参数为空', ErrorCode::$paramError);
  315. }
  316. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  317. $selectParams['limit'] = $pageParams['limit'];
  318. $selectParams['offset'] = $pageParams['offset'];
  319. if(isset($params['staffId']) && !empty($params['staffId'])){
  320. $selectParams['staffId'] = $params['staffId'];
  321. }
  322. if(isset($params['source']) && !empty($params['source'])){
  323. $selectParams['source'] = $params['source'];
  324. }
  325. if (isset($params['type']) && !empty($params['type'])){
  326. $selectParams['type'] = $params['type'];
  327. }
  328. if(isset($params['star']) && !empty($params['star']) && isset($params['end']) && !empty($params['end'])){
  329. $selectParams['createTime'] = [
  330. 'star' => $params['star'],
  331. 'end' => $params['end']
  332. ];
  333. }
  334. if(!empty($this->shopId)){
  335. $selectParams['shopId'] = $this->shopId;
  336. }
  337. $result = $this->objMStaff->getAllStaffRewardDesc($selectParams);
  338. if ($result->isSuccess()) {
  339. $returnData = $result->getData();
  340. $pageData = [
  341. 'pageIndex' => $params['page'],
  342. 'pageSize' => $params['pageSize'],
  343. 'pageTotal' => $returnData['total'],
  344. 'zc' => $returnData['zc']['amount'] ?? 0,
  345. 'sr' => $returnData['sr']['amount'] ?? 0,
  346. ];
  347. parent::sendOutput($returnData['data'], 0, $pageData);
  348. } else {
  349. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  350. }
  351. }
  352. /**
  353. * 员工业绩提成修改
  354. */
  355. public function updateStaffTargetAndReward()
  356. {
  357. $id = $this->request->param('request_id');
  358. if (empty($id)) {
  359. $this->sendOutput('参数为空', ErrorCode::$paramError);
  360. }
  361. $params = $this->request->getRawJson();
  362. $data = [];
  363. (isset($params['target']) && !empty($params['target'])) && $data['target'] = $params['target'];
  364. (isset($params['rewardGo']) && !empty($params['rewardGo'])) && $data['rewardGo'] = $params['rewardGo'];
  365. $modelResult = $this->objMStaff->updateStaffData($data, ['id' => $id]);
  366. if($modelResult->isSuccess()){
  367. parent::sendOutput($modelResult->getData());
  368. } else {
  369. parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
  370. }
  371. }
  372. /**
  373. * 员工提现列表
  374. * @return void
  375. */
  376. public function getWithdrawal()
  377. {
  378. $params = $this->request->params();
  379. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  380. $limit = $pageParams['limit'];
  381. $offset = $pageParams['offset'];
  382. if (isset($params['auditStatus']) and !empty($params['auditStatus'])){
  383. $selectParams['auditStatus'] = $params['auditStatus'];
  384. }
  385. if (isset($params['staffId']) and !empty($params['staffId'])){
  386. $selectParams['staffId'] = $params['staffId'];
  387. }
  388. if (isset($params['reflectStatus']) and !empty($params['reflectStatus'])){
  389. $selectParams['reflectStatus'] = $params['reflectStatus'];
  390. }
  391. if (isset($params['end']) and !empty($params['end']) && isset($params['start']) and !empty($params['start'])){
  392. $selectParams[] = ['createTime' , '>=', strtotime($params['start'])];
  393. $selectParams[] = ['createTime' , '<=', strtotime($params['end'])];
  394. }
  395. $db = new DStaffReflect('default');
  396. $list = $db->select($selectParams, '*', 'id DESC', $limit, $offset);
  397. foreach ($list as &$item)
  398. {
  399. $item['reflectInfo'] = json_decode($item['reflectInfo']);
  400. }
  401. $count = $db->count($selectParams);
  402. $pageData = [
  403. 'pageIndex' => $params['page'],
  404. 'pageSize' => $params['pageSize'],
  405. 'pageTotal' => $count,
  406. ];
  407. parent::sendOutput($list, 0, $pageData);
  408. }
  409. /**
  410. * 审核打款接口
  411. * @return void
  412. */
  413. public function examineWithdrawal()
  414. {
  415. $params = $this->request->params();
  416. if (empty($params['id'])) $this->sendOutput('id参数为空', ErrorCode::$paramError);
  417. $db = new DStaffReflect('default');
  418. $data = $db->get(['id' => $params['id']]);
  419. if (isset($params['auditStatus']) and !empty($params['auditStatus'])){
  420. if ($data['auditStatus'] == 2) $this->sendOutput('已审核', ErrorCode::$paramError);
  421. $res = $db->update(['auditStatus' => $params['auditStatus']]);
  422. }
  423. if (isset($params['reflectStatus']) and !empty($params['reflectStatus'])){
  424. if ($data['reflectStatus'] != 4) $this->sendOutput('当前申请已审核完成', ErrorCode::$paramError);
  425. $res = $db->update(['reflectStatus' => $params['reflectStatus']]);
  426. }
  427. if ($res){
  428. $this->sendOutput('成功');
  429. }
  430. $this->sendOutput('失败', ErrorCode::$paramError);
  431. }
  432. }