| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007 |
- <?php
- namespace app\common\model;
- use app\common\model\ShopGoodsModel as Goods;
- use app\common\model\UserModel as User;
- use app\common\model\UserMoneyModel as UserMoney;
- use app\api\services\ExpressServices;
- use jobs\ShopOrderJob;
- use mysql_xdevapi\Result;
- use services\CacheServices;
- use think\db\concern\ResultOperation;
- use think\Exception;
- use think\facade\Db;
- use think\Model;
- use app\common\model\ShopOrderGoodsModel as OrderGoods;
- use app\common\model\ShopOrderShippingModel as OrderShipping;
- use jobs\ShopBonusJob;
- use utils\Json;
- use utils\Queue;
- class ShopOrderModel extends Model
- {
- protected $name = 'shop_order';
- const waitPay = 0; //待支付
- const waitDelivery = 1; //待发货
- const doneDelivery = 2; //已发货
- const doneOrder = 3; //交易完成
- const cancelOrder = 4; //已取消
- /**
- * @return \think\model\relation\HasMany
- * 关联订单商品
- */
- public function withOrderGoods ()
- {
- return self::hasMany(OrderGoods::class, 'order_id', 'order_id')->field('goods_name,goods_id,goods_img,num,price,total_fee,spec_text,goods_spec_id');
- }
- /**
- * @return \think\model\relation\HasOne
- * 关联订单配送
- */
- public function withOrderShipping ()
- {
- return self::hasOne(OrderShipping::class, 'order_id', 'order_id')->field('sp_name,sp_mobile,sp_province,sp_city,sp_county,sp_remark,sp_mergename');
- }
- /**
- * 订单列表
- */
- public static function list (array $map = [], array $paginate = [], $userId, ?string $field = '*')
- {
- $map['user_id'] = $userId;
- $field = 'order_id,status,order_sn,order_type,coupon_number,created_time,order_remark,payment,ship_postfee,rebate_score,ship_name,ship_code,ship_number,expires_time,rebate_lock_score';
- foreach ($map as $k => $v) {
- if (empty($v) && $v != '0') {
- unset($map[$k]);
- }
- }
- if (isset($map['status'])) {
- if ($map['status'] == -1) {
- $map['status'] = ['0','1','2', '4', '5', '6'];
- }
- }
- // if (isset($map['order_type'])) {
- // if ($map['order_type'] == -1) {
- // $map['order_type'] = ['1','2','3', '4', '5'];
- // }
- // }
- $data = self::where($map)->where('status', '<>', 3)->relation(['withOrderGoods'])->field($field)->order('order_id desc')->paginate($paginate['limit'])->toArray();
- foreach ($data['data'] as $k=>&$v){
- foreach ($v['withOrderGoods'] as $key=>$val){
- $goods_sn = Db::name('shop_goods')->where('goods_id', $val['goods_id'])->value('goods_sn');
- // sr_log($goods_sn);
- // $val['goods_sn'] = $goods_sn;
- $data['data'][$k]['withOrderGoods'][$key]['goods_sn'] = $goods_sn;
- // $data['data'][$k]['withOrderGoods'][$key]['goods_name'] = '云德商品购买';
- }
- $v['withOrderShipping'] = Db::name('shop_order_shipping')->where('order_id', $v['order_id'])->find();
- }
- $cur_xzmoney = Db::name('system_config')->where('name', 'xz_cur_money')->where('group', 'xzconfig')->value('value');
- foreach ($data['data'] as $k => &$v) {
- $v['withOrderExpress'] = [];
- // if (in_array($v['status'], [1, 2])) {
- $v['withOrderExpress'] = ExpressServices::instance()->orderId($v['order_id'])->code($v['ship_code'])->number($v['ship_number'])->find() ?: [];
- if (intval($v['status']) == 2){
- $v['express_url'] = "https://m.kuaidi100.com/result.jsp?nu=".$v['ship_number'];
- }else{
- $v['express_url'] = "";
- }
- // }
- if ($v['status'] == 1){
- if ($v['order_type'] == 1){
- $v['can_hg'] = 1;
- }
- }else{
- $v['can_hg'] = 2;
- }
- // if ($v['status'] == 4){
- // $v['yg_goodsid'] = Db::name('yg_order')->where('order_sn', $v['order_sn'])->max('id');
- // }else{
- // $v['yg_goodsid'] = 0;
- //
- // }
- switch ($v['status']){
- case 0:
- $v['status_title'] = '待付款';
- break;
- case 1:
- $v['status_title'] = '待发货';
- break;
- case 2:
- $v['status_title'] = '已发货';
- break;
- case 3:
- $v['status_title'] = '已过期';
- break;
- case 4:
- $v['status_title'] = '已完成';
- break;
- case 5:
- $v['status_title'] = '已完成';
- break;
- case 6:
- $v['status_title'] = '已完成';
- break;
- }
- // if ($v['expires_time']){
- // $v['expires_time']=strtotime($v['expires_time']);
- // }
- // 建议竞拍价格
- // $back_redscore = number_format($v['rebate_lock_score']*env('app.YP_APPLY_HUIGOU_SCALE')/100, '2', '.', '');
- // $cur_xzmoney = Db::name('system_config')->where('name', 'xz_cur_money')->where('group', 'xzconfig')->value('value');
- // $xz_price = getXzPirceWithPrice($cur_xzmoney, $back_redscore);
- // $v['jy_buyprice'] = number_format($xz_price + 1, '0', '.', '');
- // if ($xz_price < 1){
- // $v['jy_jpprice'] = 0.1;
- // }
- //
- // if ($xz_price > 1){
- // $v['jy_jpprice'] = 1;
- // }
- //
- // if ($xz_price > 4){
- // $v['jy_jpprice'] = number_format($xz_price-2, '0', '.', '');
- // }
- // if ($xz_price > 5){
- // $v['jy_jpprice'] = number_format($xz_price-3, '0', '.', '');
- // }
- // if ($v['order_type'] == 5){
- // foreach ($v['withOrderGoods'] as $k1=>&$v1){
- // $v1['price'] = getXzPirceWithPrice($cur_xzmoney ,$v1['price']);
- // }
- //
- // $v['payment'] = getXzPirceWithPrice($cur_xzmoney, $v['payment']);
- // }
- }
- return $data['data'];
- }
- /**
- * 订单详情
- */
- public static function details (array $map, $userId, ?string $field = '*'): array
- {
- $field = 'order_type,order_id,status,order_sn,created_time,coupon_number,order_remark,total_price,ship_postfee,payment,rebate_score,ship_code,ship_name,ship_number,expires_time,rebate_lock_score';
- $cur_xzmoney = Db::name('system_config')->where('name', 'xz_cur_money')->where('group', 'xzconfig')->value('value');
- $map['user_id'] = $userId;
- $details = self::where($map)->relation(['withOrderGoods', 'withOrderShipping'])->field($field)->findOrEmpty()->toArray();
- if (empty($details))
- throw new Exception('订单不存在');
- $details['withOrderExpress'] = [];
- if (in_array($details['status'], [1, 2])) {
- $details['withOrderExpress'] = ExpressServices::instance()->orderId($details['order_id'])->code($details['ship_code'])->number($details['ship_number'])->find() ?: [];
- if (intval($details['status']) == 2){
- $details['express_url'] = "https://m.kuaidi100.com/result.jsp?nu=".$details['ship_number'];
- }else{
- $details['express_url'] = "";
- }
- }
- if ($details['order_type'] == 5){
- $details['total_price'] = getXzPirceWithPrice($cur_xzmoney, $details['total_price']);
- }
- switch ($details['status']){
- case 0:
- $details['status_title'] = '待付款';
- break;
- case 1:
- $details['status_title'] = '待发货';
- break;
- case 2:
- $details['status_title'] = '已发货';
- break;
- case 3:
- $details['status_title'] = '取消订单';
- break;
- case 4:
- $details['status_title'] = '待发货(正在竞拍)';
- break;
- case 5:
- $details['status_title'] = '待发货(竞拍完成)';
- break;
- case 6:
- $details['status_title'] = '已发货(竞拍已发货)';
- break;
- }
- // if ($details['expires_time']){
- // $details['expires_time']=strtotime($details['expires_time']);
- // }
- // foreach ($details['data'] as $k=>$v){
- foreach ($details['withOrderGoods'] as $key=>$val){
- $goods_sn = Db::name('shop_goods')->where('goods_id', $val['goods_id'])->value('goods_sn');
- // sr_log($goods_sn);
- // $val['goods_sn'] = $goods_sn;
- //
- $details['withOrderGoods'][$key]['goods_sn'] = $goods_sn;
- // $details['withOrderGoods'][$key]['goods_name'] = '云德商品购买';
- $details['withOrderGoods'][$key]['price'] = getXzPirceWithPrice($cur_xzmoney, $details['withOrderGoods'][$key]['price']);
- }
- // }
- return $details;
- }
- /**
- * 下单详情,获取商品信息
- */
- public static function buyDetails (array $data, array $address = [])
- {
- $result = [];
- foreach ($data as $key => $value) {
- $result[$value['goods_sn'] . '_' . $value['spec_id']][] = $value;
- }
- $details = [];
- try {
- array_walk($result, function ($value, $key) use (&$details, &$flag) {
- $key_arrkass = explode('_', $key);
- $goodsBase = Db::name('shop_goods')->where(['goods_sn' => $key_arrkass[0]])->find();
- $num = array_sum(array_column($value, 'num'));
- $goodsSpec = Db::name('shop_goods_spec')->where(['goods_id' => $goodsBase['goods_id'], 'spec_ids' => $value[0]['spec_id']])->find();
- if (empty($goodsSpec) || empty($goodsBase)) {
- throw new Exception('操作失败,所选商品含有失效商品');
- }
- $superlizer = Db::name('shop_supplier')->where('id', $goodsBase['supplier'])->find();
- $details[] = [
- 'goods_sn' => $goodsBase['goods_sn'],
- 'goods_id' => $goodsBase['goods_id'],
- 'goods_type' => $goodsBase['goods_type'],
- 'goods_name' => $goodsBase['goods_name'],
- 'picture' => $goodsSpec['picture'] ?: $goodsBase['goods_img'],
- 'num' => $num, //数量
- 'price' => $goodsSpec['price'],//单价
- 'weight' => $goodsSpec['weight'], //重量
- 'total_fee' => bcmul($num, $goodsSpec['price'], 2), //总价
- 'rebate_score' => $goodsSpec['rebate_score'],//单个返利积分
- // 'rebate_score_most' => bcmul($num, $goodsBase['rebate_score_most'], 2),//返回积分 马上
- // 'rebate_score_lock' => bcmul($num, $goodsBase['rebate_score_lock'], 2),//返回积分 锁定
- 'total_items_rebate_score' => bcmul($num, $goodsSpec['rebate_score'], 2),//总返利积分
- 'spec_ids' => $goodsSpec['spec_ids'],
- 'spec_text' => $goodsSpec['spec_text'],
- // 'coupon_num' => $goodsBase['coupon_num'],
- 'post_template_id' => $goodsBase['post_template_id'], //是否包邮,否就是运费模板ID
- 'stock' => $goodsSpec['stock'],
- 'supplier_name'=>$superlizer['name']
- ];
- });
- //组合数据
- $details_arr = [];
- foreach ($details as $k => $v) {
- $details_arr[$v['goods_sn']]['goods'][] = $v;
- $total_rebate_score = multi_array_sum($details_arr[$v['goods_sn']]['goods'], 'total_items_rebate_score');
- // $total_rebate_most_score = multi_array_sum($details_arr[$v['goods_sn']]['goods'], 'rebate_score_most');
- // $total_rebate_lock_score = multi_array_sum($details_arr[$v['goods_sn']]['goods'], 'rebate_score_lock');
- $total_num = multi_array_sum($details_arr[$v['goods_sn']]['goods'], 'num');
- $total_weight = multi_array_sum($details_arr[$v['goods_sn']]['goods'], 'weight');
- $total_price = multi_array_sum($details_arr[$v['goods_sn']]['goods'], 'total_fee');
- // $total_coupon_num = multi_array_sum($details_arr[$v['goods_sn']]['goods'], 'coupon_num');
- $details_arr[$v['goods_sn']]['total_rebate_score'] = $total_rebate_score; //总返利积分
- // $details_arr[$v['goods_sn']]['total_rebate_most_score'] = $total_rebate_most_score; // 返利积分马上到账
- // $details_arr[$v['goods_sn']]['total_rebate_lock_score'] = $total_rebate_lock_score; //总返利积分
- $details_arr[$v['goods_sn']]['total_num'] = $total_num; //总数量
- $details_arr[$v['goods_sn']]['total_price'] = $total_price; //总金额
- $details_arr[$v['goods_sn']]['total_weight'] = $total_weight; //总重量
- // $details_arr[$v['goods_sn']]['total_coupon_num'] = $total_coupon_num * $total_num; //总抢购值
- //获取运费
- // if ($address) {
- // $details_arr[$v['goods_sn']]['post_fee'] = self::postFee(
- // $address['city'],
- // [
- // 'goods_sn' => $v['goods_sn'],
- // 'num' => $total_num,
- // 'total_price' => $total_price,
- // 'total_weight' => $details_arr
- // ],
- // $v['post_template_id']
- // );
- // } else {
- // $details_arr[$v['goods_sn']]['post_fee'] = '0.00';
- // }
- $details_arr[$v['goods_sn']]['post_fee'] = '0.00';
- $details_arr[$v['goods_sn']]['payment'] = bcadd($details_arr[$v['goods_sn']]['total_price'], $details_arr[$v['goods_sn']]['post_fee'], 2);
- $details_arr[$v['goods_sn']]['supplier_name'] = $details[$key]['supplier_name'];
- }
- $total = [
- 'count_payment' => sprintf("%.2f", multi_array_sum($details_arr, 'payment')),
- 'count_rebate_score' => (string)multi_array_sum($details_arr, 'total_rebate_score'),
- // 'count_rebate_most_score' => (string)multi_array_sum($details_arr, 'total_rebate_most_score'),
- // 'count_rebate_lock_score' => (string)multi_array_sum($details_arr, 'total_rebate_lock_score'),
- 'count_num' => multi_array_sum($details_arr, 'total_num'),
- // 'total_coupon_num' => multi_array_sum($details_arr, 'total_coupon_num')
- ];
- $details_arr = array_values($details_arr);
- return ['details' => $details_arr, 'total' => $total];
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- /**
- * 生成订单
- */
- public static function createOrder (array $data, int $userId, $user, $buyData, $cart_id)
- {
- $m_machine = new MacheineExchangeModel();
- Db::startTrans();
- try {
- //-----组装商品数据,不同商品将拆分------
- $result = $data['order'];
- $results = [];
- $specStock = [];
- // $m_id = createdMachineID();
- // if ($data['order_type'] == 3){
- // // 生成矿机兑换券
- // $m_machine->insertGetId([
- // 'number'=>$m_id,
- // 'create_time'=>sr_getcurtime(time())
- // ]);
- // }
- array_walk($result, function ($value, $key) use (&$results, &$specStock) {
- $key_arrkass = explode('_', $key);
- $goodsBase = Db::name('shop_goods')->where(['goods_sn' => $key_arrkass[0]])->find();
- $results[$key_arrkass[0]]['goods'] = $goodsBase;
- $goodsSpec = Db::name('shop_goods_spec')->where(['goods_id' => $goodsBase['goods_id'], 'spec_ids' => $value[0]['spec_id']])->find();
- $num = array_sum(array_column($value, 'num'));
- $results[$key_arrkass[0]]['spec'][$key_arrkass[1]] = $specStock[] = [
- 'num' => $num, //数量
- 'total_fee' => bcmul($num, $goodsSpec['price'], 3), //总价
- 'total_weight' => bcmul($num, $goodsSpec['weight'], 3), //总重量
- 'spec_id' => $value[0]['spec_id'], //规格ids
- 'spec_data' => $goodsSpec, //规格数据
- 'total_rebate_score' => bcmul($num, $goodsBase['rebate_score'], 3),//总返积分
- // 'total_rebate_lock_score' => bcmul($num, $goodsBase['rebate_score_lock'], 3)//返利积分锁定
- ];
- });
- //------减库存--------
- $num = 0;
- foreach ($specStock as $k => $v) {
- Db::name('shop_goods_spec')->where(['goods_spec_id' => $v['spec_data']['goods_spec_id']])->dec('stock', $v['num'])->update();
- $spec_info = Db::name('shop_goods_spec')->where(['goods_spec_id' => $v['spec_data']['goods_spec_id']])->find();
- Db::name('shop_goods')->where(['goods_id' => $spec_info['goods_id']])->dec('inventory', $v['num'])->update();
- // CacheServices::clear(md5('goodsDetail_' . $v['goods_sn']));
- CacheServices::set(md5('goodsDetail_' . $spec_info['goods_sn']), false);
- }
- //-------生成订单商品,返回订单号--------
- $createOrder = [];
- $i = -1;
- $total_rebate_score = 0;
- // $total_rebate_lock_score = 0;
- array_walk($results, function ($value, $key) use ($userId, $data, &$createOrder, &$i, &$total_rebate_score) {
- $nowTime = date('Y-m-d H:i:s', time());
- $nums = multi_array_sum($value['spec'], 'num');
- $total_price = multi_array_sum($value['spec'], 'total_fee');
- $total_weight = multi_array_sum($value['spec'], 'total_weight');
- $total_rebate_score = multi_array_sum($value['spec'], 'total_rebate_score');
- // $total_rebate_score = $value['goods']['rebate_score_most']*
- // $total_rebate_lock_score = multi_array_sum($value['spec'], 'total_rebate_lock_score');
- //运费
- $post_fee = self::postFee(
- $data['address']['city'],
- [
- 'goods_sn' => $value['goods']['goods_sn'],
- 'num' => $nums,
- 'total_price' => $total_price,
- 'total_weight' => $total_weight
- ],
- $value['goods']['post_template_id']
- );
- $post_fee = 0;
- $give_vip = 0;
- if ($data['cls'] == 1 && $value['goods']['give_vip']) {
- $give_vip = $value['goods']['give_vip'];
- }
- $i++;
- $status = 0;
- // 积分订单 被锁积分订单
- // if (in_array($data['order_type'], [2, 4])){
- // $status = 1;
- // }
- //
- // // 自营订单 余额支付
- // if ( $data['order_type'] == 1 && $data['pay_type'] == 3){
- // $status = 1;
- // }
- // // 矿机 余额支付
- // if ( $data['order_type'] == 3 && $data['pay_type'] == 3){
- // $status = 2;
- // }
- //
- //
- //
- // // 星钻兑换
- // if ( $data['order_type'] == 5){
- // $status = 1;
- // }
- // sr_log(json_encode($data));
- // //写入订单
- $payment = bcadd($total_price, $post_fee, 3);
- $order = self::create([
- 'order_sn' => createdOrderSn(),
- 'total_price' => $total_price,
- 'payment' => $payment,
- 'user_id' => $userId,
- 'num' => $nums,
- 'status'=>$status,
- 'ship_postfee' => $post_fee,
- 'rebate_score' => $total_rebate_score, //总返利积分
- // 'rebate_lock_score' => $total_rebate_lock_score, //总返利积分锁定
- // 'rebate_money' => bcmul($value['goods']['rebate_money'], $nums, 2), //总返利金额
- 'created_time' => $nowTime, 'updated_time' => $nowTime,
- 'give_vip' => $give_vip,
- 'order_remark' => isset($data['order_remark']) ? $data['order_remark'] : '',
- 'pay_type'=>$data['pay_type'],
- 'order_type'=>$data['order_type'],
- 'coupon_number'=>'',
- 'expires_time'=>sr_getcurtime(time()+intval(env('common.ORDER_EXPIRES_TIME'))),
- 'supplier_name'=>$data['supplier_name']
- ]);
- // sr_log('ccccc'.$data['order_type']);
- //写入订单商品详情
- $orderGoods = [];
- foreach ($value['spec'] as $specKey => $specValue) {
- $orderGoods[] = [
- 'order_id' => $order->id, 'goods_id' => $value['goods']['goods_id'],
- 'goods_name' => $value['goods']['goods_name'], 'goods_category' => $value['goods']['category'],
- 'goods_img' => $specValue['spec_data']['picture'] ?: $value['goods']['goods_img'],
- 'num' => $specValue['num'],
- 'price' => $specValue['spec_data']['price'],
- 'total_fee' => $specValue['total_fee'],
- 'goods_spec_id' => $specValue['spec_data']['goods_spec_id'],
- 'spec_ids' => $specValue['spec_data']['spec_ids'],
- 'spec_text' => $specValue['spec_data']['spec_text'],
- 'rebate_score' => $specValue['spec_data']['rebate_score'],
- 'total_rebate_score' => $specValue['total_rebate_score'],
- 'order_type'=>$data['order_type'],
- 'uid'=>$userId
- // 'rebate_tpl' => $value['goods']['rebate_tpl']
- // 'rebate_money' => $value['goods']['rebate_money'],
- // 'total_rebate_money' => bcmul($value['goods']['rebate_money'], $specValue['num'], 3),
- ];
- }
- (new OrderGoods())->saveAll($orderGoods);
- //写入订单配送
- OrderShipping::create(OrderShipping::packageAddressShipPing($data['address'], $order->id));
- $createOrder[] = ['order_sn' => $order->order_sn, 'order_data' => $value['spec']];
- });
- if ($data['cart_ids']) {
- Db::name('shop_cart')->where('id', 'in', $data['cart_ids'])->where(['user_id' => $userId])->delete();
- }
- $total_score = multi_array_sum($specStock, 'total_rebate_score');
- // return $total_score;
- $m_user = new UserModel();
- // 积分订单
- // if ($data['order_type'] == 2){
- // edit_user_score(6,$userId, $buyData['total']['count_payment']);
- // }
- // 锁定积分 提货券 兑换
- // if ($data['order_type'] == 4){
- // edit_user_lock_score(2,$userId, $buyData['total']['count_payment']);
- // }
- // 如果是星钻订单
- // if ($data['order_type'] == 5){
- // $cur_xzmoney = Db::name('system_config')->where('name', 'xz_cur_money')->where('group', 'xzconfig')->value('value');
- //
- // $paymoney = getXzPirceWithPrice($cur_xzmoney, $buyData['total']['count_payment']);
- // $shouxu_scale = get_user_shouxufei($userId);
- // $shouxu = number_format($paymoney*$shouxu_scale/100, '2', '.', '');
- //
- // // 扣除星钻
- // edit_user_xz(9, $userId, ($shouxu + $paymoney) );
- // // 扣除抢购值
- // edit_user_couponnum(2, $userId, $buyData['total']['total_coupon_num']);
- // }
- // 自营商城 余额购买
- // if ($data['order_type']== 1 && $data['pay_type'] == 3){
- // edit_user_money(1, $userId, $buyData['total']['count_payment']);
- // edit_user_score(5, $userId, $total_rebate_score);
- // edit_user_redscore(1, $userId, $total_rebate_lock_score);
- // $m_user->buyGoodsSuccess($userId, $buyData['total']['count_rebate_score']);
- // }
- // 矿机 余额支付
- // if ($data['order_type']== 3 && $data['pay_type'] == 3){
- // edit_user_money(1, $userId, $buyData['total']['count_payment']);
- // }
- // 矿机 购买商品
- if (in_array($data['order_type'], [1, 3])){
- $ex_time = intval(env('common.ORDER_EXPIRES_TIME', 60*60));
- $order_sn = $createOrder[0]['order_sn'];
- Queue::instance()->log('订单过期执行成功')->job(ShopOrderJob::class)->do('ShopOrderExpired')->secs($ex_time)->push([$order_sn]);
- }
- // 增加销量
- if (in_array($data['order_type'], [2,4,5])){
- foreach ($createOrder as $key=>$val){
- $order_id = Db::name('shop_order')->where('order_sn', $val['order_sn'])->value('order_id');
- $orderGoods = OrderGoods::where(['order_id' => $order_id])->select()->toArray();
- foreach ($orderGoods as $k => $v) {
- ShopGoodsModel::where(['goods_id' => $v['goods_id']])->inc('sales_volume', $v['num'])->inc('real_sales_volume', $v['num'])->update();
- }
- }
- }
- if (intval($cart_id) > 0){
- Db::name('shop_cart')->where('id', $cart_id)->delete();
- }
- Db::commit();
- return ['flag' => 200, 'data' => array_column($createOrder, 'order_sn')];
- } catch (\Exception $e) {
- Db::rollback();
- if (strpos($e->getMessage(), 'SQLSTATE[22003]') !== false) {
- throw new Exception('所选商品含有库存不足');
- }
- throw new Exception($e->getMessage());
- }
- }
- /**
- * 校验库存
- */
- public static function checkStock (array $data)
- {
- $results = [];
- array_walk($data, function ($value, $key) use (&$results) {
- $num = array_sum(array_column($value, 'num'));
- $results[] = ['spec_id' => $value[0]['spec_id'], 'goods_sn' => $value[0]['goods_sn'], 'num' => $num];
- });
- foreach ($results as $k => $v) {
- $stock = Db::name('shop_goods_spec')->where(['goods_sn' => $v['goods_sn'], 'spec_ids' => $v['spec_id']])->value('stock') ?: 0;
- sr_log($stock);
- if ($v['num'] > $stock) {
- return false;
- }
- }
- return true;
- }
- /**
- * 支付前校验订单
- */
- public static function checkingOrder ($order_sn, $userPayMoney, $userId)
- {
- $order = self::where(['order_sn' => explode(',', $order_sn)])->select()->toArray();
- if (empty($order)) {
- return ['flag' => 110, 'msg' => '校验订单失败'];
- }
- $orderPayment = multi_array_sum($order, 'payment');
- // if ($orderPayment != $userPayMoney) {
- // return ['flag' => 111, 'msg' => '金额错误'];
- // }
- foreach ($order as $k => $v) {
- if ($v['user_id'] != $userId) {
- return ['flag' => 112, 'msg' => '校验订单失败'];
- }
- }
- return ['flag' => 200, 'msg' => '成功', 'orderPayment' => $orderPayment, 'order' => $order];
- }
- /**
- * 支付订单回调
- */
- public static function payDoneOrderCallback ($order, $payType, $userId)
- {
- $nowTime = date('Y-m-d H:i:s', time());
- self::where(['order_sn' => array_column($order, 'order_sn')])
- ->update(['pay_type' => $payType, 'status' => self::waitDelivery, 'updated_time' => $nowTime]);
- //根据订单商品 生成分红
- $orderId = array_column($order, 'order_id');
- $orderGoods = OrderGoods::where(['order_id' => $orderId])->select()->toArray();
- foreach ($orderGoods as $k => $v) {
- ShopGoodsModel::where(['goods_id' => $v['goods_id']])->inc('sales_volume', $v['num'])->inc('real_sales_volume', $v['num'])->update();
- }
- //赠送送会员
- if ($order[0]['give_vip']) {
- $vip = $order[0]['give_vip'] == 2 ? 'year_card' : 'monthly_card';
- $user = Db::name('user')->where('id', $userId)->field('vip,vip_expire_time')->find();
- $vip_config = get_vip_config($vip);
- $date = new \DateTime($user['vip_expire_time']);
- $date->add(new \DateInterval(get_vip_datetime_string($vip_config['type'])['time']));
- $vip_expire_time = $date->format('Y-m-d H:i:s'); // vip到期时间
- //更新用户vip信息
- Db::name('User')->where('id', $userId)->update(['vip' => get_vip_datetime_string($vip_config['type'])['vip_type'], 'vip_expire_time' => $vip_expire_time]);
- Db::name('vip_log')->insert(['uid' => $userId, 'type' => 2, 'pay_price' => 0, 'vip_expire_time' => $vip_expire_time, 'vip_type' => $vip]);
- }
- Queue::instance()->log('执行成功')->job(ShopBonusJob::class)->do('shopBonusJob')->push([$userId, $orderGoods]);
- }
- /**
- * 取消订单
- */
- public static function cancelOrder ($data, $userId)
- {
- $nowTime = date('Y-m-d H:i:s', time());
- $data = self::where(['order_sn' => $data['order_sn'], 'user_id' => $userId])->relation(['withOrderGoods'])->findOrEmpty()->toArray();
- if (empty($data)) {
- throw new Exception('参数错误');
- }
- if ($data['status'] != 0) {
- throw new Exception('订单状态错误');
- }
- self::where(['order_sn' => $data['order_sn']])->save(['status' => 3, 'updated_time' => $nowTime]);
- foreach ($data['withOrderGoods'] as $kk => $vv) {
- Db::name('shop_goods_spec')->where(['goods_spec_id' => $vv['goods_spec_id']])->inc('stock', $vv['num'])->update();
- }
- }
- /**
- * 确定订单
- */
- public static function receiveOrder ($data, $userId)
- {
- $nowTime = date('Y-m-d H:i:s', time());
- $data = self::where(['order_sn' => $data['order_sn'], 'user_id' => $userId])->relation(['withOrderGoods'])->findOrEmpty()->toArray();
- if (empty($data)) {
- throw new Exception('参数错误');
- }
- if ($data['status'] != 2) {
- throw new Exception('订单状态错误');
- }
- self::where(['order_sn' => $data['order_sn']])->save(['status' => 4, 'updated_time' => $nowTime]);
- }
- /**
- * @param $address 用户地址
- * @param $data 商品数据
- * @param $express_template_id 模板ID
- * 获取运费
- */
- public static function postFee ($address, $data, $express_template_id)
- {
- //获取运费模板
- $expressDelivery = Db::name('express_delivery')->where(['id' => $express_template_id])->findOrEmpty();
- //获取运费模板列表
- $expressDeliveryItems = Db::name('express_shipping_method')->where(['template_id' => $expressDelivery['id']])->select()->toArray();
- $price_method_field = '';
- switch ($expressDelivery['price_method']) {
- case 1:
- $price_method_field = $data['total_price']; //按金额
- break;
- case 2:
- $price_method_field = $data['num']; //按件数
- break;
- case 3:
- $price_method_field = $data['total_weight']; //按重量
- break;
- default:
- break;
- }
- if ($expressDelivery['price_method'] == 1) {
- //计算默认运费 【配送区域、免运费金额、达标时运费、未达标时运费;】
- if ($price_method_field < $expressDelivery['default_free_price']) {
- $default_postfee = $expressDelivery['default_fastandard_price'];
- } else {
- //达标时运费
- $default_postfee = $expressDelivery['default_price'];
- }
- if (empty($expressDeliveryItems)) {
- return $default_postfee;
- }
- //按指定地区计算邮费
- foreach ($expressDeliveryItems as $k => $node) {
- if (in_array($address, explode(',', $node['address_items_ids']))) {
- //未达标运费
- if ($price_method_field < $node['free_price']) {
- return $node['fastandard_price'];
- } else {
- //达标时运费
- return $node['price'];
- }
- } else {
- return $default_postfee;
- }
- }
- } else {
- //按件数 重量计费 【配送区域、首件、达标时运费、续件、未达标时运费】
- //计算默认运费
- if ($price_method_field <= $expressDelivery['default_pieces']) {
- $default_postfee = $expressDelivery['default_price'];
- } else {
- //达到续件条件时的运费
- if ($expressDelivery['add_pieces']) {
- //配置过续件费用
- //续件数 = (下单件数 - 首件) /续件数
- //总运费 = 首件运费 + (续件数 * 续件费用)
- $add_pieces = bcdiv(($price_method_field - $expressDelivery['default_pieces']), $expressDelivery['add_pieces'], 2);
- $default_postfee = bcadd($expressDelivery['default_price'], bcmul($add_pieces, $expressDelivery['add_price'], 2), 2);
- } else {
- //未配置过续件,按首件价格
- $default_postfee = $expressDelivery['default_price'];
- }
- }
- if (empty($expressDeliveryItems)) {
- return $default_postfee;
- }
- //按指定地区计算邮费
- foreach ($expressDeliveryItems as $k => $node) {
- if (in_array($address, explode(',', $node['address_items_ids']))) {
- //未达到续件条件时的运费
- if ($price_method_field <= $node['pieces']) {
- return $node['price'];
- } else {
- //达到续件条件时的运费
- if ($node['add_pieces']) {
- //配置过续件费用
- //续件数 = (下单件数 - 首件) /续件数
- //总运费 = 首件运费 + (续件数 * 续件费用)
- $add_pieces = bcdiv(($price_method_field - $node['pieces']), $node['add_pieces'], 2);
- return bcadd($node['price'], bcmul($add_pieces, $node['add_price'], 2), 2);
- } else {
- //未配置过续件,按首件价格
- return $node['price'];
- }
- }
- } else {
- return $default_postfee;
- }
- }
- }
- }
- public function boxGoodsSurePost($uid, $params){
- $hand_ids = $params['hand_ids'];
- $address_id = $params['address_id'];
- $total_ids = explode(',', $hand_ids);
- $model_hands = new BoxHandleModel();
- $list = $model_hands->where('id', 'in', $hand_ids)->where('status', 1)->where('uid', $uid)->select();
- if (count($list) != count($total_ids)){
- sr_throw('参数错误');
- }
- $address_info = Db::name('user_address')->where('id', $address_id)->where('uid', $uid)->find();
- if (!$address_info){
- sr_throw('地址错误');
- }
- $cur_time = sr_getcurtime(time());
- foreach ($total_ids as $key=>$val){
- $hand_info = Db::name('box_handle')->where('id', $val)->find();
- $goods_info = Db::name('shop_goods')->where('goods_id', $hand_info['goods_id'])->find();
- $supplier_info = Db::name('shop_supplier')->where('id', $goods_info['supplier'])->find();
- $order_id = Db::name('shop_order')->insertGetId([
- 'order_sn'=>createdFDOrderSn(),
- 'payment'=>0,
- 'ship_postfee'=>0,
- 'user_id'=>$uid,
- 'status'=>1,
- 'num'=>1,
- 'order_type'=>5,
- 'supplier_name'=>$supplier_info['name'],
- 'total_price'=>0,
- 'created_time'=>$cur_time,
- 'rebate_score'=>0,
- ]);
- Db::name('shop_order_goods')->insertGetId([
- 'order_id'=>$order_id,
- 'goods_id'=>$goods_info['goods_id'],
- 'goods_name'=>$goods_info['goods_name'],
- 'goods_category'=>$goods_info['category'],
- 'goods_img'=>$goods_info['goods_img'],
- 'num'=>1,
- 'price'=>$goods_info['price'],
- 'total_fee'=>$goods_info['price'],
- 'spec_ids'=>1,
- 'spec_text'=>$goods_info['spec_name'],
- 'uid'=>$uid,
- 'rebate_score'=>0,
- 'total_rebate_score'=>0
- ]);
- $pname = Db::name('area')->where('id', $address_info['province'])->value('name');
- $cname = Db::name('area')->where('id', $address_info['city'])->value('name');
- $countyname = Db::name('area')->where('id', $address_info['county'])->value('name');
- Db::name('shop_order_shipping')->insert([
- 'order_id'=>$order_id,
- 'sp_name'=>$address_info['name'],
- 'sp_mobile'=>$address_info['mobile'],
- 'sp_province'=>$pname,
- 'sp_city'=>$cname,
- 'sp_county'=>$countyname,
- 'sp_remark'=>$address_info['remark'],
- 'sp_mergename'=> $pname.' '.$cname.' '.$countyname.' '.$address_info['remark'],
- 'created_time'=>$cur_time
- ]);
- Db::name('box_handle')->where('id', $val)->save([
- 'status'=>2,
- 'handle_type'=>1
- ]);
- }
- }
- // 一件回收
- public function boxGoodsReBuy($uid, $params, $user_info){
- $hand_ids = $params['hand_ids'];
- $total_ids = explode(',', $hand_ids);
- $model_hands = new BoxHandleModel();
- $total_goodsnum = count($total_ids);
- $list = $model_hands->where('id', 'in', $total_ids)->where('status', 1)->where('uid', $uid)->select();
- if (count($list) != $total_goodsnum){
- sr_throw('参数或状态错误');
- }
- if ($model_hands->where('box_type', 10)->where('id', 'in', $hand_ids)->find()){
- sr_throw('普通商品只能发货,不能回收');
- }
- $cur_time = sr_getcurtime(time());
- // 待处理的总金额
- $total_money = Db::name('box_handle')->where('id', 'in', $hand_ids)->sum('goods_price');
- // 最终金额 总共商品金额减去 每个商品 10快
- $end_money = $total_money - $total_goodsnum* env('boxsetting.RECYCLE_ONBOX_PRICE');
- if ($end_money < 0){
- sr_throw('错误,请联系客服');
- }
- if ($params['action'] == 'apply'){
- return [
- 'total_count'=>count($list),
- 'end_money'=>$end_money,
- 'total_money'=>$total_money
- ];
- }
- if ($params['action'] == 'surerecycle'){
- $user_info = Db::name('user')->where(['id'=> $uid])->field('id,score,money,recycle_count')->find();
- // 回收卡扣除
- if ($user_info['recycle_count'] < $total_goodsnum){
- sr_throw('回收卡不足'.$total_goodsnum.'个');
- }
- // 扣除回收卡
- edit_user_recyclecard(3, $uid, $total_goodsnum, 0, $hand_ids);
- // 288的原价
- $oring_price = count($total_ids) * env('boxsetting.ONE_BOX_PRICE');
- edit_user_money(2, $uid, $oring_price);
-
- // 备注利润是否可以为负 by wes
- if ($total_money > $end_money){
- // 待结算利润
- Db::name('user')->where('id', $uid)->inc('profit_money',$end_money-$oring_price)->update();
- // 添加用户利润记录
- Db::name('user_unmoney')->insert([
- 'uid'=>$uid,
- 'money'=>$end_money-$oring_price,
- 'ids'=>$hand_ids,
- 'create_time'=>sr_getcurtime(time())
- ]);
- }
- // 改变用户处理状态
- Db::name('box_handle')->where('id', 'in', $hand_ids)->save([
- 'status'=>2,
- 'handle_type'=>2
- ]);
- }
- }
- }
|