RejectedAnalyzeOwner.php 6.0 KB

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