manghe/app/admin/controller/Goods.php
2025-04-10 03:53:07 +08:00

1949 lines
74 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\common\model\Category;
use app\common\model\Yushou;
use \think\Request;
use think\facade\View;
use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsList;
use app\common\model\AdminGoodsLog;
use think\facade\Db;
use app\common\model\Shang;
use app\common\model\Reward;
use app\common\model\GoodsExtend;
class Goods extends Base
{
/**
* 盒子列表
*/
public function goods(Request $request)
{
return View::fetch("Goods/goods");
}
/**
* 获取盒子列表数据(前后端分离接口)
*/
public function getGoodsList(Request $request)
{
$page = $request->param('page/d', 1);
$limit = $request->param('limit/d', 20);
$title = $request->param('title/s', '');
$status = $request->param('status/s', '');
$type = $request->param('type/s', '');
// 构建查询条件
$where = [];
if (!empty($title)) {
$where[] = ['title', 'like', "%{$title}%"];
}
if ($status !== '') {
$where[] = ['status', '=', $status];
}
if ($type !== '') {
$where[] = ['type', '=', $type];
}
$query = GoodsModel::where($where)->order('id desc');
$count = $query->count();
$list = $query->page($page, $limit)->select()->toArray();
// 获取盒子ID集合
$goodsIds = array_column($list, 'id');
// 获取盒子扩展信息
$goodsExtendList = [];
if (!empty($goodsIds)) {
$goodsExtends = \app\common\model\GoodsExtend::whereIn('goods_id', $goodsIds)->select()->toArray();
foreach ($goodsExtends as $extend) {
$goodsExtendList[$extend['goods_id']] = $extend;
}
}
// 处理图片路径
foreach ($list as &$item) {
$item['imgurl'] = imageUrl($item['imgurl']);
if (!empty($item['imgurl_detail'])) {
$item['imgurl_detail'] = imageUrl($item['imgurl_detail']);
}
// 添加格式化时间
$item['addtime_text'] = date('Y-m-d H:i', $item['addtime']);
// 添加扩展信息
$item['goods_extend'] = isset($goodsExtendList[$item['id']]) ? $goodsExtendList[$item['id']] : null;
// 修改时间处理
if (isset($item['update_time']) && $item['update_time']) {
$item['update_time_text'] = date('Y-m-d H:i', $item['update_time']);
}
}
return $this->renderTable('获取成功', $count, $list);
}
/**
* 获取盒子类型列表(前后端分离接口)
*/
public function getGoodsTypeList()
{
// 查询可用的盒子类型
$goodsTypeList = Db::name('goods_type')
->field('value,sort_order,remark,is_fenlei,fl_name,pay_wechat,pay_balance,pay_currency,pay_currency2,pay_coupon,is_deduction')
->where('is_fenlei', 1)
->order('sort_order')
->select()
->toArray();
return $this->renderTable('获取成功', count($goodsTypeList), $goodsTypeList);
}
/**
* 添加盒子
*/
public function goods_add(Request $request)
{
if (!$request->isPost()) {
$item_card = \app\common\model\ItemCard::where('id', '<', 3)->select()->toArray();
$shang = Shang::where('id', 'between', [34, 38])->select()->toArray();
// 查询可用的盒子类型
$goodsTypeList = Db::name('goods_type')
->field('value,sort_order,remark,is_fenlei,fl_name')
->where('is_fenlei', 1)
->order('sort_order')
->select()
->toArray();
View::assign('goodsTypeList', $goodsTypeList);
View::assign('shang', $shang);
View::assign('item_card', $item_card);
return View::fetch("Goods/goods_add");
} else {
$data = input('post.');
if (empty($data['title'])) {
return $this->renderError("请输入盒子名称");
}
if (RegMoney($data['price'])) {
return $this->renderError("盒子价格输入不规范,最多保留两位小数");
}
if (RegZero($data['sort'])) {
return $this->renderError("排序输入不规范,请输入整数");
}
// 检查daily_xiangou
if (!isset($data['daily_xiangou']) || $data['daily_xiangou'] === '') {
$data['daily_xiangou'] = 0;
} else if (!is_numeric($data['daily_xiangou']) || intval($data['daily_xiangou']) < 0) {
return $this->renderError("每日限购次数必须是非负整数");
} else {
$data['daily_xiangou'] = intval($data['daily_xiangou']);
}
if ($data['type'] == 1 || $data['type'] == 11) {
if (RegInt($data['stock'])) {
return $this->renderError("库存输入不规范,请设置大于0的整数");
}
if ($data['lock_is'] == 1) {
if (RegInt($data['lock_time'])) {
return $this->renderError("请设置锁箱时间");
}
} elseif ($data['lock_is'] == 0) {
$data['lock_time'] = 0;
} else {
return $this->renderError("锁箱类型选择错误");
}
if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
return $this->renderError("发积分开关选择错误");
}
if ($data['show_is'] != 0 && $data['show_is'] != 1) {
return $this->renderError("首页显示开关选择错误");
}
$data['prize_num'] = 0;
$data['category_id'] = 0;
} elseif ($data['type'] == 2 || $data['type'] == 16 || $data['type'] == 17) {
$data['stock'] = 0;
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['show_is'] = 0;
$data['prize_num'] = 0;
if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
return $this->renderError("发积分开关选择错误");
}
$data['category_id'] = 0;
} elseif ($data['type'] == 3) {
$data['stock'] = 1;
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['show_is'] = 0;
$data['category_id'] = 0;
$data['prize_num'] = 0;
// if (RegInt($data['prize_num'])) {
// return $this->renderError("擂台赏抽全局赏数量设置错误,请设置大于0的整数");
// }
if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
return $this->renderError("发积分开关选择错误");
}
} elseif ($data['type'] == 5) {
if (RegInt($data['stock'])) {
return $this->renderError("库存输入不规范,请设置大于0的整数");
}
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['prize_num'] = 0;
$data['show_is'] = 0;
$data['coupon_is'] = 0;
$data['coupon_pro'] = 0;
$data['integral_is'] = 0;
$data['category_id'] = 0;
} elseif ($data['type'] == 6) {
if (RegInt($data['stock'])) {
return $this->renderError("库存输入不规范,请设置大于0的整数");
}
if ($data['lock_is'] == 1) {
if (RegInt($data['lock_time'])) {
return $this->renderError("请设置锁箱时间");
}
} elseif ($data['lock_is'] == 0) {
$data['lock_time'] = 0;
} else {
return $this->renderError("锁箱类型选择错误");
}
if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
return $this->renderError("发积分开关选择错误");
}
if ($data['show_is'] != 0 && $data['show_is'] != 1) {
return $this->renderError("首页显示开关选择错误");
}
$data['prize_num'] = 0;
$data['category_id'] = 0;
} elseif ($data['type'] == 8) {
$data['stock'] = 0;
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['show_is'] = 0;
$data['prize_num'] = 0;
if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
return $this->renderError("发积分开关选择错误");
}
$data['category_id'] = 0;
} elseif ($data['type'] == 9) {
$data['stock'] = 0;
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['show_is'] = 0;
$data['prize_num'] = 0;
if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
return $this->renderError("发积分开关选择错误");
}
$data['category_id'] = 0;
} elseif ($data['type'] == 10) {
if (RegInt($data['stock'])) {
return $this->renderError("库存输入不规范,请设置大于0的整数");
}
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['prize_num'] = 0;
$data['show_is'] = 0;
$data['coupon_is'] = 0;
$data['coupon_pro'] = 0;
$data['integral_is'] = 0;
$data['category_id'] = 0;
$data['stock'] = 1;
} elseif ($data['type'] == 15) {
// 福利屋类型处理
$data['stock'] = 0; // 隐藏套数
$data['lock_is'] = 0; // 隐藏锁箱类型
$data['lock_time'] = 0; // 隐藏锁箱时间
$data['coupon_is'] = 0; // 隐藏发券开关
$data['coupon_pro'] = 0; // 隐藏发券概率
$data['integral_is'] = 0; // 隐藏发积分开关
$data['prize_num'] = 0;
$data['category_id'] = 0;
$data['is_flw'] = 1; // 标记为福利屋
// 处理时间字段
if (!empty($data['flw_start_time'])) {
$data['flw_start_time'] = strtotime($data['flw_start_time']);
} else {
return $this->renderError("请选择开始时间");
}
if (!empty($data['flw_end_time'])) {
$data['flw_end_time'] = strtotime($data['flw_end_time']);
} else {
return $this->renderError("请选择结束时间");
}
if (!empty($data['open_time'])) {
$data['open_time'] = strtotime($data['open_time']);
} else {
return $this->renderError("请选择开奖时间");
}
// 验证时间逻辑
if ($data['flw_start_time'] >= $data['flw_end_time']) {
return $this->renderError("开始时间必须早于结束时间");
}
if ($data['flw_end_time'] >= $data['open_time']) {
return $this->renderError("结束时间必须早于开奖时间");
}
// 验证抽奖门槛
if (!isset($data['choujiang_xianzhi']) || $data['choujiang_xianzhi'] === '') {
$data['choujiang_xianzhi'] = 0;
} else if (!is_numeric($data['choujiang_xianzhi']) || intval($data['choujiang_xianzhi']) < 0) {
return $this->renderError("抽奖门槛必须是非负整数");
}
// 默认未开奖
$data['is_open'] = 0;
} else {
return $this->renderError("盒子类型选择错误");
}
$data['sale_time'] = 0;
// if (isset($data['sale_time'])) {
// $data['sale_time'] = strtotime($data['sale_time']);
// } else {
// $data['sale_time'] = 0;
// }
if (empty($data['imgurl'])) {
return $this->renderError("请上传盒子封面图");
}
if (empty($data['imgurl_detail'])) {
return $this->renderError("请上传盒子详情图");
}
$data['status'] = 2;
$data['addtime'] = time();
$dd = GoodsModel::insert($data);
if ($dd) {
return $this->renderSuccess("添加成功");
} else {
return $this->renderError("网络繁忙,请稍后");
}
}
}
/**
* 编辑盒子
*/
public function goods_edit(Request $request)
{
if (!$request->isPost()) {
$id = $request->param('id/d', 0);
$info = GoodsModel::where(['id' => $id])->find();
if (!$info) {
return $this->renderError("请求参数错误");
}
$info['sale_time'] = '';
// if ($info['sale_time']) {
// $info['sale_time'] = date('Y-m-d', $info['sale_time']);
// } else {
// $info['sale_time'] = '';
// }
$type = $info['type'];
$item_card = \app\common\model\ItemCard::where('id', '<', 3)->select()->toArray();
$shang = Shang::where('id', 'between', [34, 38])->select()->toArray();
// 查询可用的盒子类型
$goodsTypeList = Db::name('goods_type')
->field('value,sort_order,remark,is_fenlei,fl_name')
->where('is_fenlei', 1)
->order('sort_order')
->select()
->toArray();
View::assign('goodsTypeList', $goodsTypeList);
View::assign('shang', $shang);
View::assign('item_card', $item_card);
View::assign('type', $type);
View::assign('info', $info);
// 确保daily_xiangou字段存在
if (!isset($info['daily_xiangou'])) {
$info['daily_xiangou'] = 0;
}
return View::fetch("Goods/goods_edit");
} else {
$data = input('post.');
if (empty($data['id'])) {
return $this->renderError("请求参数错误");
}
$info = GoodsModel::where(['id' => $data['id']])->find();
if (!$info) {
return $this->renderError("请求参数错误1");
}
if (empty($data['title'])) {
return $this->renderError("请输入盒子名称");
}
if (RegMoney($data['price'])) {
return $this->renderError("盒子价格输入不规范,最多保留两位小数");
}
if (RegZero($data['sort'])) {
return $this->renderError("排序输入不规范,请输入整数");
}
// 检查daily_xiangou
if (!isset($data['daily_xiangou']) || $data['daily_xiangou'] === '') {
$data['daily_xiangou'] = 0;
} else if (!is_numeric($data['daily_xiangou']) || intval($data['daily_xiangou']) < 0) {
return $this->renderError("每日限购次数必须是非负整数");
} else {
$data['daily_xiangou'] = intval($data['daily_xiangou']);
}
$type = $info['type'];
if ($type == 1 || $type == 11) {
if (RegInt($data['stock'])) {
return $this->renderError("库存输入不规范,请设置大于0的整数");
}
if ($data['lock_is'] == 1) {
if (RegInt($data['lock_time'])) {
return $this->renderError("请设置锁箱时间");
}
} elseif ($data['lock_is'] == 0) {
$data['lock_time'] = 0;
} else {
return $this->renderError("锁箱类型选择错误");
}
$data['prize_num'] = 0;
$data['category_id'] = 0;
} elseif ($type == 2 || $type == 16 || $type == 17) {
$data['stock'] = 0;
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['prize_num'] = 0;
$data['category_id'] = 0;
} elseif ($type == 3) {
if ($info['stock'] != $data['stock']) {
return $this->renderError("不可修改库存");
}
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['category_id'] = 0;
$data['prize_num'] = 0;
} elseif ($type == 5) {
if (RegInt($data['stock'])) {
return $this->renderError("库存输入不规范,请设置大于0的整数");
}
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['prize_num'] = 0;
$data['coupon_is'] = 0;
$data['coupon_pro'] = 0;
$data['integral_is'] = 0;
$data['category_id'] = 0;
} elseif ($type == 6) {
if (RegInt($data['stock'])) {
return $this->renderError("库存输入不规范,请设置大于0的整数");
}
if ($data['lock_is'] == 1) {
if (RegInt($data['lock_time'])) {
return $this->renderError("请设置锁箱时间");
}
} elseif ($data['lock_is'] == 0) {
$data['lock_time'] = 0;
} else {
return $this->renderError("锁箱类型选择错误");
}
if ($data['show_is'] != 0 && $data['show_is'] != 1) {
return $this->renderError("首页显示开关选择错误");
}
$data['prize_num'] = 0;
$data['category_id'] = 0;
} elseif ($type == 8) {
$data['stock'] = 0;
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['prize_num'] = 0;
$data['category_id'] = 0;
} elseif ($type == 9) {
$data['stock'] = 0;
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['prize_num'] = 0;
$data['category_id'] = 0;
} elseif ($type == 10) {
if (RegInt($data['stock'])) {
return $this->renderError("库存输入不规范,请设置大于0的整数");
}
$data['lock_is'] = 0;
$data['lock_time'] = 0;
$data['prize_num'] = 0;
$data['show_is'] = 0;
$data['coupon_is'] = 0;
$data['coupon_pro'] = 0;
$data['integral_is'] = 0;
$data['category_id'] = 0;
$data['stock'] = 1;
} elseif ($type == 15) {
// 福利屋类型处理
$data['stock'] = 0; // 隐藏套数
$data['lock_is'] = 0; // 隐藏锁箱类型
$data['lock_time'] = 0; // 隐藏锁箱时间
$data['coupon_is'] = 0; // 隐藏发券开关
$data['coupon_pro'] = 0; // 隐藏发券概率
$data['integral_is'] = 0; // 隐藏发积分开关
$data['prize_num'] = 0;
$data['category_id'] = 0;
$data['is_flw'] = 1; // 标记为福利屋
// 处理时间字段
if (!empty($data['flw_start_time'])) {
$data['flw_start_time'] = strtotime($data['flw_start_time']);
} else {
return $this->renderError("请选择开始时间");
}
if (!empty($data['flw_end_time'])) {
$data['flw_end_time'] = strtotime($data['flw_end_time']);
} else {
return $this->renderError("请选择结束时间");
}
if (!empty($data['open_time'])) {
$data['open_time'] = strtotime($data['open_time']);
} else {
return $this->renderError("请选择开奖时间");
}
// 验证时间逻辑
if ($data['flw_start_time'] >= $data['flw_end_time']) {
return $this->renderError("开始时间必须早于结束时间");
}
if ($data['flw_end_time'] >= $data['open_time']) {
return $this->renderError("结束时间必须早于开奖时间");
}
// 验证抽奖门槛
if (!isset($data['choujiang_xianzhi']) || $data['choujiang_xianzhi'] === '') {
$data['choujiang_xianzhi'] = 0;
} else if (!is_numeric($data['choujiang_xianzhi']) || intval($data['choujiang_xianzhi']) < 0) {
return $this->renderError("抽奖门槛必须是非负整数");
}
// 默认未开奖
$data['is_open'] = 0;
} else {
return $this->renderError("盒子类型选择错误");
}
$data['sale_time'] = 0;
// if (isset($data['sale_time'])) {
// $data['sale_time'] = strtotime($data['sale_time']);
// } else {
// $data['sale_time'] = 0;
// }
if (empty($data['imgurl'])) {
return $this->renderError("请上传盒子封面图");
}
if (empty($data['imgurl_detail'])) {
return $this->renderError("请上传盒子封面图");
}
$data['update_time'] = time();
if ($data['stock'] < $info['stock']) {
return $this->renderError("不能减少库存");
}
Db::startTrans();
$res = [];
#添加套
if ($type == 1 || $type == 11 || $type == 5 || $type == 10 || $type == 6) {
if (($data['stock'] > $info['stock'])) {
#赏品
$goods_list = GoodsList::where(['goods_id' => $info['id']])
->where(['num' => 1])
->select()->toArray();
if ($goods_list) {
#循环数据
$save_sports_data = [];
$start_num = $info['stock'] + 1;
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'];
$save_sports_data[] = $v;
}
}
#添加赏品
$res[] = GoodsList::insertAll($save_sports_data);
}
}
}
$res[] = $info->allowField([])->update($data);
#添加日志
$info['update_time'] = date('Y-m-d H:i:i:s', $info['update_time']);
$data['update_time'] = date('Y-m-d H:i:i:s', $data['update_time']);
$res[] = AdminGoodsLog::add_goods_log(session('admin_id'), $info['id'], 0, json_encode($info), json_encode($data));
if (resCheck($res)) {
Db::commit();
return $this->renderSuccess("编辑成功");
} else {
Db::rollback();
return $this->renderError("网络繁忙,请稍后");
}
}
}
/**
* 上、下架,删除盒子
*/
public function goods_del(Request $request)
{
$id = $request->post('id/d');
$status = $request->post('type/d');
$data = GoodsModel::where(['id' => $id])->field('id')->find();
if (!$data) {
return $this->renderError("数据不存在");
}
if ($status == 1 || $status == 2) {
$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("请求参数错误");
}
if ($result) {
return $this->renderSuccess("操作成功");
} else {
return $this->renderError("网络繁忙,请稍后");
}
}
/**
* 盒子奖品
*/
public function goodslist(Request $request)
{
$goods_id = $request->param('goods_id/d', 0);
$info = GoodsModel::where(['id' => $goods_id])->find();
if (!$info) {
return $this->renderError('请求参数错误');
}
$type = $info['type'];
View::assign('goods_id', $goods_id);
View::assign('type', $type);
return View::fetch("Goods/goodslist");
}
/**
* 获取奖品列表数据(前后端分离接口)
*/
public function getGoodsListData(Request $request)
{
$goods_id = $request->param('goods_id/d', 0);
$page = $request->param('page/d', 1);
$limit = $request->param('limit/d', 50);
$title = $request->param('title/s', '');
$lian_ji_type = $request->param('lian_ji_type/s', '');
$info = GoodsModel::where(['id' => $goods_id])->find();
if (!$info) {
return $this->renderError('请求参数错误');
}
$type = $info['type'];
$where = [];
$where[] = ['goods_id', '=', $goods_id];
if ($type == 1 || $type == 3 || $type == 5 || $type == 6 || $type == 10 || $type == 11) {
$where[] = ['num', '=', 1];
} elseif ($type == 2 || $type == 8 || $type == 9 || $type == 15 || $type == 16 || $type == 17) {
$where[] = ['num', '=', 0];
} else {
return $this->renderError('请求参数错误1');
}
if (!empty($title)) {
$where[] = ['title', 'like', "%{$title}%"];
}
if (!empty($lian_ji_type)) {
$where[] = ['lian_ji_type', '=', $lian_ji_type];
}
$where[] = ['goods_list_id', '=', 0];
$query = GoodsList::where($where)->order('sort desc, shang_id asc, id asc');
$count = $query->count();
$list = $query->page($page, $limit)->select()->toArray();
$real_pro = 0;
// 处理数据
foreach ($list as &$item) {
$item['shang'] = Shang::where(['id' => $item['shang_id']])->value('title');
$item['imgurl'] = imageUrl($item['imgurl']);
if (!empty($item['imgurl_detail'])) {
$item['imgurl_detail'] = imageUrl($item['imgurl_detail']);
}
$real_pro += $item['real_pro'];
if ($item['goods_type'] == 4) {
$goods_list = GoodsList::where('goods_id', $item['goods_id'])
->where('goods_list_id', '=', $item['id'])
->select()
->toArray();
if ($goods_list) {
$item['isParent'] = 'true';
$real_pro1 = 0;
foreach ($goods_list as &$goods_list_item) {
$goods_list_item['shang'] = Shang::where(['id' => $goods_list_item['shang_id']])->value('title');
$goods_list_item['imgurl'] = imageUrl($goods_list_item['imgurl']);
if (!empty($goods_list_item['imgurl_detail'])) {
$goods_list_item['imgurl_detail'] = imageUrl($goods_list_item['imgurl_detail']);
}
$real_pro1 += $goods_list_item['real_pro'];
}
if ($type == 2 || $type == 8 || $type == 9 || $type == 15 || $type == 16 || $type == 17) {
$goods_list[] = ['title' => '宝箱概率合计', 'real_pro' => $real_pro1, 'id' => -1];
}
$item['children'] = $goods_list;
}
}
}
return $this->renderTable('获取成功', $count, $list, ['code' => 0, 'real_pro' => $real_pro]);
}
/**
* 添加奖品
*/
public function goodslist_add(Request $request)
{
if (!$request->isPost()) {
$goods_id = $request->param('goods_id/d', 0);
$info = GoodsModel::where(['id' => $goods_id])->find();
$goods_list_id = $request->param('goods_list_id/d', 0);
if (!$info) {
return $this->renderError('请求参数错误');
}
if ($info['type'] == 1 || $info['type'] == 5 || $info['type'] == 10 || $info['type'] == 11 || $info['type'] == 6) {
$shang = Shang::where('id', '<=', 33)->where('id', '<>', 5)->select()->toArray();
if ($goods_list_id) {
$shang = Shang::where('id', '<=', 33)->where('id', '>', 5)->select()->toArray();
}
} elseif ($info['type'] == 2 || $info['type'] == 8 || $info['type'] == 9 || $info['type'] == 16 || $info['type'] == 17) {
$shang = Shang::where('id', 'between', [34, 38])->select()->toArray();
} elseif ($info['type'] == 3) {
$shang = Shang::where('id', 'between', [4, 33])->select()->toArray();
} elseif ($info['type'] == 15) {
$shang = Shang::where('id', '>=', 114)->select()->toArray();
} else {
return $this->renderError('请求参数错误1');
}
View::assign('shang', $shang);
View::assign('goods_id', $goods_id);
View::assign('goods_list_id', $goods_list_id);
View::assign('type', $info['type']);
return View::fetch('Goods/goodslist_add');
} else {
$data = input('post.');
if (empty($data['goods_id'])) {
return $this->renderError('请求参数错误1');
}
$info = GoodsModel::where(['id' => $data['goods_id']])->find();
if (!$info) {
return $this->renderError('请求参数错误2');
}
$type = $info['type'];
if (empty($data['title'])) {
return $this->renderError('请输入奖品名称');
}
if (empty($data['shang_id'])) {
return $this->renderError('请选择奖品类型');
}
if (RegMoney($data['price'])) {
return $this->renderError('奖品售价设置错误,最多保留两位小数');
}
if (RegMoney($data['money'])) {
return $this->renderError('兑换价格设置错误,最多保留两位小数');
}
if (RegMoney($data['sc_money'])) {
return $this->renderError('参考价格设置错误,最多保留两位小数');
}
if ($data['goods_type'] == 1) {
$data['sale_time'] = 0;
} else if (in_array($data['goods_type'], [2, 8, 9, 16])) {
if (empty($data['sale_time'])) {
return $this->renderError('请选择预售时间');
}
$today_time = strtotime(date('Y-m-d', time()));
$data['sale_time'] = strtotime($data['sale_time']);
if ($today_time >= $data['sale_time']) {
return $this->renderError('预售时间请在今天之后');
}
} elseif ($type == 15) {
$data['sale_time'] = 0;
} elseif ($data['goods_type'] == 3 || $data['goods_type'] == 4) {
$data['sale_time'] = 0;
} else {
return $this->err('奖品类型选择错误');
}
//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) {
return $this->renderError('特殊奖品库存请设置为1');
}
}
if (RegInt($data['stock'])) {
return $this->renderError('奖品数量设置错误,请设置大于0的整数');
}
$data['surplus_stock'] = $data['stock'];
#盒子套数
$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;
}
} elseif (in_array($type, [2, 8, 9, 16])) {
if (RegMoney($data['real_pro'] * 10000)) {
return $this->renderError('真实概率设置错误,最多保留四位小数');
}
$prize_code = getPrizeCode() . '_' . time();
$data['prize_code'] = $prize_code;
} elseif ($type == 3) {
if ($data['shang_id'] == 1 || $data['shang_id'] == 2 || $data['shang_id'] == 3 || $data['shang_id'] == 5) {
if ($data['stock'] != 1) {
return $this->renderError('特殊奖品库存请设置为1');
}
}
if (RegInt($data['stock'])) {
return $this->renderError('奖品数量设置错误,请设置大于0的整数');
}
$data['surplus_stock'] = $data['stock'];
#盒子套数
$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;
}
if (RegInt($data['prize_num']) && $data['shang_id'] == 5) {
return $this->renderError("擂台赏抽全局赏数量设置错误,请设置大于0的整数");
}
} elseif ($type == 15) {
$prize_code = getPrizeCode() . '_' . time();
$data['prize_code'] = $prize_code;
$data['surplus_stock'] = $data['stock'];
if (RegInt($data['stock'])) {
return $this->renderError('奖品数量设置错误,请设置大于0的整数');
}
} elseif ($type == 17) {
$prize_code = getPrizeCode() . '_' . time();
$data['prize_code'] = $prize_code;
$data['surplus_stock'] = $data['stock'];
} else {
return $this->err('请求参数错误');
}
if (RegZero($data['sort'])) {
return $this->renderError('排序请输入整数');
}
$data['card_no'] = NULL;
// }
if (empty($data['imgurl'])) {
return $this->renderError('请上传图片');
}
$data['addtime'] = time();
if (isset($save_sports_data)) {
$res = GoodsList::insertAll($save_sports_data);
} else {
$res = GoodsList::insert($data);
}
if ($res) {
return $this->renderSuccess('添加成功');
} else {
return $this->renderError('添加失败');
}
}
}
/**
* 编辑奖品
*/
public function goodslist_edit(Request $request)
{
if (!$request->isPost()) {
$id = $request->param('id/d', 0);
$goods = GoodsList::where(['id' => $id])->find();
$goods_list_id = $request->param('goods_list_id/d', 0);
if (!$goods) {
return $this->renderError('请求参数错误');
}
if ($goods['goods_type'] == 2) {
$goods['sale_time'] = date('Y-m-d', $goods['sale_time']);
} else {
$goods['sale_time'] = '';
}
$goods_id = $goods['goods_id'];
$info = GoodsModel::where(['id' => $goods_id])->find();
if (!$info) {
return $this->renderError('请求参数错误1');
}
if ($info['type'] == 1 || $info['type'] == 5 || $info['type'] == 6 || $info['type'] == 10 | $info['type'] == 11) {
$shang = Shang::where('id', '<=', 33)->where('id', '<>', 5)->select()->toArray();
if ($goods_list_id) {
$shang = Shang::where('id', '<=', 33)->where('id', '>', 5)->select()->toArray();
}
} elseif ($info['type'] == 2 || $info['type'] == 8 || $info['type'] == 9 || $info['type'] == 16 || $info['type'] == 17) {
$shang = Shang::where('id', 'between', [34, 38])->select()->toArray();
} elseif ($info['type'] == 15) {
$shang = Shang::where('id', '>=', 114)->select()->toArray();
} elseif ($info['type'] == 3) {
$shang = Shang::where('id', 'between', [4, 33])->select()->toArray();
} else {
return $this->renderError('请求参数错误1');
}
View::assign('goods', $goods);
View::assign('shang', $shang);
View::assign('type', $info['type']);
View::assign('goods_list_id', $goods_list_id);
return View::fetch('Goods/goodslist_edit');
} else {
$data = input('post.');
if (empty($data['id'])) {
return $this->renderError('请求参数错误1');
}
$goods = GoodsList::where(['id' => $data['id']])->find();
if (!$goods) {
return $this->renderError('请求参数错误2');
}
$info = GoodsModel::where(['id' => $goods['goods_id']])->find();
if (!$info) {
return $this->renderError('请求参数错误3');
}
$type = $info['type'];
if (empty($data['title'])) {
return $this->renderError('请输入奖品名称');
}
if (empty($data['shang_id'])) {
return $this->renderError('请选择奖品类型');
}
if (RegMoney($data['price'])) {
return $this->renderError('奖品售价设置错误,最多保留两位小数');
}
if (RegMoney($data['money'])) {
return $this->renderError('兑换价格设置错误,最多保留两位小数');
}
if (RegMoney($data['sc_money'])) {
return $this->renderError('参考价格设置错误,最多保留两位小数');
}
if ($data['goods_type'] == 1) {
$data['sale_time'] = 0;
} else if ($data['goods_type'] == 2) {
if (empty($data['sale_time'])) {
return $this->renderError('请选择预售时间');
}
$today_time = strtotime(date('Y - m - d', time()));
$data['sale_time'] = strtotime($data['sale_time']);
if ($today_time >= $data['sale_time']) {
return $this->renderError('预售时间请在今天之后');
}
} elseif ($data['goods_type'] == 3 || $data['goods_type'] == 4) {
$data['sale_time'] = 0;
} else {
return $this->err('奖品类型选择错误');
}
//2023-11-17无作用
$data['reward_num'] = 0;
//
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)) {
return $this->renderError('真实概率设置错误,最多保留四位小数');
}
} else if ($type == 3) {
if (RegInt($data['prize_num']) && $data['shang_id'] == 5) {
return $this->renderError("擂台赏抽全局赏数量设置错误,请设置大于0的整数");
}
} elseif ($type == 15) {
$data['surplus_stock'] = $data['stock'];
} else {
return $this->err('请求参数错误');
}
if (RegZero($data['sort'])) {
return $this->renderError('排序请输入整数');
}
$data['card_no'] = NULL;
if (empty($data['imgurl'])) {
return $this->renderError('请上传图片');
}
$data['update_time'] = time();
unset($data['id']);
$res1 = GoodsList::where(['prize_code' => $goods['prize_code']])->update($data);
if ($res1) {
#添加日志
$goods['update_time'] = date('Y-m-d H:i:i:s', $goods['update_time']);
$data['update_time'] = date('Y-m-d H:i:i:s', $data['update_time']);
$res2 = AdminGoodsLog::add_goods_log(session('admin_id'), $info['id'], $goods['id'], json_encode($goods), json_encode($data));
return $this->renderSuccess('编辑成功');
} else {
return $this->renderError('编辑失败');
}
}
}
/**
* 奖品删除
*/
public function goodslist_del(Request $request)
{
$id = \request()->param('id/d', 0);
$prize_code = GoodsList::field('prize_code')
->where(['id' => $id])
->value('prize_code');
// if (!$prize_code) {
// return $this->renderError("请勿重复操作");
// }
#删除奖品
$res = GoodsList::field('id')
->where(['prize_code' => $prize_code])
->delete();
if ($res) {
return $this->renderSuccess("操作成功");
} else {
return $this->renderError("操作失败");
}
}
/**
* 消费赠送
*/
public function give()
{
$title = trim(input('get.title'));
$whe = array();
$whe[] = ['goods_id', '=', 0];
if (!empty($title)) {
$whe[] = ['title', 'like', '%' . $title . '%'];
}
$field = "*";
$order = "give_money asc,id asc";
$data = GoodsList::getList($whe, $field, $order, $this->page);
foreach ($data['list'] as &$value) {
$value['shang'] = Shang::where(['id' => $value['shang_id']])->value('title');
}
$range_time = getConfig('give_time')['range_time'];
View::assign('range_time', $range_time);
View::assign('list', $data['list']);
View::assign('count', $data['count']);
View::assign('page', $data['page']);
return View::fetch("Goods/give");
}
/**
* 消费赠送添加
*/
public function give_add(Request $request)
{
if (!$request->isPost()) {
$shang = Shang::where('id', 'between', [10, 33])->select()->toArray();
View::assign('shang', $shang);
return View::fetch('Goods/give_add');
} else {
$data = input('post.');
if (empty($data['title'])) {
return $this->renderError('请输入奖品名称');
}
if (empty($data['shang_id'])) {
return $this->renderError('请选择奖品类型');
}
if (RegMoney($data['price'])) {
return $this->renderError('奖品售价设置错误,最多保留两位小数');
}
if (RegMoney($data['money'])) {
return $this->renderError('兑换价格设置错误,最多保留两位小数');
}
if (RegInt($data['give_money'])) {
return $this->renderError('消费阀值设置错误,请设置大于0的整数');
}
if (empty($data['imgurl'])) {
return $this->renderError('请上传图片');
}
$prize_code = getPrizeCode() . '_' . time();
$data['prize_code'] = $prize_code;
$data['goods_id'] = 0;
$data['addtime'] = time();
$dd = (new GoodsList())->insert($data);
if ($dd) {
return $this->renderSuccess('添加成功');
} else {
return $this->renderError('添加失败');
}
}
}
/**
* 消费赠送编辑
*/
public function give_edit(Request $request)
{
if (!$request->isPost()) {
$id = request()->param('id/d', 0);
$info = GoodsList::where(['id' => $id])->find();
if (!$info) {
return $this->renderError('请求参数错误');
}
$shang = Shang::where('id', 'between', [10, 33])->select()->toArray();
View::assign('shang', $shang);
View::assign('info', $info);
return View::fetch('Goods/give_edit');
} else {
$data = input('post.');
if (empty($data['id'])) {
return $this->renderError('请求参数错误');
}
$info = GoodsList::where(['id' => $data['id']])->find();
if (!$info) {
return $this->renderError('请求参数错误1');
}
if (empty($data['title'])) {
return $this->renderError('请输入奖品名称');
}
if (empty($data['shang_id'])) {
return $this->renderError('请选择奖品类型');
}
if (RegMoney($data['price'])) {
return $this->renderError('奖品售价设置错误,最多保留两位小数');
}
if (RegMoney($data['money'])) {
return $this->renderError('兑换价格设置错误,最多保留两位小数');
}
if (RegInt($data['give_money'])) {
return $this->renderError('消费阀值设置错误,请设置大于0的整数');
}
if (empty($data['imgurl'])) {
return $this->renderError('请上传图片');
}
$data['update_time'] = time();
unset($data['id']);
$dd = $info->allowField([])->save($data);
if ($dd) {
return $this->renderSuccess('编辑成功');
} else {
return $this->renderError('编辑失败');
}
}
}
/**
* 消费赠送删除
*/
public function give_del(Request $request)
{
$id = \request()->param('id/d', 0);
$info = GoodsList::field('prize_code')
->where(['id' => $id])
->find();
if (!$info) {
return $this->renderError("请勿重复操作");
}
#删除奖品
$res = GoodsList::field('id')
->where(['id' => $id])
->delete();
if ($res) {
return $this->renderSuccess("操作成功");
} else {
return $this->renderError("操作失败");
}
}
#无限令规则
public function give_time()
{
$data = request()->param();
$data['time'] = time();
$result = setConfig('give_time', $data);
if ($result) {
return $this->renderSuccess('修改成功');
} else {
return $this->renderError('修改失败');
}
}
/**
* 预售日历
*/
public function yushou_rili()
{
$title = trim(input('get.title'));
$whe = array();
if ($title) {
$whe[] = ['title', 'like', '%' . $title . '%'];
}
$field = "*";
$order = "sort desc,id desc";
$data = Yushou::getList($whe, $field, $order, $this->page);
foreach ($data['list'] as &$value) {
$value['goods_title'] = GoodsModel::where(['id' => $value['goods_id']])->value('title');
$value['sale_time'] = date('Y-m-d', $value['sale_time']);
}
View::assign('list', $data['list']);
View::assign('count', $data['count']);
View::assign('page', $data['page']);
return View::fetch("Goods/yushou_rili");
}
/**
* 预售日历添加
*/
public function yushou_rili_add(Request $request)
{
if (!$request->isPost()) {
$goods = GoodsModel::where('type', '=', 1)->select()->toArray();
View::assign('goods', $goods);
return View::fetch('Goods/yushou_rili_add');
} else {
$data = input('post.');
if (empty($data['title'])) {
return $this->renderError('请输入标题');
}
if ($data['goods_id']) {
$goods = GoodsModel::where('type', '=', 1)->where('id', '=', $data['goods_id']);
if (!$goods) {
return $this->renderError('盒子选择错误');
}
} else {
$data['goods_id'] = 0;
}
if (empty($data['sale_time'])) {
return $this->renderError('请选择预售时间');
}
$data['sale_time'] = strtotime($data['sale_time']);
$today_time = strtotime(date('Y-m-d', time()));
if ($data['sale_time'] < $today_time) {
return $this->renderError('预售时间选择错误');
}
if (RegZero($data['sort'])) {
return $this->renderError('排序设置错误,请输入整数');
}
if (empty($data['imgurl'])) {
return $this->renderError('请上传图片');
}
$data['addtime'] = time();
$dd = (new Yushou())->insert($data);
if ($dd) {
return $this->renderSuccess('添加成功');
} else {
return $this->renderError('添加失败');
}
}
}
/**
* 预售日历编辑
*/
public function yushou_rili_edit(Request $request)
{
if (!$request->isPost()) {
$id = request()->param('id/d', 0);
$info = Yushou::where(['id' => $id])->find();
if (!$info) {
return $this->renderError('请求参数错误');
}
$info['sale_time'] = date('Y-m-d', $info['sale_time']);
$goods = GoodsModel::where('type', '=', 1)->select()->toArray();
View::assign('goods', $goods);
View::assign('info', $info);
return View::fetch('Goods/yushou_rili_edit');
} else {
$data = input('post.');
if (empty($data['id'])) {
return $this->renderError('请求参数错误');
}
$info = Yushou::where(['id' => $data['id']])->find();
if (!$info) {
return $this->renderError('请求参数错误1');
}
if (empty($data['title'])) {
return $this->renderError('请输入标题');
}
if ($data['goods_id']) {
$goods = GoodsModel::where('type', '=', 1)->where('id', '=', $data['goods_id']);
if (!$goods) {
return $this->renderError('盒子选择错误');
}
} else {
$data['goods_id'] = 0;
}
if (empty($data['sale_time'])) {
return $this->renderError('请选择预售时间');
}
$data['sale_time'] = strtotime($data['sale_time']);
$today_time = strtotime(date('Y-m-d', time()));
if ($data['sale_time'] < $today_time) {
return $this->renderError('预售时间选择错误');
}
if (RegZero($data['sort'])) {
return $this->renderError('排序设置错误,请输入整数');
}
if (empty($data['imgurl'])) {
return $this->renderError('请上传图片');
}
$data['update_time'] = time();
unset($data['id']);
$dd = $info->allowField([])->save($data);
if ($dd) {
return $this->renderSuccess('编辑成功');
} else {
return $this->renderError('编辑失败');
}
}
}
/**
* 预售日历删除
*/
public function yushou_rili_del(Request $request)
{
$id = \request()->param('id/d', 0);
$info = Yushou::field('id')->where(['id' => $id])->find();
if (!$info) {
return $this->renderError("请勿重复操作");
}
#删除奖品
$res = Yushou::field('id')->where(['id' => $id])->delete();
if ($res) {
return $this->renderSuccess("操作成功");
} else {
return $this->renderError("操作失败");
}
}
/**
* 获取同步地址列表
*/
public function get_sync_addresses()
{
$config = getConfig('systemconfig');
$syncAddresses = isset($config['sync_address']) && is_array($config['sync_address'])
? $config['sync_address']
: [];
return $this->renderSuccess('获取成功', $syncAddresses);
}
/**
* 盒子自动下架日志
*/
public function offshelf_log(Request $request)
{
$goods_id = trim(input('get.goods_id'));
$start_time = trim(input('get.start_time'));
$end_time = trim(input('get.end_time'));
$whe = [];
if ($goods_id) {
$whe[] = ['goods_id', '=', $goods_id];
}
if ($start_time && $end_time) {
$start = strtotime($start_time);
$end = strtotime($end_time . ' 23:59:59');
$whe[] = ['create_time', 'between', [$start, $end]];
} elseif ($start_time) {
$start = strtotime($start_time);
$whe[] = ['create_time', '>=', $start];
} elseif ($end_time) {
$end = strtotime($end_time . ' 23:59:59');
$whe[] = ['create_time', '<=', $end];
}
// 获取日志数据
$list = Db::name('goods_offshelf_log')
->where($whe)
->order('id desc')
->paginate([
'list_rows' => $this->page,
'query' => request()->param(),
]);
// 获取所有相关的盒子信息
$goodsIds = array_column($list->items(), 'goods_id');
$goodsInfo = [];
if (!empty($goodsIds)) {
$goods = Db::name('goods')
->field('id, title, status')
->whereIn('id', $goodsIds)
->select()
->toArray();
foreach ($goods as $item) {
$goodsInfo[$item['id']] = [
'title' => $item['title'],
'status' => $item['status']
];
}
}
// 转换时间戳并处理数据
$listItems = $list->items();
foreach ($listItems as &$item) {
$item['create_time_text'] = date('Y-m-d H:i:s', $item['create_time']);
$item['goods_title'] = isset($goodsInfo[$item['goods_id']]) ? $goodsInfo[$item['goods_id']]['title'] : '未知盒子';
$item['goods_status'] = isset($goodsInfo[$item['goods_id']]) ? $goodsInfo[$item['goods_id']]['status'] : 2; // 默认下架状态
}
View::assign('list', $listItems);
View::assign('page', $list->render());
View::assign('count', $list->total());
return View::fetch('Goods/offshelf/log');
}
/**
* 同步盒子数据
*/
public function sync_goods(Request $request)
{
if (!$request->isPost()) {
return $this->renderError('请求方式错误');
}
$id = $request->post('id/d');
$url = $request->post('url/s');
// 验证URL格式
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return $this->renderError('URL格式不正确');
}
// 确保URL以/结尾
if (substr($url, -1) !== '/') {
$url .= '/';
}
$url .= 'api/goods/receive_sync';
// 获取盒子数据
$goods = GoodsModel::where(['id' => $id])->find();
if (!$goods) {
return $this->renderError('盒子不存在');
}
// 生成或获取同步代码
$async_code = $goods['async_code'];
if (empty($async_code)) {
$async_code = 'MHHZ' . date('YmdHis') . mt_rand(1000, 9999);
$goods->save(['async_code' => $async_code]);
}
// 获取盒子奖品数据
$goodsLists = GoodsList::where(['goods_id' => $id])->select()->toArray();
// 获取盒子扩展配置
$goodsExtend = GoodsExtend::where(['goods_id' => $id])->find();
// 收集所有奖励ID
$rewardIds = [];
foreach ($goodsLists as $item) {
if (!empty($item['reward_id'])) {
$rewardIds[] = $item['reward_id'];
}
}
// 获取奖励数据
$rewards = [];
if (!empty($rewardIds)) {
$rewards = Db::name('reward')->whereIn('reward_id', $rewardIds)->select()->toArray();
}
// 准备发送的数据
$postData = [
'goods' => $goods->toArray(),
'goodsList' => $goodsLists,
'async_code' => $async_code,
'rewards' => $rewards
];
if ($goodsExtend) {
$postData['goodsExtend'] = $goodsExtend->toArray();
}
// 发送同步请求
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen(json_encode($postData))
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status !== 200) {
return $this->renderError('同步失败,服务器返回状态码: ' . $status);
}
$responseData = json_decode($response, true);
if (!$responseData || $responseData['code'] !== 1) {
$errorMsg = isset($responseData['msg']) ? $responseData['msg'] : '服务器未返回预期响应';
return $this->renderError('同步失败: ' . $errorMsg);
}
// 更新同步状态
$goods->save([
'sync_status' => 1,
'sync_url' => $url,
'sync_time' => date('Y-m-d H:i:s')
]);
return $this->renderSuccess('同步成功');
} catch (\Exception $e) {
return $this->renderError('同步失败: ' . $e->getMessage());
}
}
/**
* 清空盒子抽奖数据
*/
public function clear_goods_data(Request $request)
{
if (!$request->isPost()) {
return $this->renderError('请求方式错误');
}
$id = $request->post('id/d');
$goods = GoodsModel::where(['id' => $id])->find();
if (!$goods) {
return $this->renderError('盒子不存在');
}
// 开启事务
Db::startTrans();
try {
// 删除相关订单数据
Db::name('order')->where('goods_id', $id)->delete();
Db::name('order_list')->where('goods_id', $id)->delete();
// 重置奖品剩余数量
Db::name('goods_list')->where('goods_id', $id)->update(['surplus_stock' => Db::raw('stock')]);
Db::commit();
return $this->renderSuccess('盒子抽奖数据已清空');
} catch (\Exception $e) {
Db::rollback();
return $this->renderError('操作失败:' . $e->getMessage());
}
}
/**
* 复制盒子
*/
public function copy_goods(Request $request)
{
if (!$request->isPost()) {
return $this->renderError('请求方式错误');
}
$id = $request->post('id/d');
$goods = GoodsModel::where(['id' => $id])->find();
if (!$goods) {
return $this->renderError('盒子不存在');
}
// 开启事务
Db::startTrans();
try {
// 复制盒子基本信息
$goodsData = $goods->toArray();
unset($goodsData['id']);
unset($goodsData['delete_time']);
// 修改标题,添加"复制"标识
$goodsData['title'] = $goodsData['title'] . '(复制)';
$goodsData['status'] = 2; // 默认下架状态
$goodsData['addtime'] = time();
$goodsData['async_code'] = ''; // 清空同步代码,避免混淆
$goodsData['is_open'] = 0;
// 插入新盒子
$newGoods = new GoodsModel();
$newGoods->save($goodsData);
$newGoodsId = $newGoods->id;
// 复制盒子扩展配置
$goodsExtend = GoodsExtend::where('goods_id', $id)->find();
if ($goodsExtend) {
$extendData = $goodsExtend->toArray();
unset($extendData['id']);
$extendData['goods_id'] = $newGoodsId;
$newExtend = new GoodsExtend();
$newExtend->save($extendData);
}
// 创建ID映射表用于处理子奖品关联
$idMapping = [];
// 复制盒子奖品
$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);
}
}
}
// 插入新奖品
$newGoodsList = new GoodsList();
$newGoodsList->save($listData);
// 记录ID映射关系
$idMapping[$oldId] = $newGoodsList->id;
}
// 复制子奖品(宝箱类型的子奖品)
$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);
}
}
}
// 插入子奖品
$newChildList = new GoodsList();
$newChildList->save($childData);
}
Db::commit();
return $this->renderSuccess('盒子复制成功');
} catch (\Exception $e) {
Db::rollback();
return $this->renderError('复制失败:' . $e->getMessage());
}
}
/**
* 生成UUID
*/
private function generateUUID()
{
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
}
/**
* 发送同步数据到目标
*/
private function sendSyncData($target, $data)
{
try {
# 准备请求数据
$jsonData = json_encode($data);
# 初始化CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target . "/api/goods/receive_sync");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); # 30秒超时
# 执行请求
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
return ['success' => false, 'message' => "CURL错误: $error"];
}
if ($httpCode != 200) {
return ['success' => false, 'message' => "HTTP错误码: $httpCode"];
}
$result = json_decode($response, true);
if (!$result || !isset($result['status'])) {
return ['success' => false, 'message' => "无效的响应: $response"];
}
if ($result['status'] != 1) {
return ['success' => false, 'message' => $result['msg'] ?? '同步失败'];
}
return ['success' => true];
} catch (\Exception $e) {
return ['success' => false, 'message' => "异常: " . $e->getMessage()];
}
}
/**
* 盒子扩展设置
*/
public function goods_extend(Request $request)
{
$goods_id = $request->param('goods_id/d');
if (!$goods_id) {
return $this->renderError("参数错误");
}
// 获取盒子信息
$goods = GoodsModel::where('id', $goods_id)->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
if ($request->isPost()) {
$data = $request->post();
// 验证参数
$data['goods_id'] = $goods_id;
$data['pay_wechat'] = isset($data['pay_wechat']) ? 1 : 0;
$data['pay_balance'] = isset($data['pay_balance']) ? 1 : 0;
$data['pay_currency'] = isset($data['pay_currency']) ? 1 : 0;
$data['pay_currency2'] = isset($data['pay_currency2']) ? 1 : 0;
$data['pay_coupon'] = isset($data['pay_coupon']) ? 1 : 0;
$data['is_deduction'] = isset($data['is_deduction']) ? 1 : 0;
// 查询是否已存在
$exist = \app\common\model\GoodsExtend::getByGoodsId($goods_id);
Db::startTrans();
try {
if ($exist) {
// 更新
\app\common\model\GoodsExtend::where('goods_id', $goods_id)->update($data);
} else {
// 新增
\app\common\model\GoodsExtend::create($data);
}
Db::commit();
return json(['status' => 1, 'msg' => '保存成功']);
} catch (\Exception $e) {
Db::rollback();
return json(['status' => 0, 'msg' => '保存失败: ' . $e->getMessage()]);
}
}
// 获取扩展设置
$extend = \app\common\model\GoodsExtend::getByGoodsId($goods_id);
if (!$extend) {
// 从goods_type表获取默认值
$goodsType = Db::name('goods_type')
->field('pay_wechat, pay_balance, pay_currency, pay_currency2, pay_coupon, is_deduction')
->where('value', $goods['type'])
->find();
if ($goodsType) {
// 如果找到了盒子类型的默认配置
$extend = [
'goods_id' => $goods_id,
'pay_wechat' => $goodsType['pay_wechat'],
'pay_balance' => $goodsType['pay_balance'],
'pay_currency' => $goodsType['pay_currency'],
'pay_currency2' => $goodsType['pay_currency2'],
'pay_coupon' => $goodsType['pay_coupon'],
'is_deduction' => $goodsType['is_deduction']
];
} else {
// 如果没找到,使用默认值
$extend = [
'goods_id' => $goods_id,
'pay_wechat' => 1,
'pay_balance' => 1,
'pay_currency' => 1,
'pay_currency2' => 1,
'pay_coupon' => 1,
'is_deduction' => 1
];
}
}
View::assign('goods', $goods);
View::assign('extend', $extend);
return View::fetch("Goods/goods_extend");
}
/**
* 删除盒子扩展设置
*/
public function goods_extend_del(Request $request)
{
$goods_id = $request->param('goods_id/d');
if (!$goods_id) {
return json(['status' => 0, 'msg' => '参数错误']);
}
// 获取盒子信息
$goods = GoodsModel::where('id', $goods_id)->find();
if (!$goods) {
return json(['status' => 0, 'msg' => '盒子不存在']);
}
// 删除扩展设置
$result = \app\common\model\GoodsExtend::deleteByGoodsId($goods_id);
if ($result) {
return json(['status' => 1, 'msg' => '删除成功']);
} else {
return json(['status' => 0, 'msg' => '删除失败或扩展设置不存在']);
}
}
/**
* 更新盒子排序
*/
public function update_goods_sort(Request $request)
{
$id = $request->post('id/d');
$sort = $request->post('sort/d');
if (empty($id)) {
return json(['status' => 0, 'msg' => '参数错误']);
}
// 验证排序值
if (!is_numeric($sort) || $sort < 0) {
return json(['status' => 0, 'msg' => '排序值必须是非负整数']);
}
// 获取盒子信息
$goods = GoodsModel::where('id', $id)->find();
if (!$goods) {
return json(['status' => 0, 'msg' => '盒子不存在']);
}
// 更新排序值
$result = GoodsModel::where('id', $id)->update(['sort' => $sort]);
if ($result) {
return json(['status' => 1, 'msg' => '更新成功']);
} else {
return json(['status' => 0, 'msg' => '更新失败']);
}
}
/**
* 更新奖品排序
*/
public function goodslist_edit_sort(Request $request)
{
$id = $request->post('id/d');
$sort = $request->post('sort/d');
if (empty($id)) {
return json(['status' => 0, 'msg' => '参数错误']);
}
// 验证排序值
if (!is_numeric($sort) || $sort < 0) {
return json(['status' => 0, 'msg' => '排序值必须是非负整数']);
}
// 获取奖品信息
$goodsList = GoodsList::where('id', $id)->find();
if (!$goodsList) {
return json(['status' => 0, 'msg' => '奖品不存在']);
}
// 更新排序值
$result = GoodsList::where('id', $id)->update(['sort' => $sort]);
if ($result) {
return json(['status' => 1, 'msg' => '更新成功']);
} else {
return json(['status' => 0, 'msg' => '更新失败']);
}
}
}