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