ProjectRecord.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\admin\model;
  3. use think\model;
  4. use app\admin\model\Keywords;
  5. use think\facade\Db;
  6. class ProjectRecord 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('RecordKeywords')->strict(false)->field(true)->insertAll($insert);
  25. // }
  26. /**
  27. * 获取分页列表
  28. * @param $where
  29. * @param $param
  30. */
  31. public function getRecordList($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('RecordCate 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 addRecord($param)
  53. {
  54. $insertId = 0;
  55. try {
  56. $param['create_time'] = time();
  57. $insertId = $this->strict(false)->field(true)->insertGetId($param);
  58. //关联关键字
  59. // if (isset($param['keyword_names']) && $param['keyword_names']) {
  60. // $keywordArray = explode(',', $param['keyword_names']);
  61. // $res_keyword = $this->insertKeyword($keywordArray,$insertId);
  62. // }
  63. add_log('add', $insertId, $param);
  64. } catch(\Exception $e) {
  65. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  66. }
  67. return to_assign(0,'操作成功',['aid'=>$insertId]);
  68. }
  69. /**
  70. * 编辑信息
  71. * @param $param
  72. */
  73. public function editRecord($param)
  74. {
  75. try {
  76. $param['update_time'] = time();
  77. $this->where('id', $param['id'])->strict(false)->field(true)->update($param);
  78. //关联关键字
  79. // if (isset($param['keyword_names']) && $param['keyword_names']) {
  80. // \think\facade\Db::name('RecordKeywords')->where(['aid'=>$param['id']])->delete();
  81. // $keywordArray = explode(',', $param['keyword_names']);
  82. // $res_keyword = $this->insertKeyword($keywordArray,$param['id']);
  83. // }
  84. add_log('edit', $param['id'], $param);
  85. } catch(\Exception $e) {
  86. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  87. }
  88. return to_assign();
  89. }
  90. /**
  91. * 根据id获取信息
  92. * @param $id
  93. */
  94. public function getRecordById($id)
  95. {
  96. $info = $this->where('id', $id)->find();
  97. return $info;
  98. }
  99. /**
  100. * 删除信息
  101. * @param $id
  102. * @return array
  103. */
  104. public function delRecordById($id,$type=0)
  105. {
  106. if($type==0){
  107. //逻辑删除
  108. try {
  109. $param['delete_time'] = time();
  110. $this->where('id', $id)->update(['delete_time'=>time()]);
  111. add_log('delete', $id);
  112. } catch(\Exception $e) {
  113. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  114. }
  115. }
  116. else{
  117. //物理删除
  118. try {
  119. $this->where('id', $id)->delete();
  120. add_log('delete', $id);
  121. } catch(\Exception $e) {
  122. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  123. }
  124. }
  125. return to_assign();
  126. }
  127. public function detail($id)
  128. {
  129. $detail = Db::name('ProjectRecord')->where(['id' => $id])->find();
  130. // if (!empty($detail)) {
  131. // $detail['project_name'] = '-';
  132. // if ($detail['project_id'] > 0) {
  133. // $detail['project_name'] = Db::name('Project')->where(['id' => $detail['project_id']])->value('name');
  134. // }
  135. // $detail['admin_name'] = Db::name('Admin')->where(['id' => $detail['admin_id']])->value('name');
  136. // $detail['times'] = time_trans($detail['create_time']);
  137. // $detail['create_time'] = date('Y-m-d', $detail['create_time']);
  138. // }
  139. return $detail;
  140. }
  141. }