ShopRostering.Class.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. var_dump($user);
  526. if (empty($user['userId'])) parent::sendOutput('该账号未绑定钉钉无法排班', 1005);
  527. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/schedule/async?access_token='.$this->voucher();
  528. $data = [
  529. 'op_user_id' => $user['userId'],
  530. 'group_id' => $param['group_id'],
  531. ];
  532. $DGroupRecord = new DShopGroupRecord('default');
  533. $recordData = [];
  534. foreach ($param['rostering'] as $item)
  535. {
  536. $staff = $this->staff->getStaffInfo(['id' => $item['userId'], 'shopId' => $this->shopId])->getData();
  537. $data['schedules'][] = [
  538. 'shift_id' => $item['shift_id'],
  539. 'work_date' => $this->get_data_format($item['work_date']),
  540. 'is_rest' => $item['is_rest'],
  541. 'userid' => $staff['userId']
  542. ];
  543. $record = $DGroupRecord->get(['group_id' => $param['group_id'], 'time' => strtotime($item['work_date'])]);
  544. if (empty($record)){
  545. $recordData = [
  546. 'shop_id' => $this->shopId,
  547. 'group_id' => $param['group_id'],
  548. 'time' => strtotime($item['work_date'])
  549. ];
  550. $DGroupRecord->insert($recordData);
  551. }
  552. if ($item['shift_id'] > 1){
  553. $time = $this->post_json('https://oapi.dingtalk.com/topapi/attendance/shift/query?access_token='.$this->voucher(), json_encode(['shift_id' => $item['shift_id']]));
  554. $time = json_decode($time)->result->sections;
  555. $time_slot = [];
  556. foreach ($time as $vo)
  557. {
  558. $time_slot[] = [explode(' ', $vo->punches[0]->check_time)[1], explode(' ', $vo->punches[1]->check_time)[1]];
  559. }
  560. $roData[] = [
  561. 'name' => $item['name'],
  562. 'shop_id' => $this->shopId,
  563. 'template_id' => $item['shift_id'],
  564. 'uid' => $staff['id'],
  565. 'time' => $item['work_date'],
  566. 'time_slot' => json_encode($time_slot)
  567. ];
  568. }else{
  569. $roData[] = [
  570. 'name' => $item['name'],
  571. 'shop_id' => $this->shopId,
  572. 'template_id' => $item['shift_id'],
  573. 'uid' => $staff['id'],
  574. 'time' => $item['work_date'],
  575. 'time_slot' => ''
  576. ];
  577. }
  578. }
  579. $data = json_encode($data);
  580. $res = $this->post_json($url, $data);
  581. $res = json_decode($res);
  582. if ($res->errcode > 0){
  583. parent::sendOutput($res->errmsg, 1);
  584. }
  585. foreach ($roData as $item)
  586. {
  587. $res = $this->Mrostering->insert($item);
  588. }
  589. parent::sendOutput('添加成功');
  590. }
  591. /**
  592. * 删除考勤组用户
  593. * @return void
  594. */
  595. public function remove()
  596. {
  597. $param = $this->request->getRawJson();
  598. // $user = $this->staff->getStaffInfo(['userCenterId' => $this->onlineUserId, 'shopId' => $this->shopId])->getData();
  599. // if (!$user) parent::sendOutput('不是该店铺店员', 1);
  600. if (!$param['id']) parent::sendOutput('传入考情组id');
  601. if (!$param['staff_id']) parent::sendOutput('请选择删除用户');
  602. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/users/remove?access_token='.$this->voucher();
  603. $uids = [];
  604. foreach (explode(',', $param['staff_id']) as $item){
  605. $staff = $this->staff->getStaffInfo(['id' => $item, 'shopId' => $this->shopId])->getData();
  606. $uids[] = $staff['userId'];
  607. }
  608. $data = [
  609. 'group_key' => $this->group_key($param['id']),
  610. 'user_id_list' => implode(',', $uids),
  611. ];
  612. $data = json_encode($data);
  613. $res = $this->post_json($url, $data);
  614. $res = json_decode($res);
  615. if ($res->errcode > 0){
  616. parent::sendOutput($res->errmsg, 1);
  617. }
  618. $group = $this->MGroup->details(['group_id' => $param['id']])->getData();
  619. if (empty($group)) parent::sendOutput('考勤组不存在', 1005);
  620. $staff_id = explode( ',', $group['staff_id']);
  621. foreach ($staff_id as $k => $v)
  622. {
  623. if ($v == $param['staff_id']){
  624. unset($staff_id[$k]);
  625. }
  626. }
  627. $staff_id = implode(',', $staff_id);
  628. $this->MGroup->update(['staff_id' => $staff_id], $group['id']);
  629. parent::sendOutput('删除成功');
  630. }
  631. public function user_add()
  632. {
  633. $param = $this->request->getRawJson();
  634. if (!$param['id']) parent::sendOutput('传入考情组id');
  635. if (!$param['staff_id']) parent::sendOutput('请选择删除用户');
  636. $url = 'https://oapi.dingtalk.com/topapi/attendance/group/users/add?access_token='.$this->voucher();
  637. $uids = [];
  638. foreach (explode(',', $param['staff_id']) as $item){
  639. $staff = $this->staff->getStaffInfo(['id' => $item, 'shopId' => $this->shopId])->getData();
  640. $uids[] = $staff['userId'];
  641. }
  642. $data = [
  643. 'group_key' => $this->group_key($param['id']),
  644. 'user_id_list' => implode(',', $uids),
  645. ];
  646. $data = json_encode($data);
  647. $res = $this->post_json($url, $data);
  648. $res = json_decode($res);
  649. if ($res->errcode > 0){
  650. parent::sendOutput($res->errmsg, 1005);
  651. }
  652. $group = $this->MGroup->details(['group_id' => $param['id']])->getData();
  653. if (empty($group)) parent::sendOutput('考勤组不存在', 1005);
  654. $staff_id = explode( ',', $group['staff_id']);
  655. foreach (explode(',', $param['staff_id']) as $v)
  656. {
  657. $staff_id[] = $v;
  658. }
  659. $staff_id = implode(',', $staff_id);
  660. $this->MGroup->update(['staff_id' => $staff_id], $group['id']);
  661. parent::sendOutput('新增成功');
  662. }
  663. /**
  664. *排班详情
  665. * @return void
  666. */
  667. public function scheduling_details()
  668. {
  669. $param = $this->request->getRawJson();
  670. // $user = $this->staff->getStaffInfo(['userCenterId' => $this->onlineUserId, 'shopId' => $this->shopId])->getData();
  671. // if (!$user) parent::sendOutput('不是该店铺店员', 1);
  672. // $staff = $this->staff->getStaffInfo(['userCenterId' => $param['uid'], 'shopId' => $this->shopId])->getData();
  673. if (isset($param['uid'])){
  674. $uid = $param['uid'];
  675. }
  676. $pageParams = pageToOffset($param['page'] ?: 1, $param['pageSize'] ?: 10);
  677. $limit = $pageParams['limit'];
  678. $offset = $pageParams['offset'];
  679. $month = $param['month'] ? $param['month'] : date('Y-m', time());
  680. $where = [];
  681. if (isset($uid) and !empty($uid)){
  682. $where[] = ['uid', '=', $uid];
  683. }
  684. if ($month){
  685. $where[] = ['time', 'like', '%'.$month.'%'];
  686. }
  687. if (isset($param['export']) and !empty($param['export'])){
  688. $where['export'] = $param['export'];
  689. }
  690. $where[] = ['shop_id', '=', $this->shopId];
  691. $rostering = $this->Mrostering->select($where,'*', 'time DESC', $limit, $offset);
  692. if ($rostering->isSuccess()) {
  693. $returnData = $rostering->getData();
  694. $pageData = [
  695. 'pageIndex' => $offset,
  696. 'pageSize' => $limit,
  697. 'pageTotal' => $returnData['total'],
  698. ];
  699. parent::sendOutput($returnData['data'], 0, $pageData);
  700. } else {
  701. parent::sendOutput($rostering->getData(), ErrorCode::$dberror);
  702. }
  703. }
  704. public function obtain_clock()
  705. {
  706. $data = $this->request->getRawJson();
  707. if ($data['time']){
  708. $today = $data['time'];
  709. }else{
  710. $today = date('Y-m-d', strtotime('today'));
  711. }
  712. $url = 'https://oapi.dingtalk.com/attendance/listRecord?access_token='.$this->voucher();
  713. $rostering = $this->Mrostering->details([['time', 'like', '%'.$today.'%'], ['uid', '=' , $data['uid']]]);
  714. if (!$rostering->isSuccess()){
  715. parent::sendOutput('今天未有班次', ErrorCode::$dberror);
  716. }
  717. $rostering = $rostering->getData();
  718. $staff = $this->staff->getStaffInfo(['id' => $data['uid'], 'shopId' => $this->shopId])->getData();
  719. $data['userIds'] = [$staff['userId']];
  720. $data['checkDateFrom'] = date('Y-m-d H:i:s', strtotime($today));
  721. $data['checkDateTo'] = date('Y-m-d H:i:s', strtotime($today) + 86400);
  722. $res = $this->post_json($url, json_encode($data));
  723. $res = json_decode($res);
  724. $recordresult = $res->recordresult;
  725. $time_slot = json_decode($rostering['time_slot']);
  726. $clock = $rostering['clock']? json_decode( $rostering['clock']) : [];
  727. $num = 0;
  728. foreach ($time_slot as $item)
  729. {
  730. foreach ($item as $vo)
  731. {
  732. $times = strtotime($today.' '.$vo) * 1000;
  733. $count = count($recordresult);
  734. $time = [];
  735. for ($i=0;$i < $count; $i++){
  736. if (!isset($recordresult[$i]->invalidRecordType)){
  737. if ($times == $recordresult[$i]->baseCheckTime){
  738. $time = $recordresult[$i];
  739. }
  740. }
  741. }
  742. if ($time){
  743. if (isset($clock[$num])){
  744. if ($clock[$num]->timeResult == 'NotSigned'){
  745. $clock[$num]->userCheckTime = date('Y-m-d H:i:s', $time->userCheckTime/1000);
  746. $clock[$num]->timeResult = $time->timeResult;
  747. }
  748. }else{
  749. $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]));
  750. }
  751. }else{
  752. if (strtotime($vo) < time() and !isset($clock[$num])){
  753. $clock[] = json_decode(json_encode(['baseCheckTime' => date('Y-m-d H:i:s', strtotime($today.' '.$vo)), 'userCheckTime' => '','timeResult' => 'NotSigned']));
  754. }
  755. }
  756. $num++;
  757. }
  758. }
  759. $status = 0;
  760. $price = 0;//打卡金额
  761. if ($num == count($clock)){
  762. foreach ($clock as $item){
  763. if ($rostering['status'] == 0){
  764. if ($item->timeResult == 'Normal'){
  765. $tem_price = $this->MTem->details(['shop_id' => $this->shopId, 'shift_id' => $rostering['template_id']])->getData();
  766. $price += $tem_price['single_time'];
  767. }
  768. }
  769. if ($item->timeResult != 'Normal') $status = -1;// 不正常今天的打卡未完成
  770. }
  771. if ($status == 0) $status = 1;// 如果都打卡正常那么打完完成
  772. }
  773. if (strtotime($today) < time()){
  774. if (empty($clock)) $status = -1;
  775. }
  776. if ($price > 0 and $staff['is_clock'] == 1){
  777. $balance = $staff['reward'] + $price;
  778. $rewardTotal = $staff['rewardTotal'] + $price;
  779. $this->staff->clockReward($price, $balance, $staff['userCenterId'], $staff['id'], $this->shopId, $rewardTotal);
  780. }
  781. if (empty($clock)){
  782. $res = $this->Mrostering->update(['status' => $status], $rostering['id']);
  783. }else{
  784. $res = $this->Mrostering->update(['status' => $status, 'clock' => json_encode($clock)], $rostering['id']);
  785. }
  786. parent::sendOutput('更新成功');
  787. }
  788. public function get_data_format($time)
  789. {
  790. list($usec) = explode(".", $time);
  791. $date = strtotime($usec);
  792. $return_data = str_pad($date,13,"0",STR_PAD_RIGHT); //不足13位。右边补0
  793. return $return_data;
  794. }
  795. /**
  796. * 排班记录
  797. * @return void
  798. */
  799. public function group_record()
  800. {
  801. $param = $this->request->getRawJson();
  802. $DGroupRecord = new DShopGroupRecord('default');
  803. $pageParams = pageToOffset($param['page'] ?: 1, $param['pageSize'] ?: 10);
  804. $limit = $pageParams['limit'];
  805. $offset = $pageParams['offset'];
  806. // $monthFirstDay = strtotime(date( 'Y-m-1 00:00:00', strtotime($param['time'])));
  807. // $mdays = date( 't', strtotime($param['time']) );
  808. // $monthLastDay = strtotime(date( 'Y-m-' . $mdays . ' 23:59:59', strtotime($param['time']) ));
  809. //
  810. // $where[] = ['time', '>=', $monthFirstDay];
  811. // $where[] = ['time', '<=', $monthLastDay];
  812. // $where[] = ['a.group_id', '=', $param['group_id']];
  813. $where = [];
  814. $where[] = ['a.shop_id', '=', $this->shopId];
  815. if ($param['name']) $where[] = ['b.name', '=', $param['name']];
  816. $join = 'Left Join qianniao_shop_group as b ON a.group_id = b.group_id';
  817. $list = $DGroupRecord->select($where, 'a.*,b.name,b.staff_id,b.shift_vo_list', null, $limit, $offset, [], true, false, $join);
  818. $count = $DGroupRecord->count($where, [], $join);
  819. foreach ($list as &$item){
  820. $item['time'] = date('Y-m-d', $item['time']);
  821. $item['group_user'] = $this->staff->getAllStaffDatas([['id', 'in', $item['staff_id']],['shopId', '=',$this->shopId]])->getData();
  822. $item['shift'] = $this->MTem->list([['shift_id', 'in', $item['shift_vo_list']], 'limit' => 0, 'offset' => 100])->getData()['data'];
  823. }
  824. $pageData = [
  825. 'pageIndex' => $param['page'],
  826. 'pageSize' => $param['pageSize'],
  827. 'pageTotal' => $count,
  828. ];
  829. parent::sendOutput($list, 0, $pageData);
  830. }
  831. }