|
@@ -0,0 +1,311 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Goods\Lib;
|
|
|
+
|
|
|
+use Dever;
|
|
|
+
|
|
|
+class Sku
|
|
|
+{
|
|
|
+
|
|
|
+ private $col = array
|
|
|
+ (
|
|
|
+ 'f_price' => '销售价',
|
|
|
+ 's_price' => '市场价',
|
|
|
+ 'price' => '进货价',
|
|
|
+ );
|
|
|
+
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+
|
|
|
+ Dever::load('manage/auth.init');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function config()
|
|
|
+ {
|
|
|
+ $data['action'] = Dever::url('goods/lib/sku.update');
|
|
|
+ $data['goods_id'] = Dever::input('goods_id');
|
|
|
+ $data['other'] = Dever::input('other');
|
|
|
+ $data['other_id'] = Dever::input('other_id');
|
|
|
+ if ($data['other']) {
|
|
|
+ $data['other_col'] = $data['other'] . '_id';
|
|
|
+ }
|
|
|
+
|
|
|
+ $data['attr'] = Dever::input('attr');
|
|
|
+ $data['key'] = Dever::input('key');
|
|
|
+ $data['hook'] = Dever::input('hook');
|
|
|
+
|
|
|
+ $data['col'] = $this->getCol($data['other']);
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getCol($other)
|
|
|
+ {
|
|
|
+ if ($other) {
|
|
|
+ if (Dever::project($other)) {
|
|
|
+ $col = Dever::load($other . '/lib/sku')->col;
|
|
|
+ } else {
|
|
|
+ Dever::alert($other . '不存在');
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $col = $this->col;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $col;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function show()
|
|
|
+ {
|
|
|
+ $config = Dever::load('goods/lib/sku')->config();
|
|
|
+
|
|
|
+ return $this->table($config['goods_id'], $config['other'], $config['other_id'], $config['col'], true);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function update_api()
|
|
|
+ {
|
|
|
+ $config = Dever::load('goods/lib/sku')->config();
|
|
|
+
|
|
|
+ $where = array();
|
|
|
+ if ($config['other']) {
|
|
|
+ $where[$config['other_col']] = $config['other_id'];
|
|
|
+ $where['goods_id'] = $config['goods_id'];
|
|
|
+ $key = 'sku_id';
|
|
|
+ $table = $config['other'] . '/goods';
|
|
|
+ } else {
|
|
|
+ $where['info_id'] = $config['goods_id'];
|
|
|
+ $key = 'key';
|
|
|
+ $table = 'goods/info';
|
|
|
+ }
|
|
|
+
|
|
|
+ $sku_table = $table . '_sku';
|
|
|
+
|
|
|
+ if ($config['key'] == -1) {
|
|
|
+
|
|
|
+ if ($config['other']) {
|
|
|
+ $info = Dever::db($table)->one($where);
|
|
|
+ } else {
|
|
|
+ $info = Dever::db($table)->one($config['goods_id']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $update = $where;
|
|
|
+ foreach ($config['col'] as $k => $v) {
|
|
|
+ $update[$k] = Dever::input($k);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($info) {
|
|
|
+ $update['where_id'] = $info['id'];
|
|
|
+ Dever::db($table)->update($update);
|
|
|
+
|
|
|
+ unset($update['where_id']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $where[$key] = -1;
|
|
|
+
|
|
|
+ $info = Dever::db($sku_table)->one($where);
|
|
|
+
|
|
|
+ $update[$key] = $where[$key];
|
|
|
+ Dever::config('base')->hook = false;
|
|
|
+ if (!$info) {
|
|
|
+ $state = Dever::db($sku_table)->insert($update);
|
|
|
+ } else {
|
|
|
+ $update['where_id'] = $info['id'];
|
|
|
+ $state = Dever::db($sku_table)->update($update);
|
|
|
+ }
|
|
|
+
|
|
|
+ } elseif ($config['key']) {
|
|
|
+
|
|
|
+ if (!is_array($config['key'])) {
|
|
|
+ $config['key'] = explode(',', $config['key']);
|
|
|
+ }
|
|
|
+ foreach ($config['key'] as $k => $v) {
|
|
|
+ if (isset($config['attr'][$k])) {
|
|
|
+
|
|
|
+ $data = $where;
|
|
|
+ if ($config['other']) {
|
|
|
+ $sku = Dever::db('goods/info_sku')->one(array('key' => $v));
|
|
|
+ if (!$sku) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $data[$key] = $sku['id'];
|
|
|
+
|
|
|
+ $info = Dever::db($sku_table)->one($data);
|
|
|
+ } else {
|
|
|
+ $info = Dever::db('goods/info_sku')->one(array('key' => $v));
|
|
|
+ $data['attr'] = $config['attr'][$k];
|
|
|
+ $data[$key] = $v;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($config['col'] as $k1 => $v1) {
|
|
|
+ $value = Dever::input($k1);
|
|
|
+ if (isset($value[$k])) {
|
|
|
+ $data[$k1] = $value[$k];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Dever::config('base')->hook = false;
|
|
|
+ if (!$info) {
|
|
|
+ Dever::db($sku_table)->insert($data);
|
|
|
+ } else {
|
|
|
+ $data['where_id'] = $info['id'];
|
|
|
+ Dever::db($sku_table)->update($data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Dever::config('base')->hook = true;
|
|
|
+
|
|
|
+ Dever::out('yes');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function table($goods_id, $other = false, $other_id = false, $col = false, $update = true)
|
|
|
+ {
|
|
|
+ if (!$col) {
|
|
|
+ $col = $this->getCol($other);
|
|
|
+ }
|
|
|
+ $input = '';
|
|
|
+ if ($other) {
|
|
|
+ $other_col = $other . '_id';
|
|
|
+ $sku_value = Dever::db($other . '/goods_sku')->getData(array('goods_id' => $goods_id, $other_col => $other_id));
|
|
|
+
|
|
|
+ $input = '<input type="hidden" name="goods_id" value="'.$goods_id.'"/><input type="hidden" name="'.$other_col.'" value="'.$other_id.'"/>';
|
|
|
+ }
|
|
|
+
|
|
|
+ $sku = Dever::db('goods/info_sku')->getData(array('info_id' => $goods_id));
|
|
|
+
|
|
|
+ $info = Dever::db('goods/info')->one($goods_id);
|
|
|
+
|
|
|
+ $info = Info::init(-1)->info($info, 'list_reorder');
|
|
|
+
|
|
|
+ $option = array();
|
|
|
+ $head = '<thead><tr>';
|
|
|
+ $html = '请先选择属性';
|
|
|
+
|
|
|
+ if (isset($info['sell_attr']) && $info['sell_attr']) {
|
|
|
+ foreach ($info['sell_attr'] as $k => $v) {
|
|
|
+ $head .= '<th>'.$v['name'].'</th>';
|
|
|
+ if (isset($v['option_sku'])) {
|
|
|
+ $option[$k] = $v['option_sku'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $col_num = 0;
|
|
|
+ foreach ($col as $k => $v) {
|
|
|
+ if (!$update && strstr($v, '^')) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $col_num++;
|
|
|
+ if (strstr($v, '^')) {
|
|
|
+ $v = str_replace('^', '', $v);
|
|
|
+ }
|
|
|
+ if (strstr($v, '|')) {
|
|
|
+ $v = str_replace('|', '', $v);
|
|
|
+ }
|
|
|
+
|
|
|
+ $head .= '<th>'.$v.'</th>';
|
|
|
+ }
|
|
|
+
|
|
|
+ $head .= '</tr></thead>';
|
|
|
+
|
|
|
+ $body = '<tbody>';
|
|
|
+
|
|
|
+ if (!$option) {
|
|
|
+
|
|
|
+ if (!$update && $col_num <= 1) {
|
|
|
+ $head = '';
|
|
|
+ }
|
|
|
+
|
|
|
+ $input .= '<input type="hidden" name="key" value="-1"/>';
|
|
|
+
|
|
|
+ $body .= '<tr>';
|
|
|
+
|
|
|
+ foreach ($col as $k1 => $v1) {
|
|
|
+ if (!$update && strstr($v1, '^')) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $value = '';
|
|
|
+ if (isset($sku_value[-1]) && isset($sku_value[-1][$k1])) {
|
|
|
+ $value = $sku_value[-1][$k1];
|
|
|
+ } elseif (isset($sku[-1]) && isset($sku[-1][$k1])) {
|
|
|
+ $value = $sku[-1][$k1];
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($update) {
|
|
|
+ if (strstr($v1, '|')) {
|
|
|
+ $v1 = str_replace('|', '', $v1);
|
|
|
+ $body .= '<td width="30">'.$value.'</td>';
|
|
|
+ } else {
|
|
|
+ $body .= '<td width="30"><input type="text" class="layui-input" name="'.$k1.'" value="'.$value.'"/></td>';
|
|
|
+ }
|
|
|
+
|
|
|
+ } elseif ($col_num > 1) {
|
|
|
+ $body .= '<td width="30">'.$value.'</td>';
|
|
|
+ } else {
|
|
|
+ $body .= $value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $body .= '</tr>';
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ $option = Dever::cartesian($option);
|
|
|
+
|
|
|
+ foreach ($option as $k => $v) {
|
|
|
+ $body .= '<tr data-row="' .($k+1). '">';
|
|
|
+
|
|
|
+ $key = $id = array();
|
|
|
+ foreach ($v['name'] as $k1 => $v1) {
|
|
|
+ $rows = 1;
|
|
|
+ $body .= '<td width="50" rowspan="'.$rows.'">'.$v1.'</td>';
|
|
|
+ $id[] = array
|
|
|
+ (
|
|
|
+ 'id' => $v['id'][$k1],
|
|
|
+ 'attr_id' => $v['info_id'][$k1],
|
|
|
+ );
|
|
|
+ $key[] = $v['info_id'][$k1] . '-' . $v['id'][$k1];
|
|
|
+ }
|
|
|
+ $key = implode('_', $key);
|
|
|
+
|
|
|
+ foreach ($col as $k1 => $v1) {
|
|
|
+ if (!$update && strstr($v1, '^')) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $value = '';
|
|
|
+ if (isset($sku[$key])) {
|
|
|
+ if (isset($sku_value[$sku[$key]['id']][$k1])) {
|
|
|
+ $value = $sku_value[$sku[$key]['id']][$k1];
|
|
|
+ } elseif (isset($sku[$key][$k1])) {
|
|
|
+ $value = $sku[$key][$k1];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($update) {
|
|
|
+ if (strstr($v1, '|')) {
|
|
|
+ $v1 = str_replace('|', '', $v1);
|
|
|
+ $body .= '<td width="30">'.$value.'</td>';
|
|
|
+ } else {
|
|
|
+ $body .= '<td width="30"><input type="text" class="layui-input" name="'.$k1.'['.$k.']" value="'.$value.'"/></td>';
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ $body .= '<td width="30">'.$value.'</td>';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $body .= '<input type="hidden" name="key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="attr['.$k.']">'.json_encode($id).'</textarea>';
|
|
|
+ $body .= '</tr>';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $body .= $input;
|
|
|
+
|
|
|
+ $html = $head . $body;
|
|
|
+
|
|
|
+ if (!$update) {
|
|
|
+ $html = '<table>' . $html . '</table>';
|
|
|
+ }
|
|
|
+
|
|
|
+ return $html;
|
|
|
+ }
|
|
|
+}
|