<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 商品管理(SPU)
 */
namespace app\allwin\model;
use app\common\facade\Inform;
use think\Model;

class AllwinShop extends Model{
    
    protected $pk    = 'id';
    protected $autoWriteTimestamp = true;

    //店铺
    public function store(){
        return $this->hasOne('AllwinStore','id','store_id');
    }

    //栏目名称
    public function cate(){
        return $this->hasOne('AllwinShopCate','id','category_id');
    }

    //选择优惠券
    public function coupon(){
        return $this->hasOne('Coupon','id','coupon_id');
    }

    //搜索器
    public function searchNameAttr($query,$value){
        if(!empty($value)){
            $query->where('name','like', '%'.$value .'%');
        }
    }

    /**
     * @return \think\model\relation\HasOne
     * 觅探
     */
    public function detective(){
        return $this->hasOne('app\common\model\SystemUser','id','detective_uid');
    }
    /**
     * 检测单个SPU商品是否上架
     * @param integer $spu_id
     * @return void
     */
    public function isSell(int $spu_id){
        return  self::where(['id' => $spu_id,'is_sale'=>2])->find();
    }

    //添加或编辑
    public static function edit(array $param){
        $data['category_id']       = $param['category_id'];
        $data['store_id']          = $param['store_id'];
        $data['name']              = $param['name'];
        $data['title']             = $param['title'];
        $data['types']             = $param['types'];
        $data['market_price']      = $param['market_price'];
        $data['sell_price']        = $param['sell_price'];
        $data['cost_price']        = $param['cost_price'];
        $data['share_price']       = $param['share_price'];
        $data['vip_price']         = $param['vip_price'];
        $data['points']            = $param['points'];
        $data['warehouse_num']     = $param['warehouse_num'];
        $data['warehouse_sellnum'] = $param['warehouse_sellnum'];
        $data['coupon_id']         = $param['coupon_id'];
        $data['content']           = $param['content'];
        $data['img']               = $param['img'];
        $data['imgs']              = json_encode($param['imgs']);
        $data['types']             = $param['types'];
        $data['group_title']       = $param['group_title'];
        $data['group_note']        = $param['group_note'];
        $data['group_img']         = $param['group_img'];
        $data['keyword']           = $param['keyword'];
        $data['notice']            = $param['notice'];
        $data['detective_price']   = $param['detective_price'];
        $data['detective_uid']     = $param['detective_uid'];
        $data['end_time']          = empty($param['end_time']) ? '' : strtotime($param['end_time']);
        $data['update_time']       = time();
        if(isset($param['id'])){
            self::where('id',$param['id'])->update($data);
            return $param['id'];
        }else{
            $data['is_sale']           = 0;
            $data['member_miniapp_id'] = $param['member_miniapp_id'];
            return self::insertGetId($data);
        }
    }
    

    //添加或编辑
    public static function editItem(array $param){
        $param['update_time'] = time();
        $param['is_sale']     = 0;
        if(isset($param['id'])){
            $id = (int)$param['id'];
            self::where('id',$id)->update($param);
            return $id;
        }else{
            return self::insertGetId($param);
        }
    }

    //批量操作
    public static function ids_action(int $is_sale,string $ids){
        switch ($is_sale) {
            case 1:
                $data['is_sale'] = 1; //在售
                break;
            case 2:
                $data['is_del']  = 1;  //删除
                break;
            default:
                $data['is_sale'] = 0;
                $data['is_del']  = 0;
                break;
        }
        return self::whereIn('id',ids($ids))->data($data)->update(); //操作所有SPU商品
    }  

    /**
     * 删除SPU商品
     * @param [type] $id
     * @param [type] $ids
     * @return void
     */
    public static function ids_delete(int $id,$ids){
        if(empty($ids)){
            $ids = (int)$id;
        }else{
            $ids = ids($ids);
        }
        $rel = self::whereIn('id',$ids)->field('is_del,id')->select()->toArray();
        if(!empty($rel)){
            $del_data = [];
            $up_data  = [];
            foreach ($rel as $value) {
                if($value['is_del'] == 1){
                    $del_data[] = $value['id'];
                }else{
                    $up_data[]  = $value['id'];
                }
            }
            if(!empty($del_data)){
               self::whereIn('id',$del_data)->delete();
            }
            if(!empty($up_data)){
                self::whereIn('id',$up_data)->update(['is_del' => 1]);
            }
        }
        return true;
    }

    /**
     * 如果使用了优惠券的默认价格
     */
    public static function calculatePrice($sell_price,$coupon){
        if($coupon->types){
            $coupon_user_price = $sell_price - $sell_price * $coupon->discount/10; 
        }else{
            $coupon_user_price = $coupon->price;
        }
        return $coupon_user_price;
    }
}