RejectedAnalyzeOwner.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. 'uncheck_amount'=>$result->bounce_amount-$result->check_amount,
  49. 'in_storage_count'=>$result->in_storage_count,
  50. 'not_in_storage_count'=>$result->bounce_amount-$result->in_storage_count,
  51. ];
  52. $list[] = $rao;
  53. }
  54. return $list;
  55. }
  56. public static function getExcelFromHead($array = null){
  57. $arr = [[
  58. 'id_owner'=>'货主编号',
  59. 'owner_name'=>'货主名',
  60. 'bounce_amount'=>'退件单数',
  61. 'check_amount'=>'审核单数',
  62. 'uncheck_amount'=>'未审核单数',
  63. 'in_storage_count'=>'入库单数',
  64. 'not_in_storage_count'=>'未入库单数',
  65. ]];
  66. return $arr;
  67. }
  68. // 拼接条件 没有ids zengjun
  69. public static function getCondition($array)
  70. {
  71. $condition = '';
  72. if (!is_null($array)) {
  73. foreach ($array as $key => $value) {
  74. if (!is_null($value)) {
  75. if ($key == 'owner_id' and $value!= '' and $value!='null' and $value!= '""') {
  76. $condition .= ' and id_owner = ';
  77. $condition .= $value;
  78. }
  79. if($key == 'ids'){
  80. $sql=RejectedAnalyzeOwner::getSqlToIDs($value);
  81. $condition.=$sql;
  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. $condition .= ' group by id_owner';
  95. return $condition;
  96. }
  97. public static function getSqlToIDs($ids):string
  98. {
  99. $sql = '';
  100. if(!is_null($ids) && $ids !='' ) {
  101. $ids = str_replace('[','(',$ids);
  102. $ids = str_replace(']',')',$ids);
  103. $ids = str_replace('"','',$ids);
  104. $sql = $sql.' and id_owner in '.$ids;
  105. }
  106. return $sql;
  107. }
  108. // 返回sql zengjun
  109. public static function getQuerySQL($array= null){
  110. $condition = RejectedAnalyzeOwner::getCondition($array);// 条件
  111. $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';
  112. $sql .= '(';
  113. // 退件单数
  114. $sql .= ' select distinct id_owner,count(1) bounce_amount,0 check_amount,0 in_storage_count from rejected_bills where 1=1 ';
  115. $sql .= $condition;
  116. $sql .= ' UNION ';
  117. // 审核单数
  118. $sql .= ' select distinct id_owner,0 bounce_amount,count(1) check_amount,0 in_storage_count from rejected_bills where is_checked = 1 ';
  119. $sql .= $condition;
  120. $sql .= ' UNION ';
  121. // 入库单数
  122. $sql .= ' select distinct id_owner,0 bounce_amount,0 check_amount,count(1) in_storage_count from rejected_bills where is_loaded = 1 ';
  123. $sql .= $condition;
  124. $sql .= ') rao ';
  125. $sql .= ' left join owners on owners.id = rao.id_owner ';
  126. $sql .= ' group by rao.id_owner ';
  127. return $sql;
  128. }
  129. }