Supplier.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\api\model\supplier;
  3. use app\common\model\supplier\Supplier as SupplierModel;
  4. use app\common\model\supplier\User as SupplierUserModel;
  5. use app\api\model\user\Favorite as FavoriteModel;
  6. use app\api\model\product\Product as ProductModel;
  7. /**
  8. * 供应商模型类
  9. */
  10. class Supplier extends SupplierModel
  11. {
  12. /**
  13. * 添加
  14. */
  15. public function addData($data)
  16. {
  17. // 开启事务
  18. $this->startTrans();
  19. try {
  20. if (SupplierUserModel::checkExist($data['user_name'])) {
  21. $this->error = '用户名已存在';
  22. return false;
  23. }
  24. // 添加供应商
  25. $this->save($data);
  26. //添加供应商账号
  27. $SupplierUserModel = new SupplierUserModel;
  28. $data['shop_supplier_id'] = $this['shop_supplier_id'];
  29. $data['is_super'] = 1;
  30. $SupplierUserModel->save($data);
  31. $this->commit();
  32. return true;
  33. } catch (\Exception $e) {
  34. $this->error = $e->getMessage();
  35. $this->rollback();
  36. return false;
  37. }
  38. }
  39. public function getUserName($user_name)
  40. {
  41. return $this->where('user_name', '=', $user_name)->count();
  42. }
  43. //获取店铺信息
  44. public function getDetail($data, $user)
  45. {
  46. $detail = $this->alias('s')->where(['shop_supplier_id' => $data['shop_supplier_id']])
  47. ->field("name as store_name,shop_supplier_id,logo_id,category_id,server_score,fav_count,user_id,product_sales")
  48. ->with(['logo', 'category'])
  49. ->find();
  50. if ($detail) {
  51. $detail['logos'] = $detail['logo']['file_path'];
  52. $detail['category_name'] = $detail['category']['name'];
  53. unset($detail['logo']);
  54. unset($detail['category']);
  55. $detail['isfollow'] = 0;
  56. $detail['supplier_user_id'] = (new SupplierUserModel())->where('shop_supplier_id', '=', $detail['shop_supplier_id'])->value('supplier_user_id');
  57. if ($user) {
  58. $detail['isfollow'] = (new FavoriteModel)
  59. ->where('pid', '=', $data['shop_supplier_id'])
  60. ->where('user_id', '=', $user['user_id'])
  61. ->where('type', '=', 10)
  62. ->count();
  63. }
  64. }
  65. return $detail;
  66. }
  67. //获取微店账号信息
  68. public function getAccount($shop_supplier_id, $field = "*")
  69. {
  70. $detail = $this->where(['shop_supplier_id' => $shop_supplier_id])->field("$field")->find();
  71. return $detail;
  72. }
  73. //店铺列表
  74. public function supplierList($param)
  75. {
  76. // 排序规则
  77. $sort = [];
  78. if ($param['sortType'] === 'all') {
  79. $sort = ['s.create_time' => 'desc'];
  80. } else if ($param['sortType'] === 'sales') {
  81. $sort = ['product_sales' => 'desc'];
  82. } else if ($param['sortType'] === 'score') {
  83. $sort = ['server_score' => 'desc'];
  84. }
  85. $model = $this;
  86. if (isset($param['name']) && $param['name']) {
  87. $model = $model->where('name', 'like', '%' . $param['name'] . '%');
  88. }
  89. // 查询列表数据
  90. $list = $model->alias('s')->with(['logo', 'category'])
  91. ->where('s.is_delete', '=', '0')
  92. ->where('s.is_recycle', '=', 0)
  93. //->where('s.is_full', '=', 1)
  94. ->field("s.shop_supplier_id,s.name,s.fav_count,logo_id,category_id,server_score,product_sales")
  95. ->order($sort)
  96. ->paginate($param);
  97. $product_model = new ProductModel();
  98. foreach ($list as $key => &$v) {
  99. $productList = $product_model->with(['image.file'])
  100. ->where([
  101. 'shop_supplier_id' => $v['shop_supplier_id'],
  102. 'product_status' => 10,
  103. 'audit_status' => 10,
  104. 'is_delete' => 0
  105. ])
  106. ->order('product_sort asc,product_id desc')
  107. ->limit(3)
  108. ->field('product_id,product_price,product_name,sales_initial,sales_actual,line_price')
  109. ->select();
  110. $v['productList'] = $productList;
  111. $v['logos'] = $v['logo']['file_path'];
  112. $v['category_name'] = $v['category']['name'];
  113. unset($v['logo']);
  114. unset($v['category']);
  115. }
  116. return $list;
  117. }
  118. }