ExpressService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\common\service;
  15. use app\common\model\OrderLogisticsUrge;
  16. use think\Db;
  17. /**
  18. * 商城邮费服务
  19. * Class ExpressService
  20. * @package app\store\service
  21. */
  22. class ExpressService
  23. {
  24. /**
  25. * @param array $goods_info 商品详情
  26. * @param array $address_info 收货地址
  27. * @param int $num 商品数量
  28. * @return array
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. * @throws \think\exception\DbException
  32. */
  33. public static function getGoodsExpressPrice($goods_info,$address_info,$num)
  34. {
  35. if($goods_info['freight_type'] == 0){
  36. return ['code'=>200,'freight'=> $goods_info['postage'],'msg'=>'','id'=>0];
  37. } else if($goods_info['freight_id']){
  38. return self::getExpressPrice($address_info,$goods_info['freight_id'],$num) ; // 快递费用
  39. }else{
  40. return ['code'=>200,'freight'=> 0,'msg'=>'','id'=>0];
  41. }
  42. }
  43. /**
  44. * @param array $address_info 收货地址
  45. * @param int $freight_id 运费模板id
  46. * @param int $num 商品数量
  47. * @return array
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. public static function getExpressPrice($address_info,$freight_id,$num = 1)
  53. {
  54. $data = ['code'=>200,'freight'=>0 ,'msg'=>'','id'=>0];
  55. $template_info = Db::name('freight_template')->find($freight_id);
  56. if(empty($address_info)){
  57. $data['code'] = 201;
  58. $data['msg'] = '运费模板不存在';
  59. return $data;
  60. }
  61. $city_id = $address_info['city_id'];
  62. $no_express = explode(',',$template_info['no_express']);
  63. if(in_array($city_id,$no_express)) return ['code'=>202,'freight'=>0 ,'msg'=>'该收货地址不配送物流'];
  64. // 模板自定义分组
  65. $custom = json_decode($template_info['custom'],true);
  66. $base_num = $template_info['base_num'];// 基件数量
  67. $base_price = $template_info['base_price']; // 基件费用
  68. $base_keep_price = $template_info['base_keep_price']; // 续件费用
  69. foreach($custom as $cus_info) {
  70. $express =explode(',',$cus_info['express']);
  71. if(in_array($city_id,$express)) {
  72. $base_num = $cus_info['first_num'];
  73. $base_price = $cus_info['first_price'];
  74. $base_keep_price = $cus_info['keep_price'];
  75. break;
  76. }
  77. }
  78. if($base_num >= $num ) return $data = ['code'=>200,'freight'=>$base_price ,'msg'=>'ok'];
  79. $sub_keep = bcmul(bcsub($num,$base_num),$base_keep_price,2);
  80. return $data = ['code'=>200,'freight'=>bcadd($base_price,$sub_keep,2) ,'msg'=>'ok'];
  81. }
  82. /**
  83. * @param int $order_id 订单id
  84. * @param int $user_id 会员id
  85. * @param string $table 订单表
  86. * @return array
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. * @throws \think\exception\DbException
  90. */
  91. public static function orderUrge($order_id,$user_id,$table = 'dd_store_order')
  92. {
  93. $check_id = OrderLogisticsUrge::where(['order_id'=>$order_id,'order_table'=>$table])->where('create_at','> time',date('Y-m-d H:i:s',strtotime('-1 days')))->value('id');
  94. if($check_id){
  95. return ['code'=>201,'msg'=>'24小时之内只能催发一次'];
  96. }
  97. OrderLogisticsUrge::create(['user_id'=>$user_id,'order_id'=>$order_id,'table_name'=>$table]);
  98. return ['code'=>200,'msg'=>'已催促发货,请耐心等待~~'];
  99. }
  100. }