SuccessCaseModel.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Web\Models;
  3. use App\Models\BaseModel;
  4. use Illuminate\Support\Facades\DB;
  5. class SuccessCaseModel extends BaseModel
  6. {
  7. /**
  8. * 状态字段
  9. */
  10. const DELETED_AT = 'status';
  11. /**
  12. * @var string
  13. */
  14. protected $table = 'success_case';
  15. /**
  16. * 检查 标签是否唯一
  17. * */
  18. public function checkTitleUnique($title,$id=0){
  19. $where=[];
  20. if(!empty($id)){
  21. $where[]=['id', '<>', $id];
  22. }
  23. $where[]=['status', '<', 2];
  24. $where[]=['title', '=', trim($title)];
  25. return $this->checkFieldUnique('title',$where);
  26. }
  27. /**
  28. * 保存成功案例
  29. * @param array $data
  30. * */
  31. public function saveSuccessCaseData($data){
  32. if (!empty($data['id'])) {
  33. $id = $data['id'];
  34. $this->newInstance()->where('id', $data['id'])->update($data);
  35. } else {
  36. $id = $this->newInstance()->insertGetId($data);
  37. }
  38. return $id;
  39. }
  40. /**
  41. * 获取成功案例列表
  42. * */
  43. public function getSuccessCaseList($params,$fields=''){
  44. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  45. $where=[];
  46. if(empty($fields)){
  47. $fields="a.*,b.name as 'function_name',c.name as 'industry_name' ";
  48. }
  49. if(isset($params['status'])){
  50. $where[]=['a.status','=',$params['status']];
  51. }else{
  52. $where[]=['a.status','<',2];
  53. }
  54. if(isset($params['is_handpick'])){
  55. $isHandpick=empty($params['is_handpick'])?0:$params['is_handpick'];
  56. $where[]=['a.is_handpick','=',$isHandpick];
  57. }
  58. if(!empty($params['function_id'])){
  59. $functionId=empty($params['function_id'])?0:$params['function_id'];
  60. $where[]=['a.function_id','=',$functionId];
  61. }
  62. if(!empty($params['industry_id'])){
  63. $industryId=empty($params['industry_id'])?0:$params['industry_id'];
  64. $where[]=['a.industry_id','=',$industryId];
  65. }
  66. $query= $this->newInstance()->alias('a')->where($where);
  67. if (!empty($params['keyword'])) {
  68. $keyword = $params['keyword'];
  69. $query->where('a.title', 'like', "%" . $keyword . "%");
  70. }
  71. $totalCount = $query->count();
  72. $list= $query->leftJoin('dict_function as b', 'a.function_id', '=', 'b.id')
  73. ->leftJoin('dict_industry as c', 'a.industry_id', '=', 'c.id')
  74. ->skip($skip)
  75. ->limit($pageSize)
  76. ->selectRaw($fields)
  77. ->orderBy('a.sort')
  78. ->get();
  79. if(!empty($list)){
  80. $list=$list->toArray();
  81. }else{
  82. $list=[];
  83. }
  84. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  85. return $result;
  86. }
  87. /**
  88. * 获取成功案例详情
  89. * @param array $params
  90. * */
  91. public function getSuccessCaseInfo($params=[]){
  92. if(empty($params)){
  93. return [];
  94. }
  95. $where=[];
  96. $where[]=['a.status','<',2];
  97. if(!empty($params['id'])){
  98. $where['a.id']=$params['id'];
  99. }
  100. $info= $this->newInstance()->alias('a')
  101. ->leftJoin('dict_function as b', 'a.function_id', '=', 'b.id')
  102. ->leftJoin('dict_industry as c', 'a.industry_id', '=', 'c.id')
  103. ->where($where)
  104. ->selectRaw("a.*,b.name as 'function_name',c.name as 'industry_name' ")
  105. ->first();
  106. if(!empty($info)){
  107. return $info->toArray();
  108. }else{
  109. return [];
  110. }
  111. }
  112. }