stat.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Output;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. class stat extends Command
  10. {
  11. protected function configure()
  12. { $this->setName('stat')
  13. ->setDescription('the good command');
  14. }
  15. protected function execute(Input $input, Output $output)
  16. {
  17. // 指令输出
  18. $sttime = Cache::get("departTips");
  19. if($sttime==1) return '';
  20. Cache::set("departTips",1,1800);
  21. $daprtid = [52,53,56,57];
  22. $companyNo='GS2203161855277894' ;
  23. $date=date("Y-m-d");
  24. $depart= Db::connect("mysql_sys")->name("company_item")->where(["id"=>$daprtid])->column("name","id");
  25. $company= Db::connect("mysql_sys")->name("business")->where(["companyNo"=>$companyNo])->column
  26. ("company","companyNo");
  27. foreach ($depart as $itmeid=>$depart_name){
  28. $ist=Db::name("depart_everyday")
  29. ->where(["depart_id"=>$itmeid,'companyNo'=>$companyNo,"day_time"=>$date])
  30. ->findOrEmpty();
  31. $uidArr =getUidByDepartId($itmeid);
  32. $sale=$this->GetData($uidArr,$companyNo,$date);
  33. $cgd=$this->GetCgd($uidArr,$companyNo,$date);
  34. $zy=$this->GetData($uidArr,$companyNo,$date,1);
  35. $channel=$this->GetData($uidArr,$companyNo,$date,2);
  36. if($ist){
  37. $ist['sale_total']=$sale['sale_total']??0;
  38. $ist['th_total']=$sale['th_total']??0;
  39. $ist['cgd_total']=$cgd['cgd_total']??0;
  40. $ist['cgd_th_total']=$cgd['th_cgd']??0;
  41. $ist['zy_sale_total']=$zy['sale_total']-$zy['th_total'];
  42. $ist['channel_sale_total']=$channel['sale_total']-$channel['th_total'];
  43. }else{
  44. $ist=[
  45. "depart_id"=>$itmeid,
  46. "depart_name"=>$depart_name,
  47. "day_time"=>date("Y-m-d"),
  48. "sale_total"=>$sale['sale_total']??0,
  49. "th_total"=>$sale['th_total']??0,
  50. "cgd_total"=>$cgd['cgd_total']??0,
  51. "cgd_th_total"=>$cgd['th_cgd']??0,
  52. "zy_sale_total"=>$zy['sale_total']-$zy['th_total'],
  53. "channel_sale_total"=>$channel['sale_total']-$channel['th_total'],
  54. "companyNo"=>$companyNo,
  55. "companyName"=>$company[$companyNo],
  56. ];
  57. }
  58. Db::name("depart_everyday")->save($ist);
  59. }
  60. $date=date('Y-m-d H:i:s');
  61. $output->writeln("[{$date}] 更新数据成功");
  62. }
  63. //各部门统计数据
  64. /**
  65. * @param $uid
  66. * @param $companyNo
  67. * @param $date
  68. * @return array
  69. */
  70. public function GetData($uid,$companyNo,$date,$type=0):array{
  71. $where=[];
  72. if($type==1) $where[]=["cxCode","=",''];
  73. if($type==2) $where[]=["cxCode","<>",''];
  74. $day_total =Db::name("qrd_info")->where([["ownerid","in",$uid],["is_del","=",0],['companyNo',"=",$companyNo]])
  75. ->whereDay("createdTime",$date)
  76. ->where($where)
  77. ->sum(Db::raw('totalPrice+th_fee'));
  78. $day_thtotal=Db::name("th_source")->alias("a")
  79. ->leftJoin("qrd_info b","a.th_qrdNo=b.sequenceNo or a.th_qrdNo=b.cxCode")
  80. ->where([["b.ownerid","in",$uid],['b.companyNo',"=",$companyNo]])
  81. ->where($where)
  82. ->whereDay("a.createtime",$date)->sum(Db::raw('a.th_num*b.goodPrice'));
  83. return ["sale_total"=>$day_total,"th_total"=>$day_thtotal];
  84. }
  85. public function GetCgd($uid,$companyNo,$date){
  86. $where=[];
  87. $day_total =Db::name("qrd_info")->alias("a")
  88. ->leftJoin('cgd_info b', 'b.sequenceNo=a.cgdNo and b.is_del=0')
  89. ->where([["a.ownerid","in",$uid],["a.is_del","=",0],['a.companyNo',"=",$companyNo]])
  90. ->whereDay("a.createdTime",$date)
  91. ->where($where)
  92. ->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))"));
  93. $day_thtotal=Db::name("th_source")->alias("a")
  94. ->leftJoin("qrd_info b","a.th_qrdNo=b.sequenceNo or a.th_qrdNo=b.cxCode")
  95. ->leftJoin('cgd_info c', 'c.sequenceNo=b.cgdNo and c.is_del=0')
  96. ->where([["b.ownerid","in",$uid],['b.companyNo',"=",$companyNo]])
  97. ->where($where)
  98. ->whereDay("a.createtime",$date)->sum(Db::raw('a.th_num*ifnull(c.goodPrice,0)'));
  99. return ["cgd_total"=>$day_total,"th_cgd"=>$day_thtotal];
  100. }
  101. }