load->library('session');
$this->load->_model('Model_commodity','commodity');
$this->load->_model('Model_commodityread','commodityread');
$this->load->_model('Model_shop','shop');
$this->load->_model('Model_apismt','apismt');
$this->load->_model('Model_typeclass','typeclass');
$this->load->_model('Model_commodityread_del','commodityreaddel');
$this->load->_model('Model_classid','classid');
$this->load->_model("Model_logic_order","logic_order");
}
//定义方法的调用规则 获取URI第二段值
public function _remap($arg,$arg_array)
{
if($arg == 'aeorder')//同步
{
$this->_aeorder();
}
else if($arg == 'edit')//修改
{
$this->_edit($arg_array);
}
else if($arg == 'batchedit')//批量修改
{
$this->_batchedit();
}
else if($arg == 'skuedit')//修改
{
$this->_skuedit($arg_array);
}
else if($arg == 'excel_export'){
$this->_excel_export();
}
else
{
$this->_index();
}
}
//管理
public function _index()
{
$dt = 0;
if(isset($_SESSION['api']))
{
$user = $this->user->get_api($_SESSION['api']);
$usp = $user;
$fgshop = "";$sid = "";
$usersp = explode('|',trim($user['shop'],'|'));
foreach ($usersp as $value)
{
$fgshop .= " shop = ".$value." or";
$sid .= " id = ".$value." or";
}
}
$post = $this->input->post(NULL, TRUE);
if(isset($post['page']))
{
$page = $this->input->post('page',true);
$perpage = $this->input->post('perpage',true);
$shop = $this->input->post('shop',true);
$productid = $this->input->post('productid',true);
$title = $this->input->post('title',true);
$type = $this->input->post('type',true);
$code = $this->input->post('code',true);
$skuid = $this->input->post('skuid',true);
$category = $this->input->post('category',true);
$categorytypeclass = $this->typeclass->get_titleclassid($category,16);
$where = "1=1 and (".rtrim($fgshop,'or').")";
if($shop)
{
$where .= " and shop = '$shop'";
}
if($productid)
{
$where .= " and productid = '$productid'";
}
if($title)
{
$where .= " and title like '%$title%'";
}
if($type)
{
$where .= " and type = '$type'";
}
if($category)
{
$where .= " and category like '%".$categorytypeclass['id']."%'";
}
if($code)
{
$u = $this->commodityread->get_sku($code);
$u = $u['productid'];
$where .= " and productid = '$u'";
}
if($skuid)
{
$u = $this->commodityread->get_skuid($skuid);
$u = $u['productid'];
$where .= " and productid = '$u'";
}
//数据排序
$order_str = "time desc";
if(empty($page))
{
$start = 0;
$perpage = 1;
}
else
{
$start = ($page - 1)*$perpage;
}
//取得信息列表
$info_list = $this->commodity->find_all($where,'id,shop,productid,img,title,category,time',$order_str,$start,$perpage);
//格式化数据
foreach ($info_list as $key=>$v)
{
$shop = $this->shop->read($v['shop']);
$info_list[$key]['shop'] = $shop['shopname'];
$info_list[$key]['img'] = "
";
$category = '';
if($v['category'])
{
$v['category'] = explode(',',trim($v['category'],','));
foreach ($v['category'] as $vv)
{
$typeclass = $this->typeclass->read($vv);
$category .= "
".$typeclass['title']."
";
}
$info_list[$key]['category'] = $category;
}
else
{
$info_list[$key]['category'] = '';
}
$info_list[$key]['time'] = "编辑
浏览
";
}
$total = $this->commodity->find_count($where);
$pagenum = ceil($total/$perpage);
$over = $total-($start+$perpage);
$rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
echo json_encode($rows);exit;
}
$wlshop = $this->shop->find_all('1=1 and '.rtrim($sid,'or'));
$this->data['wlshop'] = $wlshop;
$this->_Template('commodity',$this->data);
}
//修改
public function _edit($arg_array)
{
$post = $this->input->post(NULL, TRUE);
if(isset($post['id']))
{
$id = $this->input->post('id',true);
$category = $this->input->post('category',true);
$data = $this->input->post('data');
if($data)
{
$data = explode('|',rtrim($data,'|'));
foreach ($data as $v)
{
$d = explode('@',$v);
$this->commodityread->save(array('sku'=>$d[1]),$d[0]);
}
}
if($this->commodity->save(array('category'=>','.$category),$id))
{
echo json_encode(array('msg'=>'操作成功','success'=>true));exit;
}
else
{
echo json_encode(array('msg'=>'操作失败,请重试','success'=>false));exit;
}
}
$arg_array = $arg_array[0];
$commodity = $this->commodity->read($arg_array);
$this->data['commodity'] = $commodity;
$sku = $this->commodityread->find_all('productid = '.$commodity['productid']);
$this->data['sku'] = $sku;
$this->_Template('commodity_edit',$this->data);
}
public function _batchedit_b()//老系统形式,保留,不用
{
$dictionaries = $this->typeclass->find_all('ae!= "" and (classid=13 or classid=22 or classid=8 or classid=15 or classid=27 or classid=25 or classid=26 or classid=18 or classid=14 or classid=9 or classid=12 or classid=10 or classid=6)'); //产品人发类型颜色
$dtc = array();$ctd = array();
foreach ($dictionaries as $v)
{
if(stripos($v['spare'],'|') !== false)//如果有多个值
{
$v['spare'] = explode('|',$v['spare']);
foreach ($v['spare'] as $vs)
{
$dtc[strtolower($vs)] = array('classid'=>$v['classid'],'title'=>$v['title'],'id'=>$v['id']);
}
}
else
{
$dtc[strtolower($v['spare'])] = array('classid'=>$v['classid'],'title'=>$v['title'],'id'=>$v['id']);
}
$ctd[$v['id']] = $v['title'];
}
$ctd[126] = 'Hair Weaving';
$ctd[127] = 'Closure';
$ctd[128] = 'Wigs';
$ctd[130] = 'Clip-in Full Head';
$post = $this->input->post(NULL, TRUE);
if(isset($post['id']))
{
$id = $this->input->post('id',true);
$ndata = $this->input->post('data',true);
$id = explode(',',rtrim($id,','));
$data = explode('-',rtrim($ndata,'-'));
$rows = array();
foreach ($id as $val)
{
//a:颜色 b:密度 c:头路设计
$u = $this->commodityread->read($val);
$u['codeid'] = strtolower($u['codeid']);
$u['codeid'] = str_replace(array('stretched length','inches','& '),array('length','',''),$u['codeid']);
$sku = explode(';',rtrim($u['codeid'],';'));
$codeid = array();$whlabel = ''; $fpdata = '';
foreach ($sku as $v)
{
$va = explode(':',$v);
$codeid[$va[0]] = $va[1];
}
$num = '';$ti = '';$tit = '';$error = '';$i = 1;
foreach ($data as $k=>$v)
{
if($v == 'a')
{
if(isset($codeid['color']) && isset($dtc[$codeid['color']]))
{
$num .= $dtc[$codeid['color']]['id'].'-';
$ti .= $dtc[$codeid['color']]['title'].' ';
}
else
{
$num .= '57-';
$ti .= 'Natural Black ';
}
}
else if($v == 'b')
{
if(isset($codeid['density']) && isset($dtc[$codeid['density']]))
{
$num .= $dtc[$codeid['density']]['id'].'-';
$ti .= $dtc[$codeid['density']]['title'].' ';
}
else
{
$num .= '71-';
$ti .= '150% ';
}
}
else if($v == 'c')
{
if(isset($codeid['part design']) && isset($dtc[$codeid['part design']]))
{
$num .= $dtc[$codeid['part design']]['id'].'-';
$ti .= $dtc[$codeid['part design']]['title'].' ';
}
else
{
$num .= '75-';
$ti .= 'Free Part ';
}
}
else
{
if($i == 2)
{
$num .= $v.'-id-';
}
else
{
$num .= $v.'-';
}
$ti .= $ctd[$v].' ';
}
$i++;
}
if(stripos($codeid['length'],'closure') !== false && stripos($ndata,'127') !== false)
{
$v = preg_replace(array('/([\s\S]*)closure/','/([\s\S]*)closure\s/'),array('',''),$codeid['length']);
$v = preg_replace('/([\s]*)/','',$v);
$fpdata = $dtc[$v]['id'].',-'.str_replace('id-','',$num).'|'.$ti.$v.'inch |1|0.00|0.00|'.$v.'|0.00|0.00;';
$tit = ''.$ti.$v.'inch
';
$whlabel = '|'.str_replace(array('-','id'),array('',$dtc[$v]['id']),$num).'-1';
}
else
{
$codeid['length'] = explode(' ',$codeid['length']);
foreach ($codeid['length'] as $v)
{
if(is_numeric($v))
{
$fpdata .= $dtc[$v]['id'].',-'.str_replace('id-','',$num).'|'.$ti.$v.'inch |1|0.00|0.00|'.$v.'|0.00|0.00;';
$tit .= ''.$ti.$v.'inch
';
$whlabel .= '|'.str_replace(array('-','id'),array('',$dtc[$v]['id']),$num).'-1';
}
else
{
break;
}
}
}
$fa = '';$wl = '';
$u['fpdata'] = explode(';',rtrim($u['fpdata'],';'));
$u['whlabel'] = explode('|',trim($u['whlabel'],'|'));
for($i=0;$i';
}
}
if(stripos($u['whlabel'][$i],$data[0]) === false)
{
if($u['whlabel'][$i] != '')
{
$wl .= '|'.$u['whlabel'][$i];
}
}
}
$this->commodityread->save(array('fpdata'=>$fa.$fpdata,'whlabel'=>$wl.$whlabel.'|'),$val);
$rows[] = array('id'=>$val,'title'=>$tit);
}
echo json_encode(array('rows'=>$rows,'success'=>true));exit;
}
}
public function _skuedit($arg_array)
{
$post = $this->input->post(NULL, TRUE);
if(isset($post['id']))
{
$id = $this->input->post('id',true);
$post['fpdata'] = $this->input->post('fpdata',true);
$post['whlabel'] = $this->input->post('whlabel',true);
if($this->commodityread->save($post,$id))
{
echo json_encode(array('msg'=>'操作成功','success'=>true));exit;
}
else
{
echo json_encode(array('msg'=>'操作失败,请重试','success'=>false));exit;
}
}
$arg_array = $arg_array[0];$fpdata = array();
$commodityread = $this->commodityread->read($arg_array);
if(stripos($commodityread['fpdata'],';') !== false)
{
$fpdata = explode(';',rtrim($commodityread['fpdata'],';'));
foreach ($fpdata as $k=>$v)
{
$fpdata[$k] = explode('|',$v);
}
}
$this->data['fpdata'] = $fpdata;
$this->data['commodityread'] = $commodityread;
$this->_Template('commodityread_edit',$this->data);
}
//获取
public function _aeorder()
{
$sid = '';
if(isset($_SESSION['api']))
{
$user = $this->user->get_api($_SESSION['api']);
$user = explode('|',trim($user['shop'],'|'));
foreach ($user as $value)
{
$sid .= " id = '$value' or";
}
$sid = " and (".rtrim($sid,'or').")";
}
$post = $this->input->post(NULL, TRUE);
if($sid)
{
$shop = $this->shop->find_all("code != '' and type = '270'".$sid);//获取店铺信息
foreach ($shop as $value)
{
$res = array();
for($x=1;$x<5;$x++)
{
$resi = $this->apismt->get_commoditylist($x,100,$value['code']);//获取商品列表
if($resi)
{
$res += $resi;
}
}
if($res)
{
$yzpid = $this->commodity->find_all('shop = '.$value['id']);
foreach ($yzpid as $v)
{
if($v['productid'] == '' || $v['productid'] == '0')
{
$this->commodity->remove($v['id']);
continue;
}
if(!isset($res[$v['productid']]))
{
$this->commodity->save(array('type'=>'offline'),$v['id']);
}
}
foreach ($res as $v)
{
$productid = $this->commodity->get_productid($v);
//if(!$productid)
//{
@$read = $this->apismt->get_commodityread($v,$value['code']);
if(is_array($read))
{
@$cid = $this->apismt->get_commoditysku($read['cid'],$value['code']);
}
else
{
continue;
}
//处理结束
if(isset($cid[0]))
{
// 处理 $cid
$cc = array();
foreach ($cid as $c)
{
$names = json_decode($c['names'],true);
$cc[$c['id']] = $names['en'];
if(!isset($c['values']))
{
continue;
}
if(isset($c['values']['aeop_attr_value_dto'][0]))
{
foreach ($c['values']['aeop_attr_value_dto'] as $cv)
{
if(isset($read['gg'][$cv['id']]))//如果有自定义
{
$cc[$cv['id']] = $read['gg'][$cv['id']];
}
else
{
$cnames = json_decode($cv['names'],true);
$cc[$cv['id']] = $cnames['en'];
}
}
}
else
{
$cnames = json_decode($c['values']['aeop_attr_value_dto']['names'],true);
$cc[$c['values']['aeop_attr_value_dto']['id']] = $cnames['en'];
}
}
$read['title'] = ($read['title'])?$read['title']:'';
if(!$productid)
{
//处理结束
$this->commodity->insert(array('shop'=>$value['id'],'productid'=>$v,'title'=>$read['title'],'img'=>$read['img'],'type'=>$read['type'],'time'=>time()));
}
else
{
$this->commodity->save(array('shop'=>$value['id'],'productid'=>$read['productid'],'title'=>$read['title'],'img'=>$read['img'],'type'=>$read['type']),$productid['id']);
}
$this->_aeordersku($read['sku'],$value['id'],$v,$cc);
}
//}
}
}
else
{
continue;
}
}
echo json_encode(array('msg'=>'同步完成!','success'=>true));exit;
}
}
public function _aeordersku($sku,$shop,$productid,$cc)
{
$yzsku = $this->commodityread->find_all("productid = '$productid'");
foreach ($yzsku as $v)
{
$s = 0;
if(!isset($sku[$v['skuid']]))
{
$skuid = explode(';',$v['skuid']);
if(count($skuid) < 2)
{
$this->commodityreaddel->insert($v);
$this->commodityread->remove($v['id']);//asd
continue;
}
else
{
$skuidarray = $this->_qpl($skuid);
foreach ($skuidarray as $skuv)
{
if(isset($sku[$skuv]))
{
$this->commodityread->save(array('skuid'=>$skuv),$v['id']);
$s++;
break 1;
}
}
}
if($s < 1)
{
$this->commodityreaddel->insert($v);
$this->commodityread->remove($v['id']);//asd
}
}
}
foreach ($sku as $v)
{
$read = $this->commodityread->get_productid($productid,$v['skuid']);
if(isset($read['productid']))
{
$cd = explode(';',$v['skuid']);$cod = '';
foreach ($cd as $vv)
{
$cv = explode(':',$vv);
$cod .=$cc[$cv[0]].":".$cc[$cv[1]].";";
}
$this->commodityread->save(array('shop'=>$shop,'productid'=>$productid,'codeid'=>$cod,'skuid'=>$v['skuid'],'code'=>$v['code'],'time'=>time()),$read['id']);
}
else
{
$cd = explode(';',$v['skuid']);$cod = '';
foreach ($cd as $vv)
{
$cv = explode(':',$vv);
$cod .=$cc[$cv[0]].":".$cc[$cv[1]].";";
}
$this->commodityread->insert(array('shop'=>$shop,'productid'=>$productid,'codeid'=>$cod,'skuid'=>$v['skuid'],'code'=>$v['code'],'time'=>time()));
}
}
}
public function _qpl($source)//全排列
{
$qpl = array();
sort($source); //保证初始数组是有序的
$last = count($source) - 1; //$source尾部元素下标
$x = $last;
$count = 1; //组合个数统计
$qpl[implode(';', $source)] = implode(';', $source); //输出第一种组合
while (true)
{
$y = $x--; //相邻的两个元素
if ($source[$x] < $source[$y])//如果前一个元素的值小于后一个元素的值
{
$z = $last;
while ($source[$x] > $source[$z]) //从尾部开始,找到第一个大于 $x 元素的值
{
$z--;
}
/* 交换 $x 和 $z 元素的值 */
list($source[$x], $source[$z]) = array($source[$z], $source[$x]);
/* 将 $y 之后的元素全部逆向排列 */
for ($i = $last; $i > $y; $i--, $y++)
{
list($source[$i], $source[$y]) = array($source[$y], $source[$i]);
}
$qpl[implode(';', $source)] = implode(';', $source); //输出组合
$x = $last;
$count++;
}
if ($x == 0)//全部组合完毕
{
break;
}
}
return $qpl;
}
public function _skudelhf($a)//commodityread 同步删除的恢复
{
$this->db->trans_begin();
$qbpid = array();
$a = $this->commodityread->find_all("time > '1653613200'");
foreach ($a as $v)
{
$qbpid[$v['productid']] = $v['productid'];
}
foreach ($qbpid as $valer)
{
$del = $this->commodityreaddel->find_all("productid = '".$valer."' and sku != ''",'*','id asc');
if($del)
{
foreach ($del as $val)
{
$skuid = explode(';',$val['skuid']);
if(count($skuid) < 2)
{
continue;
}
$skuidarray = $this->_qpl($skuid);
$red = $this->commodityread->find_all("productid = '".$val['productid']."' and sku = ''");
foreach ($red as $v)
{
if(isset($skuidarray[$v['skuid']]))
{
$this->commodityread->save(array('sku'=>$val['sku']),$v['id']);
}
}
}
}
}
if ($this->db->trans_status() === TRUE)
{
$this->db->trans_commit();
echo 1;
}
else
{
$this->db->trans_rollback();
echo 2;
}
}
public function _excel_export(){
if(isset($_SESSION['api']))
{
$user = $this->user->get_api($_SESSION['api']);
$usp = $user;
$fgshop = "";$sid = "";
$usersp = explode('|',trim($user['shop'],'|'));
foreach ($usersp as $value)
{
$fgshop .= " shop = ".$value." or";
$sid .= " id = ".$value." or";
}
}
$params = $this->input->get(NULL, TRUE);
if(isset($params['excel']) && $params['excel'] == 1){
$shop = $this->input->get('shop',true);
$productid = $this->input->get('productid',true);
$title = $this->input->get('title',true);
$type = $this->input->get('type',true);
$code = $this->input->get('code',true);
$skuid = $this->input->get('skuid',true);
$category = $this->input->get('category',true);
$categorytypeclass = $this->typeclass->get_titleclassid($category,16);
$where = "1=1 and (".rtrim($fgshop,'or').")";
if($shop)
{
$where .= " and shop = '$shop'";
}
if($productid)
{
$where .= " and productid = '$productid'";
}
if($title)
{
$where .= " and title like '%$title%'";
}
if($type)
{
$where .= " and type = '$type'";
}
if($category)
{
$where .= " and category like '%".$categorytypeclass['id']."%'";
}
if($code)
{
$u = $this->commodityread->get_sku($code);
$u = $u['productid'];
$where .= " and productid = '$u'";
}
if($skuid)
{
$u = $this->commodityread->get_skuid($skuid);
$u = $u['productid'];
$where .= " and productid = '$u'";
}
//数据排序
$order_str = "time desc";
//取得信息列表
$info_list = $this->commodity->find_all($where,'id,shop,productid,img,title,category,time',$order_str);
//格式化数据
$final_list = [];
$shop_where = trim($sid,'or');
$shop_list = $this->shop->find_all($shop_where,'id,shopname');
$shop_list = array_column($shop_list,'shopname','id');
$typeclass = [];
$classid = $this->classid->sku();
$tcall = $this->typeclass->find_all();
foreach ($tcall as $v)
{
//$tcjm[$v['id']] = array($v['jm'],$v['classid']);
$typeclass[$v['id']] = array('zh'=>$v['zh'],'classid'=>$v['classid'],'bm'=>$v['bm'],'title'=>$v['title'],'jm'=>$v['jm']);
}
$dtctitle = [];
foreach($tcall as $v){
$dtctitle[$v['id']] = $v['title'];
}
$dtc = [];
foreach ($tcall as $v) {
if (stripos($v['spare'], '|') !== false) //如果有多个值
{
$v['spare'] = explode('|', $v['spare']);
foreach ($v['spare'] as $k => $vs) {
if (stripos($v['zh'], '|') !== false) {
$vzh = explode('|', $v['zh']);
$dtc[strtolower($vs)] = array('classid' => $v['classid'], 'zh' => $vzh[$k], 'id' => $v['id'], 'jm' => $v['jm'], 'spare' => $vs);
} else {
$dtc[strtolower($vs)] = array('classid' => $v['classid'], 'zh' => $v['zh'], 'id' => $v['id'], 'jm' => $v['jm'], 'spare' => $vs);
}
}
} else {
$dtc[strtolower($v['spare'])] = array('classid' => $v['classid'], 'zh' => $v['zh'], 'id' => $v['id'], 'jm' => $v['jm'], 'spare' => $v['spare']);
}
}
foreach ($info_list as $key=>$v)
{
$shopname = isset($shop_list[$v['shop']])?$shop_list[$v['shop']]:'';
$skulist = $this->commodityread->find_all("productid = ".$v['productid']);
$tmp_list = [];
foreach($skulist as $skv){
$tmp_sku = str_replace('--','-',$skv['sku']);
$u9_info = $this->logic_order->skuTransferFeatures($tmp_sku,$typeclass,$dtc,$dtctitle,$classid);
$tmp_list[] = [
'codeid' => $skv['codeid'],
'erp_sku' => $skv['sku'],
'jm'=>$u9_info['jm'],
'zh'=>$u9_info['zh'],
];
}
$tmp_codeid = "";
$tmp_erp_sku = "";
$tmp_jm = "";
$tmp_zh = "";
foreach($tmp_list as $t){
$tmp_codeid .= $t['codeid']."
";
$tmp_erp_sku .= $t['erp_sku']."
";
$tmp_jm .= $t['jm']."
";
$tmp_zh .= $t['zh']."
";
}
$final_list[] = [
'shopname' => $shopname,
'productid' => "'".$v['productid'],
'codeid' => $tmp_codeid,
'erp_sku' => $tmp_erp_sku,
'jm' => $tmp_jm,
'zh' => $tmp_zh,
'title' => $v['title'],
];
}
//生成excel
$filename = '速卖通-'.date('Ymd',time()).".xlsx";
$str = "
";
$str .= "";
$str .= "店铺名称 | 速卖通产品编码 | 速卖通标题 | 速卖通SKU名称 | erpSKU | 用友简码 | 仓库品名 |
";
foreach ($final_list as $key=>$value)
{
$str .= "";
$str .= "". $value['shopname']. " | ";
$str .= "". $value['productid']. " | ";
$str .= "". $value['title']. " | ";
$str .= "". $value['codeid']. " | ";
$str .= "". $value['erp_sku']. " | ";
$str .= "". $value['jm']. " | ";
$str .= "". $value['zh']. " | ";
$str .= "
";
}
$str .= "
";
header( "Content-Type: application/vnd.ms-excel; name='excel'" );
header( "Content-type: application/octet-stream" );
header( "Content-Disposition: attachment; filename=".$filename );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Pragma: no-cache" );
header( "Expires: 0" );
exit($str);
}
}
}