Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProductSpecLogic.php 48KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 陈风任 <491085389@qq.com>
  11. * Date: 2019-07-08
  12. */
  13. namespace app\admin\logic;
  14. use think\Model;
  15. use think\Db;
  16. /**
  17. * 逻辑定义
  18. * 用于产品规格逻辑功能处理
  19. * @package admin\Logic
  20. */
  21. class ProductSpecLogic extends Model
  22. {
  23. /**
  24. * 初始化操作
  25. */
  26. public function _initialize() {
  27. parent::_initialize();
  28. }
  29. // 数组降级返回
  30. public function ArrayDowngrade($GetData = array())
  31. {
  32. $ReturnData = [];
  33. foreach ($GetData as $value){
  34. foreach ($value as $vv){
  35. $ReturnData[] = $vv;
  36. }
  37. }
  38. return $ReturnData;
  39. }
  40. // 一维数组转为二级数组,携带子数组
  41. public function GetPresetData($GetData = array())
  42. {
  43. $GetData = group_same_key($GetData, 'preset_mark_id');
  44. $ReturnData = [];
  45. foreach ($GetData as $key => $value) {
  46. $ReturnData[] = [
  47. 'preset_id' => $value[0]['preset_id'],
  48. 'preset_mark_id' => $value[0]['preset_mark_id'],
  49. 'preset_name' => $value[0]['preset_name'],
  50. 'spec_sync' => $value[0]['spec_sync'],
  51. 'sort_order' => $value[0]['sort_order'],
  52. 'preset_value' => $value,
  53. ];
  54. }
  55. return $ReturnData;
  56. }
  57. // 删除规格名称\规格值条件
  58. public function GetDeleteSpecWhere($GetData = array())
  59. {
  60. $where = [];
  61. // 删除规格值
  62. if (isset($GetData['preset_id']) && !empty($GetData['preset_id'])) {
  63. $where = [
  64. 'lang' => get_admin_lang(),
  65. 'preset_id' => $GetData['preset_id'],
  66. ];
  67. }
  68. // 删除规格名称,包扣规格名称下的所有规格值
  69. if (isset($GetData['preset_mark_id']) && !empty($GetData['preset_mark_id'])) {
  70. $where = [
  71. 'lang' => get_admin_lang(),
  72. 'preset_mark_id' => ['IN', $GetData['preset_mark_id']],
  73. ];
  74. }
  75. return $where;
  76. }
  77. // 拼装预设值下拉选项
  78. public function GetPresetValueOption($GetData = array(), $mark_id = null, $aid = null, $type = null)
  79. {
  80. $result = $PresetValueOption = '';
  81. if (!empty($mark_id)) {
  82. $where = [
  83. 'lang' => get_admin_lang(),
  84. 'aid' => $aid,
  85. 'spec_is_select' => 0, // 未选中的
  86. 'spec_mark_id' => ['IN', $mark_id],
  87. ];
  88. $GetData = Db::name('product_spec_data_handle')->where($where)->field('spec_value_id, spec_name, spec_value')->select();
  89. $PresetValueOption .= "<option value='0'>选择规格值</option>";
  90. foreach ($GetData as $value) {
  91. $PresetValueOption .= "<option value='{$value['spec_value_id']}'>{$value['spec_value']}</option>";
  92. }
  93. if (2 == $type) {
  94. $result = [
  95. 'PresetName' => $GetData[0]['spec_name'],
  96. 'PresetValueOption' => $PresetValueOption,
  97. ];
  98. } else {
  99. $result = $PresetValueOption;
  100. }
  101. } else {
  102. $result .= "<option value='0'>选择规格值</option>";
  103. if (!empty($GetData)) {
  104. foreach ($GetData as $value) {
  105. $result .= "<option value='{$value['preset_id']}'>{$value['preset_value']}</option>";
  106. }
  107. }
  108. }
  109. return $result;
  110. }
  111. // 预设规格标记ID数组
  112. public function GetPresetMarkIdArray($GetData = array())
  113. {
  114. $result = '';
  115. if (isset($GetData['aid']) && !empty($GetData['aid'])) {
  116. $spec_mark_id_arr = $GetData['spec_mark_id_arr'];
  117. if (empty($spec_mark_id_arr)) {
  118. $result = $GetData['spec_mark_id'];
  119. } else {
  120. $return_spec_mark_id = $spec_mark_id_arr . ',' . $GetData['spec_mark_id'];
  121. if (3 < count(explode(',', $return_spec_mark_id))) {
  122. $result = false;
  123. } else {
  124. $result = $return_spec_mark_id;
  125. }
  126. }
  127. } else {
  128. $preset_mark_id_arr = $GetData['preset_mark_id_arr'];
  129. if (empty($preset_mark_id_arr)) {
  130. $result = $GetData['preset_mark_id'];
  131. } else {
  132. $return_preset_mark_id = $preset_mark_id_arr . ',' . $GetData['preset_mark_id'];
  133. if (3 < count(explode(',', $return_preset_mark_id))) {
  134. $result = false;
  135. } else {
  136. $result = $return_preset_mark_id;
  137. }
  138. }
  139. }
  140. return $result;
  141. }
  142. // 拼装规格名称下拉选项
  143. public function GetPresetNameOption($preset_mark_id_arr = null)
  144. {
  145. $MarkIdWhere = explode(',', $preset_mark_id_arr);
  146. $where = [
  147. 'lang' => get_admin_lang(),
  148. 'preset_mark_id' => ['NOT IN', $MarkIdWhere],
  149. ];
  150. $PresetName = Db::name('product_spec_preset')->where($where)->field('preset_id, preset_mark_id, preset_name')->group('preset_mark_id')->order('preset_mark_id desc')->select();
  151. if (!empty($PresetName)) {
  152. // 拼装下拉选项
  153. foreach ($PresetName as $value) {
  154. $Option .= "<option value='{$value['preset_mark_id']}'>{$value['preset_name']}</option>";
  155. }
  156. }
  157. $result = [
  158. 'Option' => $Option,
  159. 'MarkId' => implode(',', $MarkIdWhere),
  160. ];
  161. return $result;
  162. }
  163. public function GetPresetSpecAssembly($GetData = array())
  164. {
  165. $preset_mark_id = $GetData['preset_mark_id'];
  166. if (isset($GetData['aid']) && !empty($GetData['aid'])) {
  167. $PresetMarkIdArray = $GetData['spec_mark_id_arr'];
  168. }else{
  169. $PresetMarkIdArray = $GetData['preset_mark_id_arr'];
  170. }
  171. $i = 0;
  172. if (!empty($PresetMarkIdArray) && !empty($preset_mark_id)) {
  173. $mark_id_arr = explode(',', $PresetMarkIdArray);
  174. foreach ($mark_id_arr as $key => $value) {
  175. if ($value == $preset_mark_id) {
  176. unset($mark_id_arr[$key]);
  177. $i++;
  178. }
  179. }
  180. if (!empty($i)) {
  181. $PresetMarkIdArray = implode(',', $mark_id_arr);
  182. }
  183. }
  184. $HtmlTable = '';
  185. if (!empty($i) && !empty($preset_mark_id)) {
  186. $spec_arr_ses = session('spec_arr');
  187. unset($spec_arr_ses[$preset_mark_id]);
  188. session('spec_arr', $spec_arr_ses);
  189. $HtmlTable = $this->SpecAssembly($spec_arr_ses);
  190. }
  191. $return = [
  192. 'HtmlTable' => $HtmlTable,
  193. 'PresetMarkIdArray' => $PresetMarkIdArray,
  194. ];
  195. return $return;
  196. }
  197. public function GetResetPresetNameOption($GetData = array())
  198. {
  199. $del_mark_id = $GetData['del_mark_id'];
  200. $return = [
  201. 'Option' => '',
  202. 'MarkId' => '',
  203. ];
  204. if (isset($del_mark_id) && !empty($del_mark_id) && !isset($GetData['del_preset_id'])) {
  205. // 删除商品规格处理库的规格数据
  206. if (isset($GetData['aid']) && !empty($GetData['aid'])) {
  207. $WhereData = [
  208. 'aid' => $GetData['aid'],
  209. 'spec_mark_id' => $del_mark_id,
  210. ];
  211. Db::name('product_spec_data_handle')->where($WhereData)->delete(true);
  212. // $Update = [
  213. // 'spec_is_select' => 0,
  214. // 'update_time' => getTime(),
  215. // ];
  216. // Db::name('product_spec_data_handle')->where($WhereData)->update($Update);
  217. } else {
  218. // 删除 session 数据并覆盖原数据
  219. $spec_arr_ses = session('spec_arr');
  220. unset($spec_arr_ses[$del_mark_id]);
  221. session('spec_arr', $spec_arr_ses);
  222. }
  223. // 获取提交的规格标记ID数组并处理生成预设规格下拉信息
  224. $PresetMarkIdArray = !empty($GetData['preset_mark_id_arr']) ? $GetData['preset_mark_id_arr'] : $GetData['spec_mark_id_arr'];
  225. if (!empty($PresetMarkIdArray)) {
  226. $mark_id_arr = explode(',', $PresetMarkIdArray);
  227. foreach ($mark_id_arr as $key => $value) {
  228. if ($value == $del_mark_id) unset($mark_id_arr[$key]);
  229. }
  230. $PresetMarkIdArray = implode(',', $mark_id_arr);
  231. }
  232. $return = $this->GetPresetNameOption($PresetMarkIdArray);
  233. }
  234. return $return;
  235. }
  236. public function ClearSpecValueID($GetData = array())
  237. {
  238. // 清除单个规格值
  239. $del_mark_id = $GetData['del_mark_id'];
  240. $return = [
  241. 'Option' => '',
  242. ];
  243. if (isset($del_mark_id) && !empty($del_mark_id) && isset($GetData['del_preset_id'])) {
  244. // 删除指定规格值并覆盖原数据
  245. $spec_arr_ses = session('spec_arr');
  246. foreach ($spec_arr_ses[$del_mark_id] as $key => $value) {
  247. if ($value == $GetData['del_preset_id']) unset($spec_arr_ses[$del_mark_id][$key]);
  248. }
  249. // 如果规格大类下的规格值都删完了则删除规格大类
  250. if (empty($spec_arr_ses[$del_mark_id])) unset($spec_arr_ses[$del_mark_id]);
  251. // 覆盖原数据
  252. session('spec_arr', $spec_arr_ses);
  253. // 处理生成预设规格下拉信息
  254. $preset_id = $spec_arr_ses[$del_mark_id];
  255. if (empty($GetData['aid'])) {
  256. $WhereNew = [
  257. 'preset_mark_id' => $del_mark_id,
  258. 'preset_id' => ['NOT IN', $preset_id],
  259. ];
  260. // 加载选中的规格值
  261. $PresetValue = Db::name('product_spec_preset')->where($WhereNew)->select();
  262. $return['Option'] = $this->GetPresetValueOption($PresetValue);
  263. } else {
  264. // 更新规格值
  265. $Where = [
  266. 'aid' => $GetData['aid'],
  267. 'spec_mark_id' => $del_mark_id,
  268. 'spec_value_id' => $GetData['del_preset_id'],
  269. 'spec_is_select'=> 1,
  270. ];
  271. Db::name('product_spec_data_handle')->where($Where)->update(['spec_is_select'=>0, 'update_time'=>getTime()]);
  272. // 拼装新的下拉框数据
  273. $WhereNew = [
  274. 'aid' => $GetData['aid'],
  275. 'spec_mark_id' => $del_mark_id,
  276. 'spec_is_select' => 0,
  277. ];
  278. $Data = Db::name('product_spec_data_handle')->where($WhereNew)->field('spec_value_id,spec_value')->select();
  279. $return['Option'] .= "<option value='0'>选择规格值</option>";
  280. foreach ($Data as $value) {
  281. $return['Option'] .= "<option value='{$value['spec_value_id']}'>{$value['spec_value']}</option>";
  282. }
  283. }
  284. }
  285. return $return;
  286. }
  287. public function GetSessionPostArrayMerge($GetData = array())
  288. {
  289. $preset_id = $GetData['preset_id'];
  290. $preset_mark_id = $GetData['preset_mark_id'];
  291. $spec_arr_ses = session('spec_arr');
  292. if (!empty($preset_mark_id) && !empty($preset_id)) {
  293. $spec_arr_new = [
  294. $preset_mark_id => [
  295. $preset_id,
  296. ],
  297. ];
  298. foreach ($spec_arr_new as $key => $value) {
  299. if (isset($spec_arr_ses[$key])) {
  300. if (in_array($value[0], $spec_arr_ses[$key])) {
  301. $msg['error'] = '已有相同规格值,无需重复添加!';
  302. return $msg;
  303. } else {
  304. // session存在数据,但选中参数在数据中还没有,合并数据
  305. $spec_arr_ses[$key] = array_merge($spec_arr_ses[$key], $value);
  306. }
  307. } else {
  308. if (!empty($spec_arr_ses)) {
  309. // 不存在
  310. $spec_arr_ses = $spec_arr_ses + $spec_arr_new;
  311. } else {
  312. $spec_arr_ses = $spec_arr_new;
  313. }
  314. }
  315. }
  316. }
  317. session('spec_arr', $spec_arr_ses);
  318. return $spec_arr_ses;
  319. }
  320. public function SpecAssembly($GetData = array())
  321. {
  322. if (empty($GetData)) return ' ';
  323. // 参数处理
  324. foreach ($GetData as $k => $v) {
  325. $spec_arr_sort[$k] = count($v);
  326. }
  327. asort($spec_arr_sort);
  328. foreach ($spec_arr_sort as $key =>$val) {
  329. $spec_arr2[$key] = $GetData[$key];
  330. }
  331. // 获取KEY值
  332. $clo_name = array_keys($spec_arr2);
  333. // 数据组合
  334. $spec_arr2 = $this->DataCombination($spec_arr2);
  335. // 查询预设名称
  336. $spec = Db::name('product_spec_preset')->group('preset_mark_id')->getField('preset_mark_id,preset_name');
  337. // 查询预设值
  338. $specItem = Db::name('product_spec_preset')->getField('preset_id,preset_value,preset_mark_id');
  339. // 组合HTML
  340. $ReturnHtml = "<table class='table table-bordered' id='spec_input_tab' border='1' cellpadding='10' cellspacing='10'><thead><tr>";
  341. // 显示第一行的数据
  342. foreach ($clo_name as $k => $v) {
  343. $ReturnHtml .= "<td><b><input type='hidden' class='prese_name_input_{$v}' name='preset_mark_id[$v][preset_name]' value='{$spec[$v]}'><span class='preset_name_span_{$v}'>{$spec[$v]}</span></b></td>";
  344. }
  345. $ReturnHtml .= "<td><b>价格 <a href=\"javascript:void(0);\" onclick=\"BulkSetPrice(this);\">批量设置 </a></b></td>";
  346. $ReturnHtml .= "<td><b>库存 <a href=\"javascript:void(0);\" onclick=\"BulkSetStock(this);\">批量设置 </a></b></td>";
  347. $ReturnHtml .= "<td><b>虚拟销量</b></td>";
  348. $ReturnHtml .= "</tr></thead><tbody>";
  349. // 显示第二行开始
  350. foreach ($spec_arr2 as $k => $v) {
  351. $ReturnHtml .= "<tr id='preset_value_tr_{$v[0]}'>";
  352. $preset_key_name = array();
  353. foreach($v as $k2 => $v2) {
  354. $ReturnHtml .= "<td><input type='hidden' class='preset_value_input_{$v2}' name='preset_id[$v2][preset_value]' value='{$specItem[$v2]['preset_value']}'><span class='preset_value_span_{$v2}'>{$specItem[$v2]['preset_value']}</span></td>";
  355. $preset_key_name[$v2] = $spec[$specItem[$v2]['preset_mark_id']].':'.$specItem[$v2]['preset_value'];
  356. }
  357. ksort($preset_key_name);
  358. $preset_key = implode('_', array_keys($preset_key_name));
  359. $ReturnHtml .="<td><input class='users_price' name='preset_price[$preset_key][users_price]' value='0' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");UpPrice(this);' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")'/></td>";
  360. $ReturnHtml .="<td><input class='stock_count' name='preset_stock[$preset_key][stock_count]' value='0' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");UpStock(this);' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' data-old_stock='0'/></td>";
  361. $ReturnHtml .="<td><input type='text' name='preset_sales[$preset_key][spec_sales_num]' value='0' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\")' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")'></td>";
  362. $ReturnHtml .="</tr>";
  363. }
  364. $ReturnHtml .= "</tbody>";
  365. $ReturnHtml .= "</table>";
  366. // $ReturnHtml .= '批量设置:<a href="javascript:void(0);" onclick="BulkSetPrice(this);" >价格</a> <a href="javascript:void(0);" onclick="BulkSetStock(this);" >库存</a>';
  367. return $ReturnHtml;
  368. }
  369. public function SpecAssemblyEdit($GetData = array(), $aid = 0, $quick = false)
  370. {
  371. if (empty($GetData)) return ' ';
  372. // 参数处理
  373. foreach ($GetData as $k => $v) {
  374. $spec_arr_sort[$k] = count($v);
  375. }
  376. asort($spec_arr_sort);
  377. foreach ($spec_arr_sort as $key =>$val) {
  378. $spec_arr2[$key] = $GetData[$key];
  379. }
  380. // 获取KEY值
  381. $clo_name = array_keys($spec_arr2);
  382. // 数据组合
  383. $spec_arr2 = $this->DataCombination($spec_arr2);
  384. $aid = !empty($aid) ? $aid : session('handleAID');
  385. $where = [
  386. 'aid' => $aid,
  387. 'spec_is_select' => 1,
  388. ];
  389. // 快速编辑时执行
  390. if (!empty($quick)) {
  391. // 查询规格名称
  392. $order1 = 'spec_mark_id asc, spec_value_id asc';
  393. $spec = Db::name('product_spec_data')->where($where)->order($order1)->group('spec_mark_id')->getField('spec_mark_id, spec_name');
  394. // 查询规格值
  395. $order2 = 'spec_value_id desc';
  396. $specItem = Db::name('product_spec_data')->where($where)->order($order2)->getField('spec_value_id, spec_value, spec_mark_id');
  397. // 查询规格价格
  398. $specPrice = Db::name('product_spec_value')->where('aid', $aid)->order('spec_price asc')->getField('spec_value_id, value_id, spec_price, spec_crossed_price, spec_stock, spec_sales_num');
  399. } else {
  400. // 查询规格名称
  401. $order1 = 'spec_mark_id asc, spec_value_id asc';
  402. $spec = Db::name('product_spec_data_handle')->where($where)->order($order1)->group('spec_mark_id')->getField('spec_mark_id, spec_name');
  403. // 查询规格值
  404. $order2 = 'spec_value_id desc';
  405. $specItem = Db::name('product_spec_data_handle')->where($where)->order($order2)->getField('spec_value_id, spec_value, spec_mark_id');
  406. // 查询规格价格
  407. $specPrice = Db::name('product_spec_value_handle')->where('aid', $aid)->order('spec_price asc')->getField('spec_value_id, value_id, spec_price, spec_crossed_price, spec_stock, spec_sales_num');
  408. }
  409. // 组合HTML
  410. $ReturnHtml = "<table class='table table-bordered' id='spec_input_tab' border='1' cellpadding='10' cellspacing='10'><thead><tr>";
  411. // 显示第一行的数据
  412. foreach ($clo_name as $k => $v) {
  413. $ReturnHtml .= "<td><b><input type='hidden' class='spec_name_input_{$v}' name='spec_mark_id[$v][spec_name]' value='{$spec[$v]}'><span class='spec_name_span_{$v}'>{$spec[$v]}</span></b></td>";
  414. }
  415. $ReturnHtml .= "<td><b>价格 <a href=\"javascript:void(0);\" onclick=\"BulkSetPrice(this);\">批量设置 </a></b></td>";
  416. $ReturnHtml .= "<td><b>划线价 <a href=\"javascript:void(0);\" onclick=\"BulkSetCrossedPrice(this);\">批量设置 </a></b></td>";
  417. $ReturnHtml .= "<td><b>库存 <a href=\"javascript:void(0);\" onclick=\"BulkSetStock(this);\">批量设置 </a></b></td>";
  418. $ReturnHtml .= "<td><b>虚拟销量</b></td>";
  419. $ReturnHtml .= "</tr></thead><tbody>";
  420. // 显示第二行开始
  421. foreach ($spec_arr2 as $k => $v) {
  422. $ReturnHtml .= "<tr>";
  423. $spec_key_name = array();
  424. foreach($v as $k2 => $v2) {
  425. $ReturnHtml .= "<td><input type='hidden' class='spec_value_input_{$v2}' name='spec_value_id[$v2][spec_value]' value='{$specItem[$v2]['spec_value']}'><span class='spec_value_span_{$v2}'>{$specItem[$v2]['spec_value']}</span></td>";
  426. $spec_key_name[$v2] = $spec[$specItem[$v2]['spec_mark_id']].':'.$specItem[$v2]['spec_value'];
  427. }
  428. ksort($spec_key_name);
  429. $spec_key = implode('_', array_keys($spec_key_name));
  430. $ReturnHtml .="<input type='hidden' name='value_ids[$spec_key][value_id]' value='{$specPrice[$spec_key]['value_id']}'/></td>";
  431. $ReturnHtml .="<td><input type='text' class='users_price' name='spec_price[$spec_key][users_price]' value='{$specPrice[$spec_key]['spec_price']}' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");UpPrice(this);' onchange='setProductSpecPrice();'/></td>";
  432. $ReturnHtml .="<td><input type='text' class='crossed_price' name='spec_crossed_price[$spec_key][crossed_price]' value='{$specPrice[$spec_key]['spec_crossed_price']}' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");UpPrice(this);' onchange='setProductSpecPrice();'/></td>";
  433. $ReturnHtml .="<td><input type='text' class='stock_count' name='spec_stock[$spec_key][stock_count]' value='{$specPrice[$spec_key]['spec_stock']}' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");UpStock(this);' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' data-old_stock='{$specPrice[$spec_key]['spec_stock']}' onchange='setProductSpecPrice();'/></td>";
  434. $ReturnHtml .="<td><input type='text' name='spec_sales[$spec_key][spec_sales_num]' value='{$specPrice[$spec_key]['spec_sales_num']}' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\")' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")'></td>";
  435. $ReturnHtml .="</tr>";
  436. }
  437. $ReturnHtml .= "</tbody>";
  438. $ReturnHtml .= "</table>";
  439. // $ReturnHtml .= '批量设置:<a href="javascript:void(0);" onclick="BulkSetPrice(this);" >批量设置</a> <a href="javascript:void(0);" onclick="BulkSetStock(this);" >批量设置</a>';
  440. return $ReturnHtml;
  441. }
  442. /**
  443. * 2020/12/18 大黄 秒杀 多规格设置
  444. */
  445. public function SharpSpecAssemblyEdit($GetData = array(), $aid = null)
  446. {
  447. if (empty($GetData)) return ' ';
  448. // 参数处理
  449. foreach ($GetData as $k => $v) {
  450. $spec_arr_sort[$k] = count($v);
  451. }
  452. asort($spec_arr_sort);
  453. foreach ($spec_arr_sort as $key =>$val) {
  454. $spec_arr2[$key] = $GetData[$key];
  455. }
  456. // 排序
  457. $order = 'spec_value_id asc, spec_id asc, spec_mark_id asc';
  458. // 获取KEY值
  459. $clo_name = array_keys($spec_arr2);
  460. // 数据组合
  461. $spec_arr2 = $this->DataCombination($spec_arr2);
  462. // 查询规格名称
  463. $spec = Db::name('product_spec_data')->where('aid',$aid)->order($order)->group('spec_mark_id')->getField('spec_mark_id, spec_name');
  464. // 查询规格值
  465. $specItem = Db::name('product_spec_data')->where('aid',$aid)->order($order)->getField('spec_value_id, spec_value, spec_mark_id');
  466. // 查询规格价格
  467. $specPrice = Db::name('product_spec_value')->where('aid', $aid)->order('spec_price asc')->getField('spec_value_id, spec_price, spec_stock, seckill_price, seckill_stock');
  468. // 组合HTML
  469. $ReturnHtml = "<table class='table table-bordered' id='spec_input_tab' border='1' cellpadding='10' cellspacing='10'><thead><tr>";
  470. // 显示第一行的数据
  471. foreach ($clo_name as $k => $v) {
  472. $ReturnHtml .= "<td><b><input type='hidden' class='spec_name_input_{$v}' name='spec_mark_id[$v][spec_name]' value='{$spec[$v]}'><span class='spec_name_span_{$v}'>{$spec[$v]}</span></b></td>";
  473. }
  474. $ReturnHtml .= "<td><b>价格 </b></td>";
  475. $ReturnHtml .= "<td><b>库存 </b></td>";
  476. $ReturnHtml .= "<td><b><em class='red'>*</em>秒杀价格 <a href=\"javascript:void(0);\" onclick=\"BulkSetPrice(this);\">批量设置 </a></b></td>";
  477. $ReturnHtml .= "<td><b><em class='red'>*</em>秒杀库存 <a href=\"javascript:void(0);\" onclick=\"BulkSetStock(this);\" >批量设置 </a></b></td>";
  478. $ReturnHtml .= "</tr></thead><tbody>";
  479. // 显示第二行开始
  480. foreach ($spec_arr2 as $k => $v) {
  481. $ReturnHtml .= "<tr>";
  482. $spec_key_name = array();
  483. foreach($v as $k2 => $v2) {
  484. $ReturnHtml .= "<td><input type='hidden' class='spec_value_input_{$v2}' name='spec_value_id[$v2][spec_value]' value='{$specItem[$v2]['spec_value']}'><span class='spec_value_span_{$v2}'>{$specItem[$v2]['spec_value']}</span></td>";
  485. $spec_key_name[$v2] = $spec[$specItem[$v2]['spec_mark_id']].':'.$specItem[$v2]['spec_value'];
  486. }
  487. ksort($spec_key_name);
  488. $spec_key = implode('_', array_keys($spec_key_name));
  489. $ReturnHtml .="<td>{$specPrice[$spec_key]['spec_price']}<input type='hidden' class='users_price' name='spec_price[$spec_key][users_price]' value='{$specPrice[$spec_key]['spec_price']}' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");'/></td>";
  490. $ReturnHtml .="<td>{$specPrice[$spec_key]['spec_stock']}<input type='hidden' class='stock_count' name='spec_stock[$spec_key][stock_count]' value='{$specPrice[$spec_key]['spec_stock']}' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' data-old_stock='{$specPrice[$spec_key]['spec_stock']}'/></td>";
  491. if ($specPrice[$spec_key]['seckill_price'] > 0){
  492. $seckill_price = $specPrice[$spec_key]['seckill_price'];
  493. }else{
  494. $seckill_price = '';
  495. }
  496. $ReturnHtml .="<td><input class='spec_seckill_price' name='seckill_price[$spec_key][spec_seckill_price]' value='{$seckill_price}' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");'/></td>";
  497. if ($specPrice[$spec_key]['seckill_stock'] > 0){
  498. $seckill_stock = $specPrice[$spec_key]['seckill_stock'];
  499. }else{
  500. $seckill_stock = '';
  501. }
  502. $ReturnHtml .="<td><input class='spec_seckill_stock' name='seckill_stock[$spec_key][spec_seckill_stock]' value='{$seckill_stock}' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' data-old_stock='{$specPrice[$spec_key]['seckill_stock']}'/></td>";
  503. $ReturnHtml .="</tr>";
  504. }
  505. $ReturnHtml .= "</tbody>";
  506. $ReturnHtml .= "</table>";
  507. // $ReturnHtml .= '批量设置:<a href="javascript:void(0);" onclick="BulkSetPrice(this);" >批量设置</a> <a href="javascript:void(0);" onclick="BulkSetStock(this);" >批量设置</a>';
  508. return $ReturnHtml;
  509. }
  510. /**
  511. * 2022/03/08 大黄 限时折扣 多规格设置
  512. */
  513. public function DiscountSpecAssemblyEdit($GetData = array(), $aid = null)
  514. {
  515. if (empty($GetData)) return ' ';
  516. // 参数处理
  517. foreach ($GetData as $k => $v) {
  518. $spec_arr_sort[$k] = count($v);
  519. }
  520. asort($spec_arr_sort);
  521. foreach ($spec_arr_sort as $key =>$val) {
  522. $spec_arr2[$key] = $GetData[$key];
  523. }
  524. // 排序
  525. $order = 'spec_value_id asc, spec_id asc, spec_mark_id asc';
  526. // 获取KEY值
  527. $clo_name = array_keys($spec_arr2);
  528. // 数据组合
  529. $spec_arr2 = $this->DataCombination($spec_arr2);
  530. // 查询规格名称
  531. $spec = Db::name('product_spec_data')->where('aid',$aid)->order($order)->group('spec_mark_id')->getField('spec_mark_id, spec_name');
  532. // 查询规格值
  533. $specItem = Db::name('product_spec_data')->where('aid',$aid)->order($order)->getField('spec_value_id, spec_value, spec_mark_id');
  534. // 查询规格价格
  535. $specPrice = Db::name('product_spec_value')->where('aid', $aid)->order('spec_price asc')->getField('spec_value_id, spec_price, spec_stock, discount_price, discount_stock');
  536. // 组合HTML
  537. $ReturnHtml = "<table class='table table-bordered' id='spec_input_tab' border='1' cellpadding='10' cellspacing='10'><thead><tr>";
  538. // 显示第一行的数据
  539. foreach ($clo_name as $k => $v) {
  540. $ReturnHtml .= "<td><b><input type='hidden' class='spec_name_input_{$v}' name='spec_mark_id[$v][spec_name]' value='{$spec[$v]}'><span class='spec_name_span_{$v}'>{$spec[$v]}</span></b></td>";
  541. }
  542. $ReturnHtml .= "<td><b>价格 </b></td>";
  543. $ReturnHtml .= "<td><b>库存 </b></td>";
  544. $ReturnHtml .= "<td><b><em class='red'>*</em>限时折扣价格 <a href=\"javascript:void(0);\" onclick=\"BulkSetPrice(this);\">批量设置 </a></b></td>";
  545. $ReturnHtml .= "<td><b><em class='red'>*</em>限时折扣库存 <a href=\"javascript:void(0);\" onclick=\"BulkSetStock(this);\" >批量设置 </a></b></td>";
  546. $ReturnHtml .= "</tr></thead><tbody>";
  547. // 显示第二行开始
  548. foreach ($spec_arr2 as $k => $v) {
  549. $ReturnHtml .= "<tr>";
  550. $spec_key_name = array();
  551. foreach($v as $k2 => $v2) {
  552. $ReturnHtml .= "<td><input type='hidden' class='spec_value_input_{$v2}' name='spec_value_id[$v2][spec_value]' value='{$specItem[$v2]['spec_value']}'><span class='spec_value_span_{$v2}'>{$specItem[$v2]['spec_value']}</span></td>";
  553. $spec_key_name[$v2] = $spec[$specItem[$v2]['spec_mark_id']].':'.$specItem[$v2]['spec_value'];
  554. }
  555. ksort($spec_key_name);
  556. $spec_key = implode('_', array_keys($spec_key_name));
  557. $ReturnHtml .="<td>{$specPrice[$spec_key]['spec_price']}<input type='hidden' class='users_price' name='spec_price[$spec_key][users_price]' value='{$specPrice[$spec_key]['spec_price']}' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");'/></td>";
  558. $ReturnHtml .="<td>{$specPrice[$spec_key]['spec_stock']}<input type='hidden' class='stock_count' name='spec_stock[$spec_key][stock_count]' value='{$specPrice[$spec_key]['spec_stock']}' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' data-old_stock='{$specPrice[$spec_key]['spec_stock']}'/></td>";
  559. if ($specPrice[$spec_key]['discount_price'] > 0){
  560. $discount_price = $specPrice[$spec_key]['discount_price'];
  561. }else{
  562. $discount_price = '';
  563. }
  564. $ReturnHtml .="<td><input class='spec_discount_price' name='discount_price[$spec_key][spec_discount_price]' value='{$discount_price}' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");'/></td>";
  565. if ($specPrice[$spec_key]['discount_stock'] > 0){
  566. $discount_stock = $specPrice[$spec_key]['discount_stock'];
  567. }else{
  568. $discount_stock = '';
  569. }
  570. $ReturnHtml .="<td><input class='spec_discount_stock' name='discount_stock[$spec_key][spec_discount_stock]' value='{$discount_stock}' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\");' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' data-old_stock='{$specPrice[$spec_key]['discount_stock']}'/></td>";
  571. $ReturnHtml .="</tr>";
  572. }
  573. $ReturnHtml .= "</tbody>";
  574. $ReturnHtml .= "</table>";
  575. // $ReturnHtml .= '批量设置:<a href="javascript:void(0);" onclick="BulkSetPrice(this);" >批量设置</a> <a href="javascript:void(0);" onclick="BulkSetStock(this);" >批量设置</a>';
  576. return $ReturnHtml;
  577. }
  578. // 数据组合
  579. private function DataCombination()
  580. {
  581. $data = func_get_args();
  582. $data = current($data);
  583. $result = array();
  584. $arr1 = array_shift($data);
  585. foreach($arr1 as $key => $item) {
  586. $result[] = array($item);
  587. }
  588. foreach($data as $key => $item) {
  589. $result = $this->DataCombinationArray($result, $item);
  590. }
  591. return $result;
  592. }
  593. private function DataCombinationArray($arr1, $arr2)
  594. {
  595. $result = array();
  596. foreach ($arr1 as $item1) {
  597. foreach ($arr2 as $item2) {
  598. $temp = $item1;
  599. $temp[] = $item2;
  600. $result[] = $temp;
  601. }
  602. }
  603. return $result;
  604. }
  605. // 添加自定义规格
  606. public function addProductCustomSpec($post = [])
  607. {
  608. // 最大ID值
  609. // $spec_mark_id = Db::name('product_spec_data_handle')->max('spec_mark_id');
  610. // if (empty($spec_mark_id)) $spec_mark_id = Db::name('product_spec_data')->max('spec_mark_id');
  611. $spec_mark_id = date('s') . rand(0, 9);
  612. $spec_value_id = Db::name('product_spec_data_handle')->max('spec_value_id');
  613. if (empty($spec_value_id)) $spec_value_id = Db::name('product_spec_data')->max('spec_value_id');
  614. // 数据处理
  615. $time = getTime();
  616. $handleAID = $handleSpecID = date('His');
  617. if (session('handleAID')) $handleAID = session('handleAID');
  618. $handleSpecID = $handleSpecID . rand(0, 9);
  619. $handleAID = !empty($post['aid']) ? $post['aid'] : $handleAID;
  620. if ('value' === strval($post['action']) && !empty($post['spec_mark_id'])) {
  621. // 添加的数据
  622. $where = [
  623. 'aid' => $handleAID,
  624. 'spec_mark_id' => $post['spec_mark_id'],
  625. ];
  626. $specName = Db::name('product_spec_data_handle')->where($where)->getField('spec_name');
  627. $insert = [
  628. 'spec_id' => intval($handleSpecID),
  629. 'aid' => $handleAID,
  630. 'spec_mark_id' => $post['spec_mark_id'],
  631. 'spec_name' => $specName,
  632. 'spec_value_id' => ++$spec_value_id,
  633. 'spec_value' => '',
  634. 'spec_is_select' => 1,
  635. 'lang' => get_admin_lang(),
  636. 'add_time' => $time,
  637. 'update_time' => $time,
  638. ];
  639. $spec_mark_id = $post['spec_mark_id'];
  640. } else if ('name' === strval($post['action'])) {
  641. // 添加的数据
  642. $insert = [
  643. 'spec_id' => intval($handleSpecID),
  644. 'aid' => $handleAID,
  645. 'spec_mark_id' => ++$spec_mark_id,
  646. 'spec_name' => '',
  647. 'spec_value_id' => ++$spec_value_id,
  648. 'spec_value' => '',
  649. 'spec_is_select' => 1,
  650. 'lang' => get_admin_lang(),
  651. 'add_time' => $time,
  652. 'update_time' => $time,
  653. ];
  654. } else if ('specName' === strval($post['action'])) {
  655. // 添加的数据
  656. $where = [
  657. 'preset_mark_id' => $post['preset_mark_id'],
  658. ];
  659. $specPreset = Db::name('product_spec_preset')->where($where)->select();
  660. $insertAll = [];
  661. foreach ($specPreset as $key => $value) {
  662. $handleSpecID++;
  663. // 添加的数据
  664. $insertAll[] = [
  665. 'spec_id' => intval($handleSpecID),
  666. 'aid' => $handleAID,
  667. 'spec_mark_id' => $value['preset_mark_id'],
  668. 'spec_name' => $value['preset_name'],
  669. 'spec_value_id' => $value['preset_id'],
  670. 'spec_value' => $value['preset_value'],
  671. 'spec_is_select' => 0,
  672. 'lang' => get_admin_lang(),
  673. 'add_time' => $time,
  674. 'update_time' => $time,
  675. ];
  676. }
  677. $spec_mark_id = $post['preset_mark_id'];
  678. } else if ('specValue' === strval($post['action'])) {
  679. $where = [
  680. 'aid' => $handleAID,
  681. 'spec_mark_id' => intval($post['spec_mark_id']),
  682. 'spec_value_id' => intval($post['spec_value_id']),
  683. ];
  684. $update = [
  685. 'spec_is_select' => 1,
  686. 'update_time' => $time,
  687. ];
  688. $spec_mark_id = $post['spec_mark_id'];
  689. }
  690. // 执行添加
  691. if (!empty($insertAll)) {
  692. $resultID = Db::name('product_spec_data_handle')->insertAll($insertAll);
  693. } else if (!empty($insert)) {
  694. $resultID = Db::name('product_spec_data_handle')->insert($insert);
  695. } else if (!empty($where) && !empty($update)) {
  696. $resultID = Db::name('product_spec_data_handle')->where($where)->update($update);
  697. }
  698. // 添加后续操作
  699. if (!empty($resultID)) {
  700. // 查询商品的规格信息
  701. $where = [
  702. 'aid' => $handleAID,
  703. 'spec_is_select' => 1,
  704. ];
  705. $field = 'spec_mark_id, spec_value_id';
  706. $order = 'spec_value_id asc, spec_id asc';
  707. $data = Db::name('product_spec_data_handle')->where($where)->field($field)->order($order)->select();
  708. // 处理规格数组
  709. $spec_array = [];
  710. if (!empty($data)) {
  711. foreach ($data as $key => $value) {
  712. $spec_array[$value['spec_mark_id']][] = $value['spec_value_id'];
  713. }
  714. }
  715. // 处理规格标记ID串
  716. $spec_mark_id_arr = implode(',', array_keys($spec_array));
  717. // 规格数据库表的自定义aid存入
  718. session('handleAID', $handleAID);
  719. // 处理废弃规格ID,刷新商品添加页时执行删除废弃规格
  720. $del_spec = session('del_spec') ? session('del_spec') : [];
  721. array_push($del_spec, $spec_mark_id);
  722. session('del_spec', $del_spec);
  723. // 返回结束
  724. return [
  725. 'spec_array' => $spec_array,
  726. 'spec_mark_id' => $spec_mark_id,
  727. 'spec_value_id' => $spec_value_id,
  728. 'spec_mark_id_arr' => $spec_mark_id_arr
  729. ];
  730. }
  731. }
  732. // 获取商品规格名称值下拉框
  733. public function getProductSpecValueOption($specMarkID = 0, $post = [])
  734. {
  735. $handleAID = session('handleAID');
  736. $handleAID = !empty($post['aid']) ? $post['aid'] : $handleAID;
  737. if (!empty($post['del'])) {
  738. if ('specName' === strval($post['del'])) {
  739. $where = [
  740. 'aid' => $handleAID,
  741. 'lang' => get_admin_lang(),
  742. ];
  743. $specMarkIDarr = Db::name('product_spec_data_handle')->where($where)->column('spec_mark_id');
  744. $specMarkIDarr = array_unique($specMarkIDarr);
  745. $where = [
  746. 'lang' => get_admin_lang(),
  747. 'preset_mark_id' => ['NOT IN', $specMarkIDarr],
  748. ];
  749. $field = 'preset_id, preset_mark_id, preset_name';
  750. $PresetName = Db::name('product_spec_preset')->where($where)->field($field)->group('preset_mark_id')->order('preset_mark_id desc')->select();
  751. $preset_name_option .= "<option value='0'>从规格库提取</option>";
  752. if (!empty($PresetName)) {
  753. // 拼装下拉选项
  754. foreach ($PresetName as $value) {
  755. $preset_name_option .= "<option value='{$value['preset_mark_id']}'>{$value['preset_name']}</option>";
  756. }
  757. }
  758. } else if ('specValue' === strval($post['del'])) {
  759. $where = [
  760. 'aid' => $handleAID,
  761. 'lang' => get_admin_lang(),
  762. 'spec_mark_id' => intval($post['spec_mark_id']),
  763. ];
  764. $specDataHandle = Db::name('product_spec_data_handle')->where($where)->field('spec_is_select, spec_value_id, spec_name, spec_value')->select();
  765. $spec_value_option .= "<option value='0'>选择规格值</option>";
  766. foreach ($specDataHandle as $value) {
  767. if (0 === intval($value['spec_is_select'])) $spec_value_option .= "<option value='{$value['spec_value_id']}'>{$value['spec_value']}</option>";
  768. }
  769. }
  770. return [
  771. 'spec_value_option' => !empty($spec_value_option) ? $spec_value_option : '',
  772. 'preset_name_option' => !empty($preset_name_option) ? $preset_name_option : '',
  773. ];
  774. } else if (!empty($post['action'])) {
  775. $where = [
  776. 'aid' => $handleAID,
  777. 'lang' => get_admin_lang(),
  778. 'spec_mark_id' => intval($specMarkID),
  779. ];
  780. $specDataHandle = Db::name('product_spec_data_handle')->where($where)->field('spec_is_select, spec_value_id, spec_name, spec_value')->select();
  781. $spec_value = '';
  782. $spec_value_option .= "<option value='0'>选择规格值</option>";
  783. foreach ($specDataHandle as $value) {
  784. if (0 === intval($value['spec_is_select'])) $spec_value_option .= "<option value='{$value['spec_value_id']}'>{$value['spec_value']}</option>";
  785. if (1 === intval($value['spec_is_select']) && intval($post['spec_value_id']) === intval($value['spec_value_id'])) {
  786. $spec_value = $value['spec_value'];
  787. }
  788. }
  789. if ('specName' === strval($post['action'])) {
  790. $where = [
  791. 'aid' => $handleAID,
  792. 'lang' => get_admin_lang(),
  793. ];
  794. $specMarkIDarr = Db::name('product_spec_data_handle')->where($where)->column('spec_mark_id');
  795. $specMarkIDarr = array_unique($specMarkIDarr);
  796. $where = [
  797. 'lang' => get_admin_lang(),
  798. 'preset_mark_id' => ['NOT IN', $specMarkIDarr],
  799. ];
  800. $field = 'preset_id, preset_mark_id, preset_name';
  801. $PresetName = Db::name('product_spec_preset')->where($where)->field($field)->group('preset_mark_id')->order('preset_mark_id desc')->select();
  802. $preset_name_option .= "<option value='0'>从规格库提取</option>";
  803. if (!empty($PresetName)) {
  804. // 拼装下拉选项
  805. foreach ($PresetName as $value) {
  806. $preset_name_option .= "<option value='{$value['preset_mark_id']}'>{$value['preset_name']}</option>";
  807. }
  808. }
  809. }
  810. return [
  811. 'spec_value' => !empty($spec_value) ? $spec_value : '',
  812. 'spec_value_option' => !empty($spec_value_option) ? $spec_value_option : '',
  813. 'preset_name_option' => !empty($preset_name_option) ? $preset_name_option : '',
  814. 'spec_name' => !empty($specDataHandle[0]['spec_name']) ? $specDataHandle[0]['spec_name'] : '',
  815. ];
  816. }
  817. }
  818. // 添加自定义规格名称
  819. public function addProductCustomSpecName($post = [])
  820. {
  821. if (!empty($post['spec_mark_id']) && !empty($post['set_spec_name'])) {
  822. $time = getTime();
  823. $handleAID = session('handleAID');
  824. $handleAID = !empty($post['aid']) ? $post['aid'] : $handleAID;
  825. $where = [
  826. 'aid' => $handleAID,
  827. 'spec_mark_id' => intval($post['spec_mark_id']),
  828. ];
  829. $update = [
  830. 'spec_name' => strval($post['set_spec_name']),
  831. 'update_time' => $time,
  832. ];
  833. $updateID = Db::name('product_spec_data_handle')->where($where)->update($update);
  834. if (!empty($updateID)) {
  835. // 查询商品的规格信息
  836. $where = [
  837. 'aid' => $handleAID,
  838. 'spec_is_select' => 1,
  839. ];
  840. $field = 'spec_mark_id, spec_value_id';
  841. $order = 'spec_value_id asc, spec_id asc';
  842. $data = Db::name('product_spec_data_handle')->field($field)->where($where)->order($order)->select();
  843. // 处理规格数组
  844. $spec_array = [];
  845. if (!empty($data)) {
  846. foreach ($data as $key => $value) {
  847. $spec_array[$value['spec_mark_id']][] = $value['spec_value_id'];
  848. }
  849. }
  850. // 返回结束
  851. return $spec_array;
  852. }
  853. }
  854. }
  855. // 添加自定义规格值
  856. public function addProductCustomSpecValue($post = [])
  857. {
  858. if (!empty($post['spec_mark_id']) && !empty($post['spec_value_id']) && !empty($post['set_spec_value'])) {
  859. $time = getTime();
  860. $handleAID = session('handleAID');
  861. $handleAID = !empty($post['aid']) ? $post['aid'] : $handleAID;
  862. $where = [
  863. 'aid' => $handleAID,
  864. 'spec_mark_id' => intval($post['spec_mark_id']),
  865. 'spec_value_id' => intval($post['spec_value_id']),
  866. ];
  867. $update = [
  868. 'spec_value' => strval($post['set_spec_value']),
  869. 'update_time' => $time,
  870. ];
  871. $updateID = Db::name('product_spec_data_handle')->where($where)->update($update);
  872. if (!empty($updateID)) {
  873. // 查询商品的规格信息
  874. $where = [
  875. 'aid' => $handleAID,
  876. 'spec_is_select' => 1,
  877. ];
  878. $field = 'spec_mark_id, spec_value_id';
  879. $order = 'spec_value_id asc, spec_id asc';
  880. $data = Db::name('product_spec_data_handle')->field($field)->where($where)->order($order)->select();
  881. // 处理规格数组
  882. $spec_array = [];
  883. if (!empty($data)) {
  884. foreach ($data as $key => $value) {
  885. $spec_array[$value['spec_mark_id']][] = $value['spec_value_id'];
  886. }
  887. }
  888. // 返回结束
  889. return $spec_array;
  890. }
  891. }
  892. }
  893. // 删除自定义规格
  894. public function delProductCustomSpec($post = [])
  895. {
  896. $handleAID = !empty($post['aid']) ? $post['aid'] : session('handleAID');
  897. // 清除整条规格信息
  898. if (in_array($post['del'], ['name', 'specName'])) {
  899. if (!empty($post['spec_mark_id'])) {
  900. $where = [
  901. 'aid' => $handleAID,
  902. 'spec_mark_id' => intval($post['spec_mark_id']),
  903. ];
  904. $deleteID = Db::name('product_spec_data_handle')->where($where)->delete(true);
  905. }
  906. }
  907. // 清除单个规格信息
  908. else if (in_array($post['del'], ['value'])) {
  909. if (!empty($post['spec_mark_id']) && !empty($post['spec_value_id'])) {
  910. $where = [
  911. 'aid' => $handleAID,
  912. 'spec_mark_id' => intval($post['spec_mark_id']),
  913. 'spec_value_id' => intval($post['spec_value_id']),
  914. ];
  915. $deleteID = Db::name('product_spec_data_handle')->where($where)->delete(true);
  916. }
  917. }
  918. // 清除单个规格信息
  919. else if (in_array($post['del'], ['specValue'])) {
  920. if (!empty($post['spec_mark_id']) && !empty($post['spec_value_id'])) {
  921. $where = [
  922. 'aid' => $handleAID,
  923. 'spec_mark_id' => intval($post['spec_mark_id']),
  924. 'spec_value_id' => intval($post['spec_value_id']),
  925. ];
  926. $update = [
  927. 'spec_is_select' => 0,
  928. 'update_time' => getTime(),
  929. ];
  930. $deleteID = Db::name('product_spec_data_handle')->where($where)->update($update);
  931. }
  932. }
  933. if (!empty($deleteID)) {
  934. // 查询商品的规格信息
  935. $where = [
  936. 'aid' => $handleAID,
  937. 'spec_is_select' => 1,
  938. ];
  939. $field = 'spec_mark_id, spec_value_id';
  940. $order = 'spec_value_id asc, spec_id asc';
  941. $data = Db::name('product_spec_data_handle')->field($field)->where($where)->order($order)->select();
  942. // 处理规格数组
  943. $spec_array = [];
  944. if (!empty($data)) {
  945. foreach ($data as $key => $value) {
  946. $spec_array[$value['spec_mark_id']][] = $value['spec_value_id'];
  947. }
  948. }
  949. // 返回结束
  950. return $spec_array;
  951. }
  952. }
  953. }