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.

1181 lines
44 KiB

<?php
namespace app\mall\controller\admin;
use app\base\model\freight\Freight;
use app\base\model\platform\PlatformKdniaoCode;
use app\mall\model\MallAttach;
use app\mall\model\MallCategory;
use app\mall\model\MallCollect;
use app\mall\model\MallLabel;
use app\mall\model\MallOrder;
use app\mall\model\MallOrderProduct;
use app\mall\model\MallProduct;
use app\mall\model\MallProductAttach;
use app\mall\model\MallProductCategory;
use app\mall\model\MallProductLabel;
use app\mall\model\MallProductSku;
use app\mall\model\MallProductSpec;
use app\mall\model\MallProductSpecItem;
use app\mall\model\MallSpikeConfig;
use app\mall\model\MallSpikeProduct;
use app\mall\model\MallSpikeProductSku;
use app\mall\model\MallSpikeProductTime;
use app\mall\model\MallSpikeTime;
use Godruoyi\Snowflake\Snowflake;
use think\facade\Db;
use think\facade\View;
class Spike extends Base
{
/**
* 基础设置
* @return string|\think\response\Json
*/
public function config()
{
$config_model = new MallSpikeConfig();
if (request()->isPost()) {
$data = input('post.');
if ($data['r'] == 'base') {
$data['uid'] = UID;
$res = $config_model->dataUpdate($data);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
}
} else {
$r = input('param.r');
if ($r == "base") {
$data = $config_model->getOneData(['uid' => UID]);
View::assign('data', $data);
}
View::assign('r', $r);
return View::fetch();
}
}
//产品列表
public function product()
{
$search = input('get.');
autoSearch(['time', 'keyword', 'is_publish', 'category_id', 'is_recommend'], $search);
View::assign('search', $search);
$mall_product_model = new MallProduct();
$where = [['uid','=',UID],['type','=',2]];
$hasWhere = [];
if ($search['keyword']) {
$where[] = ['name','like', '%' . $search['keyword'] . '%'];
}
if ($search['time']) {
$time = explode('-', $search['time']);
$where[] = ['create_time','between time', [$time[0], $time[1]]];
}
if (isset($search['is_publish']) && $search['is_publish'] !== '') {
$where []= ['is_publish','=',$search['is_publish']];
}
if (isset($search['category_id']) && $search['category_id'] != '') {
$hasWhere[] = ['category_id','=', $search['category_id']];
}
if (isset($search['is_recommend']) && $search['is_recommend'] !== '') {
$where []= ['is_recommend','=',$search['is_recommend']];
}
$where = $mall_product_model->whereChange($where);
$dataList = $mall_product_model::hasWhere('mallProductCategory',$hasWhere)
->where($where)->order('id desc')->paginate([
'list_rows' => 20,
'page' => array_key_exists("page", $search) ? (string)$search['page'] : "1",
'query' => $search
], false)
->each(function ($item, $key) {
$category_model = new MallProductCategory();
$item['category_id'] = $category_model->where(['product_id' => $item['id']])->column('category_id');
//计算收藏量
$interactive_model = new MallCollect();
$item['collect_number'] = $interactive_model->getNumber(['product_id' => $item['id']]);
//计算总库存数
if ($item['is_spec_open'] == 1) {
$sku_model = new MallProductSku();
$item['stock'] = $sku_model->where(['product_id' => $item['id']])->sum('stock');
}
return $item;
});
View::assign('dataList', $dataList);
$attach = [
'total_html' => [['商品总数', $dataList->total()]]
];
View::assign('attach', $attach);
//获取二级分类信息
$mall_category_model = new MallCategory();
$where = [['uid','=',UID],['pid','>=',0]];
$category_list = $mall_category_model->getInfiniteNameData($where, 'sort', 2);
View::assign('category_list',$category_list);
return View::fetch();
}
/**
* 添加商品
* @return string
*/
public function productadd()
{
$mall_label_model = new MallLabel();
$mall_attach_model = new MallAttach();
$mall_product_model = new MallProduct();
$mall_category_model = new MallCategory();
$mall_product_label_model = new MallProductLabel();
$mall_product_attach_model = new MallProductAttach();
$mall_product_category_model = new MallProductCategory();
if (request()->isPost()) {
$data = input('post.');
if (!empty($data['is_change'])) {
return sendErrorMessage(1,'规格数据有变动,请重新点击[刷新规格项目表] 按钮!');
}
$data['uid'] = UID;
$data['is_spec_open'] = array_key_exists('is_spec_open', $data) ? $data['is_spec_open'] : 0;
//获取商品最高价与最低价
$price_min = $data['price'];
$price_max = $data['price'];
if($data['is_spec_open'] == 1){
if (!array_key_exists('spec_id', $data)) {
return sendErrorMessage(1,'请添加规格');
}
$price = [];
foreach ($data['option_ids'] as $key => $value) {
$price[] = $data['option_price_' . $value];
}
$price_min = min($price);
$price_max = max($price);
}
$update = [
'id' => $data['id'],
'uid' => UID,
'type' => 2,
'name' => $data['name'],
'price' => $data['price'],
'price_original' => $data['price_original'],
'price_min' => $price_min,
'price_max' => $price_max,
'attach_id' => array_key_exists('attach_id', $data) ? implode(',', $data['attach_id']) : null,
'cover_img' => $data['cover_img'],
'img_path' => $data['img_path'],
'video_url' => $data['video_url'],
'video_cover_img' => $data['video_cover_img'],
'description' => array_key_exists('description', $data) ? $data['description'] : '',
'is_recommend' => array_key_exists('is_recommend', $data) ? $data['is_recommend'] : 0,
'is_publish' => array_key_exists('is_publish', $data) ? $data['is_publish'] : 0,
'publish_time' => array_key_exists('is_publish', $data) ? time():0,
'sales_origial_number' => $data['sales_origial_number'],
'is_spec_open' => $data['is_spec_open'],
'stock' => $data['stock'],
'freight_type' => $data['freight_type'],
'freight_id' => $data['freight_type'] == 2 ? $data['freight_id'] : 0,
'freight_money' => $data['freight_type'] == 1 ? $data['freight_money'] : 0,
'weight' => $data['weight']
];
Db::startTrans();
$product_id = $mall_product_model->dataUpdate($update);
if ($product_id === false) {
Db::rollback();
return sendErrorMessage();
}
//商品标签操作
$res = $this->productLabel($product_id, $data);
if ($res == false) {
Db::rollback();
return sendErrorMessage();
}
//商品分类操作
$res = $this->productCategory($product_id, $data);
if ($res == false) {
Db::rollback();
return sendErrorMessage();
}
//商品规格操作
$res = $this->productSpec($product_id, $data);
if ($res == false) {
Db::rollback();
return sendErrorMessage();
}
//商品服务保障操作
$res = $this->productAttach($product_id, $data);
if ($res == false) {
Db::rollback();
return sendErrorMessage();
}
Db::commit();
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $mall_product_model->getOneData(['id' => $id]);
if (!empty($id)) {
$attach_list = explode(',', $data['attach_id']);
}
$where = [['uid','=',UID],['pid','>=',0]];
$categoryList = $mall_category_model->getInfiniteNameData($where, 'sort', 2);
//查询商品关联分类
$category_id = $mall_product_category_model->where('product_id', $id)->column('category_id');
if (empty($category_id)) {
$category_id = [];
}
foreach ($categoryList as $key => &$value) {
if (in_array($value['id'], $category_id)) {
$value['is_select'] = 1;
} else {
$value['is_select'] = 0;
}
}
View::assign('categoryList', $categoryList);
//查询服务保障
$attach_id = $mall_product_attach_model->where('product_id', $id)->column('attach_id');
$where_attach = ['uid' => UID];
$attach = $mall_attach_model->getAllData($where_attach);
foreach ($attach as $key => &$value) {
if (in_array($value['id'], $attach_id)) {
$value['is_select'] = 1;
} else {
$value['is_select'] = 0;
}
}
View::assign('attach', $attach);
//查询标签
$labelList = $mall_label_model->getAllData(['uid'=>UID]);
$label_id = $mall_product_label_model->where('product_id', $id)->column('label_id');
if (empty($label_id)) {
$label_id = [];
}
foreach ($labelList as $key => &$value) {
if (in_array($value['id'], $label_id)) {
$value['is_select'] = 1;
} else {
$value['is_select'] = 0;
}
}
View::assign('labelList', $labelList);
if (!empty($data)) {
$mall_product_spec_model = new MallProductSpec();
$mall_product_spec_item_model = new MallProductSpecItem();
//查询属性及属性项
$spec = $mall_product_spec_model->getAllData(['uid' => UID, 'product_id' => $id], '', 'id');
foreach ($spec as $key => &$value) {
$value['item'] = $mall_product_spec_item_model->getAllData(['uid' => UID, 'spec_id' => $value['spec_id'], 'product_id' => $id], '', 'id asc');
}
//查询SKU
$mall_product_sku_model = new MallProductSku();
$sku = $mall_product_sku_model->getSkuListByProduct(['product_id' => $id], 'id asc');
} else {
$spec = [];
$sku = [];
}
// dump($sku);
// exit();
//获取运费模板
$freight_model = new Freight();
$freight = $freight_model->select();
View::assign('freight', $freight);
View::assign('spec', $spec);
// dump(json_encode($sku));exit();
View::assign('sku', json_encode($sku));
View::assign('data', $data);
return View::fetch('productadd');
}
}
/**
* 产品发布
*/
public function productPublish()
{
$data = input('post.');
$product_model = new MallProduct();
$update_data = ['is_publish'=>$data['value'],'publish_time'=>time()];
if($data['value'] == 0){
$update_data['publish_time'] = 0;
}
$res = $product_model->where(['id'=>$data['id']])->update($update_data);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
}
/**
* 产品删除
*/
public function productDelete()
{
$data = input('post.');
$product_model = new MallProduct();
$spike_product_model = new MallSpikeProduct();
$is_spike = $spike_product_model->where([['start_time','>=',time()]])->whereOr([['end_time','>=',time()]])->count();
if($is_spike)
{
return sendErrorMessage(1,'商品参与秒杀,不可删除');
}
$res = $product_model->destroy(['id'=>$data['id']],true);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
}
/**
* 订单列表
* @return string
*/
public function order()
{
$search = input('get.');
$r = input('param.r');
$mall_order_model = new MallOrder();
$mall_order_product_model = new MallOrderProduct();
autoSearch(['time', 'keyword', 'status', 'pay_type', 'product_name', 'remind','linkman','mobile_phone'], $search);
$where = [['uid','=',UID],['type','=',2]];
$hasWhere = [];
if ($r > 0) {//订单状态
$where []= ['status','=',$r];
}
if ($search['keyword']) {//关键字
$where[] = ['order_number','like', '%' . $search['keyword'] . '%'];
}
if ($search['time']) {//订单时间
$time = explode('-', $search['time']);
$where[] = ['create_time','between time', [$time[0], $time[1]]];
}
if ($search['product_name'] !== null && $search['product_name'] !== '') {//商品名称
$hasWhere[] = ['product_name','like','%' . $search['product_name'] . '%'];
}
if ($search['pay_type'] !== null && $search['pay_type'] !== '') {//支付方式
$where []= ['pay_type','=',$search['pay_type']];
}
if (!empty($search['remind'])) {
if($search['remind']==1){
if(!isset($where['status'])||$where['status']==2){
$where []= ['remind_time','not null',''];
$where []= ['status','=',2];
}
}else{
if(!isset($where['status'])||$where['status']==2){
$where []= ['remind_time','null',''];
$where []= ['status','=',2];
}
}
}
if ($search['linkman'] !== null && $search['linkman'] !== '') {//联系人
$where[] = ['linkman','like', '%' . $search['linkman'] . '%'];
}
if ($search['mobile_phone'] !== null && $search['mobile_phone'] !== '') {//联系电话
$where[] = ['mobile_phone','like', '%' . $search['mobile_phone'] . '%'];
}
$where = $mall_order_model->whereChange($where);
session('where', $where);
$dataList = $mall_order_model::hasWhere('mallOrderProduct',$hasWhere)
->where($where)->order('id desc')->paginate([
'list_rows' => 20,
'page' => array_key_exists("page", $search) ? (string)$search['page'] : "1",
'query' => $search
], false)
->each(function ($item, $key)use($mall_order_product_model) {
$item['product_list'] = $mall_order_product_model->getAllData(['order_id' => $item['id'], 'uid' => UID], 'product_id,product_name,is_product_spec_open,product_sku_name,product_price,number,refund_total_number,refund_cur_number,total_price', 'create_time desc');
return $item;
});
$order_total_money = $mall_order_model::hasWhere('mallOrderProduct',$hasWhere)->where($where)->sum('MallOrder.total_price');
$attach = [
'total_html' => [
['订单总数', $dataList->total()],
['订单金额', formatNumber($order_total_money)]
],
];
View::assign('r', $r);
View::assign('search', $search);
View::assign('dataList', $dataList);
View::assign('attach', $attach);
return View::fetch();
}
/**
* 订单详情信息
* @return string
*/
public function orderdetail()
{
$kdiniao_express_model = new PlatformKdniaoCode();
$mall_order_model = new MallOrder();
$id = input('param.id');
$data = $mall_order_model->with('userInfo')->where(['id' => $id])->find();
if ($id) {
$order_id = $data['id'];
$mall_order_product_model = new MallOrderProduct();
$order_product = $mall_order_product_model->getAllData([['order_id','=', $order_id]]);
$data['total_number'] = 0;
foreach ($order_product as $key => $value) {
$value['sku_name'] = $this->skuToName($value['product_sku'], $value['product_id']);
$data['total_number'] += $value['number'] - $value['refund_total_number'] - $value['refund_cur_number'];
$order_product[$key] = $value;
}
$data['order_product'] = $order_product;
}
// dump($data->toArray());
// exit();
$expressList = $kdiniao_express_model->getAllData(['is_show' => 1]);
View::assign('data', $data);
View::assign('expressList', $expressList);
return View::fetch();
}
/**
* 订单发货
*/
public function orderSend()
{
$mall_order_model = new MallOrder();
$kdniao_express_model = new PlatformKdniaoCode();
if (request()->isPost()) {
$data = input('post.');
$order = $mall_order_model->getOneData(['id' => $data['id']]);
if (!in_array($order['status'],[2,3])) {
return sendErrorMessage(1,'订单状态已发生变化,请重试');
}
if (!isMobilephone($data['mobile_phone'])) {
return sendErrorMessage(1,'手机号格式不正确');
}
$data['express_name'] = $kdniao_express_model->where('id', $data['express_id'])->value('express_company');
$data['express_time'] = time();
$res = $mall_order_model->dataUpdate($data);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
$id = input('param.id');
$data = $mall_order_model->getOneData(['id' => $id]);
View::assign('data', $data);
$expressList = $kdniao_express_model->getAllData(['is_show' => 1]);
//获取快递公司
View::assign('expressList', $expressList);
return View::fetch('ordersend');
}
}
/**
* sku转换成对应名称
* @param $sku
* @param $product_id
* @return string
*/
public function skuToName($sku, $product_id)
{
$mall_product_spec_item_model = new MallProductSpecItem();
$sku_name = '';
if (!empty($sku)) {
$where_spec = ['spec_item_id' => explode('_', $sku), 'product_id' => $product_id];
$spec = $mall_product_spec_item_model->where($where_spec)->order('spec_item_id')->column('spec_item_value');
$sku_name = implode(' ', $spec);
}
return $sku_name;
}
/**
* 商品编辑
* @return mixed
*/
public function productupdate()
{
return $this->productadd();
}
/**
* 商品标签操作
* @param $product_id
* @param array $data
* @return bool
*/
public function productLabel($product_id, $data = [])
{
$product_label_model = new MallProductLabel();
$product_label_data = [];
foreach($data['label_id'] as $key=>$value){
$product_label_data[] = [
'product_id'=>$product_id,
'label_id'=>$value,
'create_time' => time()
];
}
$res = $product_label_model->where(['product_id'=>$product_id])->delete();
if($res === false){
return false;
}
$res = $product_label_model->insertAll($product_label_data);
if($res === false){
return false;
}
return true;
}
/**
* 商品-分类 操作
* @param $product_id
* @param array $data
* @return array|string
* @throws \think\Exception
*/
public function productCategory($product_id, $data = [])
{
$mall_product_category_model = new MallProductCategory();
$res = $mall_product_category_model->where(['product_id' => $product_id])->delete();
if ($res === false) {
return sendErrorMessage();
}
$insert_array = [];
if (array_key_exists('category_id', $data)) {
foreach ($data['category_id'] as $key => $value) {
$category_id = explode('_',$value);
if($category_id[0] == 0){
$insert_array[] = [
'product_id' => $product_id,
'category_id' => $category_id[1],
'type' => 1
];
}else{
$insert_array[] = [
'product_id' => $product_id,
'category_id' => $category_id[0],
'type' => 1
];
$insert_array[] = [
'product_id' => $product_id,
'category_id' => $category_id[1],
'type' => 2
];
}
}
$insert_array = array_values(array_unique($insert_array, SORT_REGULAR));
$res = $mall_product_category_model->insertAll($insert_array);
if ($res === false) {
return false;
}
}
return true;
}
/**
* 商品规格操作
* @param $product_id
* @param array $data
* @return array|string
* @throws \think\Exception
*/
public function productSpec($product_id, $data = [])
{
$snow_flake = new Snowflake();
$mall_product_model = new MallProduct();
$mall_product_sku_model = new MallProductSku();
$mall_product_spec_model = new MallProductSpec();
$mall_product_spec_item_model = new MallProductSpecItem();
//清空数据
$where = ['uid' => UID, 'product_id' => $product_id];
$res = $mall_product_spec_model->destroy($where);
if ($res === false) {
return sendErrorMessage();
}
$res = $mall_product_spec_item_model->destroy($where);
if ($res === false) {
return sendErrorMessage();
}
$res = $mall_product_sku_model->destroy($where);
if ($res === false) {
return sendErrorMessage();
}
if ($res === false) {
return sendErrorMessage();
}
if ($data['is_spec_open'] == 1) { //商品规格开启
//保存规格名
$spec_data = [];
$spec_item_data = [];
if (!array_key_exists('spec_id', $data)) {
return sendErrorMessage(1,'请添加规格');
}
foreach ($data['spec_id'] as $key => $value) {
$spec_data[] = [
'id' => $snow_flake->id(),
'uid' => UID,
'product_id' => $product_id,
'spec_id' => $value,
'spec_title' => $data['spec_title'][$value],
'create_time' => time(),
];
if (!array_key_exists('spec_item_name_' . $value, $data)) {
return sendErrorMessage(1,'请添加' . $data['spec_title'][$value] . '的规格项');
}
foreach ($data['spec_item_name_' . $value] as $k => $v) {
$spec_item_data[] = [
'id' => $snow_flake->id(),
'uid' => UID,
'product_id' => $product_id,
'spec_id' => $value,
'spec_item_id' => $data['spec_item_id_' . $value][$k],
'spec_item_value' => $data['spec_item_name_' . $value][$k],
'create_time' => time(),
];
}
}
$res = $mall_product_spec_model->insertAll($spec_data);
if ($res === false) {
return sendErrorMessage();
}
//保存规格值
$res = $mall_product_spec_item_model->insertAll($spec_item_data);
if ($res === false) {
return sendErrorMessage();
}
//保存SKU
$sku_data = [];
foreach ($data['option_ids'] as $key => $value) {
$sku_data[] = [
'id' => $snow_flake->id(),
'uid' => UID,
'product_id' => $product_id,
'sku' => $value,
'price' => $data['option_price_' . $value],
'stock' => $data['option_stock_' . $value],
'cover_img' => $data['option_cover_img_' . $value],
'create_time' => time()
];
}
$res = $mall_product_sku_model->insertAll($sku_data);
if ($res === false) {
return sendErrorMessage();
}
$all_stock = array_sum(array_column($sku_data,'stock'));
$res = $mall_product_model->update(['stock'=>$all_stock],['id'=>$product_id]);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
return sendSuccessMessage();
}
}
/**
* 商品服务保障操作
* @param $product_id
* @param array $data
* @return bool
*/
public function productAttach($product_id, $data = [])
{
$product_attach_model = new MallProductAttach();
$product_attach_data = [];
$res = $product_attach_model->where(['product_id'=>$product_id])->delete();
if (array_key_exists('attach_id', $data)) {
foreach ($data['attach_id'] as $key => $value) {
$product_attach_data[] = [
'product_id' => $product_id,
'attach_id' => $value,
'create_time' => time()
];
}
if ($res === false) {
return false;
}
$res = $product_attach_model->insertAll($product_attach_data);
if ($res === false) {
return false;
}
}
return true;
}
/**
* 时段管理
*/
public function times(){
$search = input('get.');
autoSearch(['time'], $search);
$spike_time_model = new MallSpikeTime();
$where = [['uid', '=', UID]];
if ($search['time']) {
$time = explode('-', $search['time']);
$where[] = ['create_time', 'between time', [$time[0], $time[1]]];
}
$dataList = $spike_time_model
->where($where)->order('sort desc,id desc')
->paginate([
'list_rows' => 15,
'page' => array_key_exists("page", $search) ? (string)$search['page'] : "1",
'query' => $search
], false)->each(function ($item) {
return $item;
});
$attach = [
'total_html' => [['时段总数', $dataList->total()]]
];
View::assign('attach', $attach);
View::assign('search', $search);
View::assign('dataList', $dataList);
return View::fetch();
}
/**
* 时间段添加
*/
public function timesAdd()
{
$spike_time_model = new MallSpikeTime();
$timesList = $spike_time_model->getTimes();
if (request()->isPost()) {
$data = input('post.');
$data['uid'] = UID;
$data['start_time'] = $timesList[$data['start_key']]['time'];
$data['end_time'] = $timesList[$data['end_key']]['time'];
$where = [['start_key','between',[$data['start_key'],$data['end_key']]]];
if($data['id']){
$where[] = ['id','<>',$data['id']];
}
$times = $spike_time_model
->where($where)
->find();
if (!empty($times)) {
return sendErrorMessage(1,'时间段重复');
}
$res = $spike_time_model->dataUpdate($data);
if (!$res) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $spike_time_model->getOneData(['id' => $id]);
View::assign('data', $data);
View::assign('timesList', $timesList);
View::assign('timesListJson', json_encode($timesList));
return View::fetch('timesadd');
}
}
/**
* 时间段修改
* @return string|\think\response\Json
*/
public function timesUpdate()
{
return $this->timesAdd();
}
/**
* 活动管理
*/
public function activity()
{
$search = input('get.');
autoSearch(['time', 'product_name'], $search);
$spike_product_model = new MallSpikeProduct();
$where = [['uid', '=', UID]];
$hasWhere = [];
if ($search['time']) {
$time = explode('-', $search['time']);
$where[] = ['create_time', 'between time', [$time[0], $time[1]]];
}
if ($search['product_name']) {//关键字
$hasWhere[] = ['name','like','%' . $search['product_name'] . '%'];
}
$where = $spike_product_model->whereChange($where);
$dataList = $spike_product_model::hasWhere('mallProduct',$hasWhere)
->with(['mallProduct'])
->where($where)->order('id desc')
->paginate([
'list_rows' => 10,
'page' => array_key_exists("page", $search) ? (string)$search['page'] : "1",
'query' => $search
], false)->each(function ($item) {
return $item;
});
// dump($dataList->toArray());exit();
$attach = [
'total_html' => [['活动总数', $dataList->total()]]
];
View::assign('attach', $attach);
View::assign('search', $search);
View::assign('dataList', $dataList);
return View::fetch();
}
/**
* 添加活动
*/
public function activityAdd()
{
$snow_flake = new Snowflake();
$product_model = new MallProduct();
$spike_time_model = new MallSpikeTime();
$spike_product_model = new MallSpikeProduct();
$spike_product_sku_model = new MallSpikeProductSku();
if (request()->isPost()) {
$data = input('post.');
$data['uid'] = UID;
if(!array_key_exists('time_id',$data)){
return sendErrorMessage(1,'请选择开放时间');
}
$start_date_time = strtotime($data['start_date']);
$end_date_time = strtotime($data['end_date']);
$day_number = ($end_date_time - $start_date_time)/86400;
$dateArray = [];
for($i=0;$i<=$day_number;$i++)
{
$dateArray[] = strtotime($data['start_date'])+(86400*$i);
}
$spike_time = $spike_time_model->getAllData([['id','in',$data['time_id']]]);
$spike_product_data = [];//商品数据
$spike_product_sku_data = [];//商品规格插入数据
$spike_product_sku_all_data = [];//商品规格初始数据
//规格处理
if($data['is_spec_open'] == 0)
{//无规格
foreach($dateArray as $val){
foreach($spike_time as $value)
{
$spike_product_data[] = [
'id' => $snow_flake->id(),
'uid' => UID,
'product_id' => $data['product_id'],
'price_spike' => $data['option_price_'],
'price_spike_min' => $data['option_price_'],
'price_spike_max' => $data['option_price_'],
'number' => $data['option_number_'],
'start_time' => strtotime(date('Y-m-d',$val).' '.$value['start_time']),
'end_time' => strtotime(date('Y-m-d',$val).' '.$value['end_time']),
'create_time' => time(),
'update_time' => time(),
];
}
}
}else{//有规格
foreach($data['sku'] as $values)
{
$spike_product_sku_all_data[] = [
'id' => 0,
'uid' => UID,
'product_id' => $data['product_id'],
'spike_product_id' => 0,
'sku' => $values,
'price_spike' => $data['option_price_'.$values],
'number' => $data['option_number_'.$values],
'create_time' => time(),
'update_time' => time(),
];
}
foreach($dateArray as $val){
foreach($spike_time as $value)
{
$spike_product_id = $snow_flake->id();
$spike_product_data[] = [
'id' => $spike_product_id,
'uid' => UID,
'product_id' => $data['product_id'],
'price_spike' => min(array_column($spike_product_sku_all_data,'price_spike')),
'price_spike_min' => min(array_column($spike_product_sku_all_data,'price_spike')),
'price_spike_max' => max(array_column($spike_product_sku_all_data,'price_spike')),
'number' => array_sum(array_column($spike_product_sku_all_data,'number')),
'start_time' => strtotime(date('Y-m-d',$val).' '.$value['start_time']),
'end_time' => strtotime(date('Y-m-d',$val).' '.$value['end_time']),
'create_time' => time(),
'update_time' => time(),
];
foreach($spike_product_sku_all_data as $values){
$values['id'] = $snow_flake->id();
$values['spike_product_id'] = $spike_product_id;
$spike_product_sku_data [] = $values;
}
}
}
}
Db::startTrans();
$res = $spike_product_model->insertAll($spike_product_data);
if (!$res) {
Db::rollback();
return sendErrorMessage();
}
if(!empty($spike_product_sku_data)){
$res = $spike_product_sku_model->insertAll($spike_product_sku_data);
if($res === false){
Db::rollback();
return sendErrorMessage();
}
}
Db::commit();
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $spike_product_model->getOneData(['id' => $id]);
$timeArray = $spike_time_model->getAllData([['uid','=',UID]],'*','sort desc,id desc');
$productList = $product_model->getAllData([['type','=','2'],['uid','=',UID]]);
View::assign('data', $data);
View::assign('timeArray', $timeArray);
View::assign('productList', $productList);
return View::fetch('activityadd');
}
}
/**
* 活动规格
* @return \think\response\Json
*/
public function activitySku()
{
$mall_product_model = new MallProduct();
$mall_product_sku_model = new MallProductSku();
$mall_product_spec_item_model = new MallProductSpecItem();
$id = input('param.id', 0);
//获取商品信息
$product = $mall_product_model->getOneData(['id' => $id, 'uid' => UID]);
//获取规格信息
if (!empty($product) && $product['is_spec_open'] == 1) {
$sku = $mall_product_sku_model->getSkuListByProduct(['product_id' => $id], 'id asc');
//进行拼装sku_name
foreach ($sku as $key => $value) {
$item = explode('_', $value['sku']);
$item_value = $mall_product_spec_item_model->where([['product_id', '=', $id], ['spec_item_id', 'IN', $item]])->column('spec_item_value');
if (!empty($item_value)) {
$value['sku_name'] = implode(',', $item_value);
}
$sku[$key] = $value;
}
} else {
$sku = [];
}
View::assign('product', $product);
View::assign('sku', $sku);
View::assign('productid', $id);
$html = View::fetch('activitysku');
return sendSuccessMessage($html, '', '');
}
/**
* 活动修改
*/
public function activityUpdate()
{
$snow_flake = new Snowflake();
$spike_product_model = new MallSpikeProduct();
$spike_product_sku_model = new MallSpikeProductSku();
$mall_product_spec_item_model = new MallProductSpecItem();
if (request()->isPost()) {
$data = input('post.');
$data['uid'] = UID;
if(array_key_exists('sku',$data)){
$spike_product_sku_data = [];
foreach($data['sku'] as $values)
{
$spike_product_sku_data[] = [
'id' => $snow_flake->id(),
'uid' => UID,
'product_id' => $data['product_id'],
'spike_product_id' => $data['id'],
'sku' => $values,
'price_spike' => $data['option_price_'.$values],
'number' => $data['option_number_'.$values],
'create_time' => time(),
'update_time' => time(),
];
}
$product_Data = [
'id' => $data['id'],
'price_spike' => min(array_column($spike_product_sku_data,'price_spike')),
'price_spike_min' => min(array_column($spike_product_sku_data,'price_spike')),
'price_spike_max' => max(array_column($spike_product_sku_data,'price_spike')),
'number' => array_sum(array_column($spike_product_sku_data,'number')),
];
}else{
$product_Data = [
'id' => $data['id'],
'price_spike' => $data['option_price_'],
'price_spike_min' => $data['option_price_'],
'price_spike_max' => $data['option_price_'],
'number' => $data['option_number_'],
];
}
Db::startTrans();
$res = $spike_product_model->dataUpdate($product_Data);
if (!$res) {
Db::rollback();
return sendErrorMessage();
}
$res = $spike_product_sku_model->destroy(['spike_product_id'=>$data['id']]);
if (!$res) {
Db::rollback();
return sendErrorMessage();
}
if(array_key_exists('sku',$data)){
$res = $spike_product_sku_model->insertAll($spike_product_sku_data);
if($res === false){
Db::rollback();
return sendErrorMessage();
}
}
Db::commit();
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $spike_product_model->with('mallProduct')->where(['id' => $id])->find();
$sku = $spike_product_sku_model->with('skuStock')->where(['spike_product_id'=>$id])->select();
foreach ($sku as $key => $value) {
$item = explode('_', $value['sku']);
$item_value = $mall_product_spec_item_model->where([['product_id', '=', $value['product_id']], ['spec_item_id', 'IN', $item]])->column('spec_item_value');
if (!empty($item_value)) {
$value['sku_name'] = implode(',', $item_value);
}
$sku[$key] = $value;
}
View::assign('sku', $sku);
View::assign('data', $data);
return View::fetch('activityupdate');
}
}
/**
* 秒杀场次
*/
public function scene()
{
$search = input('get.');
autoSearch(['time', 'product_name'], $search);
$spike_product_model = new MallSpikeProduct();
$where = [['uid', '=', UID]];
$hasWhere = [];
if ($search['time']) {
$time = explode('-', $search['time']);
$where[] = ['create_time', 'between time', [$time[0], $time[1]]];
}
if ($search['product_name']) {//关键字
$hasWhere[] = ['name','like','%' . $search['product_name'] . '%'];
}
$where = $spike_product_model->whereChange($where);
$dataList = $spike_product_model::hasWhere('mallProduct',$hasWhere)
->where($where)->order('id desc')
->group('start_time,end_time')
->paginate([
'list_rows' => 10,
'page' => array_key_exists("page", $search) ? (string)$search['page'] : "1",
'query' => $search
], false)->each(function ($item) {
return $item;
});
$attach = [
'total_html' => [['场次总数', $dataList->total()]]
];
View::assign('attach', $attach);
View::assign('search', $search);
View::assign('dataList', $dataList);
return View::fetch();
}
/**
* 场次商品
*/
public function sceneProduct()
{
$search = input('get.');
autoSearch(['time', 'product_name'], $search);
$spike_product_model = new MallSpikeProduct();
$spike_product = $spike_product_model->getOneData(['id'=>$search['id']]);
$where = [['uid', '=', UID],['start_time','=',$spike_product['start_time']],['end_time','=',$spike_product['end_time']]];
$hasWhere = [];
if ($search['time']) {
$time = explode('-', $search['time']);
$where[] = ['create_time', 'between time', [$time[0], $time[1]]];
}
if ($search['product_name']) {//关键字
$hasWhere[] = ['name','like','%' . $search['product_name'] . '%'];
}
$where = $spike_product_model->whereChange($where);
$dataList = $spike_product_model::hasWhere('mallProduct',$hasWhere)
->with(['mallProduct'])
->where($where)->order('id desc')
->paginate([
'list_rows' => 10,
'page' => array_key_exists("page", $search) ? (string)$search['page'] : "1",
'query' => $search
], false)->each(function ($item) {
return $item;
});
$attach = [
'total_html' => [['场次总数', $dataList->total()]]
];
View::assign('attach', $attach);
View::assign('search', $search);
View::assign('dataList', $dataList);
return View::fetch('sceneproduct');
}
}