AdIndexList.php 4.7 KB

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