RejectedAnalyzeOwner.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\ModelLogChanging;
  9. class RejectedAnalyzeOwner extends Model
  10. {
  11. use ModelLogChanging;
  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, $owners)
  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. if (count($owners) > 0){
  98. $condition .= "AND id_owner IN (".implode(",",$owners).")";
  99. }
  100. $condition .= ' group by id_owner';
  101. return $condition;
  102. }
  103. public static function getSqlToIDs($ids):string
  104. {
  105. $sql='';
  106. if(!is_null($ids) && $ids !='' ) {
  107. $ids = explode(',',$ids);
  108. $sql = ' and id_owner in (';
  109. foreach ($ids as $index => $id){
  110. if ($index!=0)
  111. $sql .=',';
  112. $sql .= $id;
  113. }
  114. $sql .= ') ';
  115. }
  116. return $sql;
  117. }
  118. // 返回sql zengjun
  119. public static function getQuerySQL($array){
  120. $owners = app("OwnerService")->getIdArr();
  121. $condition = RejectedAnalyzeOwner::getCondition($array, $owners);// 条件
  122. $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';
  123. $sql .= '(';
  124. // 退件单数
  125. $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 ';
  126. $sql .= $condition;
  127. $sql .= ' UNION ';
  128. // 审核单数
  129. $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 ';
  130. $sql .= $condition;
  131. $sql .= ' UNION ';
  132. // 未入库数
  133. $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 ';
  134. $sql .= $condition;
  135. $sql .= ' UNION ';
  136. // 入库单数
  137. $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 ';
  138. $sql .= $condition;
  139. $sql .= ') rao ';
  140. $sql .= ' left join owners on owners.id = rao.id_owner and owners.deleted_at is null ';
  141. if (count($owners) == 0){
  142. $sql .= " WHERE 0=1 ";
  143. }
  144. $sql .= ' group by rao.id_owner';
  145. if(isset($array['uncheck_amount'])){
  146. // 审核数为0
  147. if($array['uncheck_amount'] == 0)$sql.=' and bounce_amount - check_amount in (0) ';
  148. // 审核数不为0
  149. if($array['uncheck_amount'] == 1)$sql.=' and check_amount <> check_amount ';
  150. }
  151. if(isset($array['not_in_storage'])){
  152. // 入库数为0
  153. if($array['not_in_storage'] == 0)$sql.=' and not_in_storage_count in (0) ';
  154. // 入库数不为0
  155. if($array['not_in_storage'] == 1)$sql.=' and not_in_storage_count <> 0 ';
  156. }
  157. $sql .= ' order by bounce_amount desc';
  158. return $sql;
  159. }
  160. }