<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\api\controller;

use app\api\controller\Base;
use think\Db;

class Search extends Base
{ 
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function goods(){
        $params = $this->param();
        
        
        $keyword = trim($params['keyword']);

        $where = ["uniacid"=>UNIACID,'status'=>1,'sh_status'=>1];
        if(!empty($keyword)){
            $where['title'] = ['like','%'.$keyword.'%'];
        }
        
        $list = Db::name("bbfx_goods")->where($where)->field("id,title,thumb,price,stock,type,o_price,minbuy,maxbuy,total_maxbuy,unit,sales_volume,virtual_sale,is_nodiscount,is_leveldiscount_rule,level_discount")->select();
        
        if($list){
            foreach($list as $key=>&$val){
                unset($val['plugin']);
            	$val['thumb'] = cdnurl($val['thumb']);
                $serial_total = Db::name("bbfx_serial_number")->where(["uniacid"=>UNIACID,"goods_id"=>$val['id']])->count();
                $serial_surplusl = Db::name("bbfx_serial_number")->where(["uniacid"=>UNIACID,"goods_id"=>$val['id'],'status'=>1])->count();
            	$val['total'] = $serial_total;
            	$val['surplusl'] = $serial_surplusl;
            	
            	if($val['sales_volume'] == 0){
            	    $sales_volume = Db::name("bbfx_order_goods")->alias("og")->join("bbfx_order o","og.order_id=o.id","LEFT")->where(['o.uniacid'=>UNIACID,'o.status'=>['>',0],'og.goods_id'=>$val['id']])->sum("og.total");
            	    $val['sales_volume'] = intval($sales_volume);
            	    
            	    if($val['sales_volume'] > 0){
            	        Db::name("bbfx_goods")->where(['id'=>$val['id']])->update(['sales_volume'=>$val['sales_volume']]);
            	    }
            	}
                $val['sales_volume'] = intval($val['sales_volume'] + $val['virtual_sale']);
                
            }
            unset($val);
            __success("操作成功",$list);
        }
        else{
            __error("未查询到数据");
        }
    }
}