ContractDistributionsController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Http\Controllers\ContractDistribution;
  3. use App\Http\Controllers\AdminBaseController;
  4. use App\Http\Requests\ContractDistributionCreateRequest;
  5. use App\Http\Requests\ContractDistributionUpdateRequest;
  6. use App\Models\Broadcast;
  7. use App\Models\Contract;
  8. use App\Models\ContractDistribution;
  9. use App\Repositories\Eloquent\ContractDistributionRepositoryEloquent;
  10. use App\Servers\ContractServer;
  11. use App\Validators\ContractDistributionValidator;
  12. /**
  13. * Class ContractDistributionsController.
  14. *
  15. * @package namespace App\Http\Controllers;
  16. */
  17. class ContractDistributionsController extends AdminBaseController
  18. {
  19. /**
  20. * @var ContractDistributionRepositoryEloquent
  21. */
  22. protected $repository;
  23. /**
  24. * @var ContractDistributionValidator
  25. */
  26. protected $validator;
  27. /**
  28. * ContractDistributionsController constructor.
  29. *
  30. * @param ContractDistributionRepositoryEloquent $repository
  31. * @param ContractDistributionValidator $validator
  32. */
  33. public function __construct(ContractDistributionRepositoryEloquent $repository, ContractDistributionValidator $validator)
  34. {
  35. parent::__construct($repository, $validator);
  36. }
  37. /**
  38. * 数据检索
  39. */
  40. public function _indexScopeQuery()
  41. {
  42. $where = [];
  43. $search = explode(";", request()->input('search', ""));
  44. $start = $end = null;
  45. $fieldSearchable = $this->repository->getFieldsSearchable();
  46. foreach ($search as $value) {
  47. if (!empty($value)) {
  48. list($one, $tow) = explode(":", $value);
  49. if (!empty($fieldSearchable[$one])) {
  50. if ($fieldSearchable[$one] == 'like') $tow = "%{$tow}%";
  51. $where[] = [$one, $fieldSearchable[$one], $tow];
  52. } elseif ($one == 'start') {
  53. $start = $tow . " 00:00:00";
  54. } elseif ($one == 'end') {
  55. $end = $tow . " 23:59:59";
  56. } else {
  57. continue;
  58. }
  59. }
  60. }
  61. return function ($query) use ($where, $start, $end) {
  62. if ($start) $where[] = ['created_at', '>=', $start];
  63. if ($end) $where[] = ['created_at', '<=', $end];
  64. if ($where) {
  65. $query->where($where);
  66. }
  67. };
  68. }
  69. function _indexGet()
  70. {
  71. $contractId = request()->input('contractId');
  72. $typeArr = ContractServer::creatServer()->getDistributionArr();
  73. $contractSn = '';
  74. if ($contractId) {
  75. $contractSn = Contract::where('id', $contractId)->value('contract_sn');
  76. }
  77. return ['contractSn'=>$contractSn,'typeArr'=>$typeArr];
  78. }
  79. /**
  80. * 数据更新
  81. */
  82. protected function _indexPost($datum)
  83. {
  84. $status = ['失败', '成功', '广播失败','广播成功'];
  85. foreach ($datum as $value) {
  86. $value->{'type'} = ContractServer::creatServer()->getDistributionArrStr($value->{'type'});
  87. $value->{'status'} = empty($status[$value->{'status'}]) ? '状态异常' : $status[$value->{'status'}];
  88. $value->{'hash'} =$value->{'broadcast_id'}? Broadcast::where('id',$value->{'broadcast_id'})->value('hash'):'';
  89. }
  90. return $datum;
  91. }
  92. function _indexJoin()
  93. {
  94. return ContractDistribution::from('contract_distributions as cb')
  95. ->leftJoin('contracts as c', 'c.id', '=', 'cb.contract_id')
  96. ->leftJoin('broadcasts as b', 'b.id', '=', 'cb.broadcast_id')
  97. ->select(['cb.*', 'c.contract_sn', 'b.hash']);
  98. }
  99. }