生成规格商品相关操作

发布时间:2018-10-18 10:21:53 作者:Aoe Wide 阅读量:963

/******************************************************** 生成规格相关操作 *****************************************************************/
//商品规格组合(插入新数组)
public function arrangementN($arr, $new)
{
if ($new == []) {
foreach ($arr as $key => $value) {
$new[] = $value;
}
return $new;
} else {
foreach ($new as $key => $value) {
foreach ($arr as $k => $v) {
$news[] = $value.' '.$v;
}
}
return $news;
}
}

//商品规格组合(递归)
public function arrangementD($arr)
{
$new = [0=>[]];
foreach ($arr as $key => $value) {
$keys = [];
foreach ($new as $kk => $vv) {
foreach ($value as $k => $v) {
$n = $vv?$vv.' ':'';
$keys[] = $n.$v;
}
}
$new = $keys;
}
return $new;
}

//生成商品
// $arr = [
// [1,2,3,4,5],
// [1,2,3,4,5],
// [1,2,3,4,5],
// [1,2,3,4,5]
// ];
public function goods_norms_param()
{
if ($this->checkToekn()['code'] == 400) {
return json($this->checkToekn());
}
$param = input('param.');
$paramArr=[];
foreach ($param['checkArr'] as $key => $value) {
foreach ($value['params'] as $k => $v) {
if($v['isCheck'] == 1){
$paramArr[$key][] = $v['name'];
}
}
}

$goodsinfo = db('goods')->where('goods_id',$param['goods_id'])->find();

if($paramArr){
//递归
$new = $this->arrangementD($paramArr);
$returnArr = [];
foreach ($new as $key => $value) {
$where = [
'gid' => $param['goods_id'],
'sub_name' => $value
];
$info = db('goods_list')->where($where)->find();
if(!$info){
$returnArr[] = [
'gid' => $param['goods_id'],
'name' => $goodsinfo['goods_name'],
'price' => 0,
'sub_name' => $value,
'stock' => 0,
'id' => 0,
'sales' => 1,
'hot' => 1,
'new' => 1,
'recom' => 1,
];
}else{
$returnArr[] = $info;
}
}
return json(['data'=>$returnArr]);
}

//循环
// $new = [];
// foreach ($arr as $key => $value) {
// $new = $this->arrangementN($value, $new);
// }

}


//检测规格
public function goods_norms_check($id)
{
$norms = db('goods_norms')->select();
$goods = db('goods')->where('goods_id', $id)->find();
$list = db('goods_list')->where('gid', $id)->select();
if ($list) {
$normsarray = [];
foreach ($norms as $key => $value) {
$normsarray = array_merge(json_decode($value['params']), $normsarray);
}
$list = array_column($list, 'sub_name');
$listarray = [];
foreach ($list as $key => $value) {
$listarray = array_merge(explode(' ', $value), $listarray);
}
$listarray = array_unique($listarray);
$intersect = array_intersect($normsarray, $listarray);
// return $intersect;
$newnorsm = [];
foreach ($norms as $key => $value) {
$norms[$key]['params'] = json_decode($value['params']);
}
foreach ($norms as $key => $value) {
foreach ($value['params'] as $k => $v) {
$vs = [];
$vs['name'] = $v;
if(in_array($v,$intersect)){
$vs['isCheck'] = 1;
}else{
$vs['isCheck'] = 0;
}
$norms[$key]['params'][$k] = $vs;
}
}
foreach ($norms as $key => $value) {
$norms[$key]['params'] = $value['params'];
}
return array_reverse($norms);
}
return [];
}

//删除旧规格
public function goods_norms_delete($id,$arr){
$list = db('goods_list')->where('gid',$id)->select();
$old = array_column($list,'id');
$new = array_column($arr,'id');
$diff = array_diff($old,$new);
if($diff){
$where['id'] = ['in',$diff];
db('goods_list')->where($where)->delete();
}
}

/******************************************************** 生成规格相关操作 *****************************************************************/

我要评论

Aoe WideAoe Wide   我要留言
Aoe Wide
Catfish(鲶鱼) Blog V 2.0.30