ContractDistributionsController.php 3.4 KB

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