ProjectReport.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\admin\model;
  3. use think\model;
  4. use app\admin\model\Keywords;
  5. use think\facade\Db;
  6. class ProjectReport extends Model
  7. {
  8. public static $Type = ['普通','精华','热门','推荐'];
  9. // //插入关键字
  10. // public function insertKeyword($keywordArray = [], $aid = 0)
  11. // {
  12. // $insert = [];
  13. // $time = time();
  14. // foreach ($keywordArray as $key => $value) {
  15. // if (!$value) {
  16. // continue;
  17. // }
  18. // $keywords_id = (new Keywords())->increase($value);
  19. // $insert[] = ['aid' => $aid,
  20. // 'keywords_id' => $keywords_id,
  21. // 'create_time' => $time,
  22. // ];
  23. // }
  24. // $res = Db::name('ReportKeywords')->strict(false)->field(true)->insertAll($insert);
  25. // }
  26. /**
  27. * 获取分页列表
  28. * @param $where
  29. * @param $param
  30. */
  31. public function getReportList($where, $param)
  32. {
  33. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  34. $order = empty($param['order']) ? 'a.id desc' : $param['order'];
  35. $list = self::where($where)
  36. ->field('a.*,c.id as cate_id,c.title as cate_title,u.nickname as admin_name')
  37. ->alias('a')
  38. ->join('ReportCate c', 'a.cate_id = c.id')
  39. ->join('Admin u', 'a.admin_id = u.id')
  40. ->order($order)
  41. ->paginate($rows, false, ['query' => $param])
  42. ->each(function ($item, $key) {
  43. $type = (int)$item->type;
  44. $item->type_str = self::$Type[$type];
  45. });
  46. return $list;
  47. }
  48. /**
  49. * 添加数据
  50. * @param $param
  51. */
  52. public function addReport($param)
  53. {
  54. $insertId = 0;
  55. try {
  56. $param['create_time'] = time();
  57. $insertId = $this->strict(false)->field(true)->insertGetId($param);
  58. add_log('add', $insertId, $param);
  59. } catch(\Exception $e) {
  60. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  61. }
  62. return to_assign(0,'操作成功',['aid'=>$insertId]);
  63. }
  64. /**
  65. * 编辑信息
  66. * @param $param
  67. */
  68. public function editReport($param)
  69. {
  70. try {
  71. $param['update_time'] = time();
  72. $this->where('id', $param['id'])->strict(false)->field(true)->update($param);
  73. //关联关键字
  74. // if (isset($param['keyword_names']) && $param['keyword_names']) {
  75. // \think\facade\Db::name('ReportKeywords')->where(['aid'=>$param['id']])->delete();
  76. // $keywordArray = explode(',', $param['keyword_names']);
  77. // $res_keyword = $this->insertKeyword($keywordArray,$param['id']);
  78. // }
  79. add_log('edit', $param['id'], $param);
  80. } catch(\Exception $e) {
  81. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  82. }
  83. return to_assign();
  84. }
  85. /**
  86. * 根据id获取信息
  87. * @param $id
  88. */
  89. public function getReportById($id)
  90. {
  91. $info = $this->where('id', $id)->find();
  92. return $info;
  93. }
  94. /**
  95. * 删除信息
  96. * @param $id
  97. * @return array
  98. */
  99. public function delReportById($id,$type=0)
  100. {
  101. if($type==0){
  102. //逻辑删除
  103. try {
  104. $param['delete_time'] = time();
  105. $this->where('id', $id)->update(['delete_time'=>time()]);
  106. add_log('delete', $id);
  107. } catch(\Exception $e) {
  108. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  109. }
  110. }
  111. else{
  112. //物理删除
  113. try {
  114. $this->where('id', $id)->delete();
  115. add_log('delete', $id);
  116. } catch(\Exception $e) {
  117. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  118. }
  119. }
  120. return to_assign();
  121. }
  122. public function detail($id)
  123. {
  124. $detail = Db::name('ProjectReport')->where(['id' => $id])->find();
  125. // if (!empty($detail)) {
  126. // $detail['project_name'] = '-';
  127. // if ($detail['project_id'] > 0) {
  128. // $detail['project_name'] = Db::name('Project')->where(['id' => $detail['project_id']])->value('name');
  129. // }
  130. // $detail['admin_name'] = Db::name('Admin')->where(['id' => $detail['admin_id']])->value('name');
  131. // $detail['times'] = time_trans($detail['create_time']);
  132. // $detail['create_time'] = date('Y-m-d', $detail['create_time']);
  133. // }
  134. return $detail;
  135. }
  136. }