Manage.php 10 KB

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