Ei kuvausta
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.

ProductSpecPreset.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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-7-9
  12. */
  13. namespace app\admin\model;
  14. use think\Model;
  15. use think\Config;
  16. use think\Db;
  17. /**
  18. * 产品规格预设模型
  19. */
  20. class ProductSpecPreset extends Model
  21. {
  22. //初始化
  23. protected function initialize()
  24. {
  25. // 需要调用`Model`的`initialize`方法
  26. parent::initialize();
  27. $this->admin_lang = get_admin_lang();
  28. }
  29. // 预设规格中最大的标记MarkId,结果+1返回
  30. public function GetMaxPresetMarkId()
  31. {
  32. $PresetMarkId = $this->where('lang',$this->admin_lang)->order('preset_mark_id desc')->getField('preset_mark_id');
  33. $PresetMarkId++;
  34. return $PresetMarkId;
  35. }
  36. public function GetPresetNewData($specArr = array(), $GetData = array())
  37. {
  38. $return = [
  39. 'Option' => '',
  40. 'Value' => '',
  41. ];
  42. $preset_ids = $specArr[$GetData['preset_mark_id']];
  43. if (empty($GetData['aid'])) {
  44. $WhereNew = [
  45. 'preset_id' => ['NOT IN', $preset_ids],
  46. 'preset_mark_id' => $GetData['preset_mark_id'],
  47. ];
  48. $return['Option'] = $this->where($WhereNew)->select();
  49. // 读取选中的规格值
  50. $return['Value'] = $this->where('preset_id', $GetData['preset_id'])->getField('preset_value');
  51. } else {
  52. // 更新规格值
  53. $Where = [
  54. 'aid' => $GetData['aid'],
  55. 'spec_mark_id' => $GetData['preset_mark_id'],
  56. 'spec_value_id' => $GetData['preset_id'],
  57. 'spec_is_select'=> 0,
  58. ];
  59. // 查询添加的规格值
  60. $return['Value'] = Db::name('product_spec_data_handle')->where($Where)->getField('spec_value');
  61. // 更新状态为选中
  62. Db::name('product_spec_data_handle')->where($Where)->update(['spec_is_select'=>1, 'update_time'=>getTime()]);
  63. // 拼装新的下拉框数据
  64. $WhereNew = [
  65. 'aid' => $GetData['aid'],
  66. 'spec_mark_id' => $GetData['preset_mark_id'],
  67. 'spec_is_select'=> 0,
  68. ];
  69. $Data = Db::name('product_spec_data_handle')->where($WhereNew)->field('spec_value_id, spec_value')->select();
  70. $return['Option'] .= "<option value='0'>选择规格值</option>";
  71. foreach ($Data as $value) {
  72. $return['Option'] .= "<option value='{$value['spec_value_id']}'>{$value['spec_value']}</option>";
  73. }
  74. }
  75. return $return;
  76. }
  77. // 批量添加产品规格名称、规格值、规格价格库存
  78. public function ProductSpecInsertAll($aid = null, $post = array())
  79. {
  80. if (!empty($aid) && !empty($post['preset_id'])) {
  81. $preset_ids = array_keys($post['preset_id']);
  82. $preset_mark_ids = array_keys($post['preset_mark_id']);
  83. $where = [
  84. 'lang' => get_admin_lang(),
  85. 'preset_mark_id' => ['IN', $preset_mark_ids],
  86. ];
  87. $PresetData = Db::name('product_spec_preset')->where($where)->select();
  88. $time = getTime();
  89. // 产品规格名称及规格值
  90. $AddSpecData = [];
  91. foreach ($PresetData as $key => $value) {
  92. $AddSpecData[$key] = [
  93. 'aid' => $aid,
  94. 'spec_mark_id' => $value['preset_mark_id'],
  95. 'spec_name' => $value['preset_name'],
  96. 'spec_value' => $value['preset_value'],
  97. 'spec_value_id' => $value['preset_id'],
  98. 'spec_is_select'=> 0,
  99. 'lang' => get_admin_lang(),
  100. 'add_time' => $time,
  101. 'update_time' => $time,
  102. ];
  103. if (in_array($value['preset_id'], $preset_ids)) {
  104. $AddSpecData[$key]['spec_is_select'] = 1;
  105. }
  106. }
  107. Db::name('product_spec_data')->insertAll($AddSpecData);
  108. // 产品规格价格及规格库存
  109. $AddSpecValue = [];
  110. foreach ($post['preset_price'] as $kkk => $vvv) {
  111. $AddSpecValue[] = [
  112. 'aid' => $aid,
  113. 'spec_value_id' => $kkk,
  114. 'spec_price' => $vvv['users_price'],
  115. 'spec_stock' => $post['preset_stock'][$kkk]['stock_count'],
  116. 'spec_sales_num' => $post['preset_sales'][$kkk]['spec_sales_num'],
  117. 'lang' => get_admin_lang(),
  118. 'add_time' => $time,
  119. 'update_time' => $time,
  120. ];
  121. }
  122. Db::name('product_spec_value')->insertAll($AddSpecValue);
  123. // 删除商品添加时产生的废弃规格
  124. $del_spec = session('del_spec') ? session('del_spec') : [];
  125. if (!empty($del_spec)) {
  126. $del_spec = array_unique($del_spec);
  127. $where = [
  128. 'product_add' => 1,
  129. 'preset_mark_id' => ['IN', $del_spec]
  130. ];
  131. Db::name('product_spec_preset')->where($where)->delete(true);
  132. // 清除 session
  133. session('del_spec', null);
  134. }
  135. }
  136. }
  137. }