<?php
declare (strict_types = 1);

namespace app\model\api;

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

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

    use ModelTrait;
    use JwtAuthModelTrait;
    private $expCost;

    /**
     * 根据id查询平台
     * @param $id
     * @param string $field
     * @return mixed
     */
    public function getWareHouseExpCost($wId,$field = '*') {
        $tAr = [];
        if(!empty($this->expCost)) {
            foreach ($this->expCost as $v) {
                if ($v['warehouse_id'] == $wId) {
                    $tAr[] = $field == '*' ? $v : $v[$field];
                }
            }
        }
        return $tAr;
    }

    /**
     * 平台数据
     * @return mixed|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getExpCostData($sassid = 0,$level_id = 0){
        if(empty($this->expCost))
            $this->expCost = $this
                                ->field("e.money,e.express_id,e.id,e.warehouse_id,exp.title,
                                (SELECT name from table_warehouse where id = e.warehouse_id) as warehouse_name
                                
                                ")
                                ->alias("e")
                                ->join("express exp","exp.id = e.express_id")
                                ->where('e.sassid',$sassid)
                                ->where('e.level_id',$level_id)
                                ->order("seq desc")
                                ->select()
                                ->toArray();
         //   echo $this->getLastSql();
        return $this->expCost;
    }

    /**
     * 获取平台原价
     * @param int $express_id
     * @param int $warehouse_id
     * @param int $sassid
     * @return mixed
     */
    public function getSysExpCostPrice($express_id = 0, $warehouse_id = 0,$sassid = 0){
        $money =  $this
                ->where('express_id',$express_id)
                ->where('warehouse_id',$warehouse_id)
                ->where('level_id',-1)
                ->where('sassid',$sassid)
                ->value('money');
        return $money;
    }


    /**
     * 分站价格佣金差价
     * @param $site
     * @param $parm
     */
    public function difference($site,$param) {
        //本级
        $money = $this->getSysExpCostPrice($param['express_id'],$param['warehouse_id'],$site['sassid']);
        //上级
        $money2 = $this->getSysExpCostPrice($param['express_id'],$param['warehouse_id'],$param['sassid']);
        if( empty($money) || empty($money2) || $money <= $money2) {
            return ;
        }
        $value = ($money - $money2) * $param['count'];
        (new SiteDetail)->difference($value,
            $param['sassid'],
            $param['orderid'],
            ['sitename'         => $site['name'],
             'user'             => $param['user'],
             'count'            => $param['count'],
             'money'            => $value
            ]
        );
    }

}
