model.tpl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. namespace app\<module>\model;
  14. use think\model;
  15. use think\facade\Db;
  16. class <Bmodel> extends Model
  17. {
  18. /**
  19. * 获取分页列表
  20. * @param $where
  21. * @param $param
  22. */
  23. public function datalist($param=[],$where=[],$whereOr=[])
  24. {
  25. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  26. $order = empty($param['order']) ? 'id desc' : $param['order'];
  27. try {
  28. $list = self::where($where)
  29. ->where(function ($query) use($whereOr) {
  30. if (!empty($whereOr)){
  31. $query->whereOr($whereOr);
  32. }
  33. })
  34. ->order($order)->paginate(['list_rows'=> $rows]);
  35. return $list;
  36. } catch(\Exception $e) {
  37. return ['code' => 1, 'data' => [], 'msg' => $e->getMessage()];
  38. }
  39. }
  40. /**
  41. * 添加数据
  42. * @param $param
  43. */
  44. public function add($param)
  45. {
  46. $insertId = 0;
  47. try {
  48. $param['create_time'] = time();
  49. $insertId = self::strict(false)->field(true)->insertGetId($param);
  50. add_log('add', $insertId, $param);
  51. } catch(\Exception $e) {
  52. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  53. }
  54. return to_assign(0,'操作成功',['return_id'=>$insertId]);
  55. }
  56. /**
  57. * 编辑信息
  58. * @param $param
  59. */
  60. public function edit($param)
  61. {
  62. try {
  63. $param['update_time'] = time();
  64. self::where('id', $param['id'])->strict(false)->field(true)->update($param);
  65. add_log('edit', $param['id'], $param);
  66. } catch(\Exception $e) {
  67. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  68. }
  69. return to_assign(0,'操作成功',['return_id'=>$param['id']]);
  70. }
  71. /**
  72. * 根据id获取信息
  73. * @param $id
  74. */
  75. public function getById($id)
  76. {
  77. $info = self::find($id);
  78. return $info;
  79. }
  80. /**
  81. * 删除信息
  82. * @param $id
  83. * @param $type
  84. * @return array
  85. */
  86. public function delById($id,$type=0)
  87. {
  88. if($type==0){
  89. //逻辑删除
  90. try {
  91. $param['delete_time'] = time();
  92. self::where('id', $id)->update(['delete_time'=>time()]);
  93. add_log('delete', $id);
  94. } catch(\Exception $e) {
  95. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  96. }
  97. }
  98. else{
  99. //物理删除
  100. try {
  101. self::destroy($id);
  102. add_log('delete', $id);
  103. } catch(\Exception $e) {
  104. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  105. }
  106. }
  107. return to_assign();
  108. }
  109. }