ProductionStatsEmailService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z_yang
  5. * Date: 2018/03/09
  6. * Time: 20:18
  7. */
  8. namespace App\Modules\Statistic\Services;
  9. use DB;
  10. use Redis;
  11. class ProductionStatsEmailService
  12. {
  13. public static function getNewProductionNum($from_date,$to_date){
  14. $res = DB::table('book_configs')
  15. ->where([
  16. ['book_configs.created_at','>=',$from_date],
  17. ['book_configs.created_at','<=',$to_date],
  18. ])
  19. ->count('id');
  20. return $res;
  21. }
  22. public static function getNewProductionNumGroupByGender($from_date,$to_date){
  23. $male = 0;
  24. $female=0;
  25. $num = 0;
  26. $res = DB::table('book_updates')
  27. ->select(DB::raw("channel_name,count(bid) as new_products_num"))
  28. ->where('update_type','add_book')
  29. ->whereBetween('update_date',[$from_date,$to_date])
  30. ->groupBy('channel_name')
  31. ->get();
  32. foreach ($res as $item){
  33. if($item->channel_name == '男频'){
  34. $male = $item->new_products_num;
  35. }
  36. if($item->channel_name =='女频'){
  37. $female = $item->new_products_num;
  38. }
  39. $num+=$item->new_products_num;
  40. }
  41. return ['total'=>$num,'male'=>$male,'female'=>$female];
  42. }
  43. public static function getUpdatedProductionNum($from_date,$to_date) {
  44. $num = 0;
  45. $books = DB::table('books')
  46. ->select('id','created_at','updated_at')
  47. ->where([
  48. ['updated_at','>=',$from_date],
  49. ['updated_at','<=',$to_date]
  50. ])
  51. ->get();
  52. foreach ($books as $book){
  53. if(strtotime($book->updated_at)-strtotime($book->created_at)>1) {
  54. $num++;
  55. }
  56. }
  57. return $num;
  58. }
  59. public static function getUpdatedProductionNumGroupByGender($from_date,$to_date) {
  60. $num = 0;
  61. $male = 0;
  62. $female = 0;
  63. $books = $res = DB::table('book_updates')
  64. ->select(DB::raw("channel_name,count(bid) as update_products_num"))
  65. ->where('update_type','add_chapter')
  66. ->whereBetween('update_date',[$from_date,$to_date])
  67. ->groupBy('channel_name')
  68. ->get();
  69. foreach ($books as $book){
  70. if($book->channel_name == '男频'){
  71. $male = $book->update_products_num;
  72. }
  73. if($book->channel_name =='女频'){
  74. $female = $book->update_products_num;
  75. }
  76. $num+=$book->update_products_num;
  77. }
  78. return ['total'=>$num,'male'=>$male,'female'=>$female];
  79. }
  80. public static function getUpdatedChaptersNum($from_date,$to_date) {
  81. $num = 0;
  82. DB::table('chapters')
  83. ->select('chapters.id','chapters.created_at','chapters.updated_at','book_configs.created_at as book_add_time')
  84. ->leftjoin('book_configs','book_configs.bid','=','chapters.bid')
  85. ->orderBy('chapters.id','desc')
  86. ->where([['chapters.created_at','>=',$from_date],['chapters.created_at','<=',$to_date]])
  87. ->chunk(1000,function($chapters) use(&$num,$from_date,$to_date){
  88. foreach ($chapters as $chapter){
  89. if(date('Y-m-d',strtotime($chapter->created_at))!=date('Y-m-d',strtotime($chapter->book_add_time))){
  90. $num++;
  91. }
  92. }
  93. //return false;
  94. });
  95. return $num;
  96. }
  97. public static function getUpdatedChaptersNumByGender($from_date,$to_date) {
  98. $num = 0;
  99. $male = 0;
  100. $female = 0;
  101. $books = $res = DB::table('book_updates')
  102. ->select(DB::raw("channel_name,sum(update_chapter_count) as update_chapter_count"))
  103. ->where('update_type','add_chapter')
  104. ->whereBetween('update_date',[$from_date,$to_date])
  105. ->groupBy('channel_name')
  106. ->get();
  107. foreach ($books as $book){
  108. if($book->channel_name == '男频'){
  109. $male = $book->update_chapter_count;
  110. }
  111. if($book->channel_name =='女频'){
  112. $female = $book->update_chapter_count;
  113. }
  114. $num+=$book->update_chapter_count;
  115. }
  116. return ['total'=>$num,'male'=>$male,'female'=>$female];
  117. }
  118. public static function getSerialNum(){
  119. $num = DB::table('books')
  120. ->where('status',0)
  121. ->count('id');
  122. return $num;
  123. }
  124. public static function getSerialNumGroupByGender(){
  125. $num =0;
  126. $female=0;
  127. $male=0;
  128. $res = DB::table('books')
  129. ->select(DB::raw('count(books.id) as serial_num,book_categories.pid'))
  130. ->leftjoin('book_categories','books.category_id','=','book_categories.id')
  131. ->where('books.status',0)
  132. ->groupBy('book_categories.pid')
  133. ->get();
  134. foreach ($res as $item) {
  135. if($item->pid == 1){
  136. $male=$item->serial_num;
  137. }
  138. if($item->pid == 2){
  139. $female=$item->serial_num;
  140. }
  141. $num+=$item->serial_num;
  142. }
  143. return ['num'=>$num,'male'=>$male,'female'=>$female];
  144. }
  145. public static function getNotUpdatedSerial(){
  146. $two_days_ago = date('Y-m-d H:i:s',strtotime('-2 day'));
  147. $num = DB::table('books as b')
  148. ->select(DB::raw('count(bcs.bid) as num'))
  149. ->leftjoin('book_categories as bc','b.category_id','=','bc.id')
  150. ->leftjoin('book_configs as bcs','bcs.bid','=','b.id')
  151. ->where([
  152. ['b.updated_at','<=',$two_days_ago],
  153. ['b.status','=',0],
  154. ['bcs.is_on_shelf','>=',1],
  155. ['bcs.is_on_shelf','<=',2]
  156. ])
  157. ->groupBy('bcs.is_on_shelf')
  158. ->orderBy('bcs.is_on_shelf','asc')
  159. ->get();
  160. //->count('bcs.bid');
  161. return $num;
  162. }
  163. public static function getNotUpdatedSerialGroupByGender(){
  164. $two_days_ago = date('Y-m-d H:i:s',strtotime('-2 day'));
  165. $num=0;
  166. $inter_num=0;
  167. $inter_male = 0;
  168. $inter_female = 0;
  169. $outer_male = 0;
  170. $outer_female = 0;
  171. $outer_num =0;
  172. $res = DB::table('books as b')
  173. ->select(DB::raw('count(bcs.bid) as num,is_on_shelf,pid'))
  174. ->leftjoin('book_categories as bc','b.category_id','=','bc.id')
  175. ->leftjoin('book_configs as bcs','bcs.bid','=','b.id')
  176. ->where([
  177. ['b.updated_at','<=',$two_days_ago],
  178. ['b.status','=',0],
  179. ['bcs.is_on_shelf','>=',1],
  180. ['bcs.is_on_shelf','<=',2]
  181. ])
  182. ->groupBy('bcs.is_on_shelf')
  183. ->groupBy('bc.pid')
  184. //->orderBy('bcs.is_on_shelf','asc')
  185. ->get();
  186. //->count('bcs.bid');
  187. foreach ($res as $item) {
  188. if($item->is_on_shelf ==1){
  189. $inter_num +=$item->num;
  190. if($item->pid ==1){
  191. $inter_male = $item->num;
  192. }
  193. if($item->pid ==2){
  194. $inter_female =$item->num;
  195. }
  196. }
  197. if($item->is_on_shelf==2) {
  198. $outer_num +=$item->num;
  199. if($item->pid ==1){
  200. $outer_male = $item->num;
  201. }
  202. if($item->pid ==2){
  203. $outer_female =$item->num;
  204. }
  205. }
  206. }
  207. return ['num'=>$inter_num+$outer_num,'inter_num'=>$inter_num,
  208. 'inter_male'=>$inter_male,'inter_female'=> $inter_female,
  209. 'outer_num'=>$outer_num,'outer_male'=>$outer_male,
  210. 'outer_female'=>$outer_female];
  211. }
  212. public static function setDayStats($from_date,$to_date){
  213. $new_product_num = self::getNewProductionNum($from_date,$to_date);
  214. $updated_product_num = self::getUpdatedProductionNum($from_date,$to_date);
  215. $updated_chapter_num = self::getUpdatedChaptersNum($from_date,$to_date);
  216. $serial_num = self::getSerialNum();
  217. $not_updated_serial = self::getNotUpdatedSerial();
  218. $internal_suspend = $not_updated_serial[0]->num;
  219. $outer_suspend = $not_updated_serial[1]->num;
  220. $data = array(
  221. 'new_product_num'=>$new_product_num,
  222. 'updated_product_num'=>$updated_product_num,
  223. 'updated_chapter_num'=>$updated_chapter_num,
  224. 'serial_num'=>$serial_num,
  225. 'not_updated_serial'=>$internal_suspend+$outer_suspend,
  226. 'internal_suspend'=>$internal_suspend,
  227. 'outer_suspend'=>$outer_suspend,
  228. 'date'=>date('Y-m-d',strtotime($from_date)),
  229. 'type'=>'day'
  230. );
  231. $res = DB::table('product_stats')->insert($data);
  232. return $res;
  233. }
  234. public static function setDayStatsNew($from_date,$to_date){
  235. $data = self::getAllByGender($from_date,$to_date);
  236. $data['date']=date('Y-m-d',strtotime($from_date));
  237. $data['type']='day';
  238. $res = DB::table('product_stats')->insert($data);
  239. return $res;
  240. }
  241. public static function setMonthStats($from_date,$to_date) {
  242. $new_product_num = self::getNewProductionNum($from_date,$to_date);
  243. $updated_product_num = self::getUpdatedProductionNum($from_date,$to_date);
  244. $updated_chapter_num = self::getUpdatedChaptersNum($from_date,$to_date);
  245. $serial_num = self::getSerialNum();
  246. $not_updated_serial = self::getNotUpdatedSerial();
  247. $internal_suspend = $not_updated_serial[0]->num;
  248. $outer_suspend = $not_updated_serial[1]->num;
  249. $data = array(
  250. 'new_product_num'=>$new_product_num,
  251. 'updated_product_num'=>$updated_product_num,
  252. 'updated_chapter_num'=>$updated_chapter_num,
  253. 'serial_num'=>$serial_num,
  254. 'not_updated_serial'=>$not_updated_serial,
  255. 'internal_suspend'=>$internal_suspend,
  256. 'outer_suspend'=>$outer_suspend,
  257. 'date'=>date('Y-m-01',strtotime($from_date)),
  258. 'type'=>'month'
  259. );
  260. $res = DB::table('product_stats')->insert($data);
  261. return $res;
  262. }
  263. public static function getAll($from_date,$to_date) {
  264. $new_product_num = self::getNewProductionNum($from_date,$to_date);
  265. $updated_product_num = self::getUpdatedProductionNum($from_date,$to_date);
  266. $updated_chapter_num = self::getUpdatedChaptersNum($from_date,$to_date);
  267. $serial_num = self::getSerialNum();
  268. $not_updated_serial = self::getNotUpdatedSerial();
  269. $internal_suspend = $not_updated_serial[0]->num;
  270. $outer_suspend = $not_updated_serial[1]->num;
  271. $data = array(
  272. 'new_product_num'=>$new_product_num,
  273. 'updated_product_num'=>$updated_product_num,
  274. 'updated_chapter_num'=>$updated_chapter_num,
  275. 'serial_num'=>$serial_num,
  276. 'not_updated_serial'=>$not_updated_serial,
  277. 'internal_suspend'=>$internal_suspend,
  278. 'outer_suspend'=>$outer_suspend,
  279. );
  280. return $data;
  281. }
  282. public static function getAllByGender($from_date,$to_date) {
  283. $new_product_num = self::getNewProductionNumGroupByGender($from_date,$to_date);
  284. $updated_product_num = self::getUpdatedProductionNumGroupByGender($from_date,$to_date);
  285. $updated_chapter_num = self::getUpdatedChaptersNumByGender($from_date,$to_date);
  286. $serial_num = self::getSerialNumGroupByGender();
  287. $not_updated_serial = self::getNotUpdatedSerialGroupByGender();
  288. //$internal_suspend = $not_updated_serial[0]->num;
  289. //$outer_suspend = $not_updated_serial[1]->num;
  290. $data = array(
  291. 'new_product_num'=>$new_product_num['total'],
  292. 'new_product_male_num'=>$new_product_num['male'],
  293. 'new_product_female_num'=>$new_product_num['female'],
  294. 'updated_product_num'=>$updated_product_num['total'],
  295. 'updated_product_male_num'=>$updated_product_num['male'],
  296. 'updated_product_female_num'=>$updated_product_num['female'],
  297. 'updated_chapter_num'=>$updated_chapter_num['total'],
  298. 'updated_chapter_male_num'=>$updated_chapter_num['male'],
  299. 'updated_chapter_female_num'=>$updated_chapter_num['female'],
  300. 'serial_num'=>$serial_num['num'],
  301. 'serial_male_num'=>$serial_num['male'],
  302. 'serial_female_num'=>$serial_num['female'],
  303. 'not_updated_serial'=>$not_updated_serial['num'],
  304. 'not_updated_serial_male'=>$not_updated_serial['inter_male']+$not_updated_serial['outer_male'],
  305. 'not_updated_serial_female'=>$not_updated_serial['inter_female']+$not_updated_serial['outer_female'],
  306. 'internal_suspend'=>$not_updated_serial['inter_num'],
  307. 'internal_suspend_male'=>$not_updated_serial['inter_male'],
  308. 'internal_suspend_female'=>$not_updated_serial['inter_female'],
  309. 'outer_suspend'=>$not_updated_serial['outer_num'],
  310. 'outer_suspend_male'=>$not_updated_serial['outer_male'],
  311. 'outer_suspend_female'=>$not_updated_serial['outer_female'],
  312. );
  313. return $data;
  314. }
  315. public static function getSuspendProducts ($paginate=true,$page_size=15,$filter=''){
  316. $two_days_ago = date('Y-m-d H:i:s',strtotime('-2 day'));
  317. $res = DB::table('books as b')
  318. ->select('b.id','b.name','bc.pid','b.updated_at','bcs.is_on_shelf')
  319. ->leftjoin('book_categories as bc','b.category_id','=','bc.id')
  320. ->leftjoin('book_configs as bcs','bcs.bid','=','b.id')
  321. ->where([
  322. ['b.updated_at','<=',$two_days_ago],
  323. ['b.status','=',0],
  324. ['bcs.is_on_shelf','>=',1],
  325. ['bcs.is_on_shelf','<=',2]
  326. ])
  327. ->orderBy('b.updated_at');
  328. if($filter){
  329. $res = $res->where($filter);
  330. }
  331. if($paginate){
  332. $res = $res->paginate($page_size);
  333. }else{
  334. $res = $res->get();
  335. }
  336. return $res;
  337. }
  338. }