Manage.php 11 KB

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