service.tpl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * {{desc}}
  4. * @author system
  5. * @version 1.0
  6. * @date {{date}}
  7. *
  8. */
  9. namespace App\Services\{{sortPath}};
  10. use App\Services\Base\BaseProcess;
  11. use {{modelUse}};
  12. class {{serviceName}} extends BaseProcess {
  13. /**
  14. * 模型
  15. *
  16. * @var object
  17. *
  18. */
  19. private $_objModel;
  20. /**
  21. * 初始化
  22. *
  23. * @access public
  24. *
  25. */
  26. public function __construct()
  27. {
  28. if( ! $this->_objModel) $this->_objModel = new {{modelName}}();
  29. }
  30. public function model() {
  31. return $this->_objModel;
  32. }
  33. public function search(array $search,array $orderby=['{{primaryKey}}'=>'desc'],$pagesize=PAGE_NUMS)
  34. {
  35. $currentQuery = $this->_objModel;
  36. if(isset($search['keyword']) && ! empty($search['keyword'])) {
  37. $keywords = '%' . $search['keyword'] . '%';
  38. $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
  39. {{queryKeyord}}
  40. });
  41. }
  42. if($orderby && is_array($orderby)){
  43. foreach ($orderby AS $field => $value){
  44. $currentQuery = $currentQuery -> orderBy($field, $value);
  45. }
  46. }
  47. $currentQuery = $currentQuery->paginate($pagesize);
  48. return $currentQuery;
  49. }
  50. public function getAll($where,$orderby=null) {
  51. //条件
  52. $currentQuery = $this->_objModel;
  53. if($where && is_array($where)){
  54. foreach ($where AS $field => $value){
  55. $currentQuery= $currentQuery -> where($field, $value);
  56. }
  57. }
  58. //排序
  59. if($orderby && is_array($orderby)){
  60. foreach ($orderby AS $field => $value){
  61. $currentQuery = $currentQuery -> orderBy($field, $value);
  62. }
  63. }
  64. return $currentQuery->get();
  65. }
  66. public function find($id) {
  67. return $this->_objModel->find($id);
  68. }
  69. /**
  70. * 添加
  71. * @param unknown $data
  72. */
  73. public function create($data)
  74. {
  75. return $this->_objModel->create($data);
  76. }
  77. /**
  78. * 更新
  79. * @param unknown $id
  80. * @param unknown $data
  81. */
  82. public function update($id,$data)
  83. {
  84. $obj = $this->_objModel->find($id);
  85. if(!$obj) {
  86. $this->setMsg("没有找到要修改的数据");
  87. return false;
  88. }
  89. $ok = $obj->update($data);
  90. return $ok;
  91. }
  92. public function updateStatus($id,$status) {
  93. $data = $this->_objModel->find($id);
  94. $data->status = $status;
  95. return $data->save();
  96. }
  97. /**
  98. * 删除
  99. * @param unknown $id
  100. */
  101. public function destroy($id)
  102. {
  103. return $this->_objModel->destroy($id);
  104. }
  105. }