Manage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace Mail\Lib;
  3. use Dever;
  4. class Manage
  5. {
  6. #代理商日数据统计
  7. public function day_api(){
  8. $start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
  9. $end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
  10. $time = Dever::maketime(date('Y-m-d'),'00:00:00');
  11. $where = array();
  12. $where['fstart'] = $start;
  13. $where['fend'] = $end;
  14. $w = array();
  15. $fstart = Dever::input('start', date('Y-m-01', strtotime('-0 month')));
  16. $w['fstart'] = Dever::maketime($fstart . ' 00:00:00');
  17. $w['fend'] = $end;
  18. $data = array();
  19. $data['day'] = $this->content_api($where,1);
  20. $data['money'] = $this->c_api($where);
  21. $data['all'] = $this->content_api($w,2);
  22. $data['all_money'] = $this->c_api($w);
  23. $data['time'] = $this->day_time_api($w);
  24. $data['data'] = Dever::json_encode($data['time']);
  25. $data['money_time'] = $this->money_time_api($w);
  26. $data['qu_money'] = Dever::json_encode($data['money_time']);
  27. $data['xzhou'] = $this->button_day_api($w,1);
  28. $data['zhou'] = Dever::json_encode($data['xzhou']);
  29. return Dever::render('census', $data);
  30. }
  31. #代理商周数据
  32. public function zhou_api(){
  33. $start=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
  34. $end=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
  35. $where = array();
  36. $where['fstart'] = $start;
  37. $where['fend'] = $end;
  38. $w = array();
  39. $fstart = Dever::input('start', date('Y-m-01', strtotime('-0 month')));
  40. $w['fstart'] = Dever::maketime($fstart . ' 00:00:00');
  41. $w['fend'] = $end;
  42. $data = array();
  43. $data['day'] = $this->content_api($where,1);
  44. $data['money'] = $this->c_api($where);
  45. $data['all'] = $this->content_api($w,2);
  46. $data['all_money'] = $this->c_api($w);
  47. $data['time'] = $this->day_time_api($w);
  48. $data['data'] = Dever::json_encode($data['time']);
  49. $data['money_time'] = $this->money_time_api($w);
  50. $data['qu_money'] = Dever::json_encode($data['money_time']);
  51. $data['xzhou'] = $this->button_day_api($w,1);
  52. $data['zhou'] = Dever::json_encode($data['xzhou']);
  53. return Dever::render('census', $data);
  54. }
  55. #代理商月数据统计
  56. public function month_api(){
  57. $num = Dever::input('num',1);
  58. $start = Dever::input('start', date('Y-m-01', strtotime('-'.$num.' month')));
  59. $end = date('Y-m-d', strtotime($start." +1 month -1 day"));
  60. $time = Dever::maketime(date('Y-m-01'),'00:00:00');
  61. $start = Dever::maketime($start . ' 00:00:00');
  62. $end = Dever::maketime($end . ' 23:59:59');
  63. $where = array();
  64. $where['fstart'] = $start;
  65. $where['fend'] = $end;
  66. $w = array();
  67. $fstart = Dever::input('start', date('Y-m-01', strtotime('-1 month')));
  68. $w['fstart'] = Dever::maketime($fstart . ' 00:00:00');
  69. $w['fend'] = $end;
  70. $data = array();
  71. $data['day'] = $this->content_api($where,1);
  72. $data['money'] = $this->c_api($where);
  73. $data['all'] = $this->content_api($w,2);
  74. $data['all_money'] = $this->c_api($w);
  75. $data['time'] = $this->day_time_api($w);
  76. $data['data'] = Dever::json_encode($data['time']);
  77. $data['money_time'] = $this->money_time_api($w);
  78. $data['qu_money'] = Dever::json_encode($data['money_time']);
  79. $data['xzhou'] = $this->button_day_api($w,2);
  80. $data['zhou'] = Dever::json_encode($data['xzhou']);
  81. return Dever::render('census', $data);
  82. }
  83. #按时间统计
  84. public function content_api($where,$type){
  85. $data = array();
  86. // print_R($where);die;
  87. if($type == 2){
  88. $data['time'] = date('Y.m',$where['fstart']);
  89. }else{
  90. $data['time'] = date('Y.m.d',$where['fstart']);
  91. }
  92. #确认代理商订单数
  93. $where['status'] = 4;
  94. $data['num'] = Dever::db('agent/order')->getNum($where);
  95. #确认订单总费用
  96. $money = Dever::db('agent/order')->getCash($where);
  97. if($money['total']){
  98. $data['money'] = $money['total'];
  99. }else{
  100. $data['money'] = 0;
  101. }
  102. #确认代理费
  103. $dl_money = Dever::db('agent/order')->getDailiCash($where);
  104. if($dl_money['total']){
  105. $data['dl_money'] = $dl_money['total'];
  106. }else{
  107. $data['dl_money'] = 0;
  108. }
  109. #确认软件服务费
  110. $soft_money = Dever::db('agent/order')->getSoftCash($where);
  111. if($soft_money['total']){
  112. $data['soft_money'] = $soft_money['total'];
  113. }else{
  114. $data['soft_money'] = 0;
  115. }
  116. $wh['status'] = 3;
  117. $wh['fstart'] = $where['fstart'];
  118. $wh['fend'] = $where['fend'];
  119. $cash = Dever::db('agent/soft_cash')->getTotal($wh);
  120. if($cash['total']){
  121. $data['cash'] = $cash['total'];
  122. }else{
  123. $data['cash'] = 0;
  124. }
  125. $data['soft_cash'] = $data['soft_money'] + $data['cash'];
  126. #期权价值
  127. $option_money = Dever::db('agent/order')->getOptionPrice($where);
  128. if($option_money['total']){
  129. $data['option_money'] = $option_money['total'];
  130. }else{
  131. $data['option_money'] = 0;
  132. }
  133. return $data;
  134. }
  135. #各角色的金额,数量,原始期权价值,软件服务费
  136. public function c_api($where){
  137. $role = Dever::db('setting/role')->getAll(array('id'=>1,'state'=>1));
  138. foreach($role as $k => $v){
  139. // print_R($where);die;
  140. $where['role'] = $v['id'];
  141. $data[$k]['name'] = $v['name'];
  142. $where['status'] = 2;
  143. $data[$k]['num'] = Dever::db('agent/member')->getNum($where);
  144. $where['status'] = 4;
  145. $dl = Dever::db('agent/order')->getSum($where);
  146. if($dl['total']){
  147. $data[$k]['money'] = $dl['total'];
  148. }else{
  149. $data[$k]['money'] = 0;
  150. }
  151. #期权价值:
  152. $option_money = Dever::db('agent/order')->getOptionPrice($where);
  153. if($option_money['total']){
  154. $data[$k]['option_money'] = $option_money['total'];
  155. }else{
  156. $data[$k]['option_money'] = 0;
  157. }
  158. #软件服务费
  159. $wh['role'] = $v['id'];
  160. $wh['status'] = 3;
  161. $wh['fstart'] = $where['fstart'];
  162. $wh['fend'] = $where['fend'];
  163. $cash = Dever::db('agent/soft_cash')->getTotal($wh);
  164. if($cash['total']){
  165. $data[$k]['cash'] = $cash['total'];
  166. }else{
  167. $data[$k]['cash'] = 0;
  168. }
  169. }
  170. return $data;
  171. }
  172. #数据趋势
  173. public function day_time_api($w){
  174. $days = ($w['fend']-$w['fstart'])/86400+1;
  175. // 保存每天日期
  176. $day = array();
  177. $data = array();
  178. for($i=0; $i<$days; $i++){
  179. $day[] = array($w['fstart']+(86400*$i),$w['fstart']+(86400*$i)+86400);
  180. }
  181. $res = array();
  182. $res['num'] = array();
  183. foreach($day as $k => $v){
  184. $where['fstart'] = $v[0];
  185. $where['fend'] =$v[1]-1;
  186. $data[$k]['time'] = date('m.d',$v[0]);
  187. #确认代理商订单数
  188. $where['status'] = 4;
  189. $res['num'][] = Dever::db('agent/order')->getNum($where);
  190. }
  191. $data = array();
  192. $data['name'] = '数量';
  193. $data['type'] = 'line';
  194. // $data['stack'] = 'Total';
  195. $data['data'] = $res['num'];
  196. return $data;
  197. }
  198. #金额趋势
  199. public function money_time_api($w){
  200. $days = ($w['fend']-$w['fstart'])/86400+1;
  201. // 保存每天日期
  202. $day = array();
  203. $data = array();
  204. for($i=0; $i<$days; $i++){
  205. $day[] = array($w['fstart']+(86400*$i),$w['fstart']+(86400*$i)+86400);
  206. }
  207. $res = array();
  208. $res['money'] = array();
  209. foreach($day as $k => $v){
  210. $where['fstart'] = $v[0];
  211. $where['fend'] =$v[1]-1;
  212. $data[$k]['time'] = date('m.d',$v[0]);
  213. #确认订单总费用
  214. $money = Dever::db('agent/order')->getCash($where);
  215. if($money['total']){
  216. $res['money'][] = $money['total'];
  217. }else{
  218. $res['money'][] = 0;
  219. }
  220. }
  221. $money = array();
  222. $money['name'] = '金额';
  223. $money['type'] = 'line';
  224. $money['data'] = $res['money'];
  225. return $money;
  226. }
  227. #趋势底部日期
  228. public function button_day_api($w,$type){
  229. $days = ($w['fend']-$w['fstart'])/86400+1;
  230. if($type ==1){
  231. $daysd = $days-1;
  232. }else{
  233. $daysd = $days;
  234. }
  235. // 保存每天日期
  236. $day = array();
  237. $data = array();
  238. for($i=0; $i<$daysd; $i++){
  239. $day[] = date('m.d',$w['fstart']+(86400*$i));
  240. }
  241. $data['type'] = 'category';
  242. $data['boundaryGap'] = false;
  243. $data['date'] = $day;
  244. return $day;
  245. }
  246. #代理商日统计
  247. public function index_api(){
  248. $title = '代理商日数据统计';
  249. $data = Dever::db('mail/feature')->find(array('key'=>'lib/cron.day'));
  250. $email = explode(',',$data['email_id']);
  251. foreach($email as $k => $v){
  252. $res[] = Dever::db('mail/email')->find(array('id'=>$v));
  253. }
  254. $content = $this->index_api();
  255. foreach($res as $key => $val){
  256. $this->send($val['email'], $val['name'], $title, $content);
  257. }
  258. }
  259. #代理商周统计
  260. public function week_api(){
  261. $title = '代理商周数据统计';
  262. $data = Dever::db('mail/feature')->find(array('key'=>'lib/cron.week'));
  263. $email = explode(',',$data['email_id']);
  264. foreach($email as $k => $v){
  265. $res[] = Dever::db('mail/email')->find(array('id'=>$v));
  266. }
  267. $content = $this->zhou_api();
  268. foreach($res as $key => $val){
  269. $this->send($val['email'], $val['name'], $title, $content);
  270. }
  271. }
  272. #代理商月统计
  273. public function yue_api(){
  274. $title = '代理商月数据统计';
  275. $data = Dever::db('mail/feature')->find(array('key'=>'lib/cron.yue'));
  276. $email = explode(',',$data['email_id']);
  277. foreach($email as $k => $v){
  278. $res[] = Dever::db('mail/email')->find(array('id'=>$v));
  279. }
  280. $content = $this->month_api();
  281. if(!$content){
  282. return false;
  283. }
  284. foreach($res as $key => $val){
  285. $this->send($val['email'], $val['name'], $title, $content);
  286. }
  287. }
  288. #代理商统计发送邮件
  289. public function send($email, $username, $title, $content)
  290. {
  291. $config = Dever::config('base', 'project')->email;
  292. Dever::apply('src/PHPMailer', 'email');
  293. Dever::apply('src/Exception', 'email');
  294. Dever::apply('src/SMTP', 'email');
  295. $mail = new \PHPMailer\PHPMailer\PHPMailer();
  296. $mail->isSMTP();
  297. //$mail->SMTPDebug = 2;
  298. $mail->CharSet = 'UTF-8';
  299. $mail->Host = $config['smtp'];
  300. $mail->Port = 465;
  301. $mail->SMTPSecure = 'ssl';
  302. $mail->SMTPAuth = true;
  303. $mail->Username = $config['username'];
  304. $mail->Password = $config['password'];
  305. $mail->setFrom($config['from'][0], '统计');
  306. $mail->addAddress($email, $username);
  307. $mail->Subject = "=?utf-8?B?" . base64_encode($title) . "?=";
  308. $mail->Body = $content;
  309. $mail->isHTML(true);
  310. //$mail->addAttachment($file);
  311. if (!$mail->send()) {
  312. Dever::alert("Mailer Error: " . $mail->ErrorInfo);
  313. } else {
  314. return 'ok';
  315. }
  316. }
  317. }