StoreProductModel.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. ->page($params['page'])
  67. ->paginate($params['size'] ?? 10);
  68. }
  69. public function fetchRelationsByOrder($ids = []) {
  70. $where = [
  71. ['id', 'in', $ids],
  72. ['is_delete', '=', 0]
  73. ];
  74. return $this->where($where)
  75. ->with('product')
  76. ->select();
  77. }
  78. }