diff --git a/.env b/.env index d30629e..b58ff3f 100755 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ -APP_DEBUG = false +APP_DEBUG = true [APP] DEFAULT_TIMEZONE = Asia/Shanghai @@ -11,7 +11,7 @@ USERNAME = youda_test PASSWORD = youda_test HOSTPORT = 3306 CHARSET = utf8 -DEBUG = false +DEBUG = true [LANG] default_lang = zh-cn diff --git a/app/admin/controller/Goods.php b/app/admin/controller/Goods.php index 0527789..4cca38e 100755 --- a/app/admin/controller/Goods.php +++ b/app/admin/controller/Goods.php @@ -560,22 +560,141 @@ class Goods extends Base #赏品 $goods_list = GoodsList::where(['goods_id' => $info['id']]) ->where(['num' => 1]) + ->where('goods_list_id', '=', 0) // 只查询父奖品 ->select()->toArray(); if ($goods_list) { #循环数据 $save_sports_data = []; $start_num = $info['stock'] + 1; + + // 获取宝箱类型的父奖品记录及其prize_code + $box_prize_codes = []; + foreach ($goods_list as $item) { + if ($item['goods_type'] == 4) { + $box_prize_codes[$item['prize_code']] = true; + } + } + + // 先处理父奖品(包括宝箱类型的父奖品) for ($i = $start_num; $i <= $data['stock']; $i++) { foreach ($goods_list as $k => $v) { unset($v['id']); unset($v['num']); $v['num'] = $i; $v['surplus_stock'] = $v['stock']; + // 保持prize_code不变,这样可以关联到同一套奖品 + // 添加到待插入数组 $save_sports_data[] = $v; } } - #添加赏品 - $res[] = GoodsList::insertAll($save_sports_data); + + // 先插入父奖品 + if (!empty($save_sports_data)) { + $res[] = GoodsList::insertAll($save_sports_data); + } + + // 如果有宝箱类型的奖品,需要处理子奖品 + if (!empty($box_prize_codes)) { + // 处理所有新套数的子奖品 + for ($i = $start_num; $i <= $data['stock']; $i++) { + // 先获取当前套数下所有新插入的宝箱类型父奖品 + $new_box_parents = GoodsList::where([ + 'goods_id' => $info['id'], + 'num' => $i, + 'goods_type' => 4, + 'goods_list_id' => 0 + ]) + ->select() + ->toArray(); + + if (empty($new_box_parents)) { + continue; // 如果没有宝箱父奖品,跳过 + } + + // 遍历每个宝箱父奖品 + foreach ($new_box_parents as $new_box_parent) { + // 找到第一套中相同prize_code的宝箱父奖品 + $original_parent = GoodsList::where([ + 'goods_id' => $info['id'], + 'num' => 1, + 'goods_type' => 4, + 'prize_code' => $new_box_parent['prize_code'], + 'goods_list_id' => 0 + ]) + ->find(); + + if (!$original_parent) { + continue; // 找不到原始父奖品,跳过 + } + + // 获取原始父奖品的所有子奖品 + $child_items = GoodsList::where([ + 'goods_id' => $info['id'], + 'goods_list_id' => $original_parent['id'] + ]) + ->select() + ->toArray(); + + if (empty($child_items)) { + continue; // 没有子奖品,跳过 + } + + // 复制所有子奖品到新的套数,每个子奖品创建新的prize_code + $child_save_data = []; + foreach ($child_items as $child_item) { + // 查找所有套数中是否已经有相同奖品的同一个子奖品 + $existing_children = []; + for ($j = 1; $j < $i; $j++) { + // 找到对应套数的父奖品 + $existing_parent = GoodsList::where([ + 'goods_id' => $info['id'], + 'num' => $j, + 'prize_code' => $new_box_parent['prize_code'], + 'goods_list_id' => 0 + ]) + ->find(); + + if ($existing_parent) { + // 找到这个父奖品下与当前子奖品对应的子奖品 + $similar_child = GoodsList::where([ + 'goods_id' => $info['id'], + 'num' => $j, + 'goods_list_id' => $existing_parent['id'], + 'title' => $child_item['title'], // 使用标题匹配相似子奖品 + 'shang_id' => $child_item['shang_id'] // 确保奖品类型相同 + ]) + ->find(); + + if ($similar_child) { + $existing_children[] = $similar_child; + } + } + } + + $child_data = $child_item; + unset($child_data['id']); + $child_data['num'] = $i; // 设置新的套数 + $child_data['goods_list_id'] = $new_box_parent['id']; // 关联到新的父奖品 + $child_data['surplus_stock'] = $child_data['stock']; // 重置库存 + + // 如果存在其他套数的相同子奖品,使用相同的prize_code + if (!empty($existing_children)) { + $child_data['prize_code'] = $existing_children[0]['prize_code']; + } else { + // 否则生成新的prize_code + $child_data['prize_code'] = getPrizeCode() . '_' . time() . '_' . mt_rand(1000, 9999); + } + + $child_save_data[] = $child_data; + } + + // 批量插入子奖品 + if (!empty($child_save_data)) { + $res[] = GoodsList::insertAll($child_save_data); + } + } + } + } } } } @@ -611,10 +730,7 @@ class Goods extends Base $result = GoodsModel::where(['id' => $id])->update(['status' => $status]); } elseif ($status == 3) { $result = GoodsModel::where(['id' => $id])->update(['delete_time' => time(), 'status' => 2]); - // $goods = GoodsList::field('id')->where(['goods_id' => $id])->find(); -// if ($goods) { -// GoodsList::field('id')->where(['goods_id' => $id])->delete(); -// } + } else { return $this->renderError("请求参数错误"); } @@ -797,7 +913,6 @@ class Goods extends Base //2023-11-17无作用 $data['reward_num'] = 0; // - if ($type == 1 || $type == 5 || $type == 10 || $type == 6 || $type == 11) { if ($data['shang_id'] == 1 || $data['shang_id'] == 2 || $data['shang_id'] == 3 || $data['shang_id'] == 5) { if ($data['stock'] != 1) { @@ -812,11 +927,88 @@ class Goods extends Base $stock = $info['stock']; #循环数据 $save_sports_data = []; - $prize_code = getPrizeCode() . '_' . time(); - for ($i = 1; $i <= $stock; $i++) { - $data['num'] = $i; - $data['prize_code'] = $prize_code; - $save_sports_data[] = $data; + $prize_code = getPrizeCode() . '_' . time(); // 生成唯一的prize_code + + // 处理子奖品添加的情况 + $goods_list_id = $request->param('goods_list_id/d', 0); // 获取父奖品ID,如果是子奖品 + $is_box_type = ($data['goods_type'] == 4); // 是否为宝箱类型 + + if ($goods_list_id > 0) { + // 获取父奖品信息,确保父奖品存在 + $parent_prize = GoodsList::where('id', $goods_list_id)->find(); + if (!$parent_prize) { + return $this->renderError('宝箱父奖品不存在'); + } + + // 子奖品应该生成自己的prize_code + $data['prize_code'] = getPrizeCode() . '_' . time() . '_' . mt_rand(1000, 9999); + $data['goods_list_id'] = $goods_list_id; + $data['num'] = $parent_prize['num']; + + // 如果父奖品存在于多个套数中,需要为每个套数创建对应的子奖品 + $same_prize_parents = GoodsList::where([ + 'goods_id' => $data['goods_id'], + 'prize_code' => $parent_prize['prize_code'], + 'goods_list_id' => 0 // 确保只查询父奖品 + ]) + ->select() + ->toArray(); + + if (count($same_prize_parents) > 1) { + // 有多个套数的相同父奖品 + $multi_data = []; + + foreach ($same_prize_parents as $same_parent) { + if ($same_parent['id'] == $goods_list_id) { + // 跳过当前指定的父奖品,因为下面要单独处理 + continue; + } + + $child_data = $data; + $child_data['goods_list_id'] = $same_parent['id']; + $child_data['num'] = $same_parent['num']; + $multi_data[] = $child_data; + } + + // 先插入指定父奖品的子奖品 + $res = GoodsList::insert($data); + + // 再插入其他套数的子奖品 + if (!empty($multi_data)) { + GoodsList::insertAll($multi_data); + } + } else { + // 只有一个套数,直接保存 + $res = GoodsList::insert($data); + } + } + else { + // 添加正常奖品或宝箱父奖品,为所有套数都添加 + $data['prize_code'] = $prize_code; // 使用新生成的prize_code + for ($i = 1; $i <= $stock; $i++) { + $data['num'] = $i; + $save_sports_data[] = $data; + } + + // 批量插入奖品 + $res = GoodsList::insertAll($save_sports_data); + + // 如果是宝箱类型,则需要记录下插入后的ID,以供后续可能添加的子奖品使用 + if ($is_box_type && $res) { + // 获取已插入的宝箱奖品ID + $inserted_boxes = GoodsList::where('prize_code', $prize_code) + ->where('goods_id', $data['goods_id']) + ->where('goods_type', 4) + ->select()->toArray(); + + // 通知前端已成功添加宝箱类型,提示可继续添加子奖品 + if (!empty($inserted_boxes)) { + return $this->renderSuccess('添加成功,您可以继续为此宝箱添加子奖品', [ + 'box_ids' => array_column($inserted_boxes, 'id') + ]); + } + } + return $this->renderSuccess('添加成功'); } } elseif (in_array($type, [2, 8, 9, 16])) { if (RegMoney($data['real_pro'] * 10000)) { @@ -839,12 +1031,89 @@ class Goods extends Base $stock = $info['stock']; #循环数据 $save_sports_data = []; - $prize_code = getPrizeCode() . '_' . time(); - for ($i = 1; $i <= $stock; $i++) { - $data['num'] = $i; - $data['prize_code'] = $prize_code; - $save_sports_data[] = $data; + $prize_code = getPrizeCode() . '_' . time(); // 生成唯一的prize_code + + // 处理子奖品添加的情况 + $goods_list_id = $request->param('goods_list_id/d', 0); // 获取父奖品ID,如果是子奖品 + $is_box_type = ($data['goods_type'] == 4); // 是否为宝箱类型 + + if ($goods_list_id > 0) { + // 获取父奖品信息,确保父奖品存在 + $parent_prize = GoodsList::where('id', $goods_list_id)->find(); + if (!$parent_prize) { + return $this->renderError('宝箱父奖品不存在'); + } + + // 子奖品应该生成自己的prize_code + $data['prize_code'] = getPrizeCode() . '_' . time() . '_' . mt_rand(1000, 9999); + $data['goods_list_id'] = $goods_list_id; + $data['num'] = $parent_prize['num']; + + // 如果父奖品存在于多个套数中,需要为每个套数创建对应的子奖品 + $same_prize_parents = GoodsList::where([ + 'goods_id' => $data['goods_id'], + 'prize_code' => $parent_prize['prize_code'], + 'goods_list_id' => 0 // 确保只查询父奖品 + ]) + ->select() + ->toArray(); + + if (count($same_prize_parents) > 1) { + // 有多个套数的相同父奖品 + $multi_data = []; + + foreach ($same_prize_parents as $same_parent) { + if ($same_parent['id'] == $goods_list_id) { + // 跳过当前指定的父奖品,因为下面要单独处理 + continue; + } + + $child_data = $data; + $child_data['goods_list_id'] = $same_parent['id']; + $child_data['num'] = $same_parent['num']; + $multi_data[] = $child_data; + } + + // 先插入指定父奖品的子奖品 + $res = GoodsList::insert($data); + + // 再插入其他套数的子奖品 + if (!empty($multi_data)) { + GoodsList::insertAll($multi_data); + } + } else { + // 只有一个套数,直接保存 + $res = GoodsList::insert($data); + } } + else { + // 添加正常奖品或宝箱父奖品,为所有套数都添加 + $data['prize_code'] = $prize_code; // 使用新生成的prize_code + for ($i = 1; $i <= $stock; $i++) { + $data['num'] = $i; + $save_sports_data[] = $data; + } + + // 批量插入奖品 + $res = GoodsList::insertAll($save_sports_data); + + // 如果是宝箱类型,则需要记录下插入后的ID,以供后续可能添加的子奖品使用 + if ($is_box_type && $res) { + // 获取已插入的宝箱奖品ID + $inserted_boxes = GoodsList::where('prize_code', $prize_code) + ->where('goods_id', $data['goods_id']) + ->where('goods_type', 4) + ->select()->toArray(); + + // 通知前端已成功添加宝箱类型,提示可继续添加子奖品 + if (!empty($inserted_boxes)) { + return $this->renderSuccess('添加成功,您可以继续为此宝箱添加子奖品', [ + 'box_ids' => array_column($inserted_boxes, 'id') + ]); + } + } + } + if (RegInt($data['prize_num']) && $data['shang_id'] == 5) { return $this->renderError("擂台赏抽全局赏数量设置错误,请设置大于0的整数"); } @@ -981,6 +1250,7 @@ class Goods extends Base // if ($type == 1 || $type == 5 || $type == 10 || $type == 6 || $type == 11 || $type == 15) { + } elseif ($type == 2 || $type == 8 || $type == 9 || $type == 16 || $type == 17) { if (RegMoney($data['real_pro'] * 10000)) { @@ -1603,7 +1873,8 @@ class Goods extends Base $goodsData['status'] = 2; // 默认下架状态 $goodsData['addtime'] = time(); $goodsData['async_code'] = ''; // 清空同步代码,避免混淆 - $goodsData['is_open'] = 0; + $goodsData['is_open'] = 0; + $goodsData['king_user_id'] = 0; // 插入新盒子 $newGoods = new GoodsModel(); $newGoods->save($goodsData); @@ -1619,77 +1890,144 @@ class Goods extends Base $newExtend->save($extendData); } - // 创建ID映射表,用于处理子奖品关联 - $idMapping = []; + // 创建父奖品prize_code映射表 + $parentPrizeCodeMapping = []; + + // 创建子奖品prize_code映射表 + $childPrizeCodeMapping = []; - // 复制盒子奖品 - $goodsLists = GoodsList::where(['goods_id' => $id, 'goods_list_id' => 0])->select(); - foreach ($goodsLists as $item) { - $listData = $item->toArray(); - $oldId = $item['id']; // 保存原始ID,用于建立映射 - unset($listData['id']); - $listData['goods_id'] = $newGoodsId; - $listData['addtime'] = time(); - $listData['surplus_stock'] = $listData['stock']; - - // 处理奖励ID - if ($listData['reward_id'] && $listData['reward_id'] != '') { - $reward = Reward::where(['reward_id' => $listData['reward_id']])->select(); - $listData['reward_id'] = 'MHHZ' . date('YmdHis') . mt_rand(1000, 9999); - if ($reward) { - $rewards = $reward->toArray(); - foreach ($rewards as $item2) { - unset($item2['id']); - $item2['reward_id'] = $listData['reward_id']; - $item2['create_time'] = time(); - $item2['update_time'] = time(); - Reward::insert($item2); - } - } + // 首先获取所有的父奖品,并按prize_code分组 + $parentPrizes = GoodsList::where(['goods_id' => $id, 'goods_list_id' => 0]) + ->select() + ->toArray(); + + $prizeCodeGroups = []; + foreach ($parentPrizes as $prize) { + if (!isset($prizeCodeGroups[$prize['prize_code']])) { + $prizeCodeGroups[$prize['prize_code']] = []; } - - // 插入新奖品 - $newGoodsList = new GoodsList(); - $newGoodsList->save($listData); - - // 记录ID映射关系 - $idMapping[$oldId] = $newGoodsList->id; + $prizeCodeGroups[$prize['prize_code']][] = $prize; } - - // 复制子奖品(宝箱类型的子奖品) - $childItems = GoodsList::where(['goods_id' => $id])->where('goods_list_id', '>', 0)->select(); - foreach ($childItems as $childItem) { - $childData = $childItem->toArray(); - $originalParentId = $childData['goods_list_id']; - unset($childData['id']); - $childData['goods_id'] = $newGoodsId; - $childData['addtime'] = time(); - $childData['surplus_stock'] = $childData['stock']; - - // 更新goods_list_id为新系统中的ID - if (isset($idMapping[$originalParentId])) { - $childData['goods_list_id'] = $idMapping[$originalParentId]; - } - - // 处理奖励ID - if ($childData['reward_id'] && $childData['reward_id'] != '') { - $reward = Reward::where(['reward_id' => $childData['reward_id']])->select(); - $childData['reward_id'] = 'MHHZ' . date('YmdHis') . mt_rand(1000, 9999); - if ($reward) { - $rewards = $reward->toArray(); - foreach ($rewards as $item2) { - unset($item2['id']); - $item2['reward_id'] = $childData['reward_id']; - $item2['create_time'] = time(); - $item2['update_time'] = time(); - Reward::insert($item2); + + // 为每个父prize_code组创建新的prize_code + foreach ($prizeCodeGroups as $oldPrizeCode => $prizes) { + $newPrizeCode = 'MHHZ' . date('YmdHis') . mt_rand(1000, 9999); + $parentPrizeCodeMapping[$oldPrizeCode] = $newPrizeCode; + + // 复制每个prize_code组的奖品 + foreach ($prizes as $prize) { + $newPrizeData = $prize; + unset($newPrizeData['id']); + $newPrizeData['goods_id'] = $newGoodsId; + $newPrizeData['prize_code'] = $newPrizeCode; + $newPrizeData['addtime'] = time(); + $newPrizeData['surplus_stock'] = $newPrizeData['stock']; + + // 处理奖励ID + if ($newPrizeData['reward_id'] && $newPrizeData['reward_id'] != '') { + $reward = Reward::where(['reward_id' => $newPrizeData['reward_id']])->select(); + $newPrizeData['reward_id'] = 'MHHZ' . date('YmdHis') . mt_rand(1000, 9999); + if ($reward) { + $rewards = $reward->toArray(); + foreach ($rewards as $item2) { + unset($item2['id']); + $item2['reward_id'] = $newPrizeData['reward_id']; + $item2['create_time'] = time(); + $item2['update_time'] = time(); + Reward::insert($item2); + } } } + + // 插入新奖品 + $newGoodsList = new GoodsList(); + $newGoodsList->save($newPrizeData); + } + } + + // 获取所有子奖品 + $childPrizes = GoodsList::where(['goods_id' => $id]) + ->where('goods_list_id', '>', 0) + ->select() + ->toArray(); + + // 对子奖品按prize_code分组 + $childGroups = []; + foreach ($childPrizes as $child) { + $childPrizeCode = $child['prize_code']; + if (!isset($childGroups[$childPrizeCode])) { + $childGroups[$childPrizeCode] = []; + } + $childGroups[$childPrizeCode][] = $child; + } + + // 为每组子奖品创建新的prize_code映射 + foreach ($childGroups as $oldChildCode => $group) { + $newChildCode = 'MHHZ' . date('YmdHis') . mt_rand(1000, 9999); + $childPrizeCodeMapping[$oldChildCode] = $newChildCode; + } + + // 处理每个子奖品 + foreach ($childPrizes as $childPrize) { + // 查找子奖品对应的父奖品 + $parentPrize = GoodsList::where('id', $childPrize['goods_list_id'])->find(); + if (!$parentPrize) { + continue; // 如果找不到父奖品,跳过 + } + + // 获取父奖品的prize_code + $parentPrizeCode = $parentPrize['prize_code']; + + // 如果父奖品的prize_code有映射,则复制子奖品 + if (isset($parentPrizeCodeMapping[$parentPrizeCode])) { + // 找到新盒子中对应的父奖品 + $newParent = GoodsList::where([ + 'goods_id' => $newGoodsId, + 'prize_code' => $parentPrizeCodeMapping[$parentPrizeCode], + 'num' => $parentPrize['num'] // 确保套数也匹配 + ])->find(); + + if ($newParent) { + // 复制子奖品数据 + $newChildData = $childPrize; + unset($newChildData['id']); + $newChildData['goods_id'] = $newGoodsId; + $newChildData['goods_list_id'] = $newParent['id']; // 关联到新的父奖品 + + // 使用子奖品的prize_code映射,确保相同的子奖品有相同的prize_code + if (isset($childPrizeCodeMapping[$childPrize['prize_code']])) { + $newChildData['prize_code'] = $childPrizeCodeMapping[$childPrize['prize_code']]; + } else { + // 如果没有映射,创建一个新的 + $newChildCode = 'MHHZ' . date('YmdHis') . mt_rand(1000, 9999); + $childPrizeCodeMapping[$childPrize['prize_code']] = $newChildCode; + $newChildData['prize_code'] = $newChildCode; + } + + $newChildData['addtime'] = time(); + $newChildData['surplus_stock'] = $newChildData['stock']; + + // 处理奖励ID + if ($newChildData['reward_id'] && $newChildData['reward_id'] != '') { + $reward = Reward::where(['reward_id' => $newChildData['reward_id']])->select(); + $newChildData['reward_id'] = 'MHHZ' . date('YmdHis') . mt_rand(1000, 9999); + if ($reward) { + $rewards = $reward->toArray(); + foreach ($rewards as $item2) { + unset($item2['id']); + $item2['reward_id'] = $newChildData['reward_id']; + $item2['create_time'] = time(); + $item2['update_time'] = time(); + Reward::insert($item2); + } + } + } + + // 插入新的子奖品 + $newChildList = new GoodsList(); + $newChildList->save($newChildData); + } } - - // 插入子奖品 - $newChildList = new GoodsList(); - $newChildList->save($childData); } Db::commit(); diff --git a/app/admin/controller/Statistics.php b/app/admin/controller/Statistics.php index a6813fb..0aff64a 100755 --- a/app/admin/controller/Statistics.php +++ b/app/admin/controller/Statistics.php @@ -180,8 +180,16 @@ class Statistics extends Base $lirun = round($orderStats->order_zhe_total - $goodslistMoney, 2); $liruns = round(($orderStats->count_price + $orderStats->count_yue + $orderStats->use_integral + $orderStats->use_score + $orderStats->use_coupon) - $goodslistMoney, 2); - $order_zhe_total=$orderStats->order_zhe_total; - $order_total=($orderStats->count_price + $orderStats->count_yue + $orderStats->use_integral + $orderStats->use_score + $orderStats->use_coupon); + $order_zhe_total = $orderStats->order_zhe_total; + // $order_zhe_total 转int + $order_zhe_total = intval($order_zhe_total); + if ($order_zhe_total <= 0) { + $order_zhe_total = 1; + } + $order_total = ($orderStats->count_price + $orderStats->count_yue + $orderStats->use_integral + $orderStats->use_score + $orderStats->use_coupon); + if ($order_total <= 0) { + $order_total = 1; + } return [ 'order_count' => round($orderStats->count, 2), 'count_price' => round($orderStats->count_price, 2), @@ -192,12 +200,7 @@ class Statistics extends Base 'count_use_coupon' => round($orderStats->use_coupon, 2), 'count_heji' => round($orderStats->count_price + $orderStats->count_yue + $orderStats->use_integral + $orderStats->use_score + $orderStats->use_coupon, 2), 'order_zhe_total' => round($orderStats->order_zhe_total, 2), - // 订单列表数量 'count_OrderList' => OrderList::where($whe2)->count(), // ✅ 添加这一行 - - // 订单总价验证 - // 'count_heji_yanzheng' => $count_OrderList * $value['price'], // ✅ 使用 count_OrderList - 'goodslist_price' => round($goodslistMoney, 2), 'lirun' => $lirun, 'liruns' => $liruns, diff --git a/app/admin/view/Goods/goods.html b/app/admin/view/Goods/goods.html index 21ee232..7107a8d 100755 --- a/app/admin/view/Goods/goods.html +++ b/app/admin/view/Goods/goods.html @@ -1,7 +1,7 @@ {include file="Public:header3"/}
-