<?php
//品牌目录
class shopAction extends baseAction
{
	//代理商地址
	function daiLiList()
	{
		$Params = mvc::$URL_PARAMS;
		if(!empty($_POST['submit']))
		{
			$B2cShop = new B2cShop();
			$addrArr = [];
			foreach($_POST['addr'] as $k=>$v)
			{
				$v['sha_pct_ids'] = implode(',', $v['province']);
				unset($v['province']);
				unset($v['lng_lat']);
				$addrArr[$k] = $v;
			}
			$B2cShop->setShopDaili($_SESSION['_shopid'], $addrArr);
			if($_POST['shop']){
				$where = sprintf(" and shop_id=%d",$_SESSION['_shopid']);
				$B2cShop->set('sh_shop',$_POST['shop'],$where);
			}
			LibTpl::Success('操作成功');
		}
		$B2cBase = new B2cBase();
		$where = sprintf(" and shop_id=%d",$_SESSION['_shopid']);
		$shopInfo = $B2cBase->get('sh_shop',['shop_id,shop_addr_type'],$where,true);
		
		$res = $B2cBase->get('sh_daili_addr', ['sha_id, sha_comname, sha_tel, sha_pct_ids, sha_addr, sha_seq'], sprintf(" and sha_shop_id = %d order by sha_seq asc", $_SESSION['_shopid']));
		$addArr = $res['data'];
		LibTpl::Set('addArr', $addArr);
		LibTpl::Set('shopInfo',$shopInfo['data']?:[]);
		LibTpl::Set('title', '代理商管理');
        LibTpl::Put();
	}

	//导入代理商
	function importShopDaili()
	{
		if(!empty($_FILES['file']['tmp_name']))
		{
			$OthExcel = new OthExcel();
			$res = $OthExcel->read($_FILES['file']['tmp_name'], 0);
			if(!empty($res['data']))
			{
				$tArr = $res['data'];
				array_shift($tArr);
				foreach($tArr as $tk=>$tv)
				{
					$temp=[
						'sha_comname'=>$tv[0],
						'sha_tel'=>$tv[1],
						'sha_addr'=>$tv[2]
					];
					if(stripos($temp['sha_comname'], 'XXX样例') !== false || empty($temp['sha_comname']) || empty($temp['sha_tel']) || empty($temp['sha_addr'])) continue;
					$addrArr[]=$temp;
				}
				$B2cShop = new B2cShop();
				$res = $B2cShop->importShopDaili($_SESSION['_shopid'], $addrArr);
				LibFc::ajaxJsonEncode($res);
			}
			LibFc::ajaxJsonEncode(['status'=>false, 'data'=>'请先在表格中录入需要导入的代理信息！']);
		}
		LibFc::ajaxJsonEncode(['status'=>false, 'data'=>'获取上传文件失败，请稍后重试！']);
	
	}


	/**
	 * @method 明达erp更新产品信息
	 * @author zhengweihua 
	 * @copyright 2023-06-09
	 */
	function updateMdErpProduct()
	{
		set_time_limit(0);
		$B2cCustom = new B2cCustom();
		$res = $B2cCustom->updateProductByCode(732634);
		$this->updateBdErpProduct();
		$this->updateTylErpProduct();
		LibFc::ReturnJson(true,$res['data']);
	}

	/**
	 * @method 宝德更新产品信息 https://sadmin.paojd.cn/shop/updateBdErpProduct
	 * @author zhengweihua 
	 * @copyright 2024-06-21
	 */
	function updateBdErpProduct()
	{
		$dyApi=[
			//01部门
			[
				'appname'=>'大有云配-广州宝德',
				'webname'=>'dy_MziGcr',
				'appkey'=>'a9fc3b72d0c045858a260b063a3ab6c6',
				'apppwd'=>'MziGcr2luEaN',
			],
			//03部门
			[
				'appname'=> '大有云配-广州宝德',
				'webname'=> 'dy_pO2vmy',
				'appkey'=> '3c633e46313a4d7f9c640da39f6d37e4',
				'apppwd'=> 'pO2vmytgnsPM'
			], 
		];
		set_time_limit(0);
		foreach ($dyApi as $config) {
			$B2cBDCustom = new B2cBDCustom($config);
			$B2cBDCustom->updateProductByCode(965832);
		}
		LibFc::ReturnJson(true);
	}


	/**
	 * @method 明达erp更新产品库存 https://sadmin.paojd.cn/shop/updateMdProCount
	 * @author zhengweihua 
	 * @copyright 2024-10-14
	 */
	function updateMdProCount()
	{
		set_time_limit(0);
		$B2cCustom = new B2cCustom();
		$res = $B2cCustom->updateProductCount(732634);
		LibFc::ReturnJson(true,$res['data']);
	}

	/**
	 * @method 天裕隆erp更新产品库存 https://sadmin.paojd.cn/shop/updateTylErpProduct
	 * @author zhengweihua 
	 * @copyright 2025-03-07
	 */
	function updateTylErpProduct()
	{
		$dyApi=[
			'appname'=> '跑街令',
			'webname'=>'PJL_TJ_TYL7f98',
			'appkey'=>'7f98831fb6824792afe357cd50fd7df0',
			'apppwd'=>'0YvfVN6jTg79'
		];
		set_time_limit(0);
		$B2cTylCustom = new B2cTylCustom($dyApi);
		$B2cTylCustom->updateProductByCode(250162);
		LibFc::ReturnJson(true);
	}
}