erp/app/admin/logic/merchant/MerchantLogic.php
2024-05-18 14:01:51 +08:00

257 lines
9.5 KiB
PHP

<?php
namespace app\admin\logic\merchant;
use app\admin\logic\user\UserLogic;
use app\common\model\merchant\Merchant;
use app\common\logic\BaseLogic;
use app\common\model\user\User;
use think\facade\Db;
use support\exception\BusinessException;
/**
* 商户列表逻辑
* Class MerchantLogic
* @package app\admin\logic\merchant
*/
class MerchantLogic extends BaseLogic
{
/**
* @notes 添加商户列表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/23 16:35
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
$res= Merchant::create([
'category_id' => $params['category_id'],
'type_id' => $params['type_id'] ?? '',
'mer_name' => $params['mer_name'],
'credit_buy' => $params['credit_buy'] ?? 0,
'settle_cycle' => $params['settle_cycle'] ?? 15,
'interest_rate' => $params['interest_rate'] ?? 0,
'province_id' => $params['province_id'] ?? '',
'city_id' => $params['city_id'] ?? '',
'area_id' => $params['area_id'] ?? '',
'street_id' => $params['street_id'] ?? '',
'village_id' => $params['village_id'] ?? '',
'mer_address' => $params['mer_address'] ?? '',
'mer_avatar' => $params['mer_avatar'] ?? '',
'mark' => $params['mark'] ?? '',
'sort' => $params['sort'] ?? 99,
'status' => $params['status'] ?? 0,
'commission_rate' => $params['commission_rate'] ?? '',
'commission_switch' => $params['commission_switch'] ?? '',
'long' => $params['long'] ?? '',
'lat' => $params['lat'] ?? '',
'service_phone' => $params['phone'],
'service_user' => $params['service_user'] ?? '',
'mer_money' => $params['mer_money'] ?? 0,
'financial_bank' => $params['financial_bank'] ?? '',
'financial_wechat' => $params['financial_wechat'] ?? '',
'financial_alipay' => $params['financial_alipay'] ?? '',
'financial_type' => $params['financial_type'] ?? '',
'data_json' => json_encode([
'company_address' => $params['address'] ?? '',
'company_nickname' => $params['nickname'] ?? '',
'social_credit_code' => $params['social_credit_code'] ?? '',
'company_name' => $params['company_name'] ?? '',
'images' => $params['images'][0] ?? '',
])
]);
Db::name('user_auth_shop')->insert(['pid'=>$res->id,'user_id'=>0,'shop_uid'=>$params['shop_user_id'],'status'=>0,'type'=>1,'create_time'=>time()]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑商户列表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/23 16:35
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
Merchant::where('mer_id', $params['mer_id'])->update([
'category_id' => $params['category_id'],
'type_id' => $params['type_id'] ?? '',
'mer_name' => $params['mer_name'],
'credit_buy' => $params['credit_buy'],
'settle_cycle' => $params['settle_cycle'],
'interest_rate' => $params['interest_rate'],
'province_id' => $params['province_id'] ?? '',
'city_id' => $params['city_id'] ?? '',
'area_id' => $params['area_id'] ?? '',
'street_id' => $params['street_id'] ?? '',
'village_id' => $params['village_id'] ?? '',
'mer_address' => $params['mer_address'],
'mer_avatar' => $params['mer_avatar'] ?? '',
'mark' => $params['mark'],
'sort' => $params['sort'],
'status' => $params['status'],
'commission_rate' => $params['commission_rate'] ?? '',
'commission_switch' => $params['commission_switch'] ?? '',
'long' => $params['long'] ?? '',
'lat' => $params['lat'] ?? '',
'service_phone' => $params['service_phone'],
'service_user' => $params['service_user'],
'mer_money' => $params['mer_money'],
'financial_bank' => $params['financial_bank'] ?? '',
'financial_wechat' => $params['financial_wechat'] ?? '',
'financial_alipay' => $params['financial_alipay'] ?? '',
'financial_type' => $params['financial_type'] ?? ''
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 审核商户状态
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/23 16:35
*/
public static function status(array $params): bool
{
Db::startTrans();
try {
$user=[
'real_name'=>$params['service_phone'],
'nickname' => '用户'.$params['service_phone'],
'account' => $params['service_phone'],
'mobile' => $params['service_phone'],
'password' => $params['service_phone'],
'sex' => 0,
'is_disable' => 0,
'user_money' => 0
];
$res1=UserLogic::add($user);
if($res1==false){
throw new BusinessException(UserLogic::getError(), 0);
}
if($params['status']==1){
$data=[
'user_id'=>$res1['id'],
'status'=>1,
'apply_status'=>1,
'pid'=>$params['mer_id']
];
Merchant::where('mer_id',$params['mer_id'])->update(['status'=>1,'uid'=>$res1['id']]);
Db::name('user_auth_shop')->where('id',$params['apply_id'])->update($data);
}
if($params['status']==0){
$data=[
'mark'=>$params['mark']??'',
'apply_status'=>-1
];
Db::name('user_auth_shop')->where('id',$params['apply_id'])->update($data);
}
Merchant::where('mer_id', $params['mer_id'])->update([
'status' => $params['status'],
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
d($e);
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 商户绑定商品
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/23 16:35
*/
public static function bindGoods(array $params): bool
{
Db::startTrans();
try {
foreach ($params['bind_data'] as $v) {
$data = [
'mer_id' => $params['mer_id'],
'goods_id' => $v['goods_id'],
'nums' => $v['nums']
];
$has = Db::name('merchant_bind_goods')->where('mer_id', $params['mer_id'])->where('goods_id', $v['goods_id'])->findOrEmpty();
if (!empty($has)) {
Db::name('merchant_bind_goods')->where('id', $has['id'])->update($data);
} else {
Db::name('merchant_bind_goods')->insert($data);
}
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除商户列表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/23 16:35
*/
public static function delete(array $params): bool
{
return Merchant::destroy($params['mer_id']);
}
/**
* @notes 获取商户列表详情
* @param $params
* @return array
* @author likeadmin
* @date 2024/04/23 16:35
*/
public static function detail($params): array
{
$data = Merchant::where('mer_id', $params['mer_id'])->findOrEmpty();
$province = Db::name('geo_province')->field('province_name')->where('province_code', $data['province_id'])->findOrEmpty();
$city = Db::name('geo_city')->field('city_name')->where('city_code', $data['city_id'])->findOrEmpty();
$area = Db::name('geo_area')->field('area_name')->where('area_code', $data['area_id'])->findOrEmpty();
$street = Db::name('geo_street')->field('street_name')->where('street_code', $data['street_id'])->findOrEmpty();
$village = Db::name('geo_village')->field('village_name')->where('village_code', $data['village_id'])->findOrEmpty();
$data['province_name'] = !empty($province) ? $province['province_name'] : '';
$data['city_name'] = !empty($city) ? $city['city_name'] : '';
$data['area_name'] = !empty($area) ? $area['area_name'] : '';
$data['street_name'] = !empty($street) ? $street['street_name'] : '';
$data['village_name'] = !empty($village) ? $village['village_name'] : '';
return $data->toArray();
}
}