Files
anmo/server/app/shopapi/logic/ShopLogic.php
2025-08-19 14:16:51 +08:00

448 lines
17 KiB
PHP
Executable File

<?php
namespace app\shopapi\logic;
use app\common\enum\coach\CoachEnum;
use app\common\enum\GoodsEnum;
use app\common\enum\shop\ShopEnum;
use app\common\logic\BaseLogic;
use app\common\model\coach\Coach;
use app\common\model\coach\CoachServerTime;
use app\common\model\coach\CoachUser;
use app\common\model\goods\Goods;
use app\common\model\shop\Shop;
use app\common\model\shop\ShopCategoryIndex;
use app\common\model\shop\ShopGoodsIndex;
use app\common\model\shop\ShopImage;
use app\common\model\shop\ShopUpdate;
use app\common\model\shop\ShopUser;
use app\common\service\sms\SmsDriver;
use Exception;
use think\facade\Db;
class ShopLogic extends BaseLogic
{
/**
* @notes 个人中心
* @param int $shopId
* @return Shop|array|\think\Model
* @author cjhao
* @date 2024/10/20 03:23
*/
public function centre(int $shopId)
{
$shop = Shop::where(['id'=>$shopId])
->field('sn,name,mobile,logo,audit_status,audit_remark,money,deposit')
->findOrEmpty()->toArray();
$shop['coach_count'] = Coach::where(['shop_id'=>$shopId])->count();
$shop['shop_goods_count'] = Goods::where(['shop_id'=>$shopId,'audit_status'=>GoodsEnum::AUDIT_STATUS_PASS])->count();
$shop['goods_count'] = ShopGoodsIndex::where(['shop_id'=>$shopId])->count();
$shop['audit_status'] = $shop['audit_status'] ?? 0;
$shop['audit_remark'] = $shop['audit_remark'] ?? '';
return $shop;
}
/**
* @notes 获取手机号码
* @param int $shopId
* @return array
* @author cjhao
* @date 2024/11/19 11:34
*/
public function info(int $shopId)
{
$shop = Shop::where(['id'=>$shopId])
->field('sn,name,mobile,logo,audit_status,audit_remark,money,deposit,create_time')
->findOrEmpty()
->toArray();
return $shop;
}
/**
* @notes 申请
* @param array $params
* @return string|true
* @author cjhao
* @date 2024/10/5 20:26
*/
public function apply(array $params)
{
try {
Db::startTrans();
$shop = shop::where(['shop_user_id'=>$params['shop_user_id']])
->order('id desc')
->findOrEmpty();
if( !$shop->isEmpty() && ShopEnum::AUDIT_STATUS_PASS == $shop->audit_status ){
return '当前账号已经入驻成功,请勿重复申请';
}
if( !$shop->isEmpty() && ShopEnum::AUDIT_STATUS_WAIT == $shop->audit_status ){
return '当前账号申请正在审核中,请耐心等待';
}
$shopUser = ShopUser::where(['id'=>$params['shop_user_id']])->findOrEmpty();
$shop = Shop::create([
'name' => $params['name'],
'mobile' => $shopUser['account'],
'shop_user_id' => $params['shop_user_id'],
'sn' => sprintf("%03d", Shop::count()+1),
'short_name' => $params['short_name'],
'type' => $params['type'],
'social_credit_ode' => $params['social_credit_ode'],
'legal_person' => $params['legal_person'],
'legal_id_card' => $params['legal_id_card'],
'province_id' => $params['province_id'],
'city_id' => $params['city_id'],
'region_id' => $params['region_id'],
'shop_address_detail'=> $params['shop_address_detail'],
'longitude' => $params['longitude'],
'latitude' => $params['latitude'],
'id_card_front' => $params['id_card_front'],
'id_card_back' => $params['id_card_back'],
'portrait_shooting' => $params['portrait_shooting'],
'logo' => $params['logo'],
'business_license' => $params['business_license'],
'synopsis' => $params['synopsis'] ?? '',
'audit_status' => ShopEnum::AUDIT_STATUS_WAIT,
'work_status' => $params['work_status'] ?? 0,
'server_status' => $params['server_status'] ?? 1,
'business_start_time' => $params['business_start_time'] ?? '',
'business_end_time' => $params['business_end_time'] ?? '',
]);
$categoryLists = [];
foreach ($params['category_ids'] as $categoryId){
$categoryLists[] = [
'shop_id' => $shop['id'],
'category_id' => $categoryId,
];
}
(new ShopCategoryIndex())->saveAll($categoryLists);
$goodsLists = [];
foreach ($params['goods_ids'] as $goodsId) {
$goodsLists[] = [
'shop_id' => $shop['id'],
'goods_id' => $goodsId,
];
}
(new ShopGoodsIndex())->saveAll($goodsLists);
$shopImageLists = [];
$params['shop_image'] = $params['shop_image'] ? : [];
foreach ($params['shop_image'] as $image){
$shopImageLists[] = [
'shop_id' => $shop['id'],
'uri' => $image,
];
}
(new ShopImage())->saveAll($shopImageLists);
Db::commit();
return true;
}catch (Exception $e){
Db::rollback();
return $e->getMessage();
}
}
/**
* @notes 详情接口
* @param int $id
* @param int $shopUserId
* @return array
* @author cjhao
* @date 2024/10/5 20:28
*/
public function detail($shopUserId)
{
$detail = Shop::where(['shop_user_id'=>$shopUserId])
->with(['shop_image'])
->append(['audit_status_desc','region_desc','province_name','city_name','region_name'])
->withoutField('update_time,delete_time')
->order('id desc')
->findOrEmpty()
->toArray();
$categoryLists = ShopCategoryIndex::alias('SC')
->where(['SC.shop_id'=>$detail['id'],'is_show'=>1])
->join('goods_category GC','SC.category_id = GC.id')
->field('GC.id,GC.name')
->select()
->toArray();
$goodsLists = ShopGoodsIndex::alias('SG')
->where(['SG.shop_id'=>$detail['id'],'G.status'=>1])
->join('goods G','SG.goods_id = G.id')
->field('G.id,G.name')
->select()
->toArray();
$detail['category_lists'] = $categoryLists;
$detail['goods_lists'] = $goodsLists;
$detail['goods_ids'] = array_column($goodsLists,'id');
$detail['category_ids'] = array_column($categoryLists,'id');
return $detail;
}
/**
* @notes 更新店铺信息
* @param array $params
* @return string|true
* @author cjhao
* @date 2024/10/6 16:39
*/
public function updateInfo(array $params)
{
try {
if(!$params['shop_id']){
throw new Exception('请等待你店铺申请通过后,才能提交资料');
}
$shopUpdate = ShopUpdate::where(['shop_id'=>$params['shop_id'],'audit_status'=>CoachEnum::AUDIT_STATUS_WAIT])
->findOrEmpty();
if(!$shopUpdate->isEmpty()){
throw new Exception('您提交资料正在审核中,请勿重复提交');
}
// $mobile = Shop::where(['mobile'=>$params['mobile']])->where('id','<>',$params['shop_id'])->findOrEmpty();
// if(!$mobile->isEmpty()){
// throw new Exception('手机号码已存在');
// }
// ShopUpdate::where(['mobile'=>$params['mobile'],'audit_status'=>ShopEnum::AUDIT_STATUS_WAIT])->where('id','<>',$params['shop_id'])->findOrEmpty();
// if(!$mobile->isEmpty()){
// throw new Exception('手机号码已存在');
// }
$shopUser = ShopUser::where(['id'=>$params['shop_user_id']])->findOrEmpty();
ShopUpdate::create([
'name' => $params['name'],
'shop_user_id' => $params['shop_user_id'],
'shop_id' => $params['shop_id'],
'sn' => $params['sn'],
'short_name' => $params['short_name'],
'mobile' => $shopUser['account'],
'business_start_time' => $params['business_start_time'],
'business_end_time' => $params['business_end_time'],
'type' => $params['type'],
'social_credit_ode' => $params['social_credit_ode'],
'legal_person' => $params['legal_person'],
'legal_id_card' => $params['legal_id_card'],
// 'shop_adress' => $params['shop_adress'],
'shop_address_detail' => $params['shop_address_detail'],
'province_id' => $params['province_id'],
'city_id' => $params['city_id'],
'region_id' => $params['region_id'],
'longitude' => $params['longitude'],
'latitude' => $params['latitude'],
'id_card_front' => $params['id_card_front'],
'id_card_back' => $params['id_card_back'],
'portrait_shooting' => $params['portrait_shooting'],
'logo' => $params['logo'],
'business_license' => $params['business_license'],
'synopsis' => $params['synopsis'] ?? '',
'category_ids' => $params['category_ids'],
'goods_ids' => $params['goods_ids'],
'shop_image' => $params['shop_image'] ?? []
]);
return true;
}catch (Exception $e){
return $e->getMessage();
}
}
/**
* @notes 设置服务时间
* @param array $params
* @return string|true
* @author cjhao
* @date 2024/10/21 00:27
*/
public function setServerTime(array $params)
{
try {
$coach = Coach::where(['id'=>$params['id'],'shop_id'=>$params['shop_id']])->findOrEmpty();
if($coach->isEmpty()){
throw new \think\Exception('您无法设置当前技师服务时间');
}
Db::startTrans();
$serverTime = [];
foreach ($params['server_time'] as $key => $time){
$times = [];
foreach ($time['time_lists'] as $timeList){
if(in_array($timeList['time'],$times)){
continue;
}
$serverTime[] = [
'coach_id' => $params['id'],
'date' => $time['time_date'],
'time' => $timeList['time'],
'status' => $timeList['status']
];
$times[] = $timeList['time'];
}
}
CoachServerTime::where(['coach_id'=>$params['id']])->delete();
(new CoachServerTime())->saveAll($serverTime);
Db::commit();
return true;
} catch (Exception $e) {
Db::rollback();
return $e->getMessage();
}
}
/**
* @notes 获取营业时间
* @param int $shopId
* @return Shop|array|\think\Model
* @author cjhao
* @date 2024/10/22 09:32
*/
public function getBusiness(int $shopId){
$detail = Shop::where(['id'=>$shopId])
->field('id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,business_start_time,business_end_time')
->findOrEmpty()->toArray();
$detail['monday'] = 1 == $detail['monday'] ? true : false;
$detail['tuesday'] = 1 == $detail['tuesday'] ? true : false;
$detail['wednesday'] = 1 == $detail['wednesday'] ? true : false;
$detail['thursday'] = 1 == $detail['thursday'] ? true : false;
$detail['friday'] = 1 == $detail['friday'] ? true : false;
$detail['saturday'] = 1 == $detail['saturday'] ? true : false;
$detail['sunday'] = 1 == $detail['sunday'] ? true : false;
return $detail;
}
/**
* @notes 设置营业时间
* @param array $params
* @return true
* @author cjhao
* @date 2024/10/22 09:43
*/
public function setBusiness(array $params)
{
$monday = $params['monday'] ?? 0;
$tuesday = $params['tuesday'] ?? 0;
$wednesday = $params['wednesday'] ?? 0;
$thursday = $params['thursday'] ?? 0;
$friday = $params['friday'] ?? 0;
$saturday = $params['saturday'] ?? 0;
$sunday = $params['sunday'] ?? 0;
$businessStartTime = $params['business_start_time'] ?? 0;
$businessEndTime = $params['business_end_time'] ?? 0;
Shop::where(['id'=>$params['shop_id']])
->update([
'monday' => $monday,
'tuesday' => $tuesday,
'wednesday' => $wednesday,
'thursday' => $thursday,
'friday' => $friday,
'saturday' => $saturday,
'sunday' => $sunday,
'business_start_time' => $businessStartTime,
'business_end_time' => $businessEndTime,
]);
return true;
}
/**
* @notes 获取资料详情
* @param $shopId
* @return array
* @author cjhao
* @date 2024/11/25 11:24
*/
public function updateInfoDetail($shopUserId)
{
$detail = ShopUpdate::where(['shop_user_id'=>$shopUserId])
->append(['audit_status_desc','region_desc','province_name','city_name','region_name'])
->order('id desc')->findOrEmpty()->toArray();
if(empty($detail)){
$detail = $this->detail($shopUserId);
$detail['audit_status'] = '';
$detail['audit_remark'] = '';
$detail['category_ids'] = array_column($detail['category_lists'],'id');
$detail['goods_ids'] = array_column($detail['goods_lists'],'id');
$detail['shop_image'] = array_column($detail['shop_image'],'uri');
}
foreach ($detail['goods_ids'] as $key => $val){
$detail['goods_ids'][$key] = intval($val);
}
foreach ($detail['category_ids'] as $key => $val){
$detail['category_ids'][$key] = intval($val);
}
return $detail;
}
/**
* @notes 设置服务状态
* @param $params
* @return string|true
* @author cjhao
* @date 2024/12/3 20:49
*/
public function setWorkStatus($params)
{
try {
$coach = Coach::where(['id'=>$params['id'],'shop_id'=>$params['shop_id']])->findOrEmpty();
if($coach->isEmpty()){
throw new \think\Exception('您无法设置当前技师服务时间');
}
$status = $params['status'] ?? '';
if('' === $status){
throw new \think\Exception('请选择状态');
}
$coach->work_status = $status;
$coach->save();
return true;
} catch (Exception $e) {
return $e->getMessage();
}
}
public function bindMobile($params)
{
try {
$mobile = $params['mobile'] ?? '';
$code = $params['code'] ?? '';
if (empty($mobile) || empty($code)) {
throw new \Exception('请输入手机号和验证码');
}
$smsDriver = new SmsDriver();
$result = $smsDriver->verify($params['mobile'], $params['code']);
if(!$result) {
throw new \Exception('验证码错误');
}
$user = Shop::where(['mobile'=>$params['mobile']])->where('id','<>',$params['shop_id'])->findOrEmpty();
if(!$user->isEmpty()) {
throw new \Exception('该手机号已被其他账号绑定');
}
// $user = ShopUpdate::where(['mobile'=>$params['mobile'],'audit_status'=>ShopEnum::AUDIT_STATUS_WAIT])->where('id','<>',$params['shop_id'])->findOrEmpty();
// if(!$user->isEmpty()) {
// throw new \Exception('该手机号已被其他账号绑定');
// }
unset($params['code']);
$coach = Shop::findOrEmpty($params['shop_id'])->toArray();
Shop::update(['mobile'=>$params['mobile'],'id'=>$params['shop_id']]);
ShopUser::update(['account'=>$params['mobile'],'id'=>$coach['shop_user_id']]);
return true;
} catch (\Exception $e) {
return $e->getMessage();
}
}
}