RejectedAnalyzeOwner.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelTimeFormat;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. use Illuminate\Support\Facades\DB;
  7. class RejectedAnalyzeOwner extends Model
  8. {
  9. /**
  10. * @var string[]
  11. * id_owner:货主id
  12. * owner_name:货主姓名
  13. * bounce_amount:退件数
  14. * check_amount:审核数
  15. * in_storage_count:入库数
  16. * created_at:创建时间
  17. *
  18. */
  19. protected $appends = ['id_owner', 'owner_name', 'bounce_amount', 'check_amount', 'in_storage_count'];
  20. // 按条件查询 zengjun
  21. public static function findBy($array = null)
  22. {
  23. $sql = RejectedAnalyzeOwner::getQuerySQL($array);
  24. $resultSet = DB::select($sql);
  25. $collection = array();
  26. foreach ($resultSet as $result) {
  27. $rao = new RejectedAnalyzeOwner();
  28. $rao->id_owner = $result->id_owner;
  29. $rao->owner_name = $result->name;
  30. $rao->bounce_amount = $result->bounce_amount;
  31. $rao->check_amount = $result->check_amount;
  32. $rao->in_storage_count = $result->in_storage_count;
  33. $collection[] = $rao;
  34. }
  35. return $collection;
  36. }
  37. // 按条件查询 zengjun
  38. public static function getFindBy($array){
  39. $sql = RejectedAnalyzeOwner::getQuerySQL($array);
  40. $resultSet = DB::select($sql);
  41. $list = [];
  42. foreach ($resultSet as $result) {
  43. $rao =[
  44. 'id_owner' => $result->id_owner,
  45. 'owner_name'=> $result->name,
  46. 'bounce_amount'=>$result->bounce_amount,
  47. 'check_amount'=>$result->check_amount,
  48. 'in_storage_count'=>$result->in_storage_count,
  49. ];
  50. $list[] = $rao;
  51. }
  52. return $list;
  53. }
  54. public static function getExcelFromHead($array = null){
  55. $arr = [[
  56. 'id_owner'=>'货主编号',
  57. 'owner_name'=>'货主名',
  58. 'bounce_amount'=>'退件单数',
  59. 'check_amount'=>'审核单数',
  60. 'in_storage_count'=>'入库单数',
  61. ]];
  62. return $arr;
  63. }
  64. // 拼接条件 没有ids zengjun
  65. public static function getCondition($array)
  66. {
  67. $condition = '';
  68. if (!is_null($array)) {
  69. foreach ($array as $key => $value) {
  70. if (!is_null($value)) {
  71. if ($key == 'owner_id') {
  72. $condition .= ' and id_owner = ';
  73. $condition .= $value;
  74. }
  75. if($key == 'ids'){
  76. $sql=RejectedAnalyzeOwner::getSqlToIDs($value);
  77. $condition.=$sql;
  78. }
  79. if ($key == 'created_at_start' and !is_null($value) and $value!= "null") {
  80. $value = str_replace('"','',$value);
  81. $condition .= ' and created_at > "';$condition .= $value;$condition .= '"';
  82. }
  83. if ($key == 'created_at_end' and !is_null($value) and $value!= "null") {
  84. $value = str_replace('"','',$value);
  85. $condition .= ' and created_at < "';$condition .= $value;$condition .= '"';
  86. }
  87. }
  88. }
  89. }
  90. $condition .= ' group by id_owner';
  91. return $condition;
  92. }
  93. public static function getSqlToIDs($ids):string
  94. {
  95. $sql = '';
  96. if(!is_null($ids) && $ids !='' ) {
  97. $ids = str_replace('[','(',$ids);
  98. $ids = str_replace(']',')',$ids);
  99. $ids = str_replace('"','',$ids);
  100. $sql = $sql.' and id_owner in '.$ids;
  101. }
  102. return $sql;
  103. }
  104. // 返回sql zengjun
  105. public static function getQuerySQL($array= null){
  106. $condition = RejectedAnalyzeOwner::getCondition($array);// 条件
  107. $sql = 'select distinct rao.id_owner,owners.name,sum(bounce_amount) bounce_amount,sum(check_amount) check_amount,sum(in_storage_count) in_storage_count from';
  108. $sql .= '(';
  109. // 退件单数
  110. $sql .= ' select distinct id_owner,count(1) bounce_amount,0 check_amount,0 in_storage_count from rejected_bills where 1=1 ';
  111. $sql .= $condition;
  112. $sql .= ' UNION ';
  113. // 审核
  114. $sql .= ' select distinct id_owner,0 bounce_amount,count(1) check_amount,0 in_storage_count from rejected_bills where is_checked = 1 ';
  115. $sql .= $condition;
  116. $sql .= ' UNION ';
  117. // 入库
  118. $sql .= ' select distinct id_owner,0 bounce_amount,0 check_amount,count(1) in_storage_count from rejected_bills where is_loaded = 1 ';
  119. $sql .= $condition;
  120. $sql .= ') rao ';
  121. $sql .= ' left join owners on owners.id = rao.id_owner ';
  122. $sql .= ' group by rao.id_owner ';
  123. return $sql;
  124. }
  125. }