ProductModel.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 findProducts($text = null) {
  37. $or_where = [];
  38. if(strlen($text) > 0) {
  39. array_push($or_where, ['name|bar_code', 'like', '%'.$text.'%']);
  40. }
  41. return $this->where('is_delete', 0)->where($or_where)->page(1,10)->select();
  42. }
  43. public function doesItExist($bar_code) {
  44. $customer = $this->where([
  45. ["bar_code", '=', $bar_code],
  46. ["is_delete", '=', 0]
  47. ])->find();
  48. return $customer ? true : false;
  49. }
  50. }