multi-store/app/api/logic/user/AddressLogic.php

137 lines
4.0 KiB
PHP

<?php
namespace app\api\logic\user;
use app\common\logic\BaseLogic;
use app\common\model\user\UserAddress;
use think\facade\Db;
/**
* 地址逻辑
* Class OrderLogic
* @package app\api\logic\order
*/
class AddressLogic extends BaseLogic
{
/**
* @notes 添加地址表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/24 10:37
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
if(isset($params['is_default'])&&$params['is_default']==1){
UserAddress::where('uid',$params['uid'])->update(['is_default'=>0]);
}else{
$params['is_default']=1;
}
$id=UserAddress::insertGetId([
'uid' => $params['uid'],
'real_name' => $params['real_name'],
'phone' => $params['phone'],
'province'=> $params['province'],
'city' => $params['city'],
'area' => $params['area'],
'street' => $params['street'],
'village' => $params['village'],
'brigade' => $params['brigade'],
'detail' => '',
'is_default' => $params['is_default'],
'create_time' => time(),
'update_time' => time(),
]);
Db::commit();
return $id;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑地址表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/24 10:37
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
$is_default = $params['is_default'] ??0;
if($is_default==1){
UserAddress::where('uid',$params['uid'])->update(['is_default'=>0]);
}
$find=UserAddress::where('uid', $params['uid'])->where('id', $params['id'])->find();
$data = [
'real_name' => $params['real_name'],
'phone' => $params['phone'],
'detail' => $params['detail']??'',
'detail' => $params['detail']??'',
'is_default' => $params['is_default']??0,
'province' => $params['province'],
'city' => $params['city'],
'area' => $params['area'],
'street' => $params['street'],
'village' => $params['village'],
'brigade' => $params['brigade'],
];
if($find){
UserAddress::where('uid', $params['uid'])->where('id', $params['id'])->update($data);
}else{
$data['is_default']=1;
$data['uid']=$params['uid'];
UserAddress::create($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/24 10:37
*/
public static function delete(array $params): bool
{
return UserAddress::destroy($params['address_id']);
}
/**
* @notes 获取地址详情
* @param $params
* @return array
* @author likeadmin
* @date 2024/04/24 10:37
*/
public static function detail($params): array
{
return UserAddress::field('id,real_name,phone,province,city,area,street,village,brigade,detail,is_default')->where('id',$params['address_id'])->findOrEmpty()->toArray();
}
public static function info($params): array
{
return UserAddress::field('id,real_name,phone,province,city,area,street,village,brigade,detail,is_default')->where($params)->findOrEmpty()->toArray();
}
}