Product.php 3.3 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\contract\model;
  14. use think\model;
  15. use think\facade\Db;
  16. class Product extends Model
  17. {
  18. /**
  19. * 获取分页列表
  20. * @param $where
  21. * @param $param
  22. */
  23. public function datalist($where, $param)
  24. {
  25. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  26. $order = empty($param['order']) ? 'p.id desc' : $param['order'];
  27. try {
  28. $list = self::where($where)
  29. ->field('p.*,pc.title as cate')
  30. ->alias('p')
  31. ->join('ProductCate pc', 'pc.id = p.cate_id', 'left')
  32. ->order($order)
  33. ->paginate(['list_rows'=> $rows])
  34. ->each(function ($item, $key){
  35. $item->admin_name = Db::name('Admin')->where('id',$item->admin_id)->value('name');
  36. });
  37. return $list;
  38. } catch(\Exception $e) {
  39. return ['code' => 1, 'data' => [], 'msg' => $e->getMessage()];
  40. }
  41. }
  42. /**
  43. * 添加数据
  44. * @param $param
  45. */
  46. public function add($param)
  47. {
  48. $insertId = 0;
  49. try {
  50. $param['create_time'] = time();
  51. $insertId = self::strict(false)->field(true)->insertGetId($param);
  52. add_log('add', $insertId, $param);
  53. } catch(\Exception $e) {
  54. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  55. }
  56. return to_assign(0,'操作成功',['aid'=>$insertId]);
  57. }
  58. /**
  59. * 编辑信息
  60. * @param $param
  61. */
  62. public function edit($param)
  63. {
  64. try {
  65. $param['update_time'] = time();
  66. self::where('id', $param['id'])->strict(false)->field(true)->update($param);
  67. add_log('edit', $param['id'], $param);
  68. } catch(\Exception $e) {
  69. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  70. }
  71. return to_assign();
  72. }
  73. /**
  74. * 根据id获取信息
  75. * @param $id
  76. */
  77. public function getById($id)
  78. {
  79. $info = self::find($id);
  80. return $info;
  81. }
  82. /**
  83. * 删除信息
  84. * @param $id
  85. * @param $type
  86. * @return array
  87. */
  88. public function delById($id,$type=0)
  89. {
  90. if($type==0){
  91. //逻辑删除
  92. try {
  93. $param['delete_time'] = time();
  94. self::where('id', $id)->update(['delete_time'=>time()]);
  95. add_log('delete', $id);
  96. } catch(\Exception $e) {
  97. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  98. }
  99. }
  100. else{
  101. //物理删除
  102. try {
  103. self::destroy($id);
  104. add_log('delete', $id);
  105. } catch(\Exception $e) {
  106. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  107. }
  108. }
  109. return to_assign();
  110. }
  111. }