<?php

namespace app\api\model;

use think\Model;


class Car extends Model
{
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;

    public function getCreatetimeAttr($value)
    {
        return date('Y-m-d',$value);
    }

    public function getImageAttr($value)
    {
        return cdnurl($value);
    }

    public function selectPageData($where,$page,$limit,$lang){
        if ($lang == 'ch') $field = 'c.id car_id,c.createtime,cou.ch_name country_name,c.number,c.type,g.id goods_id,g.ch_name name,g.image,g.goods_price,g.group_price,g.vip_price,g.is_vip_price,g.is_group';
        else $field = 'c.id car_id,c.createtime,cou.en_name country_name,c.number,c.type,g.id goods_id,g.en_name name,g.image,g.goods_price,g.group_price,g.vip_price,g.is_vip_price,g.is_group';

        $total =$this->alias('c')
            ->where($where)
            ->count();

        $list =  $this->alias('c')
            ->join('fa_goods g','c.goods_id=g.id')
            ->join('fa_country cou','cou.id=g.country_id')
            ->where($where)
            ->field($field)
            ->page($page,$limit)
            ->select();
        return ['total' => $total, 'list' => $list];
    }

    public function getCarMoney($where){
        $goodsId = $this->where($where)->where(['type'=>1])->column('id');
        $goodsList = $this->alias('c')
            ->join('fa_goods g','c.goods_id=g.id')
            ->where(['c.id'=>['in',$goodsId]])
            ->field('g.id,goods_price*number goods_price,g.vip_price*number vip_price,g.is_vip_price,g.ch_name,g.en_name,g.image,c.number')
            ->select();

        $integralGoodsId = $this->where($where)->where(['type'=>2])->column('id');
        $integralGoodsList = $this->alias('c')
            ->join('fa_integral_goods g','c.goods_id=g.id')
            ->where(['c.id'=>['in',$integralGoodsId]])
            ->field('g.id,goods_price*number goods_price,c.number,integral*number integral,g.ch_name,g.en_name,g.image')
            ->select();
        $list = array_merge($goodsList,$integralGoodsList);
        foreach ($list as $k => $v){
            if (!empty($v['is_vip_price'])) $list[$k]['type'] = 1;
            else $list[$k]['type'] = 2;
        }
        return $list;
    }
}