<?php
declare (strict_types = 1);

namespace app\model\admin;

use library\basic\BaseModel;
use library\traits\JwtAuthModelTrait;
use library\traits\ModelTrait;
use think\Model;

/**
 * @mixin \think\Model
 */
class SiteProduct extends BaseModel
{

    use ModelTrait;
    use JwtAuthModelTrait;
    //仓库
    private $warehouse;

    private $sassid;

    public function setSassId($sassid) {
        $this->sassid = $sassid;
    }
    /**
     * 获取列表数据
     * @param $page
     * @param $where
     * @param $pageCount
     * @param $desc
     */
    public function getList($page  = 1,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
        $data =
            $this->field("sp.id,sp.p_id,sp.price,p.title,p.commission as pro_price,sp.status,sp.sales,p.count,p.img,p.add_time,p.wget,p.seq,p.warehouse_ids,sp.ver_bug_count,(SELECT cate_name from table_category where p.cate_id = id) as cate_name")
                ->alias("sp")
                ->join("product p","p.id = sp.p_id","left")
//                ->where('sp.status',1)
                ->when(1 == 1,function ($query) use($where){
                    if(!empty($where['warehouse'])) {
                        $query->whereRaw('FIND_IN_SET(:id,p.warehouse_ids)',['id'=>$where['warehouse']]);
                    }
                    if(!empty($where['name'])){
                        $query->whereLike('p.title',"%{$where['name']}%");
                    }
                    if($where['artType']=='open'){
                        $query->where('sp.status',1);
                    }
                    if($where['artType']=='stop'){
                        $query->where('sp.status',0);
                    }
                })
                ->where('sp.sassid',$this->sassid)
                ->paginate(['list_rows'=>$pageCount,'page'=>$page])
                ->toArray();
        foreach ($data['data'] as $k=>$v) {
            $data['data'][$k]['warehouseAr'] = $this->getWarehouse($v['warehouse_ids']);
            $data['data'][$k]['add_time'] = date('Y-m-d',$v['add_time']);
        }        
        return [$data['total'],$data['data']];
    }
    /**
     * 获取仓库数据
     * @param $ids
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function getWarehouse($ids) {
        $idAr = explode(',',$ids);
        $rAr = [];
        if(empty($this->warehouse))
            $this->warehouse = (new Warehouse)->select()->toArray();
        foreach ($this->warehouse as $v) {
            if(in_array($v['id'],$idAr)) {
                $rAr[] = $v;
            }
        }
        return $rAr;
    }
    /**
     * 获取商品详情
     * @param type $post
     */
    public function getProInfo($post){
        if(empty($post["id"])){
            return self::setErrorInfo('商品不存在');
        }
        $data=$this
                ->field("sp.id,sp.p_id,p.csno,p.title,p.tag,p.cate_id,p.img,p.unit_name,p.count,p.commission as pro_price,p.wget,p.warehouse_ids,sp.price,sp.sales,sp.ver_bug_count,sp.status,sp.is_host,sp.is_new,(SELECT cate_name from table_category where p.cate_id = id) as cate_name")
                ->alias("sp")
                ->join("product p","p.id = sp.p_id","left")
                ->where('sp.id',$post["id"])
                ->where('sp.sassid',$this->sassid)
                ->find()
                ->toArray();
        $data['img'] = explode(',',$data['img']);
        if(!empty($data['warehouse_ids'])) {
            $warehouse = (new Warehouse)->whereIn('id',$data['warehouse_ids'])->column('name');
            $data['warehouse'] = $warehouse;
        } else {
            $data['warehouse'] = [];
        }
        return $data;
    }
    public function saveProduct($post){
        if(empty($post["id"])){
            return self::setErrorInfo('商品不存在');
        }
        $id = $post['id'];
        if(intval($post["ver_bug_count"])<0){
            return self::setErrorInfo('虚拟数量不符合');
        }
        if(!is_numeric($post["price"]) || floatval($post["price"]) < 0){
            return self::setErrorInfo('商品售价不符合');
        }
        //判断价格
        $pro = $this->alias("sp")
                    ->field("p.commission")
                    ->join("product p","p.id = sp.p_id","left")
                    ->where('sp.id',$id)
                    ->find();
        if(empty($pro)) {
            return self::setErrorInfo('找不到对于产品');
        }
        if($pro['commission'] > $post["price"]) {
            return  self::setErrorInfo('不能低于商品成本价');
        }
        $save = [];
        $save['price'] = $post['price'];
        $save['ver_bug_count'] = $post['ver_bug_count'];
        $save['status'] = $post['status'];
        $save['is_host'] = $post['is_host'];
        $save['is_new'] = $post['is_new'];
        unset($post['id']);
        $this->where('id',$id)->where("sassid",$this->sassid)->save($save);
        return true;
    }
    

}
