RejectedAnalyzeOwner.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // 拼接条件 zengjun
  71. public static function getCondition($array)
  72. {
  73. if (isset($array['data'])){
  74. $condition=RejectedAnalyzeOwner::getSqlToIDs($array['data']);
  75. }else{
  76. $condition = '';
  77. if (!is_null($array)) {
  78. foreach ($array as $key => $value) {
  79. if (!is_null($value)) {
  80. if ($key == 'owner_id' and $value!= '' and $value!='null' and $value!= '""') {
  81. $condition .= RejectedAnalyzeOwner::getSqlToIDs($value);
  82. }
  83. if ($key == 'created_at_start' and !is_null($value) and $value!= "null" and $value!= '""') {
  84. $value = str_replace('"','',$value);
  85. $condition .= ' and created_at > "';$condition .= $value;$condition .= '"';
  86. }
  87. if ($key == 'created_at_end' and !is_null($value) and $value!= "null" and $value!= '""') {
  88. $value = str_replace('"','',$value);
  89. $condition .= ' and created_at < "';$condition .= $value;$condition .= '"';
  90. }
  91. }
  92. }
  93. }
  94. }
  95. $condition .= ' group by id_owner';
  96. return $condition;
  97. }
  98. public static function getSqlToIDs($ids):string
  99. {
  100. $sql='';
  101. if(!is_null($ids) && $ids !='' ) {
  102. $ids = explode(',',$ids);
  103. $sql = ' and id_owner in (';
  104. foreach ($ids as $index => $id){
  105. if ($index!=0)
  106. $sql .=',';
  107. $sql .= $id;
  108. }
  109. $sql .= ') ';
  110. }
  111. return $sql;
  112. }
  113. // 返回sql zengjun
  114. public static function getQuerySQL($array= null){
  115. $condition = RejectedAnalyzeOwner::getCondition($array);// 条件
  116. $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';
  117. $sql .= '(';
  118. // 退件单数
  119. $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 ';
  120. $sql .= $condition;
  121. $sql .= ' UNION ';
  122. // 审核单数
  123. $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 ';
  124. $sql .= $condition;
  125. $sql .= ' UNION ';
  126. // 未入库数
  127. $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 ';
  128. $sql .= $condition;
  129. $sql .= ' UNION ';
  130. // 入库单数
  131. $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 ';
  132. $sql .= $condition;
  133. $sql .= ') rao ';
  134. $sql .= ' left join owners on owners.id = rao.id_owner and owners.deleted_at is null';
  135. $sql .= ' group by rao.id_owner having owners.name is not null order by bounce_amount desc';
  136. return $sql;
  137. }
  138. }