AdvService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\diy;
  12. use app\dict\diy\AdvPositionDict;
  13. use app\model\diy\Adv;
  14. use core\base\BaseAdminService;
  15. use core\exception\AdminException;
  16. /**
  17. * 广告服务层
  18. */
  19. class AdvService extends BaseAdminService
  20. {
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->model = new Adv();
  25. }
  26. /**
  27. * 获取广告列表
  28. * @param array $where
  29. * @return array
  30. */
  31. public function getPage(array $where = [])
  32. {
  33. $field = 'adv_id, site_id, ap_key, adv_title, adv_url, adv_image, slide_sort, background';
  34. $order = 'adv_id desc';
  35. $search_model = $this->model->withSearch([ 'ap_key' ], $where)->field($field)->append(['ap_name'])->order($order);
  36. $list = $this->pageQuery($search_model);
  37. return $list;
  38. }
  39. /**
  40. * 获取广告列表
  41. * @param array $where
  42. * @param string $field
  43. * @return array
  44. */
  45. public function getList(array $where = [], $field = 'adv_id, site_id, ap_key, adv_title, adv_url, adv_image, slide_sort, background')
  46. {
  47. $order = 'adv_id desc';
  48. return $this->model->withSearch([ 'ap_key' ], $where)->field($field)->append(["ap_name"])->order($order)->select()->toArray();
  49. }
  50. /**
  51. * 获取广告信息
  52. * @param int $id
  53. * @return array
  54. */
  55. public function getInfo(int $id)
  56. {
  57. $field = 'adv_id, site_id, ap_key, adv_title, adv_url, adv_image, slide_sort, background';
  58. $info = $this->model->field($field)->where([ [ 'adv_id', '=', $id ], ['site_id', '=', $this->site_id] ])->append(["ap_name"])->findOrEmpty()->toArray();
  59. return $info;
  60. }
  61. /**
  62. * 添加广告
  63. * @param array $data
  64. * @return mixed
  65. */
  66. public function add(array $data)
  67. {
  68. $adv_position = AdvPositionDict::getAdvPosition();
  69. $position_list = array_column($adv_position,null,'keywords');
  70. if(!array_key_exists($data['ap_key'], $position_list)) throw new AdminException("DIY_ADV_POSITION_NOT_EXIST");
  71. $res = $this->model->create($data);
  72. return $res->adv_id;
  73. }
  74. /**
  75. * 广告编辑
  76. * @param int $id
  77. * @param array $data
  78. * @return bool
  79. */
  80. public function edit(int $id, array $data)
  81. {
  82. $adv_position = AdvPositionDict::getAdvPosition();
  83. $position_list = array_column($adv_position,null,'keywords');
  84. if(!array_key_exists($data['ap_key'], $position_list)) throw new AdminException("DIY_ADV_POSITION_NOT_EXIST");
  85. $this->model->where([ [ 'adv_id', '=', $id ]])->update($data);
  86. return true;
  87. }
  88. /**
  89. * 删除广告
  90. * @param int $id
  91. * @return bool
  92. */
  93. public function del(int $id)
  94. {
  95. $model = $this->model->where([ [ 'adv_id', '=', $id ]])->find();
  96. $res = $model->delete();
  97. return $res;
  98. }
  99. }