ProductionStatsEmailService.php 15 KB

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