MateReport.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\cron;
  3. use app\common\util\AppMsg;
  4. use think\Db;
  5. use yunwuxin\cron\Task;
  6. class MateReport extends Task
  7. {
  8. public function configure()
  9. {
  10. $this->monthlyOn(25); // 每月25号0:00执行一次
  11. }
  12. /**
  13. * 执行任务
  14. * @return mixed
  15. */
  16. protected function execute()
  17. {
  18. try {
  19. $this->remind();
  20. } catch (\Exception $e) {
  21. trace($e->getMessage());
  22. }
  23. }
  24. public function remind(){
  25. $sday = date('Y-m', strtotime('last month')).'-25 ';
  26. $starttime = $sday." 00:00:00";
  27. $eday = date('Y-m').'-25 ';
  28. $endtime = $eday."00:00:00";
  29. $orgs = Db::name("org")->where('type',2)->where('enable',1)->where('del',0)->select();
  30. foreach($orgs as $k=>$v){
  31. $goods = Db::name('mate_goods')
  32. ->where('org_id',$v['id'])
  33. ->where('del',0)
  34. ->where('enable',1)
  35. ->field('id,nums')
  36. ->select();
  37. if(!$goods){ // 无商品不生成记录
  38. continue;
  39. }
  40. $innums = 0;
  41. $outnums = 0;
  42. $usenums = 0;
  43. $cznums = 0;
  44. $nums = 0;
  45. $reportGoods = [];
  46. foreach ($goods as $kk=>$vv){
  47. $innums1 = Db::name('mate_apply_goods')
  48. ->alias('a')
  49. ->join('mate_apply b','a.apply_id = b.id')
  50. ->where('b.del',0)
  51. ->where('b.type',1)
  52. ->where('b.org_id',$v['id'])
  53. ->where('a.goods_id',$vv['id'])
  54. ->where('b.create_time','>=',$starttime)
  55. ->where('b.create_time','<',$endtime)
  56. ->sum('a.nums');
  57. $innums += $innums1;
  58. $outnums1 = Db::name('mate_apply_goods')
  59. ->alias('a')
  60. ->join('mate_apply b','a.apply_id = b.id')
  61. ->where('b.del',0)
  62. ->where('b.type',2)
  63. ->where('b.org_id',$v['id'])
  64. ->where('a.goods_id',$vv['id'])
  65. ->where('b.create_time','>=',$starttime)
  66. ->where('b.create_time','<',$endtime)
  67. ->sum('a.nums');
  68. $outnums += $outnums1;
  69. $usenums1 = Db::name('todo_mate_item')
  70. ->alias('a')
  71. ->join('todo_mate b','a.todo_mate_id = b.id')
  72. ->where('b.org_id',$v['id'])
  73. ->where('a.items_id',$vv['id'])
  74. ->where('b.create_time','>=',$starttime)
  75. ->where('b.create_time','<',$endtime)
  76. ->sum('a.total');
  77. $usenums += $usenums1;
  78. $cznums1 = Db::name('mate_goods_log')
  79. ->where('type',1)
  80. ->where('org_id',$v['id'])
  81. ->where('goods_id',$vv['id'])
  82. ->where('create_time','>=',$starttime)
  83. ->where('create_time','<',$endtime)
  84. ->sum('nums');
  85. $cznums += $cznums1;
  86. $nums += $vv['nums'];
  87. $reportGoods[] = [
  88. 'org_id' => $v['id'],
  89. 'goods_id' => $vv['id'],
  90. 'in_nums' => $innums1,
  91. 'out_nums' => $outnums1,
  92. 'use_nums' => $usenums1,
  93. 'cz_nums' => $cznums1,
  94. 'nums' => $vv['nums'],
  95. ];
  96. }
  97. Db::startTrans();
  98. try{
  99. $reportId = Db::name("mate_report")->insertGetId([
  100. 'org_id' => $v['id'],
  101. 'start_time' => $sday,
  102. 'end_time' => $eday,
  103. 'in_nums' => $innums,
  104. 'out_nums' => $outnums,
  105. 'use_nums' => $usenums,
  106. 'cz_nums' => $cznums,
  107. 'nums' => $nums,
  108. 'create_time' => date("Y-m-d H:i:s")
  109. ]);
  110. $arr = [];
  111. foreach ($reportGoods as $kk=>$vv){
  112. $vv['report_id'] = $reportId;
  113. $arr[] = $vv;
  114. if(count($arr) == 200){
  115. $res = Db::name("mate_report_goods")->insertAll($arr);
  116. if($res != count($arr)){
  117. exception("操作失败");
  118. }
  119. $arr = [];
  120. }
  121. }
  122. if(count($arr) > 0){
  123. $res = Db::name("mate_report_goods")->insertAll($arr);
  124. if($res != count($arr)){
  125. exception("操作失败");
  126. }
  127. }
  128. Db::commit();
  129. }catch (\Exception $e){
  130. Db::rollback();
  131. }
  132. }
  133. }
  134. }