<?php


namespace app\service;

use app\model\Product;
use app\model\ProductBatch;
use app\model\Enterprises;
use app\model\Base;
use think\Model;


class ProductBatchService
{

    public static function getProductBatchList($page, $qydm): array
    {
        $map[]=["t_product_batch.qydm","=",$qydm];
        $res["row"] = (new ProductBatch)
                    ->where($map)
                    ->field("t_product_batch.*,t_product.product_name, t_product.product_class_id, t_enterprises.enterprises_name,
                            t_enterprises.fzr_phone, t_enterprises.enterprises_address")
                    ->page($page["page"],$page["size"])
                    ->leftJoin("t_product","t_product_batch.product_id=t_product.id")
                    ->leftJoin("t_enterprises","t_enterprises.qydm=t_product_batch.qydm")
                    ->select();
        $res["count"] = (new ProductBatch)->where($map)->count();
        return $res;
    }

    public static function getProductBatchAll($qydm)
    {
        $map[]=["t_product_batch.qydm","=",$qydm];
        $res = (new ProductBatch)
            ->where($map)
            ->field("t_product_batch.batch_id,t_product.product_name")
            ->leftJoin("t_product","t_product_batch.product_id=t_product.id")
            ->select();

        return $res;
    }

    public static function getProductBatchInfoById($id)
    {
        return ProductBatch::find($id);
    }


    public static function addProductBatch($info):bool
    {
        return (new ProductBatch)->save($info);
    }
    public static function editProductBatch($info,$id): ProductBatch
    {
        return ProductBatch::update($info,["id"=>$id]);
    }
    public static function delProductBatch($id): bool
    {
        return ProductBatch::destroy($id);
    }

    public static function findEnterprisesBaseInfo($qydm)
    {
        $map[] = ["qydm","=",$qydm];
        return Enterprises::field('qydm, enterprises_name, fzr_phone')
            ->where($map)
            ->find();
    }


    public static function selectProductBaseInfo($qydm)
    {
        $map[] = ["qydm","=",$qydm];
        return Product::field('id, product_name, product_class_id')
            ->where($map)
            ->select();
    }


    public static function selectChandiBaseInfo($qydm)
    {
        $map[] = ["qydm","=",$qydm];
        return Base::field('id, city, county, towns, village')
            ->where($map)
            ->select();
    }
    public static function getPchByProId($id){
        return   ProductBatch::where("product_id",$id)->select();
    }

}