<?php
|
|
namespace app\api\model\supplier;
|
|
use app\common\model\plus\shareholder\Apply as ShareholderApplyModel;
|
use app\common\model\supplier\Supplier as SupplierModel;
|
use app\common\model\supplier\User as SupplierUserModel;
|
use app\api\model\user\Favorite as FavoriteModel;
|
use app\api\model\product\Product as ProductModel;
|
use app\api\model\plus\team\Apply as teamApplyModel;
|
/**
|
* 供应商模型类
|
*/
|
class Supplier extends SupplierModel
|
{
|
/**
|
* 添加
|
*/
|
public function addData($data)
|
{
|
// 开启事务
|
$this->startTrans();
|
try {
|
if (SupplierUserModel::checkExist($data['user_name'])) {
|
$this->error = '用户名已存在';
|
return false;
|
}
|
// 添加供应商
|
$this->save($data);
|
//添加供应商账号
|
$SupplierUserModel = new SupplierUserModel;
|
$data['shop_supplier_id'] = $this['shop_supplier_id'];
|
$data['is_super'] = 1;
|
$SupplierUserModel->save($data);
|
(new teamApplyModel())->becomeTeamByAgent($data['referee_id'],70,$data['app_id']);
|
//根据团队人数判断股东 by yj
|
$shareholderModel = new ShareholderApplyModel;
|
$shareholderModel->becomeShareholderByTeam($data['referee_id'], 80, $data['app_id']);
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
}
|
|
public function getUserName($user_name)
|
{
|
return $this->where('user_name', '=', $user_name)->count();
|
}
|
|
//获取店铺信息
|
public function getDetail($data, $user)
|
{
|
$detail = $this->alias('s')->where(['shop_supplier_id' => $data['shop_supplier_id']])
|
->field("name as store_name,shop_supplier_id,logo_id,category_id,server_score,fav_count,user_id,product_sales,address,notice,description")
|
->with(['logo', 'category'])
|
->find();
|
if ($detail) {
|
$detail['logos'] = $detail['logo']?$detail['logo']['file_path']:'';
|
$detail['category_name'] = $detail['category']['name'];
|
unset($detail['logo']);
|
unset($detail['category']);
|
$detail['isfollow'] = 0;
|
$detail['notice'] = empty($detail['notice']) ? '欢迎光临' . $detail['store_name'] . '~' : $detail['notice'];
|
$detail['supplier_user_id'] = (new SupplierUserModel())->where('shop_supplier_id', '=', $detail['shop_supplier_id'])->value('supplier_user_id');
|
if ($user) {
|
$detail['isfollow'] = (new FavoriteModel)
|
->where('pid', '=', $data['shop_supplier_id'])
|
->where('user_id', '=', $user['user_id'])
|
->where('type', '=', 10)
|
->count();
|
}
|
}
|
return $detail;
|
}
|
|
//获取微店账号信息
|
public function getAccount($shop_supplier_id, $field = "*")
|
{
|
$detail = $this->where(['shop_supplier_id' => $shop_supplier_id])->field("$field")->find();
|
return $detail;
|
}
|
|
//店铺列表
|
public function supplierList($param)
|
{
|
// 排序规则
|
$sort = [];
|
if ($param['sortType'] === 'all') {
|
$sort = ['s.create_time' => 'desc'];
|
} else if ($param['sortType'] === 'sales') {
|
$sort = ['product_sales' => 'desc'];
|
} else if ($param['sortType'] === 'score') {
|
$sort = ['server_score' => 'desc'];
|
}
|
|
$model = $this;
|
if (isset($param['name']) && $param['name']) {
|
$model = $model->where('name', 'like', '%' . $param['name'] . '%');
|
}
|
if (isset($param['category_id']) && $param['category_id']) {
|
$model = $model->where('category_id', '=', $param['category_id']);
|
}
|
// 查询列表数据
|
$list = $model->alias('s')->with(['logo', 'category'])
|
->where('s.is_delete', '=', '0')
|
->where('s.is_recycle', '=', 0)
|
//->where('s.is_full', '=', 1)
|
->field("s.shop_supplier_id,s.name,s.fav_count,logo_id,category_id,server_score,product_sales,address,link_phone,longitude,latitude")
|
->order($sort)
|
->paginate($param);
|
$product_model = new ProductModel();
|
foreach ($list as $key => &$v) {
|
$productList = $product_model->with(['image.file'])
|
->where([
|
'shop_supplier_id' => $v['shop_supplier_id'],
|
'product_status' => 10,
|
'audit_status' => 10,
|
'is_delete' => 0
|
])
|
->order('product_sort asc,product_id desc')
|
->limit(3)
|
->field('product_id,product_price,product_name,sales_initial,sales_actual,line_price')
|
->select();
|
$v['productList'] = $productList;
|
$v['logos'] = isset($v['logo'])?$v['logo']['file_path']:'';
|
$v['category_name'] = $v['category']['name'];
|
$v['latitude'] = (float)$v['latitude'];
|
$v['longitude'] = (float)$v['longitude'];
|
unset($v['logo']);
|
unset($v['category']);
|
}
|
return $list;
|
}
|
|
//查询用户申请供应商状态
|
public static function getStatus($user)
|
{
|
$apply = (new Apply())->where('user_id', '=', $user['user_id'])
|
->find();
|
$supplier = (new static())->where('user_id', '=', $user['user_id'])
|
->where('is_delete', '=', 0)
|
->count();
|
$status = 0;
|
if ($user['user_type'] == 2) {
|
if ($supplier) {
|
$status = 2;
|
} else {
|
$status = 3;
|
}
|
} else {
|
if ($apply) {
|
if ($apply['status'] == 0) {
|
$status = 1;
|
}
|
}
|
}
|
return $status;
|
}
|
//店铺列表
|
public function getLists($shop_supplier_id='')
|
{
|
$model = $this;
|
// 查询列表数据
|
$list = $model->with(['qyQrcode'])->where('is_delete', '=', '0')
|
->where('is_recycle', '=', 0)
|
->select();
|
if(!empty($list)){
|
foreach ($list as $key => &$val){
|
if($val["shop_supplier_id"] == $shop_supplier_id){
|
$index=$key;
|
}
|
$val["add_group"]="加群";
|
}
|
}
|
$data["list"]=$list;
|
$data["index"]=empty($index) ? '-1' : $index;
|
return $data;
|
}
|
|
//店铺列表(简约)
|
public function getListSimple($params = [])
|
{
|
$model = $this;
|
if (!empty($params['keyword'])) {
|
$model = $model->where('name', 'like', '%'. $params['keyword'] .'%');
|
}
|
return $model->where('is_delete', '=', '0')
|
->where('is_recycle', '=', 0)
|
->order(['create_time' => 'desc'])
|
->paginate($params);
|
}
|
public static function getUserStore($user_id)
|
{
|
$model = new self();
|
return $model->where('user_id', '=', $user_id)
|
->where('is_delete', '=', 0)
|
->where('is_recycle', '=', 0)
|
->find();
|
}
|
}
|