Product.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\admin;
  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. ->order($desc)
  29. ->paginate(['list_rows'=>$pageCount,'page'=>$page])
  30. ->toArray();
  31. foreach ($data['data'] as $k=>$v) {
  32. $data['data'][$k]['warehouseAr'] = $this->getWarehouse($v['warehouse_ids']);
  33. $data['data'][$k]['add_time'] = date('Y-m-d',$v['add_time']);
  34. }
  35. return [$data['total'],$data['data']];
  36. }
  37. /**
  38. * 保存产品数据
  39. * @param $post
  40. * @return bool|int|string
  41. */
  42. public function saveProduct($post){
  43. if(!empty($post['id'])) {
  44. $this->where('id',$post['id'])->save($post);
  45. return true;
  46. } else {
  47. unset($post['id']);
  48. $post['add_time'] = time();
  49. $bool = $this->insert($post);
  50. return $bool;
  51. }
  52. }
  53. /**
  54. * 删除数据
  55. * @param $id
  56. */
  57. public function delProduct($id) {
  58. $this->where('id',$id)->delete();
  59. return true;
  60. }
  61. /**
  62. * 获取仓库数据
  63. * @param $ids
  64. * @return array
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. private function getWarehouse($ids) {
  70. $idAr = explode(',',$ids);
  71. $rAr = [];
  72. if(empty($this->warehouse))
  73. $this->warehouse = (new Warehouse)->select()->toArray();
  74. foreach ($this->warehouse as $v) {
  75. if(in_array($v['id'],$idAr)) {
  76. $rAr[] = $v;
  77. }
  78. }
  79. return $rAr;
  80. }
  81. }