<?php
namespace App\Model\Bom;



class BomItemMatchModel extends BomItemMatchBaseModel{
    protected $connection = 'bom';  //库名
    protected $table = 'bom_item_matching';  //库名
    protected $primaryKey = 'matching_id';    //设置id
    protected $guarded = ['matching_id'];  //设置字段黑名单
    public $timestamps = true;
    const CREATED_AT = 'add_time';
    const UPDATED_AT = 'update_time';
    protected $dateFormat = 'Y-m-d H:i:s';


    public function fromDateTime($value){
        return strtotime(parent::fromDateTime($value));
    }


    public function getBomAmount($bom_id){
        $suffix = substr(strrev($bom_id),0,1);
        $taoshu = BomModel::where(["bom_id"=>$bom_id])->value("amount");
        $taoshu = $taoshu ? intval($taoshu) : 1;
        return self::suffix($suffix)->where('bom_id', $bom_id)->where("status",1)->selectRaw("sum(price*number*{$taoshu})  as amount")->first();
    }


    public  function getBomItemsMatching($bom_id,$bom_item_id){
        $suffix = substr(strrev($bom_id),0,1);
        return self::suffix($suffix)->where('bom_id', $bom_id)->where("bom_item_id",$bom_item_id)->where("status",1)->first();
    }


    public  function updateBomMatch($bom_id,$where,$data){
        $suffix = substr(strrev($bom_id),0,1);
        return self::suffix($suffix)->where($where)->update($data);
    }

    public  function addBomMatch($bom_id,$data){
        $suffix = substr(strrev($bom_id),0,1);
        return self::suffix($suffix)->insert($data);
    }

    public function bomExtend(){
        return $this->belongsTo(\App\Model\Bom\BomExtendModel::class,"bom_id","bom_id");
    }

    /*
    * 获取bom下单型号的数量
    */
    public function countCOrderBomItems($is_kefu,$bom_sn,$begin_time,$end_time){
        $count = 0;
        for($i=0;$i<=9;$i++){
            $query = self::suffix($i);
            $query = $this->buildCountQuery($query,$is_kefu,$bom_sn,$begin_time,$end_time);
            $count +=  $query->where(["is_corder"=>1])->count("matching_id");
        }
        return $count;
    }

    protected function buildCountQuery($query,$is_kefu,$bom_sn,$begin_time,$end_time){
        $bom_id = 0;
        if($bom_sn != ""){
            $bom = BomModel::where("bom_sn",trim($bom_sn))->select("bom_id")->first();
            $bom_id = $bom ? $bom->bom_id : 0;
        }

        if($bom_id > 0){
            $query = $query->where(["bom_id"=>$bom_id]);
        }


        $begin_time = $begin_time ? strtotime($begin_time." 00:00:00") : 0;
        $end_time = $end_time ? strtotime($end_time." 23:59:59") : 0;
        if($begin_time){
            if($end_time){
                $query = $query->where('add_time',">=",$begin_time)->where('add_time',"<=",$end_time);
            }else{
                $query = $query->where('add_time',">=",$begin_time);
            }
        }elseif($end_time){
            $query = $query->where('add_time',"<=",$end_time);
        }



        if($is_kefu == '0'){
            $query = $query->whereDoesntHave("bomExtend",function ($query)  {
                $query->where('kefu_id', "!=",0);
            });
        }elseif($is_kefu == '1'){
            $query = $query->whereHas("bomExtend",function ($query)  {
                $query->where('kefu_id', "!=",0);
            });
        }
        return $query;
    }


    /*
    * 获取bom提交BOM金额
     * $type   1提交的bom金额   2下单的bom金额
    */
    public function bomAmount($is_kefu,$bom_sn,$begin_time,$end_time,$type=0){
        $amount = 0;
        for($i=0;$i<=9;$i++){
            $query = self::suffix($i);
            $query = $this->buildCountQuery($query,$is_kefu,$bom_sn,$begin_time,$end_time);
            if($type == 0){
                $query = $query->where(["is_corder"=>0]);
            }elseif($type == 1){
                $query = $query->where(["is_corder"=>1]);
            }
            $sumamount =  $query->selectRaw("sum(number*price) as sumamount")->first();
            $amount += $sumamount ? $sumamount->sumamount : 0;
        }
        return $amount;
    }

    /*
     * 获取匹配成功的金额
     */
    public static function getBomitemMatchingAmount($bom_id=0){
        $suffix = substr(strrev($bom_id),0,1);
        $amount = self::suffix($suffix)->where(["bom_id"=>$bom_id,"status"=>1])->selectRaw("sum(price*number) as amount")->first();
        return $amount ? $amount->amount : 0;
    }


    /*
     * 获取匹配完成的数量
     */
    public static function getBomitemMatchingSucCount($bom_id=0){
        $suffix = substr(strrev($bom_id),0,1);
        return self::suffix($suffix)->where(["bom_id"=>$bom_id,"status"=>1])->count("matching_id");
    }

}