ShopRostering.Class.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. $data = [
  329. 'op_user_id' => '',
  330. 'top_group' => [
  331. 'owner' => '',
  332. 'type' => 'TURN',
  333. 'enable_face_check' => 'true',
  334. 'name' => $param['name'],
  335. 'enable_next_day' => 'true',
  336. 'enable_position_ble' => 'true',
  337. ],
  338. ];
  339. foreach (explode(',', $param['shift_vo_list']) as $item){
  340. $data['top_group']['shift_vo_list'][] =[
  341. 'id' => $item
  342. ];
  343. }
  344. foreach (explode(',',$param['staff_id']) as $item){
  345. $user = $this->staff->getStaffInfo(['id' => $item])->getData();
  346. $data['top_group']['members'][] =[
  347. 'role' => 'Attendance',
  348. 'type' => 'StaffMember',
  349. 'user_id' => $user['userId']
  350. ];
  351. }
  352. $data = json_encode($data);
  353. $res = $this->post_json($url, $data);
  354. $res = json_decode($res);
  355. if ($res->errcode > 0){
  356. if ($res->errcode == 850015){
  357. parent::sendOutput('考勤组名称已存在', 1005);
  358. }else{
  359. parent::sendOutput($res->errmsg, 1005);
  360. }
  361. }
  362. $insert = [
  363. 'name' => $param['name'],
  364. 'shop_id' => $this->shopId,
  365. 'staff_id' => $param['staff_id'],
  366. 'shift_vo_list' => $param['shift_vo_list'],
  367. 'group_id' => $res->result->id
  368. ];
  369. $this->MGroup->insert($insert);
  370. parent::sendOutput('添加成功');
  371. }
  372. /**
  373. * 考勤组列表
  374. * @return void
  375. */
  376. public function group_list()
  377. {
  378. // $url = 'https://oapi.dingtalk.com/topapi/attendance/group/minimalism/list?access_token='.$this->voucher();
  379. // $param['cursor'] = $this->request->param('cursor');
  380. //
  381. // $param['op_user_id'] = '';
  382. // $param = json_encode($param);
  383. // $res = $this->post_json($url, $param);
  384. //
  385. // $res = json_decode($res);
  386. //
  387. // if ($res->errcode > 0){
  388. // parent::sendOutput($res->errmsg, 1);
  389. // }
  390. // if (isset($res->result->result)){
  391. // $res = $res->result->result;
  392. // }else{
  393. // $res = [];
  394. // }
  395. // parent::sendOutput($res);
  396. $params = $this->request->getRawJson();
  397. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  398. $selectParams['limit'] = $pageParams['limit'];
  399. $selectParams['offset'] = $pageParams['offset'];
  400. // $param['cursor'] = $this->request->param('cursor');
  401. // $url = 'https://oapi.dingtalk.com/topapi/attendance/shift/list?access_token='.$this->voucher();
  402. // $user = $this->staff->getStaffInfo(['userCenterId' => $this->onlineUserId, 'shopId' => $this->shopId])->getData();
  403. // $param['op_user_id'] = '';
  404. // $param = json_encode($param);
  405. // $res = $this->post_json($url, $param);
  406. // $res = json_decode($res);
  407. $selectParams['shop_id'] = $this->shopId;
  408. $result = $this->MGroup->list($selectParams);
  409. if ($result->isSuccess()) {
  410. $returnData = $result->getData();
  411. $pageData = [
  412. 'pageIndex' => $params['page'],
  413. 'pageSize' => $params['pageSize'],
  414. 'pageTotal' => $returnData['total'],
  415. ];
  416. foreach ($returnData['data'] as &$item){
  417. $item['group_user'] = $this->staff->getAllStaffDatas([['id', 'in', $item['staff_id']],['shopId', '=',$this->shopId]])->getData();
  418. $item['shift'] = $this->MTem->list([['shift_id', 'in' ,$item['shift_vo_list']], 'limit' => 0, 'offset' => 100])->getData()['data'];
  419. }
  420. parent::sendOutput($returnData['data'], 0,$pageData);
  421. } else {
  422. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  423. }
  424. }
  425. /**
  426. * 考勤组详情
  427. * @return void
  428. */
  429. public function group_details()
  430. {
  431. $id = $this->request->param('id');
  432. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/query?access_token='.$this->voucher();
  433. $param['op_user_id'] = '';
  434. $param['group_id'] = $id;
  435. $param = json_encode($param);
  436. $res = $this->post_json($url, $param);
  437. $res = json_decode($res);
  438. if ($res->errcode > 0){
  439. parent::sendOutput($res->errmsg, 1);
  440. }
  441. $res = $res->result;
  442. foreach ($res->shift_ids as $key => $item)
  443. {
  444. $url1 = 'https://oapi.dingtalk.com/topapi/attendance/shift/query?access_token='.$this->voucher();
  445. $param1['op_user_id'] = '';
  446. $param1['shift_id'] = $item;
  447. $res1 = $this->post_json($url1, json_encode($param1));
  448. $res1 = json_decode($res1);
  449. $res->shift_ids[$key] = ['name' => $res1->result->name, 'id' => $res1->result->id];
  450. }
  451. $group = $this->MGroup->list(['group_id' => $res->id, 'limit' => 0,'offset' => 10])->getData();
  452. foreach ($group['data'] as &$item){
  453. $res->group_user = $this->staff->getAllStaffDatas([['id', 'in', $item['staff_id']],['shopId', '=',$this->shopId]])->getData();
  454. }
  455. parent::sendOutput($res);
  456. }
  457. /**
  458. * 考勤组成员
  459. * @return void
  460. */
  461. public function group_user()
  462. {
  463. $id = $this->request->param('id');
  464. $size = $this->request->param('size');
  465. $cursor = $this->request->param('cursor');
  466. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/member/list?access_token='.$this->voucher();
  467. $param['cursor'] = $cursor;
  468. $param['size'] = $size;
  469. $param['op_user_id'] = '';
  470. $param['group_id'] = $id;
  471. $param = json_encode($param);
  472. $res = $this->post_json($url, $param);
  473. $res = json_decode($res);
  474. if ($res->errcode > 0){
  475. parent::sendOutput($res->errmsg, 1);
  476. }
  477. $res = $res->result->result;
  478. $data = [];
  479. foreach ($res as $item){
  480. $user = $this->staff->getStaffInfo(['userId' => $item->member_id, 'shopId' => $this->shopId])->getData();
  481. if ($user){
  482. $data[] = $user;
  483. }
  484. }
  485. parent::sendOutput($data);
  486. }
  487. /**
  488. * 删除考勤组
  489. * @return void
  490. */
  491. public function group_delete()
  492. {
  493. $id = $this->request->param('id');
  494. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/delete?access_token='.$this->voucher();
  495. $group_key = $this->post_json('https://oapi.dingtalk.com/topapi/attendance/groups/idtokey?access_token='.$this->voucher(),json_encode(['group_id' => $id]));
  496. $group_key = json_decode($group_key);
  497. if ($group_key->errcode > 0){
  498. parent::sendOutput($group_key->errmsg, 1);
  499. }
  500. $param['op_user_id'] = '';
  501. $param['group_key'] = $group_key->result;
  502. $param = json_encode($param);
  503. $res = $this->post_json($url, $param);
  504. $res = json_decode($res);
  505. if ($res->errcode > 0){
  506. parent::sendOutput($res->errmsg, 1);
  507. }
  508. $DGroupRecord = new DShopGroupRecord('default');
  509. $DGroupRecord->delete(['group_id' => $id]);
  510. $this->MGroup->delete(['group_id' => $id]);
  511. parent::sendOutput('删除成功');
  512. }
  513. /**
  514. * 排班
  515. * @return void
  516. */
  517. public function rostering()
  518. {
  519. $param = $this->request->getRawJson();
  520. $dbstaff = new DStaff('default');
  521. $dbstaff->setTable('qianniao_staff_'.$this->onlineEnterpriseId);
  522. $user = $dbstaff->get(['userCenterId' => $this->onlineUserId]);
  523. if (empty($user)) parent::sendOutput('排班只能员工排盘,总帐号无法排班', 1005);
  524. // $user = $user->getData();
  525. if (empty($user['userId'])) parent::sendOutput('该账号未绑定钉钉无法排班', 1005);
  526. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/schedule/async?access_token='.$this->voucher();
  527. $data = [
  528. 'op_user_id' => $user['userId'],
  529. 'group_id' => $param['group_id'],
  530. ];
  531. $DGroupRecord = new DShopGroupRecord('default');
  532. $recordData = [];
  533. foreach ($param['rostering'] as $item)
  534. {
  535. $staff = $this->staff->getStaffInfo(['id' => $item['userId'], 'shopId' => $this->shopId])->getData();
  536. $data['schedules'][] = [
  537. 'shift_id' => $item['shift_id'],
  538. 'work_date' => $this->get_data_format($item['work_date']),
  539. 'is_rest' => $item['is_rest'],
  540. 'userid' => $staff['userId']
  541. ];
  542. $record = $DGroupRecord->get(['group_id' => $param['group_id'], 'time' => strtotime($item['work_date'])]);
  543. if (empty($record)){
  544. $recordData = [
  545. 'shop_id' => $this->shopId,
  546. 'group_id' => $param['group_id'],
  547. 'time' => strtotime($item['work_date'])
  548. ];
  549. $DGroupRecord->insert($recordData);
  550. }
  551. if ($item['shift_id'] > 1){
  552. $time = $this->post_json('https://oapi.dingtalk.com/topapi/attendance/shift/query?access_token='.$this->voucher(), json_encode(['shift_id' => $item['shift_id']]));
  553. $time = json_decode($time)->result->sections;
  554. $time_slot = [];
  555. foreach ($time as $vo)
  556. {
  557. $time_slot[] = [explode(' ', $vo->punches[0]->check_time)[1], explode(' ', $vo->punches[1]->check_time)[1]];
  558. }
  559. $roData[] = [
  560. 'name' => $item['name'],
  561. 'shop_id' => $this->shopId,
  562. 'template_id' => $item['shift_id'],
  563. 'uid' => $staff['id'],
  564. 'time' => $item['work_date'],
  565. 'time_slot' => json_encode($time_slot)
  566. ];
  567. }else{
  568. $roData[] = [
  569. 'name' => $item['name'],
  570. 'shop_id' => $this->shopId,
  571. 'template_id' => $item['shift_id'],
  572. 'uid' => $staff['id'],
  573. 'time' => $item['work_date'],
  574. 'time_slot' => ''
  575. ];
  576. }
  577. }
  578. $data = json_encode($data);
  579. $res = $this->post_json($url, $data);
  580. $res = json_decode($res);
  581. if ($res->errcode > 0){
  582. parent::sendOutput($res->errmsg, 1);
  583. }
  584. foreach ($roData as $item)
  585. {
  586. $res = $this->Mrostering->insert($item);
  587. }
  588. parent::sendOutput('添加成功');
  589. }
  590. /**
  591. * 删除考勤组用户
  592. * @return void
  593. */
  594. public function remove()
  595. {
  596. $param = $this->request->getRawJson();
  597. // $user = $this->staff->getStaffInfo(['userCenterId' => $this->onlineUserId, 'shopId' => $this->shopId])->getData();
  598. // if (!$user) parent::sendOutput('不是该店铺店员', 1);
  599. if (!$param['id']) parent::sendOutput('传入考情组id');
  600. if (!$param['staff_id']) parent::sendOutput('请选择删除用户');
  601. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/users/remove?access_token='.$this->voucher();
  602. $uids = [];
  603. foreach (explode(',', $param['staff_id']) as $item){
  604. $staff = $this->staff->getStaffInfo(['id' => $item, 'shopId' => $this->shopId])->getData();
  605. $uids[] = $staff['userId'];
  606. }
  607. $data = [
  608. 'group_key' => $this->group_key($param['id']),
  609. 'user_id_list' => implode(',', $uids),
  610. ];
  611. $data = json_encode($data);
  612. $res = $this->post_json($url, $data);
  613. $res = json_decode($res);
  614. if ($res->errcode > 0){
  615. parent::sendOutput($res->errmsg, 1);
  616. }
  617. $group = $this->MGroup->details(['group_id' => $param['id']])->getData();
  618. if (empty($group)) parent::sendOutput('考勤组不存在', 1005);
  619. $staff_id = explode( ',', $group['staff_id']);
  620. foreach ($staff_id as $k => $v)
  621. {
  622. if ($v == $param['staff_id']){
  623. unset($staff_id[$k]);
  624. }
  625. }
  626. $staff_id = implode(',', $staff_id);
  627. $this->MGroup->update(['staff_id' => $staff_id], $group['id']);
  628. parent::sendOutput('删除成功');
  629. }
  630. public function user_add()
  631. {
  632. $param = $this->request->getRawJson();
  633. if (!$param['id']) parent::sendOutput('传入考情组id');
  634. if (!$param['staff_id']) parent::sendOutput('请选择删除用户');
  635. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/users/add?access_token='.$this->voucher();
  636. $uids = [];
  637. foreach (explode(',', $param['staff_id']) as $item){
  638. $staff = $this->staff->getStaffInfo(['id' => $item, 'shopId' => $this->shopId])->getData();
  639. $uids[] = $staff['userId'];
  640. }
  641. $data = [
  642. 'group_key' => $this->group_key($param['id']),
  643. 'user_id_list' => implode(',', $uids),
  644. ];
  645. $data = json_encode($data);
  646. $res = $this->post_json($url, $data);
  647. $res = json_decode($res);
  648. if ($res->errcode > 0){
  649. parent::sendOutput($res->errmsg, 1005);
  650. }
  651. $group = $this->MGroup->details(['group_id' => $param['id']])->getData();
  652. if (empty($group)) parent::sendOutput('考勤组不存在', 1005);
  653. $staff_id = explode( ',', $group['staff_id']);
  654. foreach (explode(',', $param['staff_id']) as $v)
  655. {
  656. $staff_id[] = $v;
  657. }
  658. $staff_id = implode(',', $staff_id);
  659. $this->MGroup->update(['staff_id' => $staff_id], $group['id']);
  660. parent::sendOutput('新增成功');
  661. }
  662. /**
  663. *排班详情
  664. * @return void
  665. */
  666. public function scheduling_details()
  667. {
  668. $param = $this->request->getRawJson();
  669. // $user = $this->staff->getStaffInfo(['userCenterId' => $this->onlineUserId, 'shopId' => $this->shopId])->getData();
  670. // if (!$user) parent::sendOutput('不是该店铺店员', 1);
  671. // $staff = $this->staff->getStaffInfo(['userCenterId' => $param['uid'], 'shopId' => $this->shopId])->getData();
  672. if (isset($param['uid'])){
  673. $uid = $param['uid'];
  674. }
  675. $pageParams = pageToOffset($param['page'] ?: 1, $param['pageSize'] ?: 10);
  676. $limit = $pageParams['limit'];
  677. $offset = $pageParams['offset'];
  678. $month = $param['month'] ? $param['month'] : date('Y-m', time());
  679. $where = [];
  680. if (isset($uid) and !empty($uid)){
  681. $where[] = ['uid', '=', $uid];
  682. }
  683. if ($month){
  684. $where[] = ['time', 'like', '%'.$month.'%'];
  685. }
  686. if (isset($param['export']) and !empty($param['export'])){
  687. $where['export'] = $param['export'];
  688. }
  689. $where[] = ['shop_id', '=', $this->shopId];
  690. $rostering = $this->Mrostering->select($where,'*', 'time DESC', $limit, $offset);
  691. if ($rostering->isSuccess()) {
  692. $returnData = $rostering->getData();
  693. $pageData = [
  694. 'pageIndex' => $offset,
  695. 'pageSize' => $limit,
  696. 'pageTotal' => $returnData['total'],
  697. ];
  698. parent::sendOutput($returnData['data'], 0, $pageData);
  699. } else {
  700. parent::sendOutput($rostering->getData(), ErrorCode::$dberror);
  701. }
  702. }
  703. public function obtain_clock()
  704. {
  705. $data = $this->request->getRawJson();
  706. if ($data['time']){
  707. $today = $data['time'];
  708. }else{
  709. $today = date('Y-m-d', strtotime('today'));
  710. }
  711. $url = 'https://oapi.dingtalk.com/attendance/listRecord?access_token='.$this->voucher();
  712. $rostering = $this->Mrostering->details([['time', 'like', '%'.$today.'%'], ['uid', '=' , $data['uid']]]);
  713. if (!$rostering->isSuccess()){
  714. parent::sendOutput('今天未有班次', ErrorCode::$dberror);
  715. }
  716. $rostering = $rostering->getData();
  717. $staff = $this->staff->getStaffInfo(['id' => $data['uid'], 'shopId' => $this->shopId])->getData();
  718. $data['userIds'] = [$staff['userId']];
  719. $data['checkDateFrom'] = date('Y-m-d H:i:s', strtotime($today));
  720. $data['checkDateTo'] = date('Y-m-d H:i:s', strtotime($today) + 86400);
  721. $res = $this->post_json($url, json_encode($data));
  722. $res = json_decode($res);
  723. $recordresult = $res->recordresult;
  724. $time_slot = json_decode($rostering['time_slot']);
  725. $clock = $rostering['clock']? json_decode( $rostering['clock']) : [];
  726. $num = 0;
  727. foreach ($time_slot as $item)
  728. {
  729. foreach ($item as $vo)
  730. {
  731. $times = strtotime($today.' '.$vo) * 1000;
  732. $count = count($recordresult);
  733. $time = [];
  734. for ($i=0;$i < $count; $i++){
  735. if (!isset($recordresult[$i]->invalidRecordType)){
  736. if ($times == $recordresult[$i]->baseCheckTime){
  737. $time = $recordresult[$i];
  738. }
  739. }
  740. }
  741. if ($time){
  742. if (isset($clock[$num])){
  743. if ($clock[$num]->timeResult == 'NotSigned'){
  744. $clock[$num]->userCheckTime = date('Y-m-d H:i:s', $time->userCheckTime/1000);
  745. $clock[$num]->timeResult = $time->timeResult;
  746. }
  747. }else{
  748. $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]));
  749. }
  750. }else{
  751. if (strtotime($vo) < time() and !isset($clock[$num])){
  752. $clock[] = json_decode(json_encode(['baseCheckTime' => date('Y-m-d H:i:s', strtotime($today.' '.$vo)), 'userCheckTime' => '','timeResult' => 'NotSigned']));
  753. }
  754. }
  755. $num++;
  756. }
  757. }
  758. $status = 0;
  759. $price = 0;//打卡金额
  760. if ($num == count($clock)){
  761. foreach ($clock as $item){
  762. if ($rostering['status'] == 0){
  763. if ($item->timeResult == 'Normal'){
  764. $tem_price = $this->MTem->details(['shop_id' => $this->shopId, 'shift_id' => $rostering['template_id']])->getData();
  765. $price += $tem_price['single_time'];
  766. }
  767. }
  768. if ($item->timeResult != 'Normal') $status = -1;// 不正常今天的打卡未完成
  769. }
  770. if ($status == 0) $status = 1;// 如果都打卡正常那么打完完成
  771. }
  772. if (strtotime($today) < time()){
  773. if (empty($clock)) $status = -1;
  774. }
  775. if ($price > 0 and $staff['is_clock'] == 1){
  776. $balance = $staff['reward'] + $price;
  777. $rewardTotal = $staff['rewardTotal'] + $price;
  778. $this->staff->clockReward($price, $balance, $staff['userCenterId'], $staff['id'], $this->shopId, $rewardTotal);
  779. }
  780. if (empty($clock)){
  781. $res = $this->Mrostering->update(['status' => $status], $rostering['id']);
  782. }else{
  783. $res = $this->Mrostering->update(['status' => $status, 'clock' => json_encode($clock)], $rostering['id']);
  784. }
  785. parent::sendOutput('更新成功');
  786. }
  787. public function get_data_format($time)
  788. {
  789. list($usec) = explode(".", $time);
  790. $date = strtotime($usec);
  791. $return_data = str_pad($date,13,"0",STR_PAD_RIGHT); //不足13位。右边补0
  792. return $return_data;
  793. }
  794. /**
  795. * 排班记录
  796. * @return void
  797. */
  798. public function group_record()
  799. {
  800. $param = $this->request->getRawJson();
  801. $DGroupRecord = new DShopGroupRecord('default');
  802. $pageParams = pageToOffset($param['page'] ?: 1, $param['pageSize'] ?: 10);
  803. $limit = $pageParams['limit'];
  804. $offset = $pageParams['offset'];
  805. // $monthFirstDay = strtotime(date( 'Y-m-1 00:00:00', strtotime($param['time'])));
  806. // $mdays = date( 't', strtotime($param['time']) );
  807. // $monthLastDay = strtotime(date( 'Y-m-' . $mdays . ' 23:59:59', strtotime($param['time']) ));
  808. //
  809. // $where[] = ['time', '>=', $monthFirstDay];
  810. // $where[] = ['time', '<=', $monthLastDay];
  811. // $where[] = ['a.group_id', '=', $param['group_id']];
  812. $where = [];
  813. $where[] = ['a.shop_id', '=', $this->shopId];
  814. if ($param['name']) $where[] = ['b.name', '=', $param['name']];
  815. $join = 'Left Join qianniao_shop_group as b ON a.group_id = b.group_id';
  816. $list = $DGroupRecord->select($where, 'a.*,b.name,b.staff_id,b.shift_vo_list', null, $limit, $offset, [], true, false, $join);
  817. $count = $DGroupRecord->count($where, [], $join);
  818. foreach ($list as &$item){
  819. $item['time'] = date('Y-m-d', $item['time']);
  820. $item['group_user'] = $this->staff->getAllStaffDatas([['id', 'in', $item['staff_id']],['shopId', '=',$this->shopId]])->getData();
  821. $item['shift'] = $this->MTem->list([['shift_id', 'in', $item['shift_vo_list']], 'limit' => 0, 'offset' => 100])->getData()['data'];
  822. }
  823. $pageData = [
  824. 'pageIndex' => $param['page'],
  825. 'pageSize' => $param['pageSize'],
  826. 'pageTotal' => $count,
  827. ];
  828. parent::sendOutput($list, 0, $pageData);
  829. }
  830. }