php sku库存算法服务器版本

正文开始

<p>考虑到镜片库存太大,前端js计算很吃力,所以写了一个php版本的,原理和js一样。</p><p>以下是逻辑:</p><p>前端js提交所有active的属性值,如:color_黑色、size_圆的、guige_12</p><p>服务器步骤:</p><p>取出以上的属性值数组:$attr_str。</p><p>定义2个数组 <br /></p><p>  $allAttrCollocation = [];//所有值的所有搭配 结构如:['color_黑色'=>['size_圆的','guige_12']]  // 注意这里的搭配是不带自身的color数据的,只有size和guige。<br /></p><p>  $allAttrValues = [];//所有值 用于求邻居,结构如:['color':['黑色','白色'], 'size': ['圆的','扁的'], 'guige': ['12','13'] ]<br /></p><p>查询当前商品所有的sku,遍历,以生成以上2个数组,代码:</p><p></p><p><br /></p><p></p>//定义sku组合分割符号<br />                $skuSplitstr = '[split]';<br />                //获取可选的skus<br />                $allSku = $db->getAll($skuTable, $skuTableFields, "k_spu_id={$spuId} AND k_online=1 ");<br />                $allAttrCollocation = [];//每个属性_值 的所有搭配对象<br />                $allAttrValues = [];//每个属性的所有值 用于求邻居<br />                foreach ($allSku as $v) {<br />                    if(spu::isDanjiaodianJingpian($s_gtype1, $s_gtype3)) {//单焦点镜片 输出 曲率 膜层 球镜 柱镜<br />                        $attrHashData = ["qulv_{$v['k_qulv']}", "moceng_{$v['k_moceng']}", "qiujing_{$v['k_qiujing']}", "zhujing_{$v['k_zhujing']}"];<br />                    } elseif(spu::isDuojiaodianJingpian($s_gtype1, $s_gtype3) || spu::isLidianJingpian($s_gtype1, $s_gtype3) || spu::isShuangGuangJingpian($s_gtype1, $s_gtype3)) {//多焦点/离焦点/双光 镜片 输出 曲率 膜层 球镜 柱镜<br />                        $attrHashData = [<br />                            "lr_". ($v['k_lr']==1 ? 'l':'r') ."",<br />                            "qulv_{$v['k_qulv']}",<br />                            "moceng_{$v['k_moceng']}",<br />                            "qiujing.{$v['k_lr']}_{$v['k_qiujing']}",<br />                            "zhujing.{$v['k_lr']}_{$v['k_zhujing']}",<br />                            "xiajiaguang.{$v['k_lr']}_{$v['k_xiajiaguang']}",<br />                            "tongdao.{$v['k_lr']}_{$v['k_tongdao']}"<br />                        ];<br />                    } elseif($s_gtype2 == spu::$spu_yinxing_changgui_id) {//隐形常规片 输出 颜色 规格 光度<br />                        $attrHashData = ["color_{$v['k_color']}", "guige_{$v['k_guige']}", "qiujing_{$v['k_qiujing']}"];<br />                    }  elseif($s_gtype2 == spu::$spu_yinxing_dingzhi_id) {//隐形定制片  输出 颜色 规格 光度 柱镜(定制片)<br />                        $attrHashData = ["color_{$v['k_color']}", "guige_{$v['k_guige']}", "qiujing_{$v['k_qiujing']}", "zhujing_{$v['k_zhujing']}"];<br />                    } elseif($s_gtype1 == spu::$spu_chengpin_jing_id) {//成品镜 输出 颜色 光度<br />                        $qiujing = $v['k_qiujing'] >= 0 ? '+'. $v['k_qiujing'] : '-'.$v['k_qiujing'];<br />                        $attrHashData = ["color_{$v['k_color']}", "qiujing_{$qiujing}"];<br />                    } else {<br />                        return (message::getMsgJson('0502', '分类'. $s_gtype1 .' hashData未配置'));<br />                    }<br />                    sort($attrHashData);// ['color_aa','guige_66'];<br />                    $attrHashStr = join($skuSplitstr, $attrHashData);  // color_aa|guige_66<br />                    foreach ($attrHashData as $tmpOutputAttr) {<br />                        //只取当前属性搭配的所有搭配 如当前筛选属性为:qulv_5,moceng_40 ,则只考虑这俩属性的所有搭配<br />                        if(in_array($tmpOutputAttr, $attr_str)) {<br />                            if(isset($allAttrCollocation[$tmpOutputAttr])) {<br />                                if(!in_array($attrHashStr, $allAttrCollocation[$tmpOutputAttr])) {<br />                                    $allAttrCollocation[$tmpOutputAttr][] = $attrHashStr;<br />                                }<br />                            } else {<br />                                $allAttrCollocation[$tmpOutputAttr][] = $attrHashStr;<br />                            }<br />                        }<br />                        //获取所有属性值<br />                        $attrName = explode('_', $tmpOutputAttr)[0];<br />                        if(strstr($attrName, '.')) {//带左右眼的属性是这样的: qiujing.2_0.25<br />                            $attrName = explode('.', $attrName)[0];<br />                        }<br />                        if(isset($allAttrValues[$attrName])) {<br />                            if(!in_array($tmpOutputAttr, $allAttrValues[$attrName])) {<br />                                $allAttrValues[$attrName][] = $tmpOutputAttr;<br />                            }<br />                        } else {<br />                            $allAttrValues[$attrName][] = $tmpOutputAttr;<br />                        }<br />                    }<br />                }<br />                //求出各种属性搭配 的交集<br />                $existSku = [];<br />                foreach ($attr_str as $tmpStr) {//$tmpStr color_黑色<br />                    $attrName = explode('_', $tmpStr)[0];<br />                    if(strstr($attrName, '.')) {//带左右眼的属性是这样的: qiujing.2_0.25<br />                        $attrName = explode('.', $attrName)[0];<br />                    }<br />                    $tmpSkus = $allAttrCollocation[$tmpStr]; //获取当前属性值 的所有搭配属性<br />                    $tmpStrSiblings = $allAttrValues[$attrName];//取出当前属性(如:color)和邻居的值<br />                    $tmpSkus= array_merge($tmpSkus, $tmpStrSiblings); //属性要加上本类所有属性 好让其他属性来取交集<br />                    if($existSku) {<br />                        $existSku = array_intersect($existSku, $tmpSkus);<br />                    } else {<br />                        $existSku = $tmpSkus;<br />                    }<br />                }<br />                /*<br />                 * $existSku :<br />                 * Array<br />                (<br />                [0] => moceng_73[split]qiujing_-0.75[split]qulv_6[split]zhujing_0.50<br />                [1] => moceng_73[split]qiujing_-0.75[split]qulv_6[split]zhujing_0.75<br />                */<br />                $newExistSku = [];<br />                foreach ($existSku as $index=> &$tmpExistV) {<br />                    if(strstr($tmpExistV, $skuSplitstr)) {<br />                        $newExistSku = array_merge($newExistSku, explode($skuSplitstr, $tmpExistV));<br />                    } else {<br />                        $newExistSku[] = $tmpExistV;<br />                    }<br />                }<br />                $newExistSku = array_unique($newExistSku);<br />                sort($newExistSku);<br />                return (message::getMsgJson('0306', $newExistSku));<br />         <br /><p><br /></p>前端获取成功后 将所有库存设为unabled,再将此服务器返回的sku移除unabled样式即可。<br /><p></p>

正文结束

PHP接口(interface)和抽象类(abstract) php数组批量加前缀