Common.php 20.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\admin\model\Car;
use app\admin\model\Ogoods;
use app\admin\model\Order;
use app\admin\model\Rcoupon;
use think\Db;
use think\db\Query;
/**
 * 公共方法
 */
class Common
{
    /**
     * 查找单条数据(软删除)
     * @ApiInternal
     */
    public static function findSoftWhereData($table,$where,$field,$except=false,$order='id desc'){
        $res = Db::name($table)
            ->where($where)
            ->field($field,$except)
            ->order($order)
            ->useSoftDelete('deletetime')
            ->find();
        return $res;
    }

    /**
     * 查找单条数据(有排序)
     * @ApiInternal
     */
    public static function findWhereData($table,$where,$field,$order='id desc'){
        $res = Db::name($table)
            ->where($where)
            ->field($field)
            ->order($order)
            ->find();
        return $res;
    }

    /**
     * 查找多条数据(无条件)
     * @ApiInternal
     */
    public static function selectData($table,$field,$order='id desc'){
        $res = Db::name($table)
            ->field($field)
            ->order($order)
            ->select();
        return $res;
    }

    /**
     * 查找多条数据
     * @ApiInternal
     */
    public static function selectWhereData($table,$where,$field,$order='id desc'){
        $res = Db::name($table)
            ->where($where)
            ->field($field)
            ->order($order)
            ->select();
        return $res;
    }

    /**
     * 查找多条数据
     * @ApiInternal
     */
    public static function selectWherePageData($table,$where,$field,$page,$limit='',$order='id desc'){
        if($limit == ''){
            $limit = config('verify.limit');
        }
        $res = Db::name($table)
            ->where($where)
            ->field($field)
            ->page($page,$limit)
            ->order($order)
            ->select();
        return $res;
    }

    /**
     * 查找多条数据(软删除)
     * @ApiInternal
     */
    public static function selectSoftData($table,$field,$order='id desc'){
        $res = Db::name($table)
            ->field($field)
            ->order($order)
            ->useSoftDelete('deletetime')
            ->select();
        return $res;
    }

    /**
     * 查找多条数据
     * @ApiInternal
     */
    public static function selectSoftWhereData($table,$where,$field,$order='id desc'){
        $res = Db::name($table)
            ->where($where)
            ->field($field)
            ->order($order)
            ->useSoftDelete('deletetime')
            ->select();
        return $res;
    }

    /**
     * 查找多条数据(软删除,分页)
     * @ApiInternal
     */
    public static function selectSoftWherePageData($table,$where,$field,$page,$limit='',$order='id desc'){
        if($limit == ''){
            $limit = config('verify.limit');
        }
        $res = Db::name($table)
            ->where($where)
            ->field($field)
            ->page($page,$limit)
            ->order($order)
            ->useSoftDelete('deletetime')
            ->select();
        return $res;
    }

    /**
     * 查找多条数据(软删除,分页)
     * @ApiInternal
     */
    public static function selectSoftPageData($table,$field,$page,$limit='',$order='id desc'){
        if($limit == ''){
            $limit = config('verify.limit');
        }
        $res = Db::name($table)
            ->field($field)
            ->page($page,$limit)
            ->order($order)
            ->useSoftDelete('deletetime')
            ->select();
        return $res;
    }

    /**
     * 查找多条数据(软删除,限制条数)
     * @ApiInternal
     */
    public static function selectSoftWhereLimitData($table,$where,$field,$order='id desc'){
        $limit = config('verify.limit');
        $res = Db::name($table)
            ->where($where)
            ->field($field)
            ->limit($limit)
            ->order($order)
            ->useSoftDelete('deletetime')
            ->select();
        return $res;
    }

    /**
     * 查找多条数据(软删除,无条件,限制条数)
     * @ApiInternal
     */
    public static function selectSoftLimitData($table,$field,$limit,$order='id desc'){
        $res = Db::name($table)
            ->field($field)
            ->limit($limit)
            ->order($order)
            ->useSoftDelete('deletetime')
            ->select();
        return $res;
    }

    /**
     * 查询总页数
     * @ApiInternal
     */
    public static function count($table,$where,$limit=''){
        if($limit == ''){
            $limit = config('verify.limit');
        }
        $count = Db::name($table)
            ->where($where)
            ->count();
        return ceil($count/$limit);
    }

    /**
     * 查询总条数
     * @ApiInternal
     */
    public static function count_count($table,$where){
        $count = Db::name($table)
            ->where($where)
            ->count();
        return $count;
    }

    /**
     * 查询总页数(软删除)
     * @ApiInternal
     */
    public static function countSoft($table,$where){
        $limit = config('verify.limit');
        $count = Db::name($table)
            ->where($where)
            ->useSoftDelete('deletetime')
            ->count();
        return ceil($count/$limit);
    }

    /**
     * 查询总数(软删除)
     * @ApiInternal
     */
    public static function countSoftTotal($table,$where){
        $count = Db::name($table)
            ->where($where)
            ->useSoftDelete('deletetime')
            ->count();
        return $count;
    }

    /**
     * 商品列表
     * @ApiInternal
     */
    public static function goodsList($where='',$page,$uid='',$limit='',$order='id desc',$flag = '',$flag1=''){
        $arr = [];
        //默认未登录
        $is_news = self::is_new($uid);
        if(empty($where)){
            if($is_news == 1){
                //去除含有新人标签的数据
                $res = Common::selectSoftWherePageData('goods',['is_new'=>0],'id,image,name,tag,style,sale_price1 sale_price,expense_price,is_new',$page,$limit,$order);
            }else{
                $res = Common::selectSoftPageData('goods','id,image,name,tag,style,sale_price1 sale_price,expense_price,is_new',$page,$limit,$order);
            }
        }else{
            if($is_news == 1){
                //旧人
                $where['is_new'] = 0;
            }
            $res = Common::selectSoftWherePageData('goods',$where,'id,image,name,tag,style,sale_price1 sale_price,expense_price,is_new',$page,$limit,$order);
        }

        foreach ($res as &$value){
            if($is_news == 2 || $is_news == 0){//未登录或者新人
                if($value['is_new'] == 0){
                    //非新人优惠标签
                    $value['is_new_tag'] = 0;//不用显示新人价标签
                }else{
                    //新人优惠标签
                    $value['is_new_tag'] = 1;//显示新人价标签
                }
            }else{
                $value['is_new_tag'] = 0;//不用显示新人价标签(新人价标签商品不会出来)
            }
            $value['image'] = self::absolutionUrlOne($value['image']);
            $value['style'] = explode('|',$value['style']);
            $value['tag'] = explode('|',$value['tag']);
            $sale_price = self::salePrice($value['sale_price']);
            if($sale_price){
                $value['sale_price'] = $sale_price[0];
            }else{
                $value['sale_price'] = '';
            }
            unset($value['is_new']);
        }
        if(!empty($flag1)){
            //销售价格排序
            if($flag1 == 1){
                //倒序
                $sale_price = array_column($res,'sale_price');
                array_multisort($sale_price,SORT_DESC,$res);
            }else{
                //正序
                $sale_price = array_column($res,'sale_price');
                array_multisort($sale_price,SORT_ASC,$res);
            }
        }

        $arr['data'] = $res;
        //总页数
        $arr['total_page'] = self::countSoft('goods',$where);
        if(empty($flag)){
            return $arr;
        }else{
            return $res;
        }
    }

    /**
     * 是否是新人
     * @ApiInternal
     */
    public static function is_new($uid){
        $is_news = 2;
        if(!empty($uid)){
            //已登录
            $res1 = self::findSoftWhereData('order',['uid'=>$uid,'status'=>config('verify.status')[8]],'id');
            if($res1){
                $is_news = 1;//旧人
            }else{
                $is_news = 0;//新人
            }
        }
        return $is_news;
    }

    /**
     * 检索商品列表
     * @ApiInternal
     */
    public static function searchGoodsList($keyword,$where,$page,$uid){
        $arr = [];
        //默认未登录
        $is_news = self::is_new($uid);
        if($is_news == 1){
            $where['is_new'] = 0;
        }
        $limit = config('verify.limit');
        $res = Db::name('goods')
            ->field('LOCATE("'.$keyword.'",name) as nameIndex,id,image,name,tag,is_new,style,sale_price1 sale_price,expense_price')//获取关键字的位置:LOCATE(关键字,字段) 返回索引位置
            ->where($where)//多字段条件查询
            ->page($page,$limit)
            ->order('nameIndex')//按tIndex 值排序
            ->select();
        foreach ($res as &$value){
            if($is_news == 2 || $is_news == 0){//未登录或者新人
                if($value['is_new'] == 0){
                    //非新人优惠标签
                    $value['is_new_tag'] = 0;//不用显示新人价标签
                }else{
                    //新人优惠标签
                    $value['is_new_tag'] = 1;//显示新人价标签
                }
            }else{
                $value['is_new_tag'] = 0;//不用显示新人价标签(新人价标签商品不会出来)
            }

            $value['image'] = self::absolutionUrlOne($value['image']);
            $value['tag'] = explode('|',$value['tag']);
            $value['style'] = explode('|',$value['style']);
            $sale_price = self::salePrice($value['sale_price']);
            if($sale_price){
                $value['sale_price'] = $sale_price[0];
            }else{
                $value['sale_price'] = '';
            }

            $value['str_count'] = 0;
            if(strpos($value['name'],$keyword) !== false){
                //包含
                $value['name'] = str_replace($keyword,'<a href="javascript:void(0);" style="color:red;">'.$keyword.'</a>',$value['name']);
                $value['str_count'] = substr_count($value['name'],$keyword);
            }
            unset($value['is_new']);
        }
        $str_count = array_column($res,'str_count');
        array_multisort($str_count,SORT_DESC,$res);
        $arr['data'] = $res;
        //总页数
        $arr['total_page'] = Common::countSoft('goods',$where);
        return $arr;
    }

    /**
     * 价格字符串转数组
     * @ApiInternal
     */
    public static function salePrice($string=''){
        $arr = [];
        if(!empty($string)){
            $arr = explode('|',$string);
        }
        return $arr;
    }

    /**
     * 单张相对路径转绝对路径
     * @ApiInternal
     */
    public static function absolutionUrlOne($image){
        $url = '';
        if(!empty($image)){
            $host = config('verify.host');
            $url = $host.$image;
        }
        return $url;
    }

    /**
     * @param $style 传递规格
     * @param $goods_number 传递数量
     * @param $g_style 数据库商品规格
     * @param $g_stock 数据库商品库存
     * @ApiInternal
     * @return bool
     */
    public static function checkStock($style,$goods_number,$g_style,$g_stock){
        $s_index = array_search($style,self::salePrice($g_style));
        $stock = self::salePrice($g_stock)[$s_index];
        if(($stock <=> $goods_number) == -1){
            //库存不足
            return false;
        }
        return true;
    }

    /**
     * 合并数组
     * @ApiInternal
     */
    public static function array_merge_more($keys, ...$arrs){
        // 检查参数是否正确
        if(!$keys || !is_array($keys) || !$arrs || !is_array($arrs) || count($keys)!=count($arrs)){
            return array();
        }
        // 一维数组中最大长度
        $max_len = 0;
        // 整理数据,把所有一维数组转重新索引
        for($i=0,$len=count($arrs); $i<$len; $i++){
            $arrs[$i] = array_values($arrs[$i]);

            if(count($arrs[$i])>$max_len){
                $max_len = count($arrs[$i]);
            }
        }
        // 合拼数据
        $result = array();
        for($i=0; $i<$max_len; $i++){
            $tmp = array();
            foreach($keys as $k=>$v){
                if(isset($arrs[$k][$i])){
                    $tmp[$v] = $arrs[$k][$i];
                }
            }
            $result[] = $tmp;
        }
        return $result;
    }

    /**
     * 订单号
     * @ApiInternal
     */
    public static function genOrderSn() {
        $res = Db::name('order')->field('id,order_sn')->order('id desc')->find();
        if($res){
            $order_sn = $res['order_sn'];
            $order_sn++;
        }else{
            $order_sn = '1000001';
        }
        return $order_sn;
    }

    /**
     * 支付订单号
     * @ApiInternal
     */
    public static function genPayOrderSn($letter = '') {
        $time = explode (" ", microtime ());
        $timeArr = explode('.',$time [0]);
        $mtime = array_pop($timeArr);
        $fulltime = $letter.$time[1].$mtime.mt_rand(1000,9999);
        return $fulltime;
    }

    /**
     * 写入订单数据
     * @ApiInternal
     */
    public static function createOrder($data,$pay_order_sn,$uid,$status=''){
        Db::startTrans();
        //写入订单主表
        $data1['order_sn'] = self::genOrderSn();
        $data1['pay_order_sn'] = $pay_order_sn;
        $data1['uid'] = $uid;
        $data1['receive_name'] = $data['receive_name'];
        $data1['receive_mobile'] = $data['receive_mobile'];
        $data1['receive_address'] = $data['receive_address'];
        if(isset($data['leave_message']) && !empty($data['leave_message'])){
            $data1['leave_message'] = $data['leave_message'];//留言
        }
        if(isset($data['coupon_id']) && !empty($data['coupon_id'])){
            $data1['coupon_id'] = $data['coupon_id'];//优惠券id
        }
        if(!empty($status)){
            $data1['status'] = $status;//更新支付成功
        }
        $data1['total_goods_price'] = $data['total_goods_price'];//商品金额
        $data1['discount_price'] = $data['discount_price'];//优惠金额
        $data1['total_expense_price'] = $data['total_expense_price'];//运费总金额
        $data1['total_price'] = $data['total_price'];//商+运-优惠
        $orderModel = new Order();
        $res1 = $orderModel->create($data1);
        //写入订单附加表
        $goods_id_s = explode(',',$data['goods_id']);
        $price_s = explode(',',$data['price']);
        $goods_number_s = explode(',',$data['goods_number']);
        $style_s = explode(',',$data['style']);
        //合并数组
        $key = ['g_id','price','goods_number','style'];
        $res_goods = self::array_merge_more($key,$goods_id_s,$price_s,$goods_number_s,$style_s);
        $o_id = $res1->id;
        foreach($res_goods as &$value){
            $value['o_id'] = $o_id;
        }
        $oGoodsModel = new Ogoods();
        $res2 = $oGoodsModel->saveAll($res_goods);
        if($res1 && $res2){
            Db::commit();
            $arr['order_id'] = $o_id;
            return $arr;
        }else{
            Db::rollback();
            return false;
        }
    }

    /**
     * 减库存
     * @ApiInternal
     */
    public static function decStock($where){
        $goodsModel = new \app\admin\model\Goods();
        $goodsModel->where($where)->setDec('stock',1);
    }

    /**
     * 增销量
     * @ApiInternal
     */
    public static function incSales($where,$sale_number){
        $goodsModel = new \app\admin\model\Goods();
        $goodsModel->where($where)->setInc('sales',$sale_number);
    }

    /**
     * 减少销量
     * @ApiInternal
     */
    public static function decSales($where,$sale_number){
        $goodsModel = new \app\admin\model\Goods();
        $goodsModel->where($where)->setDec('sales',$sale_number);
    }

    /**
     * 删购物车
     * @ApiInternal
     */
    public static function deleteCar($where){
        $carModel = new Car();
        $carModel->where($where)->delete();
    }

    /**
     * 更新优惠券
     * @ApiInternal
     */
    public static function updateRCoupon($uid,$coupon_id,$is_use){
        $rCouponModel = new Rcoupon();
        $rCouponModel->where(['uid'=>$uid,'c_id'=>$coupon_id])->update(['is_use'=>$is_use]);
    }

    /**
     * 购买成功,更新订单号为待发货,销量增加1,减库存,删除购物车
     * @ApiInternal
     */
    public static function paySuccess($out_trade_no){
        $status = config('verify.status');
        //更新订单号成功
        $orderModel = new Order();
        $orderModel->where(['pay_order_sn'=>$out_trade_no,'status'=>$status[0]])->update(['status'=>$status[2]]);//待发货
        //销量增加1
        $res = self::findSoftWhereData('order',['pay_order_sn'=>$out_trade_no,'status'=>$status[2]],'id,uid,coupon_id');
        if($res){
            $res1 = self::selectSoftWhereData('ogoods',['o_id'=>$res['id']],'id,g_id,style,goods_number');
            $g_ids = array_column($res1,'g_id');
            //减库存
            $goodsModel = new \app\admin\model\Goods();
            //查询商品的库存
            $res_stock = self::selectSoftWhereData('goods',['id'=>['in',$g_ids]],'id,style g_style,stock');
            foreach($res_stock as $g_value){
                $arr_stock = explode('|',$g_value['stock']);
                $sale_number = 1;
                foreach($res1 as $value){
                    if($g_value['id'] == $value['g_id']){
                        $s_index = array_search($value['style'],self::salePrice($g_value['g_style']));
                        $stock = self::salePrice($g_value['stock'])[$s_index] - $value['goods_number'];
                        $arr_stock[$s_index] = $stock;
                        $sale_number = $value['goods_number'];
                    }
                }
                //更新库存
                $stock1 = implode('|',$arr_stock);
                $goodsModel->where(['id'=>$g_value['id']])->update(['stock'=>$stock1]);
                //增加销售
                self::incSales(['id'=>['in',$g_ids]],$sale_number);
            }
            //删除购物车
            self::deleteCar(['uid'=>$res['uid'],'g_id'=>['in',$g_ids]]);

            //更新优惠券已使用
            $flag = config('verify.flag');
            self::updateRCoupon($res['uid'],$res['coupon_id'],$flag[1]);
        }
    }

    /**
     * 支付检测库存
     * @ApiInternal
     */
    public static function checkPayStock($order_goods,$goods_id = ''){
        //检查库存
        if(empty($goods_id)){
            $goods_id_s = array_column($order_goods,'goods_id');
            //查询商品
            $res = self::selectSoftWhereData('goods',['id'=>['in',$goods_id_s]],'id,name,stock,style g_style');
        }else{
            //查询商品
            $res = self::selectSoftWhereData('goods',['id'=>['in',$goods_id]],'id,name,stock,style g_style');
        }

        foreach($order_goods as $g_value){
            foreach ($res as $value){
                if($value['id'] == $g_value['goods_id']){
                    //检测库存
                    $check_stock = self::checkStock($g_value['style'],$g_value['goods_number'],$value['g_style'],$value['stock']);
                    if(!$check_stock){
                        return '商品名称:'.$value['name'].',规格:'.$g_value['style'].'库存不足';
                    }
                }
            }
        }
        return true;
    }
}