ProductModel.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\common\model;
  3. class ProductModel extends BaseModel
  4. {
  5. protected $table = 'erp_product';
  6. protected function genSchema(array $schema)
  7. {
  8. // TODO: Implement genSchema() method.
  9. }
  10. public function category() {
  11. return self::hasOne(ProductCategoryModel::class,'id','category_id');
  12. }
  13. public function company() {
  14. return self::hasOne(CompanyModel::class,'id','company_id');
  15. }
  16. /**
  17. * @param array $params
  18. * @return \think\Paginator
  19. * @throws \think\db\exception\DbException
  20. */
  21. public function findByPaginate(array $params) {
  22. $where = [
  23. ['erp_product.is_delete', '=', 0]
  24. ];
  25. if(!is_null($params['bar_code']))
  26. array_push($where,['erp_product.bar_code', 'like', "%".$params['bar_code']."%"]);
  27. if(!is_null($params['category_id']) && $params['category_id'] > 0)
  28. array_push($where,['erp_product.category_id', '=', $params['category_id']]);
  29. if(!is_null($params['is_serve']))
  30. array_push($where,['erp_product.is_serve', '=', $params['is_serve']]);
  31. return $this->with(['category', 'company'])
  32. ->where($where)
  33. ->order('create_time','desc')
  34. ->paginate(['list_rows'=>10, "query" => $params]);
  35. }
  36. public function fetchByStock($params) {
  37. $where = [
  38. ['erp_product.is_delete', '=', 0]
  39. ];
  40. return $this->with(['category', 'company'])
  41. ->where($where)
  42. ->order('create_time','desc')
  43. ->paginate(['list_rows'=>10, "query" => $params]);
  44. }
  45. public function findProducts($text = null) {
  46. $or_where = [];
  47. if(strlen($text) > 0) {
  48. array_push($or_where, ['name|bar_code', 'like', '%'.$text.'%']);
  49. }
  50. return $this
  51. ->where('is_delete', 0)
  52. ->where($or_where)
  53. ->page(1,10)
  54. ->select();
  55. }
  56. public function doesItExist($bar_code) {
  57. $customer = $this->where([
  58. ["bar_code", '=', $bar_code],
  59. ["is_delete", '=', 0]
  60. ])->find();
  61. return $customer ? true : false;
  62. }
  63. }