ShopRostering.Class.php 34 KB

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