220 lines
8.2 KiB
PHP
Executable File
220 lines
8.2 KiB
PHP
Executable File
<?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\GoodsExtendList as GoodsExtendListModel;
|
|
use app\common\model\AdminGoodsLog;
|
|
use think\facade\Db;
|
|
use app\common\model\Shang;
|
|
|
|
class GoodsExtend extends Base
|
|
{
|
|
/**
|
|
* 盒子奖品
|
|
*/
|
|
public function goodsextendlist(Request $request)
|
|
{
|
|
$goods_id = $request->param('goods_id/d', 0);
|
|
$num = $request->param('num/d', 1);
|
|
$info = GoodsModel::where(['id' => $goods_id])->find();
|
|
if (!$info) {
|
|
return $this->renderError('请求参数错误');
|
|
}
|
|
|
|
$type = $info['type'];
|
|
$title = trim(input('get.title'));
|
|
$lian_ji_type = input('get.lian_ji_type');
|
|
$whe = array();
|
|
$whe[] = ['goods_id', '=', $goods_id];
|
|
if ($num > $info['stock']) {
|
|
$num = $info['stock'];
|
|
}
|
|
|
|
if ($type == 1 || $type == 3 || $type == 5 || $type == 6 || $type == 10 || $type == 11) {
|
|
if ($num <= 0) {
|
|
$num = 1;
|
|
}
|
|
if ($num != 0) {
|
|
$whe[] = ['num', '=', $num];
|
|
}
|
|
} elseif ($type == 2 || $type == 8 || $type == 9) {
|
|
$whe[] = ['num', '=', 0];
|
|
} else {
|
|
return $this->renderError('请求参数错误1');
|
|
}
|
|
if (!empty($title)) {
|
|
$whe[] = ['title', 'like', '%' . $title . '%'];
|
|
}
|
|
if (!empty($lian_ji_type)) {
|
|
$whe[] = ['lian_ji_type', '=', $lian_ji_type];
|
|
}
|
|
$field = "*";
|
|
$order = "sort desc,shang_id asc,id asc";
|
|
$data = GoodsList::getList($whe, $field, $order, 150);
|
|
$stock = array_sum(array_column($data['list'], 'stock'));
|
|
$surplus_stock = array_sum(array_column($data['list'], 'surplus_stock'));
|
|
$reward_num_count = $stock - $surplus_stock;
|
|
|
|
|
|
//GoodsModel
|
|
$goodsExtendLists = GoodsExtendListModel::getGoodsExtendList($goods_id);
|
|
// $idArray = array_map(function($item) {
|
|
// return $item['id'];
|
|
// }, $data);
|
|
// Shang::where(['id' => $value['shang_id']])->value('title');
|
|
$real_pro = 0;
|
|
$goods_extend_reward_num = 0;
|
|
foreach ($data['list'] as &$value) {
|
|
$value['shang'] = Shang::where(['id' => $value['shang_id']])->value('title');
|
|
$real_pro += $value['real_pro'];
|
|
// $value['rownum'] = $goodsExtendLists::where(['id' => $goods_id])->find();
|
|
//找到匹配的数据
|
|
$value['goods_extend_reward_num'] = 0;
|
|
$value['goods_extend_rawrd_type'] = 0;
|
|
$value['goods_extend'] = [];
|
|
if (count($goodsExtendLists) > 0) {
|
|
|
|
|
|
|
|
foreach ($goodsExtendLists as $item) {
|
|
// $temp_goods_extend_data = GoodsExtendListModel::where('prize_code', '=', $goods["prize_code"])->find();
|
|
// if ($item['prize_code'] == $value['prize_code']) {
|
|
if ($item['goods_list_id'] == $value['id']) {
|
|
$goods_extend_reward_num = $item['reward_num']; // 替换为你需要的字段名
|
|
$value['goods_extend_reward_num'] = $goods_extend_reward_num;
|
|
$value['goods_extend'] = $item;
|
|
$value['goods_extend_rawrd_type'] = $item['rawrd_type'];
|
|
$value['goods_extend_reward_num_1'] = $item['reward_num_1'];
|
|
break; // 找到第一个匹配的对象后退出循环
|
|
}
|
|
}
|
|
}
|
|
}
|
|
View::assign('list', $data['list']);
|
|
View::assign('count', $data['count']);
|
|
View::assign('page', $data['page']);
|
|
View::assign('goods_id', $goods_id);
|
|
View::assign('type', $type);
|
|
View::assign('goods_model', $info);
|
|
View::assign('num', $num );
|
|
View::assign('real_pro', $real_pro);
|
|
View::assign('reward_num_count',$reward_num_count);
|
|
return View::fetch("GoodsExtend/goodsextendlist");
|
|
|
|
}
|
|
|
|
/**
|
|
* 编辑奖品
|
|
*/
|
|
public function goodsextendlist_edit(Request $request)
|
|
{
|
|
$id = 0;
|
|
if (!$request->isPost()) {
|
|
$id = $request->param('id/d', 0);
|
|
|
|
$goods_extend_list_model = GoodsExtendListModel::getGoodsIdExtendList($id);
|
|
View::assign('goods_extend', $goods_extend_list_model);
|
|
return View::fetch('GoodsExtend/goodsextend_edit');
|
|
} else {
|
|
|
|
$data = input('post.');
|
|
if (empty($data['ids'])) {
|
|
return $this->renderError('请求参数错误1');
|
|
}
|
|
if (empty($data['value'])) {
|
|
return $this->renderError('请求参数错误1');
|
|
}
|
|
|
|
// if (RegZero($data['value'])) {
|
|
// return $this->renderError('请输入整数');
|
|
// }
|
|
if (RegZero($data['goodid'])) {
|
|
return $this->renderError('请求参数错误');
|
|
}
|
|
$value = $data['value'];
|
|
$ids = $data['ids'];
|
|
$goodid = $data['goodid'];
|
|
$save_goods_extend_data = [];
|
|
|
|
// rawrd_type:3,
|
|
// reward_num:0,
|
|
// reward_num1:0,
|
|
// reward_num2:0
|
|
// $ordinary_prize = GoodsList::where('goods_id', '=', $goods_id)
|
|
// ->where('num', '=', $num)
|
|
// ->where('shang_id', 'between', self::$shang_prize_id)
|
|
// ->order('sort desc,shang_id asc')
|
|
// ->select()->toArray();
|
|
|
|
foreach ($ids as $item) {
|
|
$goods_list_id = intval($item);
|
|
if ($goods_list_id > 0) {
|
|
$goods = GoodsList::where(['id' => $goods_list_id])->find();
|
|
if ($goods) {
|
|
// $temp_goods_extend_data = GoodsExtendListModel::where('prize_code', '=', $goods["prize_code"])->find(); //getGoodsIdExtendList($goods_list_id);
|
|
$temp_goods_extend_data = GoodsExtendListModel::where('goods_list_id', '=', $goods_list_id)->find(); //getGoodsIdExtendList($goods_list_id);
|
|
if ($temp_goods_extend_data === null) {
|
|
// 创建新的数据
|
|
$temp_goods_extend_data = new GoodsExtendListModel();
|
|
}
|
|
|
|
// 更新现有数据
|
|
$temp_goods_extend_data->goods_list_id = $item;
|
|
$temp_goods_extend_data->reward_num = $value['reward_num'];
|
|
$temp_goods_extend_data->rawrd_type = $value['rawrd_type'];
|
|
$temp_goods_extend_data->reward_num_1 = $value['reward_num_1'];
|
|
$temp_goods_extend_data->goods_id = $goodid;
|
|
$temp_goods_extend_data->prize_code = $goods["prize_code"];
|
|
$save_goods_extend_data[] = $temp_goods_extend_data;
|
|
}
|
|
}
|
|
}
|
|
// 保存数据到数据库中
|
|
foreach ($save_goods_extend_data as $data) {
|
|
$data->save(); // save() 方法将自动处理是插入还是更新
|
|
}
|
|
return $this->renderSuccess('编辑成功');
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 编辑奖品
|
|
*/
|
|
public function goodsextendlist_del(Request $request)
|
|
{
|
|
if (!$request->isPost()) {
|
|
return $this->renderError('');
|
|
} else {
|
|
$data = input('post.');
|
|
if (empty($data['goods_list_id'])) {
|
|
return $this->renderError('请求参数错误1');
|
|
}
|
|
|
|
$goods_list_id = $data['goods_list_id'];
|
|
$save_goods_extend_data = GoodsExtendListModel::where('goods_list_id', '=', $goods_list_id)->select();
|
|
// 保存数据到数据库中
|
|
foreach ($save_goods_extend_data as $data) {
|
|
if ($data) {
|
|
$data->delete(); // delete() 方法将从数据库中删除该对象
|
|
}
|
|
|
|
}
|
|
|
|
|
|
return $this->renderSuccess('编辑成功');
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|