AdIndexList.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace backendApi\modules\v1\models\lists\ad;
  3. use backendApi\modules\v1\models\Admin;
  4. use backendApi\modules\v1\models\AdminCountry;
  5. use common\helpers\LoggerTool;
  6. use common\libs\dataList\DataListInterface;
  7. use common\models\Ad;
  8. use common\models\AdLocation;
  9. use common\models\Countries;
  10. use common\libs\dataList\column\DateTime;
  11. use Yii;
  12. class AdIndexList extends \common\libs\dataList\DataList implements DataListInterface
  13. {
  14. /**
  15. * 列表名称
  16. * @return string
  17. */
  18. public function getListName(){
  19. return '广告列表';
  20. }
  21. public function dataHandle()
  22. {
  23. $this->condition .= '';
  24. $this->listData = Ad::lists($this->condition, $this->params, [
  25. 'select' => 'AD.*',
  26. 'from' => Ad::tableName().' AS AD',
  27. 'orderBy' => 'AD.STATUS DESC,AD.SORT DESC,AD.CREATED_AT ASC',
  28. ]);
  29. LoggerTool::error(json_encode($this->listData));
  30. // COUNTRY_ID是逗号分割的ID,需要提取国家名称、CODE
  31. foreach($this->listData as $key => $row){
  32. $countryIds = explode(',', $row['COUNTRY_ID']);
  33. $countryNames = [];
  34. $countryCodes = [];
  35. foreach($countryIds as $countryId){
  36. $country = Countries::findOneAsArray('ID=:ID', [':ID'=>$countryId], 'NAME,CODE');
  37. $countryNames[] = $country['NAME'];
  38. $countryCodes[] = $country['CODE'];
  39. }
  40. $this->listData[$key]['COUNTRY_NAME'] = implode(',', $countryNames);
  41. $this->listData[$key]['COUNTRY_CODE'] = implode(',', $countryCodes);
  42. }
  43. }
  44. public function getColumn(){
  45. if(!$this->columns){
  46. $this->columns = [
  47. 'ID' => null,
  48. 'TITLE' => null,
  49. 'COUNTRY_ID' => [
  50. 'header' => Yii::t('ctx', 'country'),
  51. 'headerOther' => ['width' => '150'],
  52. ],
  53. 'COUNTRY_NAME' => null,
  54. 'COUNTRY_CODE' => null,
  55. 'LID' => [
  56. 'header' => '广告位',
  57. 'value' => function($row){
  58. $adLocation = AdLocation::findOneAsArray('ID=:ID', [':ID'=>$row['LID']], 'LOCATION_NAME');
  59. return $adLocation['LOCATION_NAME'];
  60. },
  61. 'headerOther' => ['width' => '180'],
  62. ],
  63. 'STATUS' => [
  64. 'header' => Yii::t('ctx', 'status'),
  65. 'headerOther' => ['width' => '100'],
  66. ],
  67. 'CONTENT' => null,
  68. 'TYPE' => null,
  69. 'SORT' => null,
  70. 'CREATED_AT' => [
  71. 'header' => Yii::t('ctx', 'createTime'),
  72. 'value' => function($row) {
  73. return (new DateTime([
  74. 'value' => $row['CREATED_AT'],
  75. ]))->result();
  76. },
  77. 'headerOther' => ['width' => '180'],
  78. ],
  79. ];
  80. }
  81. return $this->columns;
  82. }
  83. /**
  84. * 前台用于筛选的类型集合
  85. * @return mixed
  86. */
  87. public function getFilterTypes()
  88. {
  89. if (!$this->filterTypes) {
  90. $this->filterTypes = [
  91. 'TITLE'=> ['name'=> Yii::t('ctx', 'title')],
  92. 'CONTENT' => ['name'=> Yii::t('ctx', 'content')],
  93. 'COUNTRY'=> [
  94. 'name'=> \Yii::t('ctx', 'country'),
  95. 'other'=> 'select',
  96. 'selectData'=> self::getCountry()
  97. ],
  98. 'STATUS'=> [
  99. 'name'=> Yii::t('ctx', 'status'),
  100. 'other'=> 'select',
  101. 'selectData'=> [
  102. ['id'=> 0, 'name'=> Yii::t('ctx', 'Hide')],
  103. ['id'=> 1, 'name'=> Yii::t('ctx', 'Show')]
  104. ]
  105. ],
  106. ];
  107. }
  108. return $this->filterTypes;
  109. }
  110. public function getCountry()
  111. {
  112. $admin = Admin::findOne(Yii::$app->user->id);
  113. $roleId = $admin->ROLE_ID;
  114. if ($roleId == \Yii::$app->params['superAdminRoleId']) {
  115. $countries = Countries::find()->asArray()->all();
  116. } else {
  117. $countries = Countries::find()
  118. ->select('COU.ID, COU.CODE, COU.NAME')
  119. ->from(['COU' => Countries::tableName()])
  120. ->join('INNER JOIN', AdminCountry::tableName() . ' AS ADL', 'ADL.COUNTRY_ID = COU.ID')
  121. ->where(['ADL.ADMIN_ID' => $admin->ID])
  122. ->asArray()
  123. ->all();
  124. }
  125. $data = [];
  126. foreach ($countries as $country) {
  127. $data[] = [
  128. 'id' => $country['ID'],
  129. 'name' => $country['NAME'],
  130. ];
  131. }
  132. return $data;
  133. }
  134. }