ShopRostering.Class.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. <?php
  2. /**
  3. * 商铺管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/31
  7. * Time: 15:02
  8. */
  9. namespace JinDouYun\Controller\Shop;
  10. use AlibabaCloud\Credentials\Providers\ChainProvider;
  11. use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Models\GetProcessConfigResponseBody\result\handSignConf;
  12. use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Models\QuerySchemaByProcessCodeResponseBody\result\schemaContent\items\props\push;
  13. use JinDouYun\Controller\DingController;
  14. use JinDouYun\Dao\Department\DStaff;
  15. use JinDouYun\Dao\Shop\DShopGroup;
  16. use JinDouYun\Dao\Shop\DShopGroupRecord;
  17. use JinDouYun\Dao\Shop\DShopRostering;
  18. use JinDouYun\Model\Department\MStaff;
  19. use JinDouYun\Model\Enterprise\MEnterprise;
  20. use JinDouYun\Model\Shop\MShopGroup;
  21. use JinDouYun\Model\Shop\MShopProject;
  22. use JinDouYun\Model\Shop\MShopRostering;
  23. use JinDouYun\Model\Shop\MShopTemplate;
  24. use Mall\Framework\Core\ErrorCode;
  25. use Mall\Framework\Core\ResultWrapper;
  26. use Mall\Framework\Core\StatusCode;
  27. use JinDouYun\Cache\ShopCache;
  28. use JinDouYun\Controller\BaseController;
  29. use JinDouYun\Model\Shop\MShop;
  30. use JinDouYun\Cache\TempSaveCache;
  31. class ShopRostering extends BaseController
  32. {
  33. private $obj;
  34. private $objShopCache;
  35. private $objTempSaveCache;
  36. private $Mrostering;
  37. private $MTem;
  38. private $MStaffRewardDesc;
  39. private $MGroup;
  40. private $staff;
  41. public function __construct($isCheckAcl = true, $isMustLogin = true)
  42. {
  43. parent::__construct($isCheckAcl, $isMustLogin);
  44. $this->obj = new MShopRostering($this->onlineEnterpriseId, $this->onlineUserId);
  45. $this->staff = new MStaff($this->onlineEnterpriseId, $this->onlineUserId);
  46. $this->Mrostering = new MShopRostering($this->onlineEnterpriseId, $this->onlineUserId);
  47. $this->MTem = new MShopTemplate($this->onlineEnterpriseId, $this->onlineUserId);
  48. $this->MStaffRewardDesc = '';
  49. $this->objShopCache = new ShopCache();
  50. $this->objTempSaveCache = new TempSaveCache();
  51. $this->MGroup = new MShopGroup($this->onlineEnterpriseId, $this->onlineUserId);
  52. }
  53. /**
  54. * 添加和编辑商铺管理公共字段处理方法
  55. *
  56. * @return array
  57. */
  58. public function commonFieldFilter(){
  59. $params = $this->request->getRawJson();
  60. if( empty($params) ){
  61. $this->sendOutput('参数为空', ErrorCode::$paramError );
  62. }
  63. $shopData = [
  64. 'template_id' => isset($params['template_id']) ? $params['template_id'] : '',
  65. 'uid' => isset($params['uid']) ? $params['uid'] : '',
  66. ];
  67. //非暂存则验空
  68. if (!isset($params['tempSave']) || $params['tempSave'] == false) {
  69. foreach($shopData as $key => $value){
  70. if(empty($value) && $value !== 0){
  71. $this->sendOutput($key.'参数错误', ErrorCode::$paramError );
  72. }
  73. }
  74. }
  75. $shopData['start_time']= isset($params['start_time']) ? $params['start_time'] : false;
  76. $shopData['end_time']= isset($params['end_time']) ? $params['end_time'] : false;
  77. $shopData['name']= isset($params['name']) ? $params['name'] : false;
  78. return $shopData;
  79. }
  80. /**
  81. * 列表
  82. */
  83. public function list()
  84. {
  85. $params = $this->request->getRawJson();
  86. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  87. $selectParams['limit'] = $pageParams['limit'];
  88. $selectParams['offset'] = $pageParams['offset'];
  89. $selectParams['shop_id'] = $this->shopId;
  90. //昵称
  91. if(isset($params['nickname']) && !empty($params['nickname'])){
  92. $selectParams['nickname'] = $params['nickname'];
  93. }
  94. //uid
  95. if(isset($params['uid']) && !empty($params['uid'])){
  96. $selectParams['uid'] = $params['uid'];
  97. }
  98. // 时间
  99. if(isset($params['start_time']) && !empty($params['start_time']) and isset($params['end_time']) && !empty($params['end_time'])){
  100. $response = [];
  101. $dt_start = strtotime($params['start_time']);
  102. $dt_end = strtotime($params['end_time']);
  103. while ($dt_start <= $dt_end) {
  104. array_push($response, date('Y-m-d', $dt_start));
  105. $dt_start = strtotime('+1 day', $dt_start);
  106. }
  107. $selectParams['time'] = $response;
  108. }
  109. $result = $this->obj->list($selectParams);
  110. if ($result->isSuccess()) {
  111. $returnData = $result->getData();
  112. $pageData = [
  113. 'pageIndex' => $params['page'],
  114. 'pageSize' => $params['pageSize'],
  115. 'pageTotal' => $returnData['total'],
  116. ];
  117. parent::sendOutput($returnData['data'], 0, $pageData);
  118. } else {
  119. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  120. }
  121. }
  122. /**
  123. * 添加
  124. * @throws \Exception
  125. */
  126. public function add()
  127. {
  128. $addStaffData = $this->commonFieldFilter();
  129. $response = [];
  130. $dt_start = strtotime($addStaffData['start_time']);
  131. $dt_end = strtotime($addStaffData['end_time']);
  132. while ($dt_start <= $dt_end) {
  133. array_push($response, date('Y-m-d', $dt_start));
  134. $dt_start = strtotime('+1 day', $dt_start);
  135. }
  136. unset($addStaffData['start_time']);
  137. unset($addStaffData['end_time']);
  138. $addStaffData['times'] = $response;
  139. $addStaffData['shop_id'] = $this->shopId;
  140. $result = $this->obj->insert($addStaffData);
  141. if ($result->isSuccess()) {
  142. parent::sendOutput($result->getData());
  143. } else {
  144. parent::sendOutput($result->getData(), $result->getErrorCode());
  145. }
  146. }
  147. /**
  148. * 详情
  149. * @return void
  150. */
  151. public function details()
  152. {
  153. $where = [];
  154. $id = $this->request->param('id');
  155. if(!empty($id)){
  156. $where['id'] = $id;
  157. }
  158. $result = $this->obj->details($where);
  159. if ($result->isSuccess()) {
  160. parent::sendOutput($result->getData());
  161. } else {
  162. parent::sendOutput($result->getData(), $result->getErrorCode());
  163. }
  164. }
  165. /**
  166. * 修改
  167. * @return void
  168. */
  169. public function update()
  170. {
  171. $id['id'] = $this->request->param('id');
  172. if (empty($id['id'])) {
  173. $this->sendOutput('参数为空', ErrorCode::$paramError);
  174. }
  175. $params = $this->commonFieldFilter();
  176. unset($params['start_time']);
  177. unset($params['end_time']);
  178. unset($params['uid']);
  179. $result = $this->obj->update($params, $id);
  180. if ($result->isSuccess()) {
  181. parent::sendOutput($result->getData());
  182. } else {
  183. parent::sendOutput($result->getData(), $result->getErrorCode());
  184. }
  185. }
  186. public function delete()
  187. {
  188. $id['id'] = $this->request->param('id');
  189. if (empty($id['id'])) {
  190. $this->sendOutput('参数为空', ErrorCode::$paramError);
  191. }
  192. $result = $this->obj->delete($id);
  193. if ($result->isSuccess()) {
  194. parent::sendOutput($result->getData());
  195. } else {
  196. parent::sendOutput($result->getData(), $result->getErrorCode());
  197. }
  198. }
  199. /**
  200. * 班次添加
  201. * @return void
  202. */
  203. public function add_shift()
  204. {
  205. $param = $this->request->getRawJson();
  206. // $user = $this->staff->getStaffInfo(['userCenterId' => $this->onlineUserId, 'shopId' => $this->shopId])->getData();
  207. // if (empty($user['userId'])) parent::sendOutput('该账号未绑定钉钉', 1005);
  208. // if (!$user) parent::sendOutput('不是该店铺店员', 1005);
  209. $url = 'https://oapi.dingtalk.com/topapi/attendance/shift/add?access_token='.$this->voucher();
  210. $param['op_user_id'] = empty($user['userId']) ? '' : $user['userId'];
  211. $param['shift']['owner'] = empty($user['userId']) ? '' : $user['userId'];
  212. $data = json_encode($param);
  213. $time = [];
  214. foreach ($param['shift']['sections'] as $item)
  215. {
  216. $ti = [];
  217. foreach ($item['times'] as $vo)
  218. {
  219. $ti[] = explode(' ', $vo['check_time'])[1];
  220. }
  221. $time[] = $ti;
  222. }
  223. $res = $this->post_json($url, $data);
  224. $res = json_decode($res);
  225. if ($res->errcode > 0){
  226. parent::sendOutput($res->errmsg, 1);
  227. }
  228. $template = [
  229. 'shop_id' => $this->shopId,
  230. 'name' => $param['shift']['name'],
  231. 'shift_id' => $res->result->id,
  232. 'time' => json_encode($time),
  233. 'single_time' => $param['shift']['single_time']
  234. ];
  235. $this->MTem->insert($template);
  236. parent::sendOutput('添加成功');
  237. }
  238. /**
  239. * 班次列表
  240. * @return void
  241. */
  242. public function shift_list()
  243. {
  244. $params = $this->request->getRawJson();
  245. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  246. $selectParams['limit'] = $pageParams['limit'];
  247. $selectParams['offset'] = $pageParams['offset'];
  248. // $param['cursor'] = $this->request->param('cursor');
  249. // $url = 'https://oapi.dingtalk.com/topapi/attendance/shift/list?access_token='.$this->voucher();
  250. // $user = $this->staff->getStaffInfo(['userCenterId' => $this->onlineUserId, 'shopId' => $this->shopId])->getData();
  251. // $param['op_user_id'] = '';
  252. // $param = json_encode($param);
  253. // $res = $this->post_json($url, $param);
  254. // $res = json_decode($res);
  255. $selectParams['shop_id'] = $this->shopId;
  256. $result = $this->MTem->list($selectParams);
  257. if ($result->isSuccess()) {
  258. $returnData = $result->getData();
  259. $pageData = [
  260. 'pageIndex' => $params['page'],
  261. 'pageSize' => $params['pageSize'],
  262. 'pageTotal' => $returnData['total'],
  263. ];
  264. parent::sendOutput($returnData['data'], 0,$pageData);
  265. } else {
  266. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  267. }
  268. }
  269. /**
  270. * 班次详情
  271. * @return void
  272. */
  273. public function shift_details()
  274. {
  275. $where = [];
  276. $id = $this->request->param('id');
  277. if(!empty($id)){
  278. $where['id'] = $id;
  279. }
  280. $result = $this->MTem->details($where);
  281. if ($result->isSuccess()) {
  282. parent::sendOutput($result->getData());
  283. } else {
  284. parent::sendOutput($result->getData(), $result->getErrorCode());
  285. }
  286. // $url = 'https://oapi.dingtalk.com/topapi/attendance/shift/query?access_token='.$this->voucher();
  287. //
  288. // $param['op_user_id'] = '';
  289. // $param['shift_id'] = $id;
  290. // $param = json_encode($param);
  291. // $res = $this->post_json($url, $param);
  292. // $res = json_decode($res);
  293. // if ($res->errcode > 0){
  294. // parent::sendOutput($res->errmsg, 1);
  295. // }
  296. // $res = $res->result;
  297. // parent::sendOutput($res);
  298. }
  299. /**
  300. * 删除班次
  301. * @return void
  302. */
  303. public function shift_delete()
  304. {
  305. $id = $this->request->param('id');
  306. $url = 'https://oapi.dingtalk.com/topapi/attendance/shift/delete?access_token='.$this->voucher();
  307. $param['op_user_id'] = '';
  308. $param['shift_id'] = $id;
  309. $param = json_encode($param);
  310. $res = $this->post_json($url, $param);
  311. $res = json_decode($res);
  312. if ($res->errcode > 0){
  313. parent::sendOutput($res->errmsg, 1);
  314. }
  315. $this->MTem->delete(['shop_id' => $this->shopId, 'shift_id' => $id]);
  316. parent::sendOutput('删除成功');
  317. }
  318. /**
  319. * 创建考勤组
  320. * @return void
  321. */
  322. public function add_group()
  323. {
  324. $param = $this->request->getRawJson();
  325. // $user = $this->staff->getStaffInfo(['userCenterId' => $this->onlineUserId])->getData();
  326. // if (!$user) parent::sendOutput('不是该店铺店员', 1005);
  327. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/add?access_token='.$this->voucher();
  328. if(empty($param) || empty($param["shift_vo_list"])){
  329. parent::sendOutput('参数错误', 1005);
  330. }
  331. $data = [
  332. 'op_user_id' => '',
  333. 'top_group' => [
  334. 'owner' => '',
  335. 'type' => 'TURN',
  336. 'enable_face_check' => 'true',
  337. 'name' => $param['name'],
  338. 'enable_next_day' => 'true',
  339. 'enable_position_ble' => 'true',
  340. ],
  341. ];
  342. foreach (explode(',', $param['shift_vo_list']) as $item){
  343. $data['top_group']['shift_vo_list'][] =[
  344. 'id' => $item
  345. ];
  346. }
  347. var_dump(explode(',',trim($param['staff_id'])));
  348. exit;
  349. foreach (explode(',',$param['staff_id']) as $item){
  350. $user = $this->staff->getStaffInfo(['id' => $item])->getData();
  351. $data['top_group']['members'][] =[
  352. 'role' => 'Attendance',
  353. 'type' => 'StaffMember',
  354. 'user_id' => $user['userId']
  355. ];
  356. }
  357. $data = json_encode($data);
  358. $res = $this->post_json($url, $data);
  359. $res = json_decode($res);
  360. if ($res->errcode > 0){
  361. if ($res->errcode == 850015){
  362. parent::sendOutput('考勤组名称已存在', 1005);
  363. }else{
  364. parent::sendOutput($res->errmsg, 1005);
  365. }
  366. }
  367. $insert = [
  368. 'name' => $param['name'],
  369. 'shop_id' => $this->shopId,
  370. 'staff_id' => $param['staff_id'],
  371. 'shift_vo_list' => $param['shift_vo_list'],
  372. 'group_id' => $res->result->id
  373. ];
  374. $this->MGroup->insert($insert);
  375. parent::sendOutput('添加成功');
  376. }
  377. /**
  378. * 考勤组列表
  379. * @return void
  380. */
  381. public function group_list()
  382. {
  383. // $url = 'https://oapi.dingtalk.com/topapi/attendance/group/minimalism/list?access_token='.$this->voucher();
  384. // $param['cursor'] = $this->request->param('cursor');
  385. //
  386. // $param['op_user_id'] = '';
  387. // $param = json_encode($param);
  388. // $res = $this->post_json($url, $param);
  389. //
  390. // $res = json_decode($res);
  391. //
  392. // if ($res->errcode > 0){
  393. // parent::sendOutput($res->errmsg, 1);
  394. // }
  395. // if (isset($res->result->result)){
  396. // $res = $res->result->result;
  397. // }else{
  398. // $res = [];
  399. // }
  400. // parent::sendOutput($res);
  401. $params = $this->request->getRawJson();
  402. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  403. $selectParams['limit'] = $pageParams['limit'];
  404. $selectParams['offset'] = $pageParams['offset'];
  405. // $param['cursor'] = $this->request->param('cursor');
  406. // $url = 'https://oapi.dingtalk.com/topapi/attendance/shift/list?access_token='.$this->voucher();
  407. // $user = $this->staff->getStaffInfo(['userCenterId' => $this->onlineUserId, 'shopId' => $this->shopId])->getData();
  408. // $param['op_user_id'] = '';
  409. // $param = json_encode($param);
  410. // $res = $this->post_json($url, $param);
  411. // $res = json_decode($res);
  412. $selectParams['shop_id'] = $this->shopId;
  413. $result = $this->MGroup->list($selectParams);
  414. if ($result->isSuccess()) {
  415. $returnData = $result->getData();
  416. $pageData = [
  417. 'pageIndex' => $params['page'],
  418. 'pageSize' => $params['pageSize'],
  419. 'pageTotal' => $returnData['total'],
  420. ];
  421. foreach ($returnData['data'] as &$item){
  422. $item['group_user'] = $this->staff->getAllStaffDatas([['id', 'in', $item['staff_id']],['shopId', '=',$this->shopId]])->getData();
  423. $item['shift'] = $this->MTem->list([['shift_id', 'in' ,$item['shift_vo_list']], 'limit' => 0, 'offset' => 100])->getData()['data'];
  424. }
  425. parent::sendOutput($returnData['data'], 0,$pageData);
  426. } else {
  427. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  428. }
  429. }
  430. /**
  431. * 考勤组详情
  432. * @return void
  433. */
  434. public function group_details()
  435. {
  436. $id = $this->request->param('id');
  437. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/query?access_token='.$this->voucher();
  438. $param['op_user_id'] = '';
  439. $param['group_id'] = $id;
  440. $param = json_encode($param);
  441. $res = $this->post_json($url, $param);
  442. $res = json_decode($res);
  443. if ($res->errcode > 0){
  444. parent::sendOutput($res->errmsg, 1);
  445. }
  446. $res = $res->result;
  447. foreach ($res->shift_ids as $key => $item)
  448. {
  449. $url1 = 'https://oapi.dingtalk.com/topapi/attendance/shift/query?access_token='.$this->voucher();
  450. $param1['op_user_id'] = '';
  451. $param1['shift_id'] = $item;
  452. $res1 = $this->post_json($url1, json_encode($param1));
  453. $res1 = json_decode($res1);
  454. $res->shift_ids[$key] = ['name' => $res1->result->name, 'id' => $res1->result->id];
  455. }
  456. $group = $this->MGroup->list(['group_id' => $res->id, 'limit' => 0,'offset' => 10])->getData();
  457. foreach ($group['data'] as &$item){
  458. $res->group_user = $this->staff->getAllStaffDatas([['id', 'in', $item['staff_id']],['shopId', '=',$this->shopId]])->getData();
  459. }
  460. parent::sendOutput($res);
  461. }
  462. /**
  463. * 考勤组成员
  464. * @return void
  465. */
  466. public function group_user()
  467. {
  468. $id = $this->request->param('id');
  469. $size = $this->request->param('size');
  470. $cursor = $this->request->param('cursor');
  471. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/member/list?access_token='.$this->voucher();
  472. $param['cursor'] = $cursor;
  473. $param['size'] = $size;
  474. $param['op_user_id'] = '';
  475. $param['group_id'] = $id;
  476. $param = json_encode($param);
  477. $res = $this->post_json($url, $param);
  478. $res = json_decode($res);
  479. if ($res->errcode > 0){
  480. parent::sendOutput($res->errmsg, 1);
  481. }
  482. $res = $res->result->result;
  483. $data = [];
  484. foreach ($res as $item){
  485. $user = $this->staff->getStaffInfo(['userId' => $item->member_id, 'shopId' => $this->shopId])->getData();
  486. if ($user){
  487. $data[] = $user;
  488. }
  489. }
  490. parent::sendOutput($data);
  491. }
  492. /**
  493. * 删除考勤组
  494. * @return void
  495. */
  496. public function group_delete()
  497. {
  498. $id = $this->request->param('id');
  499. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/delete?access_token='.$this->voucher();
  500. $group_key = $this->post_json('https://oapi.dingtalk.com/topapi/attendance/groups/idtokey?access_token='.$this->voucher(),json_encode(['group_id' => $id]));
  501. $group_key = json_decode($group_key);
  502. if ($group_key->errcode > 0){
  503. parent::sendOutput($group_key->errmsg, 1);
  504. }
  505. $param['op_user_id'] = '';
  506. $param['group_key'] = $group_key->result;
  507. $param = json_encode($param);
  508. $res = $this->post_json($url, $param);
  509. $res = json_decode($res);
  510. if ($res->errcode > 0){
  511. parent::sendOutput($res->errmsg, 1);
  512. }
  513. $DGroupRecord = new DShopGroupRecord('default');
  514. $DGroupRecord->delete(['group_id' => $id]);
  515. $this->MGroup->delete(['group_id' => $id]);
  516. parent::sendOutput('删除成功');
  517. }
  518. /**
  519. * 排班
  520. * @return void
  521. */
  522. public function rostering()
  523. {
  524. $param = $this->request->getRawJson();
  525. $dbstaff = new DStaff('default');
  526. $dbstaff->setTable('qianniao_staff_'.$this->onlineEnterpriseId);
  527. $user = $dbstaff->get(['userCenterId' => $this->onlineUserId]);
  528. if (empty($user)) parent::sendOutput('排班只能员工排盘,总帐号无法排班', 1005);
  529. // $user = $user->getData();
  530. if (empty($user['userId'])) parent::sendOutput('该账号未绑定钉钉无法排班', 1005);
  531. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/schedule/async?access_token='.$this->voucher();
  532. $data = [
  533. 'op_user_id' => $user['userId'],
  534. 'group_id' => $param['group_id'],
  535. ];
  536. $DGroupRecord = new DShopGroupRecord('default');
  537. $recordData = [];
  538. foreach ($param['rostering'] as $item)
  539. {
  540. $staff = $this->staff->getStaffInfo(['id' => $item['userId'], 'shopId' => $this->shopId])->getData();
  541. $data['schedules'][] = [
  542. 'shift_id' => $item['shift_id'],
  543. 'work_date' => $this->get_data_format($item['work_date']),
  544. 'is_rest' => $item['is_rest'],
  545. 'userid' => $staff['userId']
  546. ];
  547. $record = $DGroupRecord->get(['group_id' => $param['group_id'], 'time' => strtotime($item['work_date'])]);
  548. if (empty($record)){
  549. $recordData = [
  550. 'shop_id' => $this->shopId,
  551. 'group_id' => $param['group_id'],
  552. 'time' => strtotime($item['work_date'])
  553. ];
  554. $DGroupRecord->insert($recordData);
  555. }
  556. if ($item['shift_id'] > 1){
  557. $time = $this->post_json('https://oapi.dingtalk.com/topapi/attendance/shift/query?access_token='.$this->voucher(), json_encode(['shift_id' => $item['shift_id']]));
  558. $time = json_decode($time)->result->sections;
  559. $time_slot = [];
  560. foreach ($time as $vo)
  561. {
  562. $time_slot[] = [explode(' ', $vo->punches[0]->check_time)[1], explode(' ', $vo->punches[1]->check_time)[1]];
  563. }
  564. $roData[] = [
  565. 'name' => $item['name'],
  566. 'shop_id' => $this->shopId,
  567. 'template_id' => $item['shift_id'],
  568. 'uid' => $staff['id'],
  569. 'time' => $item['work_date'],
  570. 'time_slot' => json_encode($time_slot)
  571. ];
  572. }else{
  573. $roData[] = [
  574. 'name' => $item['name'],
  575. 'shop_id' => $this->shopId,
  576. 'template_id' => $item['shift_id'],
  577. 'uid' => $staff['id'],
  578. 'time' => $item['work_date'],
  579. 'time_slot' => ''
  580. ];
  581. }
  582. }
  583. $data = json_encode($data);
  584. $res = $this->post_json($url, $data);
  585. $res = json_decode($res);
  586. if ($res->errcode > 0){
  587. parent::sendOutput($res->errmsg, 1);
  588. }
  589. foreach ($roData as $item)
  590. {
  591. $res = $this->Mrostering->insert($item);
  592. }
  593. parent::sendOutput('添加成功');
  594. }
  595. /**
  596. * 删除考勤组用户
  597. * @return void
  598. */
  599. public function remove()
  600. {
  601. $param = $this->request->getRawJson();
  602. // $user = $this->staff->getStaffInfo(['userCenterId' => $this->onlineUserId, 'shopId' => $this->shopId])->getData();
  603. // if (!$user) parent::sendOutput('不是该店铺店员', 1);
  604. if (!$param['id']) parent::sendOutput('传入考情组id');
  605. if (!$param['staff_id']) parent::sendOutput('请选择删除用户');
  606. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/users/remove?access_token='.$this->voucher();
  607. $uids = [];
  608. foreach (explode(',', $param['staff_id']) as $item){
  609. $staff = $this->staff->getStaffInfo(['id' => $item, 'shopId' => $this->shopId])->getData();
  610. if(!empty($staff['userId'])){
  611. $uids[] = $staff['userId'];
  612. }
  613. }
  614. if(empty($uids)){
  615. parent::sendOutput("员工未绑定钉钉账号", 1);
  616. }
  617. $data = [
  618. 'group_key' => $this->group_key($param['id']),
  619. 'user_id_list' => implode(',', $uids),
  620. ];
  621. $data = json_encode($data);
  622. $res = $this->post_json($url, $data);
  623. $res = json_decode($res);
  624. if ($res->errcode > 0){
  625. parent::sendOutput($res->errmsg, 1);
  626. }
  627. $group = $this->MGroup->details(['group_id' => $param['id']])->getData();
  628. if (empty($group)) parent::sendOutput('考勤组不存在', 1005);
  629. $staff_id = explode( ',', $group['staff_id']);
  630. foreach ($staff_id as $k => $v)
  631. {
  632. if ($v == $param['staff_id']){
  633. unset($staff_id[$k]);
  634. }
  635. }
  636. $staff_id = implode(',', $staff_id);
  637. $this->MGroup->update(['staff_id' => $staff_id], $group['id']);
  638. parent::sendOutput('删除成功');
  639. }
  640. public function user_add()
  641. {
  642. $param = $this->request->getRawJson();
  643. if (!$param['id']) parent::sendOutput('传入考情组id');
  644. if (!$param['staff_id']) parent::sendOutput('请选择删除用户');
  645. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/users/add?access_token='.$this->voucher();
  646. $uids = [];
  647. foreach (explode(',', $param['staff_id']) as $item){
  648. $staff = $this->staff->getStaffInfo(['id' => $item, 'shopId' => $this->shopId])->getData();
  649. $uids[] = $staff['userId'];
  650. }
  651. $data = [
  652. 'group_key' => $this->group_key($param['id']),
  653. 'user_id_list' => implode(',', $uids),
  654. ];
  655. $data = json_encode($data);
  656. $res = $this->post_json($url, $data);
  657. $res = json_decode($res);
  658. if ($res->errcode > 0){
  659. parent::sendOutput($res->errmsg, 1005);
  660. }
  661. $group = $this->MGroup->details(['group_id' => $param['id']])->getData();
  662. if (empty($group)) parent::sendOutput('考勤组不存在', 1005);
  663. $staff_id = explode( ',', $group['staff_id']);
  664. foreach (explode(',', $param['staff_id']) as $v)
  665. {
  666. $staff_id[] = $v;
  667. }
  668. $staff_id = implode(',', $staff_id);
  669. $this->MGroup->update(['staff_id' => $staff_id], $group['id']);
  670. parent::sendOutput('新增成功');
  671. }
  672. /**
  673. *排班详情
  674. * @return void
  675. */
  676. public function scheduling_details()
  677. {
  678. $param = $this->request->getRawJson();
  679. // $user = $this->staff->getStaffInfo(['userCenterId' => $this->onlineUserId, 'shopId' => $this->shopId])->getData();
  680. // if (!$user) parent::sendOutput('不是该店铺店员', 1);
  681. // $staff = $this->staff->getStaffInfo(['userCenterId' => $param['uid'], 'shopId' => $this->shopId])->getData();
  682. if (isset($param['uid'])){
  683. $uid = $param['uid'];
  684. }
  685. $pageParams = pageToOffset($param['page'] ?: 1, $param['pageSize'] ?: 10);
  686. $limit = $pageParams['limit'];
  687. $offset = $pageParams['offset'];
  688. $month = $param['month'] ? $param['month'] : date('Y-m', time());
  689. $where = [];
  690. if (isset($uid) and !empty($uid)){
  691. $where[] = ['uid', '=', $uid];
  692. }
  693. if ($month){
  694. $where[] = ['time', 'like', '%'.$month.'%'];
  695. }
  696. if (isset($param['export']) and !empty($param['export'])){
  697. $where['export'] = $param['export'];
  698. }
  699. $where[] = ['shop_id', '=', $this->shopId];
  700. $rostering = $this->Mrostering->select($where,'*', 'time DESC', $limit, $offset);
  701. if ($rostering->isSuccess()) {
  702. $returnData = $rostering->getData();
  703. $pageData = [
  704. 'pageIndex' => $offset,
  705. 'pageSize' => $limit,
  706. 'pageTotal' => $returnData['total'],
  707. ];
  708. parent::sendOutput($returnData['data'], 0, $pageData);
  709. } else {
  710. parent::sendOutput($rostering->getData(), ErrorCode::$dberror);
  711. }
  712. }
  713. public function obtain_clock()
  714. {
  715. $data = $this->request->getRawJson();
  716. if ($data['time']){
  717. $today = $data['time'];
  718. }else{
  719. $today = date('Y-m-d', strtotime('today'));
  720. }
  721. $url = 'https://oapi.dingtalk.com/attendance/listRecord?access_token='.$this->voucher();
  722. $rostering = $this->Mrostering->details([['time', 'like', '%'.$today.'%'], ['uid', '=' , $data['uid']]]);
  723. if (!$rostering->isSuccess()){
  724. parent::sendOutput('今天未有班次', ErrorCode::$dberror);
  725. }
  726. $rostering = $rostering->getData();
  727. $staff = $this->staff->getStaffInfo(['id' => $data['uid'], 'shopId' => $this->shopId])->getData();
  728. $data['userIds'] = [$staff['userId']];
  729. $data['checkDateFrom'] = date('Y-m-d H:i:s', strtotime($today));
  730. $data['checkDateTo'] = date('Y-m-d H:i:s', strtotime($today) + 86400);
  731. $res = $this->post_json($url, json_encode($data));
  732. $res = json_decode($res);
  733. $recordresult = $res->recordresult;
  734. $time_slot = json_decode($rostering['time_slot']);
  735. $clock = $rostering['clock']? json_decode( $rostering['clock']) : [];
  736. $num = 0;
  737. foreach ($time_slot as $item)
  738. {
  739. foreach ($item as $vo)
  740. {
  741. $times = strtotime($today.' '.$vo) * 1000;
  742. $count = count($recordresult);
  743. $time = [];
  744. for ($i=0;$i < $count; $i++){
  745. if (!isset($recordresult[$i]->invalidRecordType)){
  746. if ($times == $recordresult[$i]->baseCheckTime){
  747. $time = $recordresult[$i];
  748. }
  749. }
  750. }
  751. if ($time){
  752. if (isset($clock[$num])){
  753. if ($clock[$num]->timeResult == 'NotSigned'){
  754. $clock[$num]->userCheckTime = date('Y-m-d H:i:s', $time->userCheckTime/1000);
  755. $clock[$num]->timeResult = $time->timeResult;
  756. }
  757. }else{
  758. $clock[] = json_decode(json_encode(['baseCheckTime' => date('Y-m-d H:i:s', strtotime($today.' '.$vo)),'userCheckTime' => date('Y-m-d H:i:s', $time->userCheckTime/1000), 'timeResult' => $time->timeResult]));
  759. }
  760. }else{
  761. if (strtotime($vo) < time() and !isset($clock[$num])){
  762. $clock[] = json_decode(json_encode(['baseCheckTime' => date('Y-m-d H:i:s', strtotime($today.' '.$vo)), 'userCheckTime' => '','timeResult' => 'NotSigned']));
  763. }
  764. }
  765. $num++;
  766. }
  767. }
  768. $status = 0;
  769. $price = 0;//打卡金额
  770. if ($num == count($clock)){
  771. foreach ($clock as $item){
  772. if ($rostering['status'] == 0){
  773. if ($item->timeResult == 'Normal'){
  774. $tem_price = $this->MTem->details(['shop_id' => $this->shopId, 'shift_id' => $rostering['template_id']])->getData();
  775. $price += $tem_price['single_time'];
  776. }
  777. }
  778. if ($item->timeResult != 'Normal') $status = -1;// 不正常今天的打卡未完成
  779. }
  780. if ($status == 0) $status = 1;// 如果都打卡正常那么打完完成
  781. }
  782. if (strtotime($today) < time()){
  783. if (empty($clock)) $status = -1;
  784. }
  785. if ($price > 0 and $staff['is_clock'] == 1){
  786. $balance = $staff['reward'] + $price;
  787. $rewardTotal = $staff['rewardTotal'] + $price;
  788. $this->staff->clockReward($price, $balance, $staff['userCenterId'], $staff['id'], $this->shopId, $rewardTotal);
  789. }
  790. if (empty($clock)){
  791. $res = $this->Mrostering->update(['status' => $status], $rostering['id']);
  792. }else{
  793. $res = $this->Mrostering->update(['status' => $status, 'clock' => json_encode($clock)], $rostering['id']);
  794. }
  795. parent::sendOutput('更新成功');
  796. }
  797. public function get_data_format($time)
  798. {
  799. list($usec) = explode(".", $time);
  800. $date = strtotime($usec);
  801. $return_data = str_pad($date,13,"0",STR_PAD_RIGHT); //不足13位。右边补0
  802. return $return_data;
  803. }
  804. /**
  805. * 排班记录
  806. * @return void
  807. */
  808. public function group_record()
  809. {
  810. $param = $this->request->getRawJson();
  811. $DGroupRecord = new DShopGroupRecord('default');
  812. $pageParams = pageToOffset($param['page'] ?: 1, $param['pageSize'] ?: 10);
  813. $limit = $pageParams['limit'];
  814. $offset = $pageParams['offset'];
  815. // $monthFirstDay = strtotime(date( 'Y-m-1 00:00:00', strtotime($param['time'])));
  816. // $mdays = date( 't', strtotime($param['time']) );
  817. // $monthLastDay = strtotime(date( 'Y-m-' . $mdays . ' 23:59:59', strtotime($param['time']) ));
  818. //
  819. // $where[] = ['time', '>=', $monthFirstDay];
  820. // $where[] = ['time', '<=', $monthLastDay];
  821. // $where[] = ['a.group_id', '=', $param['group_id']];
  822. $where = [];
  823. $where[] = ['a.shop_id', '=', $this->shopId];
  824. if ($param['name']) $where[] = ['b.name', '=', $param['name']];
  825. $join = 'Left Join qianniao_shop_group as b ON a.group_id = b.group_id';
  826. $list = $DGroupRecord->select($where, 'a.*,b.name,b.staff_id,b.shift_vo_list', null, $limit, $offset, [], true, false, $join);
  827. $count = $DGroupRecord->count($where, [], $join);
  828. foreach ($list as &$item){
  829. $item['time'] = date('Y-m-d', $item['time']);
  830. $item['group_user'] = $this->staff->getAllStaffDatas([['id', 'in', $item['staff_id']],['shopId', '=',$this->shopId]])->getData();
  831. $item['shift'] = $this->MTem->list([['shift_id', 'in', $item['shift_vo_list']], 'limit' => 0, 'offset' => 100])->getData()['data'];
  832. }
  833. $pageData = [
  834. 'pageIndex' => $param['page'],
  835. 'pageSize' => $param['pageSize'],
  836. 'pageTotal' => $count,
  837. ];
  838. parent::sendOutput($list, 0, $pageData);
  839. }
  840. }