<?php
namespace api\action\v1;

use api\action\baseAction;
use model\pro\pro;
use model\pro\product;

/**
 * Class proAction
 * @package api\action\v1
 */
class proAction extends baseAction
{
	/**
     * 获取商品同步日志
	 * @param  $mod  string [选填] pro:商品 procom:事业部商品关联
	 * @param  $start_time  string [必填] 开始时间 
	 * @param  $end_time  string [选填] 结束时间
     */
    public function getSynLog()
    {
		set_time_limit(0);
        try {
            $params = $this->getParaam();
			if(empty($params['start_time'])){
                 $this->returnJson(500,'start_time必填');
            }
			$product = new product();
			$data = $product->getSynLog($params);
			return $this->returnJson(200,'ok',$data);
		}catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}
	
	
	/**
     * 菲尔科技商品信息【同步专用】
	 * @param  $p_id  int [选填] 商品ID, 多个ID用英文逗号分隔
     */
    public function getProductInfoBySyn()
    {
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);
        try {
            $params = $this->getParaam();
			$product = new product();
			$data = $product->getProductInfoBySyn(explode(',', $params['p_id']));
			return $this->returnJson(200,'ok',$data);
		}catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}

	/**
     * 根据事业部物料id获取相关信息
	 * @param  $plc_id  int [必填] 事业部商品ID, 多个ID用英文逗号分隔
     */
	public function getProductComLink()
    {
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);
        try {
            $params = $this->getParaam();
			$pro = new pro();
			$data = $pro->getProductComLink(explode(',', $params['plc_id']));
			return $this->returnJson(200,'ok',$data);
		}catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}

	/**
     * 国际车型库
	 * @param  $page  int [必填] 第几页
	 * @param  $page_size  int [选填] 每页数量 默认 500
     */
    public function getModel()
    {
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);
        try {
			$params = $this->getParaam();
			$product = new product();
			$data = $product->getModel($params);
			return $this->returnJson(200,'ok',$data);
		}catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}


	/**
     * 中国车型库
	 * @param  $page  int [必填] 第几页
	 * @param  $page_size  int [选填] 每页数量 默认 500
     */
    public function getCnModel()
    {
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);
        try {
			$params = $this->getParaam();
			$product = new product();
			$data = $product->getCnModel($params);
			return $this->returnJson(200,'ok',$data);
		}catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}


	/**
     * 小类
     */
    public function getAllCate()
    {
        try {
			$product = new product();
			$data = $product->getAllCate();
			return $this->returnJson(200,'ok',$data);
		}catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}


	/**
     * 获取关联属性
	 * @param  $o_parent_id  int [必填] 
     */
    public function getObject()
    {
        try {
			$params = $this->getParaam();
			$product = new product();
			$data = $product->getObject($params['o_parent_id']);
			return $this->returnJson(200,'ok',$data);
		}catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}


	public function getTbBarnd()
    {
        try {
			$product = new product();
			$data = $product->getTbBarnd();
			return $this->returnJson(200,'ok',$data);
		}catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}

	public function getPbBarnd()
    {
        try {
			$product = new product();
			$data = $product->getPbBarnd();
			return $this->returnJson(200,'ok',$data);
		}catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}

	/**
	 * @method 同步产品图片
	 * @param string $comnum ,多个用,分隔
	 * @author zhengweihua 
	 * @copyright 2025-05-22
	 */
	public function synProductImg()
	{	
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);
		try {
			
			$params = $this->getParaam();
			if(!$params['p_comnum']){
				return $this->returnJson(0,'参数错误',[]);
			}
			$pro = new pro();
			$data = $pro->synPic([],$params['p_comnum']?explode(',', $params['p_comnum']):[]);
			return $this->returnJson(200,'ok',$data);
		} catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}


	/**
	 * @method 同步产品
	 * @param Array $links ,关联数组 [[product_id=>1,link_id=>1]]
	 * @author zhengweihua 
	 * @copyright 2025-06-25
	 */
	public function synProductByLink()
	{	
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);
		try {
			$params = $this->getParaam();
			if(!$params['links']){
				return $this->returnJson(0,'参数错误',[]);
			}
			$pro = new pro();
			$data = $pro->synPro('',$params['links']);
			return $this->returnJson(200,'ok',$data);
		} catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}


	/**
	 * @method 同步事业部商品
	 * @author zhengweihua 
	 * @copyright 2025-07-17
	 */
	public function synCompanyImg()
	{
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);
		try {
			$params = $this->getParaam();
			if(!$params['frey_nos']){
				return $this->returnJson(0,'参数错误',[]);
			}
			if(!is_array($params['frey_nos'])){
				$frey_nos = explode(',',$params['frey_nos']);
			}else{
				$frey_nos = $params['frey_nos'];	
			}
			$pro = new pro();
			$data = $pro->synCompanyPic('',$frey_nos);
			return $this->returnJson(200,'ok',$data);
		} catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
	}
	/**
	 * @method 更新商品基础信息
	 * @param string $innum 物料编码,多个用,分隔 
	 * @param float $p_weight 重量
	 * @param string $p_weight_unit 重量单位
	 * @author zhengweihua 
	 * @copyright 2025-12-18
	 */
	public function updateProBase()
	{
		
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);
		try {
			$params = $this->getParaam();
			if(!$params['innum']){
				return $this->returnJson(0,'参数错误',[]);
			}
			$product = new product();
			$data = $product->updateProBase($params);
			return $this->returnJson(200,'ok',$data);
		} catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
		} 
	}

	/**
	 * @method 更新包材类型基础信息
	 * @param string $innum 物料编码,多个用,分隔 
	 * @param float $pam_weight 重量
	 * @param string $pam_weight_unit 重量单位
	 * @author zhengweihua 
	 * @copyright 2025-12-18
	 */
	public function updateMaterialBase()
	{
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);

		try {
			$params = $this->getParaam();
			if(!$params['innum']){
				return $this->returnJson(0,'参数错误',[]);
			}
			$product = new product();
			$data = $product->updateMaterialBase($params);
			return $this->returnJson(200,'ok',$data);
		} catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
		} 
	}
	/**
	 * @param  $page  int [必填] 第几页
	 * @param  $page_size  int [选填] 每页数量 默认 500
	 * @author zhengweihua 
	 * @copyright 2026-02-27
	 */
	public function getMaterialList()
	{
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);

		try {
			$params = $this->getParaam();
			$product = new product();
			$data = $product->getMaterialList($params);
			return $this->returnJson(200,'ok',$data);
		} catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
		} 
	}

	public function getProPackList()
	{
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);

		try {
			$params = $this->getParaam();
			$product = new product();
			$data = $product->getProPackList($params);
			return $this->returnJson(200,'ok',$data);
		} catch (Exception $e) {
			$this->returnJson(500,$e->getMessage());
		}
	}
}