Manage.php 12 KB

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