<?php
declare (strict_types = 1);

namespace app\model\admin;

use Closure;
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;
    //


    public function expSite($sassid,$sassExpData){
        $expData  = (new ExpCost)
            ->field("*,(select name from table_warehouse where id = e.warehouse_id) as warehouse_name")
            ->alias("e")
            ->where('sassid',$sassid)
            ->where('level_id',-1)
            ->select()
            ->toArray();
        $expData2 = (new ExpCost)
            ->field("e.*,(select title from table_express where id = e.express_id) as title")
            ->alias("e")
            ->where('e.sassid',$sassid)
            ->where('e.level_id','>',-1)
            ->select();
        foreach ($expData as $k => $v) {
            $rAr = $v;
            $rAr['site_money'] = $v['money'];
            //编辑自己的佣金
            foreach ($sassExpData as $vv) {
                if($v['warehouse_id'] == $vv['warehouse_id'] && $v['express_id'] == $vv['express_id']) {
                    $rAr['site_money'] = $vv['money'];
                }
            }
            foreach ($expData2 as $vv) {
                if($v['warehouse_id'] == $vv['warehouse_id'] && $v['express_id'] == $vv['express_id']) {
                    $rAr['title'] = $vv['title'];
                    $rAr['level_'.$vv['level_id']] = $vv['money'];
                }
            }
            $expData[$k] = $rAr;
        }
        return $expData;
    }




    /**
     * 获取列表数据
     * @param $page
     * @param $where
     * @param $pageCount
     * @param $desc
     */
    public function getList2($page,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
        $data =  $this
            ->field("ec.*,s.name as site_name,e.title as exp_name,w.name as warehouse_name")
            ->alias("ec")
            ->join('site s',"ec.sassid = s.sassid")
            ->join('express e',"e.id = ec.express_id")
            ->join('warehouse w',"w.id = ec.warehouse_id")
            ->where('ec.sassid',$where['sassid'])
            ->when(!empty($where),function ($query) use($where){
                if(!empty($where['level_id'])) {
                    $query->where('ec.level_id',$where['level_id']);
                }
            })
            ->order($desc)
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
        // echo $this->getLastSql();
        return [$data['total'],$data['data']];
    }
}
