| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- <?php
- namespace app\api\logic;
- use app\common\enum\GoodsEnum;
- use app\common\enum\PayEnum;
- use app\common\enum\WorkEnum;
- use app\common\logic\BaseLogic;
- use app\common\model\coupon\UserCoupon;
- use app\common\model\dict\DictData;
- use app\common\model\goods\Goods;
- use app\common\model\master_worker\MasterWorker;
- use app\common\model\orders\RechargeOrder;
- use app\common\model\recharge\OrderGoods;
- use app\common\model\works\ServiceWork;
- use app\workerapi\logic\ServiceWorkLogLogic;
- use think\Exception;
- use think\facade\Db;
- /**
- * 订单逻辑层
- * Class ServiceOrderLogic
- * @package app\api\logic
- */
- class ServiceOrderLogic extends BaseLogic
- {
- /**
- * 提交订单
- * @param array $params
- * @return array|false
- */
- public static function submitOrder($params)
- {
- Db::startTrans();
- try {
- $goods = Goods::findOrEmpty($params['goods_id']);
- if($goods->isEmpty()){
- throw new Exception('产品不存在!');
- }
- if(empty($params['user_info']['mobile'])){
- throw new Exception('请先补充您的联系方式后在提交订单');
- }
- //根据服务工单计算当前订单应支付金额
- if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
- //一口价订单
- $order_total = $goods['service_fee'];
- $order_amount = $goods['service_fee'];
- }else{
- $order_total = $goods['base_service_fee'];
- $order_amount = $goods['service_fee'];
- }
- //优惠券验证
- if(!empty($params['coupon_id'])){
- $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
- ->where('voucher_count','>',0)
- ->where('expire_time','>=',time())
- ->where('begin_use','<',time())
- ->findOrEmpty();
- if($user_coupon->isEmpty()){
- throw new Exception('该优惠券无法使用');
- }
- if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order_amount<$user_coupon['amount_require']){
- throw new Exception('该优惠劵不满足满减使用条件');
- }
- if($goods['goods_payment_type'] != GoodsEnum::ISGOODS_PAYMENT_TYPE){
- throw new Exception('请在支付尾款的时候使用该优惠券');
- }
- //优惠券折扣
- if($user_coupon['mold_type'] == 1){
- //按比例折扣
- if($user_coupon['discount_ratio']>=1){
- throw new Exception('优惠券有误,请联系客服');
- }
- $order_coupon_amount = intval($order_amount*(1-$user_coupon['discount_ratio']));
- }else{
- $order_coupon_amount = $user_coupon['amount'];
- }
- if(!empty($user_coupon['max_deductible_price'])){
- $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order_amount-$user_coupon['max_deductible_price']):($order_amount-$order_coupon_amount);
- }else{
- $order_amount = $order_amount-$order_coupon_amount;
- }
- $user_coupon->voucher_status = 1;
- $user_coupon->voucher_count = $user_coupon->voucher_count-1;
- $user_coupon->save();
- }
- //生成服务工单
- $work_data = [
- 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
- 'real_name' => $params['contact_people'],
- 'mobile' => $params['contact_number'],
- 'address' => $params['address'],
- 'title' => $goods->goods_name . '*' . $goods->goods_number.$goods->good_unit,
- 'category_type' => $goods['category_type'],
- 'goods_category_ids' => $goods['goods_category_ids'],
- 'goods_category_id' => $goods['goods_category_id'],
- 'base_service_fee' => $goods['base_service_fee'],
- 'service_fee' => $goods['service_fee'],
- 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
- 'appointment_time' => strtotime($params['appointment_time']),
- 'user_id'=>$params['user_id'],
- 'lon'=>!empty($params['lon'])?$params['lon']:0,
- 'lat'=>!empty($params['lat'])?$params['lat']:0,
- ];
- //判断是否是加单
- if(!empty($params['worker'])){
- $worker_id = MasterWorker::where('worker_number',$params['worker'])->value('id');
- $work_data['master_worker_id'] = $worker_id;
- $work_data['work_status'] = 1;
- $work_data['dispatch_time'] = time();
- $work_data['work_type'] = 2;
- }
- $service_work = ServiceWork::create($work_data);
- //生成服务订单
- $data = [
- 'work_id'=> $service_work['id'],
- 'sn' => generate_sn(RechargeOrder::class, 'sn'),
- 'order_type'=>0,//服务订单
- 'order_terminal' => $params['terminal'],
- 'payment_type'=>$goods['goods_payment_type']==1?1:0,
- 'user_id' => $params['user_id'],
- 'pay_status' => PayEnum::UNPAID,
- 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
- 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
- 'pay_way' => $params['pay_way'],
- 'order_total' => $order_total,
- 'order_amount' => $order_amount,
- ];
- $order = RechargeOrder::create($data);
- //生成订单服务详情
- OrderGoods::create([
- 'sn' => $order['sn'],
- 'goods_id' => $params['goods_id'],
- 'category_type' => $goods['category_type'],
- 'goods_category_ids' => $goods['goods_category_ids'],
- 'goods_category_id' => $goods['goods_category_id'],
- 'goods_name' => $goods['goods_name'],
- 'goods_image' => $goods['goods_image'],
- 'goods_video' => $goods['goods_video'],
- 'goods_number' => $goods['goods_number'],
- 'good_unit' => $goods['good_unit'],
- 'goods_size' => $goods['goods_size'],
- 'goods_type' => $goods['goods_type'],
- 'goods_brand' => $goods['goods_brand'],
- 'install_guide' => $goods['install_guide'],
- 'goods_payment_type'=>$goods['goods_payment_type'],
- 'base_service_fee' => $goods['base_service_fee'],
- 'service_total' => $goods['service_total'],
- 'service_fee' => $goods['service_fee'],
- 'service_image' => $goods['service_image'],
- 'warranty_period'=>$goods['warranty_period'],
- 'fee_schedule' => $goods['fee_schedule'],
- 'goods_status' => $goods['goods_status'],
- ]);
- Db::commit();
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- return [
- 'order_id' => (int)$order['id'],
- 'work_id' => (int)$order['work_id'],
- ];
- }
- /**
- * 获取订单工程师信息
- * * @param $params
- * * @return array|false
- */
- public static function getMasterWorker($params)
- {
- try {
- $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
- $query->visible(['goods_name','goods_image','goods_number','good_unit']);
- },'service_work'=>function ($query) {
- $query->visible(['real_name','mobile','address','service_status','appointment_time','master_worker_id','work_images','finished_images','finished_time'])->append(['service_status_text','user_service_status','user_service_status_text']);
- }])
- ->visible(['id','sn','payment_type','order_total','order_amount','pay_status','create_time','title','work_id'])
- ->where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->findOrEmpty()->toArray();
- $data = [];
- //获取师傅参数
- if(!empty($order_info['service_work']['master_worker_id'])){
- $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
- $data['avatar'] = $worker['avatar'];
- $data['real_name'] = $worker['real_name'];
- $data['worker_number'] = $worker['worker_number'];
- $data['mobile'] = $worker['mobile'];
- $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
- $data['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
- $data['appointment_time'] = $order_info['service_work']['appointment_time'];
- }
- return $data;
- }
- catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 获取订单详情
- * @param $params
- * @return array|false
- */
- public static function detail($params)
- {
- try {
- $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
- $query->visible(['goods_name','goods_image','goods_number','good_unit']);
- },'service_work'=>function ($query) {
- $query->visible(['real_name','mobile','address','service_status','appointment_time','master_worker_id','work_images','finished_images','finished_time'])->append(['service_status_text','user_service_status','user_service_status_text']);
- }])
- ->visible(['id','sn','payment_type','order_total','order_amount','paid_amount','pay_status','create_time','title','work_id'])
- ->where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->findOrEmpty()->toArray();
- $order_info['master_worker'] = [
- 'avatar' => '',
- 'real_name'=>'',
- 'worker_number'=>'',
- 'mobile'=>'',
- 'worker_exp'=>''
- ];
- if(empty($order_info)){
- throw new Exception('订单不存在');
- }
- //查询总价
- $order_info['order_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('order_amount');
- $order_info['paid_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('paid_amount');
- //获取师傅参数
- if(!empty($order_info['service_work']['master_worker_id'])){
- $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
- $order_info['master_worker']['avatar'] = $worker['avatar'];
- $order_info['master_worker']['real_name'] = $worker['real_name'];
- $order_info['master_worker']['worker_number'] = $worker['worker_number'];
- $order_info['master_worker']['mobile'] = $worker['mobile'];
- $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
- $order_info['master_worker']['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
- }
- //搜索当前工单下的所有订单记录
- $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status,payment_type,pay_way,pay_time,order_amount,coupon_price')->order('id asc')->select()->toArray();
- $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
- $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
- $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
- $coupon_price = 0;
- foreach ($order_info['pay_orders'] as $k=>&$v){
- $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
- $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
- $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
- $coupon_price += $v['coupon_price'];
- }
- //汇总优惠卷额度
- $order_info['coupon_price'] = $coupon_price;
- return $order_info;
- }
- catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 取消订单
- * @param $params
- * @return false|void
- */
- public static function cancelOrder($params)
- {
- Db::startTrans();
- try {
- $work_id = \app\common\model\recharge\RechargeOrder::where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->value('work_id');
- if(empty($work_id)){
- throw new Exception('订单不存在');
- }
- $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
- if(!$payed_order->isEmpty()){
- throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
- }
- //软删除订单
- $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
- $cancel_order->pay_status = 2;
- $cancel_order->save();
- //更新工单状态为已取消
- $service_work = ServiceWork::find($work_id);
- $service_work->service_status = 4;
- $service_work->save();
- //判断如果存在优惠券,返还优惠券
- if(!empty($payed_order->coupon_id)){
- $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
- $user_coupon->voucher_status = 0;
- $user_coupon->voucher_count = $user_coupon->voucher_count+1;
- $user_coupon->save();
- }
- Db::commit();
- }
- catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 用户确认尾款报价单
- * @param $params
- * @return false|void
- */
- public static function confirmOrder($params)
- {
- Db::startTrans();
- try {
- $work_id = \app\common\model\recharge\RechargeOrder::where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->value('work_id');
- if(empty($work_id)){
- throw new Exception('订单不存在');
- }
- //更新工单状态为已取消
- $service_work = ServiceWork::find($work_id);
- if($service_work->user_confirm_status==2){
- throw new Exception('请勿重复操作');
- }
- $service_work->work_status = 5;
- $service_work->user_confirm_status = 2;
- $service_work->save();
- $work_log = [
- 'work_id'=>$work_id,
- 'master_worker_id'=>$service_work->master_worker_id,
- 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
- ];
- ServiceWorkLogLogic::add($work_log);
- Db::commit();
- }
- catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 用户确认服务完成
- * @param $params
- * @return false|void
- */
- public static function confirmServiceFinish($params)
- {
- Db::startTrans();
- try {
- $work_id = \app\common\model\recharge\RechargeOrder::where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->value('work_id');
- if(empty($work_id)){
- throw new Exception('订单不存在');
- }
- $service_work = ServiceWork::find($work_id);
- if($service_work->user_confirm_status!=3){
- throw new Exception('请勿重复操作');
- }
- $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
- //确认所有订单总金额和结算金额
- //若订单是全款已支付订单
- if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
- $service_work->work_status = 7;
- $service_work->user_confirm_status = 5;
- $service_work->service_status = 3;
- $service_work->work_pay_status = 1;
- $service_work->work_total = $orders[0]['order_total'];
- $service_work->work_amount = $orders[0]['order_amount'];
- }else{
- $service_work->work_status = 6;
- $service_work->user_confirm_status = 4;
- $order_total = 0;
- foreach ($orders as $k=>$v){
- $order_total += $v['order_total'];
- }
- $service_work->work_total = $order_total;
- }
- $service_work->finished_time = time();
- $service_work->save();
- //更新师傅的进行工单数量
- MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
- $work_log = [
- 'work_id'=>$work_id,
- 'master_worker_id'=>$service_work->master_worker_id,
- 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
- ];
- ServiceWorkLogLogic::add($work_log);
- Db::commit();
- }
- catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function firmOrderSave($params)
- {
- Db::startTrans();
- try {
- $goodIds = array_column($params['order_goods'],'id');
- $goodInfo = Goods::whereIn('id',$goodIds)->select();
- $ids = $goodInfo->column('id');
- if(!empty(array_diff($goodIds,$ids))){
- throw new Exception('产品不存在!');
- }
- if(empty($params['user_info']['mobile'])){
- throw new Exception('请先补充您的联系方式后在提交订单');
- }
- $order_amount = 0;
- $order_total = 0;
- $orderGoodsData = [];
- $goodsPaymentTypeArr = [];
- //计算订单总金额和结算金额
- foreach($params['order_goods'] as $val){
- $goods = $goodInfo->where('id',$val['id'])->toArray()[0];
- //根据服务工单计算当前订单应支付金额
- $order_total = $goods['service_total'];
- if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
- //一口价订单
- $order_amount = $goods['service_fee']*$val['goods_number'];
- }else{
- $order_total = $goods['base_service_fee']*$val['goods_number'];
- $order_amount = $goods['service_fee']*$val['goods_number'];
- }
- $goodsPaymentTypeArr[] = $goods['goods_payment_type'];
- $orderGoodsData[] = [
- 'goods_id' => $goods['id'],
- 'category_type' => $goods['category_type'],
- 'goods_category_ids' => $goods['goods_category_ids'],
- 'goods_category_id' => $goods['goods_category_id'],
- 'goods_name' => $goods['goods_name'],
- 'goods_image' => $goods['goods_image'],
- 'goods_video' => $goods['goods_video'],
- 'goods_number' => $val['goods_number'],
- 'good_unit' => $goods['good_unit'],
- 'goods_size' => $goods['goods_size'],
- 'goods_type' => $goods['goods_type'],
- 'goods_brand' => $goods['goods_brand'],
- 'install_guide' => $goods['install_guide'],
- 'goods_payment_type'=>$goods['goods_payment_type'],
- 'base_service_fee' => $goods['base_service_fee'],
- 'service_total' => $goods['service_total'],
- 'service_fee' => $goods['service_fee'],
- 'service_image' => $goods['service_image'],
- 'warranty_period'=>$goods['warranty_period'],
- 'fee_schedule' => $goods['fee_schedule'],
- 'goods_status' => $goods['goods_status'],
- ];
- }
- if(count(array_unique($goodsPaymentTypeArr))>1){
- throw new Exception('订单中存在多种支付方式');
- }
- //生成服务工单
- $work_data = [
- 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
- 'real_name' => $params['contact_people'],
- 'mobile' => $params['contact_number'],
- 'address' => $params['address'],
- 'title' => $goods['goods_name'] . '*' .$val['goods_number'].$goods['good_unit'],
- 'category_type' => $goods['category_type'],
- 'goods_category_ids' => $goods['goods_category_ids'],
- 'goods_category_id' => $goods['goods_category_id'],
- 'base_service_fee' => $goods['base_service_fee'],
- 'service_fee' => $goods['service_fee'],
- 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
- 'appointment_time' => strtotime($params['appointment_time']),
- 'user_id' => $params['user_id'],
- 'work_type' => 1,
- ];
- $service_work = ServiceWork::create($work_data);
- //生成服务订单
- $data = [
- 'work_id'=> $service_work['id'],
- 'sn' => generate_sn(RechargeOrder::class, 'sn'),
- 'order_type'=>0,//服务订单
- 'order_terminal' => $params['terminal'],
- 'payment_type'=>$goods['goods_payment_type']==1?1:0,
- 'user_id' => $params['user_id'],
- 'pay_status' => PayEnum::UNPAID,
- 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
- 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
- 'pay_way' => $params['pay_way'],
- 'order_total' => $order_total,
- 'order_amount' => $order_amount,
- ];
- $order = RechargeOrder::create($data);
- array_walk($orderGoodsData, function (&$value, $key, $data) {
- $value = array_merge($value, ['sn' => $data['sn']]);
- },$data);
- dd($orderGoodsData);
- $orderGoodsModel = new OrderGoods();
- $orderGoodsModel->saveAll($orderGoodsData);
- Db::commit();
- return [
- 'order_id' => (int)$order['id'],
- ];
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- }
|