AdIndexList.php 4.8 KB

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