TechController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. /**
  3. * 后台保洁师管理
  4. * 2016-03-04
  5. */
  6. class TechController extends AdminController {
  7. /**
  8. * 首页
  9. */
  10. public function actionIndex() {
  11. $status_option = CommonFn::getComboboxData(TechInfo::$status_option, 1, true, 100);
  12. $scheme_option = CommonFn::getComboboxData(Commision::$scheme_option, 100, true, 100);
  13. $service_type = Yii::app()->params['o2o_service'];
  14. $datePickerStart = date('Y-m-d 00:00');
  15. $datePickerEnd = date('Y-m-d 00:00', strtotime('+15 day', strtotime('today')));
  16. $this->render('index', [
  17. 'status_option' => $status_option,
  18. 'scheme_option' => $scheme_option,
  19. 'service_type' => $service_type,
  20. 'datePickerStart' => $datePickerStart,
  21. 'datePickerEnd' => $datePickerEnd,
  22. ]);
  23. }
  24. /**
  25. * 列表
  26. */
  27. public function actionList() {
  28. $pageParams = CommonFn::getPageParams();
  29. $id = intval(Yii::app()->request->getParam('id', 0));
  30. $search = Yii::app()->request->getParam('search', '');
  31. $scheme = intval(Yii::app()->request->getParam('scheme', 100));
  32. $status = intval(Yii::app()->request->getParam('status', 100));
  33. $criteria = new EMongoCriteria($pageParams);
  34. // id筛选
  35. if ($id) {
  36. $criteria->_id('==', $id);
  37. }
  38. // 状态筛选
  39. if ($status != 100) {
  40. $criteria->status('==', $status);
  41. }
  42. // 提成方案筛选
  43. if ($scheme != 100) {
  44. $criteria->scheme('==', Commision::$scheme_option[$scheme]['alias']);
  45. }
  46. // 搜索
  47. if ($search) {
  48. // 搜索ID
  49. if (!preg_match('/\D/', $search)) {
  50. $criteria->_id('==', intval($search));
  51. // 搜索姓名或微信ID
  52. } else {
  53. $criteria->name('or', new MongoRegex('/'.$search.'/'));
  54. $criteria->wechat_id('or', new MongoRegex('/'.$search.'/'));
  55. }
  56. }
  57. $cursor = TechInfo::model()->findAll($criteria);
  58. $rows = CommonFn::getRowsFromCursor($cursor);
  59. $parsedRows = TechInfo::model()->parse($rows);
  60. $total = $cursor->count();
  61. echo CommonFn::composeDatagridData($parsedRows, $total);
  62. }
  63. /**
  64. * 编辑保洁师基本信息
  65. */
  66. public function actionEdit() {
  67. $_id = intval(Yii::app()->request->getParam('_id', 0));
  68. $name = Yii::app()->request->getParam('name', '');
  69. $avatar = Yii::app()->request->getParam('avatar', '');
  70. $status = intval(Yii::app()->request->getParam('status', 1));
  71. $scheme = intval(Yii::app()->request->getParam('scheme', -1));
  72. $service_type = Yii::app()->request->getParam('service_type', []);
  73. $desc = Yii::app()->request->getParam('desc', '');
  74. // intval service_type
  75. foreach ($service_type as &$value) {
  76. $value = intval($value);
  77. }
  78. $tech = TechInfo::get($_id);
  79. $user = User::get($_id);
  80. if (!$tech || !$user) {
  81. CommonFn::requestAjax(false, '保洁师信息不存在');
  82. }
  83. // 微信status更新
  84. if ($status != $tech->status) {
  85. TechInfo::updateWeixinStatus($_id, $status);
  86. }
  87. // TechInfo更新
  88. $tech->name = $name;
  89. $tech->status = $status;
  90. $tech->scheme = Commision::$scheme_option[$scheme]['alias'];
  91. $tech->avatar = $avatar;
  92. $tech->service_type = $service_type;
  93. $tech->desc = $desc;
  94. // user更新
  95. $user->status = $status;
  96. $user->name = $name;
  97. $success_tech = $tech->save();
  98. $success_user = $user->save();
  99. CommonFn::requestAjax($success_tech && $success_user, '', []);
  100. }
  101. /**
  102. * 获取保洁师可预约时间列表
  103. */
  104. public function actionGetTechTimeline() {
  105. $_id = Yii::app()->request->getParam('_id', 0);
  106. $timeline = FreeTimeRecord::model()->getTechTimeline(intval($_id));
  107. echo json_encode($timeline);
  108. }
  109. /**
  110. * 修改保洁师可预约时间
  111. */
  112. public function actionModifyFreetime() {
  113. $_id = intval(Yii::app()->request->getParam('_id', 0));
  114. $new_time_list = Yii::app()->request->getParam('new_time_list', '{}');
  115. $old_time_list = Yii::app()->request->getParam('old_time_list', '{}');
  116. if (!TechInfo::get($_id)) {
  117. CommonFn::requestAjax(false, '保洁师信息不存在', []);
  118. }
  119. $new_time_list = empty($new_time_list) ? '{}' : $new_time_list;
  120. $old_time_list = empty($old_time_list) ? '{}' : $old_time_list;
  121. $new_list = json_decode($new_time_list, true);
  122. $old_list = json_decode($old_time_list, true);
  123. if (empty($new_list)) {
  124. CommonFn::requestAjax(false, '没有新数据传入', []);
  125. }
  126. $need_set = [];
  127. $need_unset = [];
  128. foreach ($new_list as $key => $item) {
  129. if (array_key_exists($key, $old_list)) {
  130. foreach ($item as $k => $v) {
  131. if (intval($v) != intval($old_list[$key][$k])) {
  132. $hour = $k == 9 ? '09' : (string)$k;
  133. if (intval($v) == 0) {
  134. $need_unset[] = strtotime($key.$hour.'00');
  135. } else if (intval($v) == 1) {
  136. $need_set[] = strtotime($key.$hour.'00');
  137. }
  138. }
  139. }
  140. } else {
  141. foreach ($item as $k => $v) {
  142. if (intval($v) == 1) {
  143. $hour = $k == 9 ? '09' : (string)$k;
  144. $need_set[] = strtotime($key.$hour.'00');
  145. }
  146. }
  147. }
  148. }
  149. foreach ($old_list as $key => $item) {
  150. if (!array_key_exists($key, $new_list)) {
  151. foreach ($item as $k => $v) {
  152. if (intval($v) == 1) {
  153. $hour = $k == 9 ? '09' : (string)$k;
  154. $need_unset[] = strtotime($key.$hour.'00');
  155. }
  156. }
  157. }
  158. }
  159. $need_set = array_unique($need_set);
  160. $need_unset = array_unique($need_unset);
  161. foreach ($need_set as $key => $value) {
  162. FreeTimeRecord::model()->TechsetFreetime($_id, $value);
  163. }
  164. foreach ($need_unset as $key => $value) {
  165. FreeTimeRecord::model()->TechUnsetFreetime($_id, $value);
  166. }
  167. CommonFn::requestAjax(true, '', []);
  168. }
  169. /**
  170. * 修改保洁师服务范围
  171. */
  172. public function actionModifyCoverage() {
  173. $_id = intval(Yii::app()->request->getParam('_id', 0));
  174. $coverage_json = Yii::app()->request->getParam('coverage_json', '[]');
  175. $tech = TechInfo::get($_id);
  176. if (!$tech) {
  177. CommonFn::requestAjax(false, '保洁师信息不存在');
  178. }
  179. $business = [];
  180. $coverage = json_decode($coverage_json, true);
  181. foreach ($coverage as $key => $item) {
  182. if (isset($item['business'])) {
  183. $business[] = $item['business'];
  184. } else {
  185. continue;
  186. }
  187. }
  188. $tech->coverage = $coverage;
  189. $tech->business = $business;
  190. $success = $tech->save();
  191. CommonFn::requestAjax($success, '', []);
  192. }
  193. /**
  194. * 修改保洁师微信端信息
  195. */
  196. public function actionModifyWeixinInfo() {
  197. $_id = intval(Yii::app()->request->getParam('_id', 0));
  198. $name = Yii::app()->request->getParam('name', '');
  199. $weixin_userid = Yii::app()->request->getParam('weixin_userid', '');
  200. $mobile = Yii::app()->request->getParam('mobile', '');
  201. $tech = TechInfo::get($_id);
  202. if (!$tech) {
  203. CommonFn::requestAjax(false, '保洁师信息不存在', []);
  204. }
  205. // 检查userId是否重复
  206. if (!$tech->weixin_userid && TechInfo::getByUserid($weixin_userid)) {
  207. CommonFn::requestAjax(false, '微信ID已存在', []);
  208. }
  209. // 检查mobile是否重复
  210. if ($tech->mobile != $mobile && TechInfo::getByMobile($mobile)) {
  211. CommonFn::requestAjax(false, '手机号重复', []);
  212. }
  213. $option = WechatConfig::getIns()->getLinkOption();
  214. $secret = WechatConfig::getIns()->getSecret('admin_dev');
  215. $wechat = new QyWechat($option);
  216. $weixin_user_data = [
  217. 'userid' => $weixin_userid,
  218. 'name' => $name,
  219. 'mobile' => $mobile,
  220. 'department' => [4],
  221. ];
  222. if ($wechat->checkAuth($option['appid'], $secret)) {
  223. // 检查用户是否存在
  224. $weixin_userInfo = $wechat->getUserInfo($weixin_userid);
  225. var_dump($weixin_userInfo);
  226. if ($weixin_userInfo == false) {
  227. $result = $wechat->createUser($weixin_user_data);
  228. var_dump($result);
  229. if ($result['errmsg'] != 'created') {
  230. CommonFn::requestAjax(false, '微信验证失败1: '.$result['errmsg'], []);
  231. }
  232. } else {
  233. $result = $wechat->updateUser($weixin_user_data);
  234. var_dump($result);
  235. if ($result['errmsg'] != 'updated') {
  236. CommonFn::requestAjax(false, '微信验证失败2: '.$result['errmsg'], []);
  237. }
  238. }
  239. } else {
  240. CommonFn::requestAjax(false, '微信Auth验证失败', []);
  241. }
  242. // 后台信息修改
  243. $tech->weixin_userid = $weixin_userid;
  244. $tech->mobile = $mobile;
  245. $success = $tech->save();
  246. CommonFn::requestAjax($success, '', []);
  247. }
  248. /**
  249. * 选择保洁师接口
  250. */
  251. public function actionSelectTech() {
  252. $chars = Yii::app()->request->getParam('tech', '');
  253. $criteria = new EMongoCriteria();
  254. $criteria->addCond('name', 'or', new MongoRegex('/'.$chars.'/'));
  255. $criteria->addCond('weixin_userid', 'or', new MongoRegex('/'.$chars.'/'));
  256. $criteria->status('==', 1);
  257. $cursor = TechInfo::model()->findAll($criteria);
  258. $rows = CommonFn::getRowsFromCursor($cursor);
  259. $index = 0;
  260. $data = [];
  261. foreach ($rows as $key => $item) {
  262. $data[] = [
  263. 'id' => $index,
  264. 'data' => $item['name'],
  265. 'description' => $item['weixin_userid'],
  266. 'tech_id' => $item['_id'],
  267. ];
  268. $index++;
  269. }
  270. if (!$data) {
  271. $data = [
  272. 'id' => 0,
  273. 'data' => '',
  274. 'description' => '',
  275. 'tech_id' => -1,
  276. ];
  277. }
  278. echo json_encode($data);
  279. }
  280. /**
  281. * 复制保洁师信息到新表
  282. */
  283. public function actionCopyTech() {
  284. set_time_limit(0);
  285. $sign = Yii::app()->request->getParam('sign', '');
  286. if ($sign != 'mayThe4thBwithU') {
  287. die('wrong sign');
  288. }
  289. $mongoDbAuthManager = new CMongoDbAuthManager();
  290. $tech_ids = $mongoDbAuthManager->getAuthUser('保洁师');
  291. $criteria = new EMongoCriteria();
  292. $criteria->_id('in', $tech_ids);
  293. $cursor = User::model()->findAll($criteria);
  294. foreach ($cursor as $key => $item) {
  295. $tech = TechInfo::get($item->_id);
  296. if (!$tech) {
  297. $tech = new TechInfo();
  298. $tech->_id = $item->_id;
  299. }
  300. $tech->name = $item->name;
  301. $tech->desc = '';
  302. $tech->avatar = Yii::app()->params['defaultUserAvatar'];
  303. $tech->status = $item->status;
  304. $tech->scheme = isset($item->scheme) ? $item->scheme : 'no_scheme';
  305. $tech->weixin_userid = isset($item->userid) ? $item->userid : '';
  306. $tech->mobile = $item->wx_info['mobile'];
  307. $tech->service_type = isset($item->service_type) ? $item->service_type : [];
  308. $tech->coverage = [];
  309. $tech->business = [];
  310. // $tech->coverage = isset($item->coverage) ? $item->coverage : [];
  311. // $tech->weixin_info = $item->wx_info;
  312. // 保洁师接单数(状态为已完成的订单)
  313. $criteria = new EMongoCriteria();
  314. $criteria->technician('==', $tech->_id);
  315. $criteria->status('==', 6);
  316. $rOrders = ROrder::model()->findAll($criteria);
  317. $tech->order_count = $rOrders->count();
  318. // 保洁师好评数(分数为5的评价)
  319. $criteria = new EMongoCriteria();
  320. $criteria->score('==', 5);
  321. $criteria->status('==', 1);
  322. $criteria->technician('==', $tech->_id);
  323. $comments = Comment::model()->findAll($criteria);
  324. $tech->favourable_count = $comments->count();
  325. // 微信端状态修改
  326. TechInfo::updateWeixinStatus($tech->_id, $tech->status);
  327. $tech->save();
  328. }
  329. echo 'done';
  330. }
  331. /**
  332. * 保洁师统计同步
  333. */
  334. public function actionCopyTechCount() {
  335. set_time_limit(0);
  336. $techs = TechInfo::model()->findAll();
  337. foreach ($techs as $key => $item) {
  338. // 接单数统计
  339. $criteria = new EMongoCriteria();
  340. $criteria->technician('==', $item->_id);
  341. $criteria->status('==', 6);
  342. $orders = ROrder::model()->findAll($criteria);
  343. $item->order_count = $orders->count();
  344. // 保洁师好评数
  345. $criteria = new EMongoCriteria();
  346. $criteria->score('==', 5);
  347. $criteria->status('==', 1);
  348. $criteria->technician('==', $item->_id);
  349. $comments = Comment::model()->findAll($criteria);
  350. $item->favourable_count = $comments->count();
  351. $item->save();
  352. }
  353. echo 'done';
  354. }
  355. }