Product.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\system;
  4. use library\basic\BaseModel;
  5. use library\traits\JwtAuthModelTrait;
  6. use library\traits\ModelTrait;
  7. use think\Model;
  8. /**
  9. * @mixin \think\Model
  10. */
  11. class Product extends BaseModel
  12. {
  13. use ModelTrait;
  14. use JwtAuthModelTrait;
  15. //仓库
  16. private $warehouse;
  17. /**
  18. * 获取列表数据
  19. * @param $page
  20. * @param $where
  21. * @param $pageCount
  22. * @param $desc
  23. */
  24. public function getList($page,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
  25. $data = $this
  26. ->field("p.*,(SELECT cate_name from table_category where p.cate_id = id) as cate_name")
  27. ->alias("p")
  28. ->when(!empty($where),function ($query) use($where){
  29. if(!empty($where['name'])){
  30. $query->whereLike('p.title',"%{$where['name']}%");
  31. }
  32. if($where['artType']=='open'){
  33. $query->where('p.status',1);
  34. }
  35. if($where['artType']=='stop'){
  36. $query->where('p.status',0);
  37. }
  38. })
  39. ->order($desc)
  40. ->paginate(['list_rows'=>$pageCount,'page'=>$page])
  41. ->toArray();
  42. foreach ($data['data'] as $k=>$v) {
  43. $data['data'][$k]['warehouseAr'] = $this->getWarehouse($v['warehouse_ids']);
  44. $data['data'][$k]['add_time'] = date('Y-m-d',$v['add_time']);
  45. }
  46. return [$data['total'],$data['data']];
  47. }
  48. /**
  49. * 保存产品数据
  50. * @param $post
  51. * @return bool|int|string
  52. */
  53. public function saveProduct($post){
  54. if(!empty($post['id'])) {
  55. $this->where('id',$post['id'])->save($post);
  56. return true;
  57. } else {
  58. unset($post['id']);
  59. $post['add_time'] = time();
  60. $bool = $this->insert($post);
  61. return $bool;
  62. }
  63. }
  64. /**
  65. * 删除数据
  66. * @param $id
  67. */
  68. public function delProduct($id) {
  69. $this->where('id',$id)->delete();
  70. return true;
  71. }
  72. /**
  73. * 获取仓库数据
  74. * @param $ids
  75. * @return array
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. private function getWarehouse($ids) {
  81. $idAr = explode(',',$ids);
  82. $rAr = [];
  83. if(empty($this->warehouse))
  84. $this->warehouse = (new Warehouse)->select()->toArray();
  85. foreach ($this->warehouse as $v) {
  86. if(in_array($v['id'],$idAr)) {
  87. $rAr[] = $v;
  88. }
  89. }
  90. return $rAr;
  91. }
  92. }