companyStats.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\command;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;
  9. use think\facade\Db;
  10. use think\facade\Cache;
  11. class companyStats extends Command
  12. {
  13. protected function configure()
  14. {
  15. // 指令配置
  16. $this->setName('companystats')
  17. ->setDescription('the companystats command');
  18. }
  19. protected function execute(Input $input, Output $output)
  20. {
  21. $sttime = Cache::get("compTips");
  22. if($sttime==1) return '';
  23. Cache::set("compTips",1,1800);
  24. $supplier = ["GS2302231124114965","GS2302231125079621","GS2302231323386950",'GS2203161855277894',"GS2304031312553746"];
  25. try{
  26. foreach ($supplier as $sp){
  27. $this->checkData($sp);
  28. }
  29. }catch (\Exception $e){
  30. echo $e->getMessage();
  31. }
  32. }
  33. /**
  34. * @param $supplierNo
  35. */
  36. public function checkData($supplierNo,$dateArr=[]):void {
  37. $sup=Db::connect("mysql_sys")->name("headquarters")->where(["code"=>$supplierNo])->field("code,name,relation_code")->findOrEmpty();
  38. if (empty($sup)) return;
  39. $date=date("Y-m-d");
  40. // $start="20230501";
  41. // $end= date("Ymt",strtotime($start));
  42. // $date_tmp=$start;
  43. // $temp=[];
  44. // while ($date_tmp>=$start && $date_tmp<=$end) {
  45. // $date=date("Y-m-d" , strtotime($date_tmp . ''));
  46. $d=Db::name("depart_company")->where(["companyNo"=>$supplierNo , "day_time"=>$date])->column("*" , "type");
  47. $total=Db::name("qrd_info")
  48. ->where(["companyNo"=>$supplierNo , "is_del"=>0 , "cxCode"=>""])
  49. ->whereDay("createdTime" , $date)
  50. ->field("sum(totalPrice+th_fee) as total")->findOrEmpty();
  51. $day_thtotal=Db::name("th_source")->alias("a")
  52. ->leftJoin("qrd_info b" , "a.th_qrdNo=b.sequenceNo")
  53. ->where([['a.th_companyNo' , "=" , $supplierNo] , ["b.is_del" , "=" , 0]])
  54. ->whereDay("a.createtime" , $date)->field('sum(a.th_qrd_fee) as th_total')->findOrEmpty();
  55. $cgdtotal = $this->cgdTotal($supplierNo,$date,1);
  56. if (isset($d[1])) Db::name("depart_company")->where($d[1])->save([
  57. "sale_total"=>$total['total'] ?? 0 ,
  58. "th_total"=>$day_thtotal['th_total'] ?? 0,
  59. "cgd_total"=>$cgdtotal['cgd_total']??0,
  60. "cgd_th_total"=>$cgdtotal['cgd_th_total']??0,
  61. ]);
  62. else $temp[]=[
  63. "companyNo"=>$supplierNo ,
  64. "companyName"=>$sup['name'] ?? "" ,
  65. "type"=>1 ,
  66. "day_time"=>$date ,
  67. "sale_total"=>$total['total'] ?? 0 ,
  68. "th_total"=>$day_thtotal['th_total'] ?? 0 ,
  69. "cgd_total"=>$cgdtotal['cgd_total']??0,
  70. "cgd_th_total"=>$cgdtotal['cgd_th_total']??0,
  71. "addtime"=>date("Y-m-d H:i:s")
  72. ];
  73. $total_cx=Db::name("qrd_info")
  74. ->where(["companyNo"=>$supplierNo , "is_del"=>0])
  75. ->where("cxCode" , "<>" , "")
  76. ->whereDay("createdTime" , $date)
  77. ->field("sum(totalPrice+th_fee) as total")->findOrEmpty();
  78. $day_thtotal_cx=Db::name("th_source")->alias("a")
  79. ->leftJoin("qrd_info b" , "a.th_qrdNo=b.cxCode")
  80. ->where([['b.companyNo' , "=" , $supplierNo] , ["b.is_del" , "=" , 0]])
  81. ->whereDay("a.createtime" , $date)
  82. ->field("sum(a.th_num*b.goodPrice) as th_total")->findOrEmpty();
  83. $cgdtotal = $this->cgdTotal($supplierNo,$date,2);
  84. if (isset($d[2])) Db::name("depart_company")->where($d[2])->save([
  85. "sale_total"=>$total_cx['total'] ?? 0 ,
  86. "th_total"=>$day_thtotal_cx['th_total'] ?? 0,
  87. "cgd_total"=>$cgdtotal['cgd_total']??0,
  88. "cgd_th_total"=>$cgdtotal['cgd_th_total']??0,
  89. ]
  90. );
  91. else $temp[]=[
  92. "companyNo"=>$supplierNo ,
  93. "companyName"=>$sup['name'] ?? "" ,
  94. "type"=>2 ,
  95. "day_time"=>$date ,
  96. "sale_total"=>$total_cx['total'] ?? 0 ,
  97. "th_total"=>$day_thtotal_cx['th_total'] ?? 0 ,
  98. "cgd_total"=>$cgdtotal['cgd_total']??0,
  99. "cgd_th_total"=>$cgdtotal['cgd_th_total']??0,
  100. "addtime"=>date("Y-m-d H:i:s")
  101. ];
  102. $cgd=$this->cgdData($sup['relation_code'] , $date);
  103. if (isset($d[3])) Db::name("depart_company")->where($d[3])->save(["sale_total"=>$cgd['cgd_total'] ,
  104. "th_total"=>$cgd['cgd_thtotal']]
  105. );
  106. else $temp[]=[
  107. "companyNo"=>$supplierNo ,
  108. "companyName"=>$sup['name'] ?? "" ,
  109. "type"=>3 ,
  110. "day_time"=>$date ,
  111. "sale_total"=>$cgd['cgd_total'] ,
  112. "th_total"=>$cgd['cgd_thtotal'] ,
  113. 'cgd_total'=>0,
  114. 'cgd_th_total'=>0,
  115. "addtime"=>date("Y-m-d H:i:s")
  116. ];
  117. // $date_tmp++;
  118. // }
  119. if(!empty($temp))Db::name("depart_company")->insertAll($temp);
  120. }
  121. /**
  122. * @param $sup
  123. * @param $date
  124. * @return array
  125. */
  126. private function cgdData($sup,$date): array
  127. {
  128. $total =Db::name("cgd_info")
  129. ->where(["supplierNo"=>$sup,"is_del"=>0,"cxCode"=>""])
  130. ->whereDay("createdTime",$date)
  131. ->field("sum(totalPrice+th_fee) as total")->findOrEmpty();
  132. $day_thtotal=Db::name("th_source")->alias("a")
  133. ->leftJoin("cgd_info b","(a.th_cgdNo=b.sequenceNo and a.is_th=1)")
  134. ->where([['b.supplierNo',"=",$sup],["b.is_del","=",0]])
  135. ->whereDay("a.createtime",$date)
  136. ->field("sum(a.th_num*b.goodPrice) as th_total")->findOrEmpty();
  137. return ["cgd_total"=>$total['total']??0,"cgd_thtotal"=>$day_thtotal['th_total']??0];
  138. }
  139. /**
  140. * @param $sup
  141. * @param $date
  142. * @return array
  143. */
  144. private function cgdTotal($companyNo,$date,$type): array
  145. {
  146. $where=[];
  147. if($type==1)$where=[["a.cxCode","=",'']];
  148. if($type==2)$where=[["a.cxCode","<>",'']];
  149. $day_total =Db::name("qrd_info")->alias("a")
  150. ->leftJoin('cgd_info b', 'b.sequenceNo=a.cgdNo and b.is_del=0')
  151. ->where([["a.is_del","=",0],['a.companyNo',"=",$companyNo]])
  152. ->where($where)
  153. ->whereDay("a.createdTime",$date)
  154. ->sum(Db::raw("(ifnull(b.totalPrice,if(a.total_origin_price=0,a.total_plan_price,a.total_origin_price))+ifnull(b.th_fee,0))"));
  155. $day_thtotal=Db::name("th_source")->alias("b")
  156. ->leftJoin("qrd_info a","b.th_qrdNo=a.sequenceNo or b.th_qrdNo=a.cxCode")
  157. ->leftJoin('cgd_info c', 'c.sequenceNo=a.cgdNo and c.is_del=0')
  158. ->where([['a.companyNo',"=",$companyNo]])
  159. ->where($where)
  160. ->whereDay("b.createtime",$date)->sum(Db::raw('b.th_num*ifnull(c.goodPrice,0)'));
  161. return ["cgd_total"=>$day_total,"th_cgd"=>$day_thtotal];
  162. }
  163. }