StoreProductModel.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\common\model;
  3. class StoreProductModel extends BaseModel
  4. {
  5. protected $table = 'erp_store_product';
  6. protected function genSchema(array $schema)
  7. {
  8. // TODO: Implement genSchema() method.
  9. }
  10. /**
  11. * @return \think\model\relation\HasOne
  12. */
  13. public function store()
  14. {
  15. return self::hasOne(StoreModel::class,'id','store_id')->where('is_delete', 0);
  16. }
  17. public function product()
  18. {
  19. return self::hasOne(ProductModel::class,'id','product_id')->where('is_delete', 0);
  20. }
  21. public function doesItExist($store_id, $product_id) {
  22. return $this->where([
  23. ["store_id", '=', $store_id],
  24. ["product_id", '=', $product_id],
  25. ["is_delete", '=', 0]
  26. ])->find();
  27. }
  28. /**
  29. * @param array $params
  30. * @return \think\Paginator
  31. * @throws \think\db\exception\DbException
  32. */
  33. public function findByPaginate(array $params) {
  34. // if(!is_null($params['bar_code']))
  35. // array_push($where,['bar_code', 'like', "%".$params['bar_code']."%"]);
  36. // if(!is_null($params['category_id']) && $params['category_id'] > 0)
  37. // array_push($where,['category_id', '=', $params['category_id']]);
  38. // if(!is_null($params['is_serve']))
  39. // array_push($where,['is_serve', '=', $params['is_serve']]);
  40. $store_where = [
  41. ['is_delete','=', 0],
  42. ];
  43. $product_where = [
  44. ['is_delete','=', 0],
  45. ];
  46. if($params['store_id'] > 0)
  47. array_push($store_where,["id", "=", $params['store_id']]);
  48. return $this->where('erp_store_product.is_delete',0)
  49. ->hasWhere('store', $store_where)
  50. ->hasWhere('product',$product_where)
  51. ->with(['store', 'product'])
  52. ->order(isset($params['sort']) ? $params['sort'] : 'create_time','desc')
  53. ->paginate(['list_rows'=>10, "query" => $params]);
  54. }
  55. public function search(array $params) {
  56. $where = [
  57. ['is_delete', '=', 0],
  58. ['store_id', '=', $params['store_id']]
  59. ];
  60. if($params['bar_code'])
  61. array_push($where, ['product_bar_code', 'like', '%'.$params['bar_code'].'%']);
  62. if($params['product_name'])
  63. array_push($where, ['product_name', 'like', '%'.$params['product_name'].'%']);
  64. return $this->where($where)
  65. ->with('product')
  66. ->order('create_time','desc')
  67. ->page($params['page'])
  68. ->paginate($params['size'] ?? 10);
  69. }
  70. public function fetchRelationsByOrder($ids = []) {
  71. $where = [
  72. ['id', 'in', $ids],
  73. ['is_delete', '=', 0]
  74. ];
  75. return $this->where($where)
  76. ->order('create_time','desc')
  77. ->with('product')
  78. ->select();
  79. }
  80. public function fetchByStoreIdAndProductIds($s_id, $p_ids) {
  81. $where = [
  82. ['product_id', 'in', $p_ids],
  83. ['store_id', '=', $s_id],
  84. ['is_delete', '=', 0]
  85. ];
  86. return $this->where($where)
  87. ->order('create_time','desc')
  88. ->select();
  89. }
  90. }