Cron.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace Work\Lib;
  3. use Dever;
  4. Class Cron
  5. {
  6. #工单日统计
  7. public function index_api(){
  8. $title = '工单每日统计';
  9. $data = Dever::db('work/branch')->state(array('type'=>1));
  10. foreach ($data as $k => $v) {
  11. $group_id = explode(',',$v['group']);
  12. $admin = Dever::db('manage/admin')->find($group_id[1]);
  13. $content = $this->email_api($v,$admin);
  14. if ($content) {
  15. $this->send($admin['email'], $admin['username'], $title, $content);
  16. }
  17. }
  18. }
  19. public function test_api(){
  20. $title = '工单每日统计';
  21. $data = Dever::db('work/branch')->state(array('type'=>1));
  22. foreach ($data as $k => $v) {
  23. $group_id = explode(',',$v['group']);
  24. $admin = Dever::db('manage/admin')->find($group_id[1]);
  25. $content = $this->email_api($v,$admin);
  26. if ($content) {
  27. $this->send('1849506057@qq.com', $admin['username'], $title, $content);
  28. }
  29. }
  30. }
  31. #发送邮件
  32. public function email_api($v,$fsadmin)
  33. {
  34. $start=mktime(0,0,0,date('m'),date('d'),date('Y'))-86400;
  35. $end = $start+86400-1;
  36. $config = Dever::db('work/info')->config['config_status'];
  37. $dbranch = $v;
  38. $question = Dever::db('work/question')->state(array('branch_id'=>$dbranch['group_id']));
  39. $question = array_keys($question);
  40. $data['info']['app'] = Dever::db('work/info')->getCount(array('question'=>$question,'start'=>$start,'end'=>$end));
  41. // echo Dever::sql();
  42. $data['info']['xu'] = Dever::db('work/info')->getCount(array('group'=>$dbranch['group_id'],'start'=>$start,'end'=>$end,'status'=>2));
  43. $admin = explode(',',$dbranch['group']);
  44. if ($dbranch['coor']) {
  45. $coor = explode(',',$dbranch['coor']);
  46. if (isset($coor) && $coor[0] && $coor[0] >0) {
  47. $people = $dbranch['coor'].','.$admin[1];
  48. } else {
  49. $people = $admin[1];
  50. }
  51. } else {
  52. $people = $admin[1];
  53. }
  54. $data['info']['yi'] = Dever::db('work/info')->getCount(array('fpeople'=>$people,'start'=>$start,'end'=>$end,'status'=>'3,4'));
  55. if ($data['info']['app'] == 0 && $data['info']['xu'] == 0 && $data['info']['yi'] ==0 ) {
  56. return false;
  57. }
  58. $data['question'] = Dever::db('work/question')->getAll(array('branch_id'=>$dbranch['group_id']));
  59. $question = implode(',',$question);
  60. if ($question) {
  61. $data['app'] = Dever::db('work/info')->fetchAll('select * from churen_work_info where (`group` in ('.$dbranch['group_id'].') or fpeople in ('.$people.') or question in ('.$question.')) and cdate>='.$start.' and cdate <='.$end.' and state = 1');
  62. foreach ($data['app'] as $k1 => $v1) {
  63. $data['app'][$k1]['cdate'] = date('Y-m-d H:i:s',$v1['cdate']);
  64. if ($v1['zdate']) {
  65. $data['app'][$k1]['zhipai'] = date('Y-m-d H:i:s',$v1['zdate']).'<br/>'.date('Y-m-d H:i:s',$v1['chdate']);
  66. } else {
  67. $data['app'][$k1]['zhipai'] = date('Y-m-d H:i:s',$v1['chdate']);
  68. }
  69. // $data['app'][$k1]['chu'] = date('Y-m-d H:i:s',$v1['chdate']);
  70. $cate = Dever::db('work/q_cate')->find($v1['cate_id']);
  71. $data['app'][$k1]['cate_name'] = $cate['name'];
  72. $priority = Dever::db('work/priority')->find($v1['priority_id']);
  73. $data['app'][$k1]['priority_name'] = $priority['name'];
  74. $data['app'][$k1]['status']= Dever::status($config,$v1['status']);
  75. }
  76. }
  77. $msg = array();
  78. $msg['admin_email'] = $fsadmin['email'];
  79. $msg['admin_username'] = $fsadmin['username'];
  80. $msg['app'] = $data['info']['app'];
  81. $msg['xu'] = $data['info']['xu'];
  82. $msg['yi'] = $data['info']['yi'];
  83. $res = Dever::log($msg, 'work');
  84. return Dever::render('email', $data);
  85. }
  86. #代理商统计发送邮件
  87. public function send($email, $username, $title, $content)
  88. {
  89. $config = Dever::config('base', 'project')->email;
  90. Dever::apply('src/PHPMailer', 'email');
  91. Dever::apply('src/Exception', 'email');
  92. Dever::apply('src/SMTP', 'email');
  93. $mail = new \PHPMailer\PHPMailer\PHPMailer();
  94. $mail->isSMTP();
  95. //$mail->SMTPDebug = 2;
  96. $mail->CharSet = 'UTF-8';
  97. $mail->Host = $config['smtp'];
  98. $mail->Port = 465;
  99. $mail->SMTPSecure = 'ssl';
  100. $mail->SMTPAuth = true;
  101. $mail->Username = $config['username'];
  102. $mail->Password = $config['password'];
  103. $mail->setFrom($config['from'][0], '工单系统');
  104. $mail->addAddress($email, $username);
  105. $mail->Subject = "=?utf-8?B?" . base64_encode($title) . "?=";
  106. $mail->Body = $content;
  107. $mail->isHTML(true);
  108. //$mail->addAttachment($file);
  109. if (!$mail->send()) {
  110. Dever::alert("Mailer Error: " . $mail->ErrorInfo);
  111. } else {
  112. return 'ok';
  113. }
  114. }
  115. #同步info表中的product
  116. public function product_api()
  117. {
  118. $data = Dever::db('work/info')->state();
  119. if ($data) {
  120. foreach ($data as $k => $v) {
  121. if (!$v['product'] || $v['product']<=0) {
  122. $cate = explode(',',$v['cate_id']);
  123. if (isset($cate[1]) && $cate[1]) {
  124. $product = Dever::db('work/question')->find($cate[1]);
  125. if ($product) {
  126. $where['where_id'] = $v['id'];
  127. $where['product'] = $product['product_id'];
  128. Dever::db('work/info')->update($where);
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. public function updateTime_api()
  136. {
  137. $data = Dever::db('work/info')->state();
  138. foreach ($data as $k => $v) {
  139. if (!$v['fpeople'] && ($v['status'] == 3 || $v['status'] == 4)) {
  140. $app = Dever::db('work/appoint')->getAdmin(array('info_id'=>$v['id'],'admin'=>$v['audit_admin']));
  141. if ($app && $app['admin']) {
  142. $update['fpeople'] = $app['admin'];
  143. } else {
  144. if ($v['branch_id']) {
  145. $branch = explode(',',$v['branch_id']);
  146. if (isset($branch[1]) && $branch[1]) {
  147. $update['fpeople'] = $branch[1];
  148. }
  149. } else {
  150. $update['fpeople'] = $v['audit_admin'];
  151. }
  152. }
  153. $update['where_id'] = $v['id'];
  154. Dever::db('work/info')->update($update);
  155. }
  156. }
  157. }
  158. public function user_api()
  159. {
  160. $num = Dever::input('num', 1);
  161. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  162. $end = Dever::input('end', date('Y-m-d'));
  163. $start = Dever::maketime($start . ' 00:00:00');
  164. $end = Dever::maketime($end . ' 23:59:59');
  165. $day = intval(($end - $start)/86400);
  166. for($i=0; $i<=$day; $i++) {
  167. $where['start'] = $start + 86400*$i;
  168. $where['end'] = $start + 86400*$i + 86399;
  169. $where['priority_id'] = '';
  170. $cate = Dever::db('work/q_cate')->state();
  171. foreach ($cate as $k =>$v) {
  172. $where['cate'] = $v['id'];
  173. unset($where['priority_id']);
  174. $num = Dever::db('work/info')->getNum($where);
  175. if ($num <= 0) {
  176. continue;
  177. }
  178. $data = array();
  179. $data['cate'] = $where['cate'];
  180. $data['day'] = $where['start'];
  181. $info = Dever::db('work/info_stat')->find($data);
  182. $data['question'] = $num;
  183. $where['priority_id'] = 4;
  184. $data['super'] = Dever::db('work/info')->getNum($where);
  185. $where['priority_id'] = 3;
  186. $data['urgent'] = Dever::db('work/info')->getNum($where);
  187. $where['priority_id'] = 2;
  188. $data['yb'] = Dever::db('work/info')->getNum($where);
  189. $where['priority_id'] = 1;
  190. $data['pt'] = Dever::db('work/info')->getNum($where);
  191. // echo dever::sql();
  192. // print_r($data);
  193. if (!$info) {
  194. Dever::db('work/info_stat')->insert($data);
  195. } else {
  196. $data['where_id'] = $info['id'];
  197. Dever::db('work/info_stat')->update($data);
  198. }
  199. }
  200. }
  201. return 'ok';
  202. }
  203. #修改info表中的问题分类
  204. public function updateCate_api()
  205. {
  206. $data = Dever::db('work/info')->state();
  207. foreach ($data as $k => $v) {
  208. if (!$v['cate'] || $v['cate']<=0) {
  209. if ($v['cate_id']) {
  210. $cate_id = explode(',',$v['cate_id']);
  211. $where['where_id'] = $v['id'];
  212. $where['cate'] = $cate_id[0];
  213. Dever::db('work/info')->update($where);
  214. }
  215. }
  216. }
  217. }
  218. #同步info_log中的指派人(app_admin_id)
  219. public function updateInfoLog_api()
  220. {
  221. $data = Dever::db('work/info_log')->state();
  222. foreach ($data as $k => $v) {
  223. if ($v['appoint_id']) {
  224. $app = Dever::db('work/appoint')->find($v['appoint_id']);
  225. if ($app) {
  226. $where['where_id'] = $v['id'];
  227. $where['app_admin_id'] = $app['admin'];
  228. $where['clear'] = true;
  229. Dever::db('work/info_log')->update($where);
  230. }
  231. }
  232. }
  233. }
  234. }