RejectedAnalyzeOwner.php 5.9 KB

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