初始上传
This commit is contained in:
1091
addon/cardservice/model/CardGoods.php
Executable file
1091
addon/cardservice/model/CardGoods.php
Executable file
File diff suppressed because it is too large
Load Diff
616
addon/cardservice/model/MemberCard.php
Executable file
616
addon/cardservice/model/MemberCard.php
Executable file
@@ -0,0 +1,616 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
|
||||
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\cardservice\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Cron;
|
||||
use app\model\verify\Verify;
|
||||
use think\facade\Db;
|
||||
use think\facade\Log;
|
||||
|
||||
class MemberCard extends BaseModel
|
||||
{
|
||||
//卡包状态
|
||||
const STATUS_NORMAL = 1;
|
||||
const STATUS_INVALID = 0;
|
||||
|
||||
/**
|
||||
* 创建会员卡项
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
public function create($param)
|
||||
{
|
||||
$store_id = $param['store_id'] ?? 0;
|
||||
$goods_info = model('goods')->getInfo([
|
||||
['goods_id', '=', $param['goods_id']],
|
||||
['goods_state', '=', 1],
|
||||
['is_delete', '=', 0],
|
||||
], 'goods_name,goods_class,sku_id');
|
||||
if (empty($goods_info)) {
|
||||
return $this->error('', '未获取到商品信息');
|
||||
}
|
||||
|
||||
$verify_model = new Verify();
|
||||
$end_time = 0;
|
||||
$card_info = model('goods_card')->getInfo([['goods_id', '=', $param['goods_id']]], 'site_id, goods_id, card_id, card_type, card_type_name, renew_price, recharge_money, common_num, discount_goods_type, discount, validity_type, validity_day, validity_time');
|
||||
if (empty($card_info)) {
|
||||
return $this->error('', '未获取到卡项信息');
|
||||
}
|
||||
$card_item = model('goods_card_item')->getList([['ngci.card_goods_id', '=', $param['goods_id']]], 'ngci.goods_id, ngci.sku_id, ngci.num, ngci.discount, ngci.card_goods_id, ngci.id, ngs.goods_class, ngs.sku_name, ngs.sku_image, ngs.price, ngs.is_virtual', '', 'ngci', [['goods_sku ngs', 'ngci.sku_id = ngs.sku_id', 'inner']]);
|
||||
switch ($card_info['validity_type']) {
|
||||
case 1:
|
||||
$end_time = strtotime('+' . $card_info['validity_day'] . ' day');
|
||||
break;
|
||||
case 2:
|
||||
$end_time = $card_info['validity_time'];
|
||||
if ($end_time < time()) {
|
||||
return $this->error('', '卡项已超出有效期');
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
model('member_goods_card')->startTrans();
|
||||
try {
|
||||
$delivery_method = 'verify';
|
||||
$create_num = $param['num'] ?? 1;
|
||||
for ($i = 1; $i <= $create_num; $i++) {
|
||||
$card_id = model('member_goods_card')->add([
|
||||
'site_id' => $param['site_id'] ?? 0,
|
||||
'card_code' => $this->createNo(),
|
||||
'member_id' => $param['member_id'],
|
||||
'goods_id' => $param['goods_id'],
|
||||
'create_time' => time(),
|
||||
'end_time' => $end_time,
|
||||
'order_id' => $param['order_id'] ?? 0,
|
||||
'card_type' => $card_info['card_type'],
|
||||
'total_num' => $card_info['common_num'],
|
||||
'delivery_method' => $delivery_method,
|
||||
'store_id' => $store_id,
|
||||
'goods_name' => $goods_info['goods_name']
|
||||
]);
|
||||
|
||||
$total_num = 0;
|
||||
foreach ($card_item as $item) {
|
||||
if (isset($card_info) && $card_info['card_type'] == 'commoncard') {
|
||||
$item['num'] = $card_info['common_num'];
|
||||
$total_num = $item['num'];
|
||||
} else {
|
||||
$total_num += $item['num'];
|
||||
}
|
||||
$item_data = [
|
||||
'site_id' => $param['site_id'] ?? 0,
|
||||
'card_id' => $card_id,
|
||||
'member_id' => $param['member_id'],
|
||||
'goods_id' => $item['goods_id'],
|
||||
'sku_id' => $item['sku_id'],
|
||||
'num' => $item['num'],
|
||||
'end_time' => $end_time,
|
||||
'card_type' => isset($card_info) ? $card_info['card_type'] : '',
|
||||
'goods_class' => $item['goods_class'],
|
||||
'member_verify_id' => 0,
|
||||
'store_id' => $store_id
|
||||
];
|
||||
|
||||
$item_array = [
|
||||
[
|
||||
'img' => $item['sku_image'],
|
||||
'name' => $item['sku_name'],
|
||||
'price' => $item['price'],
|
||||
'num' => $item['num'],
|
||||
'remark_array' => []
|
||||
]
|
||||
];
|
||||
$verify_content_json = $verify_model->getVerifyJson($item_array, []);
|
||||
//创建核销码
|
||||
$verify_res = $verify_model->addVerify("cardgoods", $item_data['site_id'], "", $verify_content_json, $end_time, $item_data['num'], 0, $param['member_id']);
|
||||
if ($verify_res['code'] != 0) {
|
||||
model('member_goods_card')->rollback();
|
||||
return $this->error([], '核销码创建失败');
|
||||
}
|
||||
$item_data['member_verify_id'] = $verify_res['data']['verify_id'];
|
||||
model('member_goods_card_item')->add($item_data);
|
||||
}
|
||||
model('member_goods_card')->update(['total_num' => $total_num, 'delivery_method' => $delivery_method], [['card_id', '=', $card_id]]);
|
||||
|
||||
if ($end_time > 0) (new Cron())->addCron(1, 0, "会员卡项过期失效", "CronMemberCardExpire", $end_time, $card_id);
|
||||
|
||||
}
|
||||
|
||||
model('member_goods_card')->commit();
|
||||
return $this->success($card_id);
|
||||
} catch (\Exception $e) {
|
||||
model('member_goods_card')->rollback();
|
||||
return $this->error([], '会员卡项创建失败' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成订单编号
|
||||
* @param array $site_id
|
||||
*/
|
||||
public function createNo()
|
||||
{
|
||||
$time_str = date('YmdHi');
|
||||
$card_no = $time_str . (string)rand(11111, 99999);
|
||||
return $card_no;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员卡项信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getCardInfo($condition = [], $field = '*', $alias = '', $join = [])
|
||||
{
|
||||
$data = model('member_goods_card')->getInfo($condition, $field, $alias, $join);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员卡项数量
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getCardCount($condition = [], $field = '*', $alias = 'a', $join = null)
|
||||
{
|
||||
$data = model('member_goods_card')->getCount($condition, $field, $alias, $join);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员卡项分页列表
|
||||
* @param array $condition
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param int $page
|
||||
* @param int $list_rows
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getCardPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [])
|
||||
{
|
||||
$data = model('member_goods_card')->pageList($condition, $field, $order, $page, $list_rows, $alias, $join);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员卡项项列表
|
||||
* @param array $condition
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getCartItemList($condition = [], $field = true, $order = '', $alias = 'a', $join = [])
|
||||
{
|
||||
$data = model('member_goods_card_item')->getList($condition, $field, $order, $alias, $join);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员卡项项分页列表
|
||||
* @param array $condition
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param int $page
|
||||
* @param int $list_rows
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getCartItemPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [])
|
||||
{
|
||||
$data = model('member_goods_card_item')->pageList($condition, $field, $order, $page, $list_rows, $alias, $join);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员卡项项详情
|
||||
*/
|
||||
public function getCartItemInfo($condition = [], $field = '*', $alias = '', $join = [])
|
||||
{
|
||||
$data = model('member_goods_card_item')->getInfo($condition, $field, $alias, $join);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 卡项核销
|
||||
* @param $param
|
||||
*/
|
||||
public function verify($param)
|
||||
{
|
||||
$card_item_info = model('member_goods_card_item')->getInfo([['member_verify_id', '=', $param['verify_id']]], 'item_id');
|
||||
if (empty($card_item_info)) return $this->error('', '未获取到卡项信息');
|
||||
|
||||
return $this->cardUse([
|
||||
'item_id' => $card_item_info['item_id'],
|
||||
'num' => 1,
|
||||
'type' => 'verify',
|
||||
'relation_id' => $param['verify_id'],
|
||||
'store_id' => $param['store_id'] ?? 0
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 卡项使用
|
||||
* array 两种参数形式 @param
|
||||
* array('item_id' => $card_item_id,'num' => $num,'type' => $type,'relation_id' => $order_goods_id);
|
||||
* array(('item_id' => $card_item_id,'num' => $num,'type' => $type,'relation_id' => $order_goods_id));
|
||||
*/
|
||||
public function cardUse($param)
|
||||
{
|
||||
$temp_item_id = $param['item_id'] ?? 0;
|
||||
if ($temp_item_id > 0) {
|
||||
$item_list = [$param];
|
||||
} else {
|
||||
$item_list = $param;
|
||||
}
|
||||
model('member_goods_card_item')->startTrans();
|
||||
try {
|
||||
$out_relation_ids = [];
|
||||
foreach ($item_list as $item_v) {
|
||||
$item_id = $item_v['item_id'];
|
||||
$item_num = $item_v['num'] ?? 1;
|
||||
$item_type = $item_v['type'];
|
||||
$item_relation_id = $item_v['relation_id'];
|
||||
$item_store_id = $item_v['store_id'] ?? 0;
|
||||
$card_item_info = model('member_goods_card_item')->getInfo([['mci.item_id', '=', $item_id]],
|
||||
'mci.site_id,mci.card_id,mci.num,mci.use_num,mci.item_id,mci.goods_id,mci.sku_id,mci.end_time,mci.member_verify_id,mc.card_type,mc.total_num,mc.total_use_num,mc.delivery_method', 'mci',
|
||||
[
|
||||
['member_goods_card mc', 'mc.card_id = mci.card_id', 'left']
|
||||
]);
|
||||
|
||||
if (empty($card_item_info)) {
|
||||
model('member_goods_card_item')->rollback();
|
||||
return $this->error('', '未获取到卡项信息');
|
||||
}
|
||||
if ($card_item_info['end_time'] > 0 && $card_item_info['end_time'] < time()) {
|
||||
model('member_goods_card_item')->rollback();
|
||||
return $this->error('', '已超出有效期');
|
||||
}
|
||||
if ($card_item_info['card_type'] != 'timecard' && ($card_item_info['num'] - $card_item_info['use_num'] - $item_num) < 0) {
|
||||
model('member_goods_card_item')->rollback();
|
||||
return $this->error('', '卡项可用次数不足');
|
||||
}
|
||||
// 如果是通用卡项
|
||||
if ($card_item_info['card_type'] == 'commoncard') {
|
||||
model('member_goods_card_item')->setInc([['card_id', '=', $card_item_info['card_id']]], 'use_num', $item_num);
|
||||
// 同步核销码使用次数
|
||||
if ($item_type == 'order') {
|
||||
$verify_ids = model('member_goods_card_item')->getColumn([['card_id', '=', $card_item_info['card_id']]], 'member_verify_id');
|
||||
} else {
|
||||
$verify_ids = model('member_goods_card_item')->getColumn([['card_id', '=', $card_item_info['card_id']], ['item_id', '<>', $card_item_info['item_id']]], 'member_verify_id');
|
||||
}
|
||||
if (!empty($verify_ids)) {
|
||||
model('verify')->setInc([['id', 'in', $verify_ids]], 'verify_use_num', $item_num);
|
||||
model('verify')->update(['is_verify' => Verify::STATUS_IS_VERIFY, 'verify_time' => time()], [
|
||||
['id', 'in', $verify_ids],
|
||||
['verify_total_count', '>', 0],
|
||||
['', 'exp', Db::raw('verify_use_num >= verify_total_count')]
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
model('member_goods_card_item')->setInc([['item_id', '=', $card_item_info['item_id']]], 'use_num', $item_num);
|
||||
// 同步核销码使用次数
|
||||
if ($item_type == 'order') {
|
||||
model('verify')->setInc([['id', '=', $card_item_info['member_verify_id']]], 'verify_use_num', $item_num);
|
||||
model('verify')->update(['is_verify' => Verify::STATUS_IS_VERIFY, 'verify_time' => time()], [
|
||||
['id', '=', $card_item_info['member_verify_id']],
|
||||
['verify_total_count', '>', 0],
|
||||
['', 'exp', Db::raw('verify_use_num >= verify_total_count')]
|
||||
]);
|
||||
}
|
||||
}
|
||||
model('member_goods_card')->setInc([['card_id', '=', $card_item_info['card_id']]], 'total_use_num', $item_num);
|
||||
|
||||
// 如果卡项次数已使用完
|
||||
if ($card_item_info['card_type'] != 'timecard' && ($card_item_info['total_num'] - $card_item_info['total_use_num'] - $item_num) == 0) {
|
||||
model('member_goods_card')->update(['status' => 0], [['card_id', '=', $card_item_info['card_id']]]);
|
||||
(new Cron())->deleteCron([['event', '=', 'CronMemberCardExpire'], ['relate_id', '=', $card_item_info['card_id']]]);
|
||||
}
|
||||
// 添加使用记录
|
||||
model('member_goods_card_records')->add([
|
||||
'card_id' => $card_item_info['card_id'],
|
||||
'site_id' => $card_item_info['site_id'],
|
||||
'card_item_id' => $card_item_info['item_id'],
|
||||
'type' => $item_type,
|
||||
'relation_id' => $item_relation_id,
|
||||
'create_time' => time(),
|
||||
'store_id' => $item_store_id,
|
||||
'num' => $item_num
|
||||
]);
|
||||
}
|
||||
model('member_goods_card_item')->commit();
|
||||
return $this->success(['out_relation_ids' => $out_relation_ids]);
|
||||
} catch (\Exception $e) {
|
||||
model('member_goods_card_item')->rollback();
|
||||
Log::write('卡项使用错误,错误原因:' . $e->getMessage() . $e->getFile() . $e->getLine() . '请求参数:' . json_encode($param));
|
||||
return $this->error('', '卡项使用失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员卡项使用记录列表
|
||||
* @param array $condition
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberCardRecordsList($condition = [], $field = true, $order = '', $alias = 'a', $join = [])
|
||||
{
|
||||
$data = model('member_goods_card_records')->getList($condition, $field, $order, $alias, $join);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
public function getMemberCardRecordsPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [])
|
||||
{
|
||||
$data = model('member_goods_card_records')->pageList($condition, $field, $order, $page, $list_rows, $alias, $join);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询当前会员是否可以使用卡项
|
||||
* @param $params
|
||||
* @param bool $is_buy
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberCardUse($params)
|
||||
{
|
||||
if (!empty($params['card_item_id'])) {
|
||||
$card_condition = [
|
||||
['moi.site_id', '=', $params['site_id']],
|
||||
['moi.member_id', '=', $params['member_id']],
|
||||
['', 'exp', Db::raw('(mo.end_time = 0 or mo.end_time > ' . time() . ') and mo.status = ' . self::STATUS_NORMAL)],
|
||||
['moi.item_id', '=', $params['card_item_id']]
|
||||
];
|
||||
|
||||
$card_field = 'moi.end_time, moi.card_id, moi.item_id';
|
||||
$card_join = [
|
||||
['member_goods_card mo', 'mo.card_id = moi.card_id', 'left'],
|
||||
['goods_card gc', 'gc.goods_id = mo.goods_id', 'left']
|
||||
];
|
||||
$card_item_info = $this->getCartItemInfo($card_condition, $card_field, 'moi', $card_join)['data'];
|
||||
return $this->success($card_item_info);
|
||||
}
|
||||
return $this->error();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询会员可用的卡项(关联商品查询)
|
||||
* @param $params
|
||||
*/
|
||||
public function getMemberUseCardList($params)
|
||||
{
|
||||
$site_id = $params['site_id'] ?? 0;
|
||||
$member_id = $params['member_id'] ?? 0;
|
||||
$goods_id = $params['goods_id'] ?? 0;
|
||||
$sku_id = $params['sku_id'];
|
||||
$alias = 'moi';
|
||||
$condition = array(
|
||||
[$alias . '.member_id', '=', $member_id],
|
||||
['mo.status', '=', 1],
|
||||
);
|
||||
if ($goods_id > 0) {
|
||||
$condition[] = [$alias . '.goods_id', '=', $goods_id];
|
||||
}
|
||||
if ($sku_id > 0) {
|
||||
$condition[] = [$alias . '.sku_id', '=', $sku_id];
|
||||
}
|
||||
$field = 'moi.*,mo.status';
|
||||
$join = [
|
||||
['member_goods_card mo', 'mo.card_id = moi.card_id', 'left'],
|
||||
['goods_card gc', 'gc.goods_id = mo.goods_id', 'left']
|
||||
];
|
||||
$list = model('member_goods_card_item')->getList($condition, $field, '', $alias, $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检验商品是否可被卡项抵扣
|
||||
* @param $params
|
||||
*/
|
||||
public function getUseCardNum($params)
|
||||
{
|
||||
$member_id = $params['member_id'];
|
||||
$sku_id = $params['sku_id'];
|
||||
$item_id = $params['item_id'];
|
||||
$card_item_condition = array(
|
||||
['member_id', '=', $member_id],
|
||||
['sku_id', '=', $sku_id],
|
||||
['item_id', '=', $item_id]
|
||||
);
|
||||
$card_item_info = model('member_goods_card_item')->getInfo($card_item_condition);
|
||||
if (empty($card_item_info))
|
||||
return $this->error([], '没有可用的卡项');
|
||||
|
||||
$card_type = $card_item_info['card_type'];
|
||||
$card_id = $card_item_info['card_id'];
|
||||
$card_condition = array(
|
||||
['card_id', '=', $card_id]
|
||||
);
|
||||
$card_info = model('member_goods_card')->getInfo($card_condition);
|
||||
if (empty($card_item_info))
|
||||
return $this->error([], '没有可用的卡项');
|
||||
|
||||
$return_params = array(
|
||||
'card_item_info' => $card_item_info,
|
||||
'card_info' => $card_info
|
||||
);
|
||||
$status = $card_info['status'];
|
||||
if ($status != self::STATUS_NORMAL)
|
||||
return $this->error($return_params, '卡包已失效');
|
||||
|
||||
$total_num = $card_info['total_num'];
|
||||
$total_use_num = $card_info['total_use_num'];
|
||||
|
||||
switch ($card_type) {
|
||||
case 'oncecard'://限次
|
||||
$item_num = $card_item_info['num'];
|
||||
$item_use_num = $card_item_info['use_num'];
|
||||
$surplus_num = $item_num - $item_use_num;
|
||||
break;
|
||||
case 'timecard'://限时
|
||||
$surplus_num = 0;
|
||||
break;
|
||||
case 'commoncard'://通用共享次数
|
||||
$surplus_num = $total_num - $total_use_num;
|
||||
break;
|
||||
case ''://通用共享次数
|
||||
$item_num = $card_item_info['num'];
|
||||
$item_use_num = $card_item_info['use_num'];
|
||||
$surplus_num = $item_num - $item_use_num;
|
||||
break;
|
||||
}
|
||||
$return_params['card_num'] = $surplus_num;
|
||||
return $this->success($return_params);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭会员卡项
|
||||
* @param $card_id
|
||||
*/
|
||||
public function memberOncecardClose($card_ids)
|
||||
{
|
||||
model('member_goods_card')->startTrans();
|
||||
try {
|
||||
// 关闭会员卡项
|
||||
model('member_goods_card')->update(['status' => 0], [['card_id', 'in', $card_ids]]);
|
||||
(new Cron())->deleteCron([['event', '=', 'CronMemberCardExpire'], ['relate_id', 'in', $card_ids]]);
|
||||
|
||||
// 关闭核销码
|
||||
$verify_ids = model('member_goods_card_item')->getColumn([['card_id', 'in', $card_ids]], 'member_verify_id');
|
||||
model('member_verify')->update(['state' => -1], [['id', 'in', $verify_ids]]);
|
||||
(new Cron())->deleteCron([['event', 'in', ['CronMemberVerifyClose', 'CronVerifyClosePreRemind']], ['relate_id', '=', $verify_ids]]);
|
||||
|
||||
model('member_goods_card')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('member_goods_card')->rollback();
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 卡包失效
|
||||
* @param $condition
|
||||
* @param $invalid_type
|
||||
* @return array
|
||||
*/
|
||||
public function memberOncecardInvalid($condition, $invalid_type)
|
||||
{
|
||||
$config = [
|
||||
'expired' => ['reason' => '已过期', 'verify_status' => Verify::STATUS_EXPIRED],
|
||||
'refunded' => ['reason' => '已退款', 'verify_status' => Verify::STATUS_REFUNDED],
|
||||
];
|
||||
if (!in_array($invalid_type, ['expired', 'refunded'])) return $this->error(null, '非法的失效类型');
|
||||
$member_card_list = model('member_goods_card')->getList($condition);
|
||||
if (!empty($member_card_list)) {
|
||||
foreach ($member_card_list as $member_card_info) {
|
||||
model('member_goods_card')->update([
|
||||
'status' => self::STATUS_INVALID,
|
||||
'invalid_reason' => $config[$invalid_type]['reason'],
|
||||
], [['card_id', '=', $member_card_info['card_id']]]);
|
||||
$invalid_verify_ids = model('member_goods_card_item')->getColumn([
|
||||
['card_id', '=', $member_card_info['card_id']],
|
||||
['num', '<>', Db::raw('use_num')],
|
||||
], 'member_verify_id');
|
||||
if (!empty($invalid_verify_ids)) {
|
||||
$verify_model = new Verify();
|
||||
$verify_model->editVerify(['is_verify' => $config[$invalid_type]['verify_status']], [['id', 'in', $invalid_verify_ids]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 卡项使用退还
|
||||
* @param $item_list [ ['type' => '', 'relation_id' => ''] ]
|
||||
*/
|
||||
public function memberOncecardItemRefund($item_list)
|
||||
{
|
||||
model('member_goods_card_item')->startTrans();
|
||||
try {
|
||||
$records_ids = [];
|
||||
foreach ($item_list as $item) {
|
||||
$item_info = model('member_goods_card_records')->getInfo([
|
||||
['mgcr.type', '=', $item['type']],
|
||||
['mgcr.relation_id', '=', $item['relation_id']],
|
||||
], 'mgcr.id,mgcr.card_id,mgcr.num,mgci.item_id,mgci.card_type,mgci.member_verify_id,mgc.status,mgc.end_time', 'mgcr', [
|
||||
['member_goods_card_item mgci', 'mgci.item_id = mgcr.card_item_id', 'inner'],
|
||||
['member_goods_card mgc', 'mgci.card_id = mgc.card_id', 'inner']
|
||||
]);
|
||||
if (!empty($item_info)) {
|
||||
if ($item_info['card_type'] == 'commoncard') {
|
||||
model('member_goods_card_item')->setDec([['card_id', '=', $item_info['card_id']]], 'use_num', $item_info['num']);
|
||||
$verify_ids = model('member_goods_card_item')->getColumn([['card_id', '=', $item_info['card_id']]], 'member_verify_id');
|
||||
if (!empty($verify_ids)) {
|
||||
model('verify')->setDec([['id', 'in', $verify_ids]], 'verify_use_num', $item_info['num']);
|
||||
if ($item_info['status'] == 0 && ($item_info['end_time'] == 0 || $item_info['end_time'] > time())) {
|
||||
model('verify')->update(['is_verify' => Verify::STATUS_NOT_VERIFY, 'verify_time' => 0], [['id', 'in', $verify_ids]]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
model('member_goods_card_item')->setDec([['item_id', '=', $item_info['item_id']]], 'use_num', $item_info['num']);
|
||||
model('verify')->setDec([['id', '=', $item_info['member_verify_id']]], 'verify_use_num', $item_info['num']);
|
||||
// 恢复核销码状态
|
||||
if ($item_info['status'] == 0 && ($item_info['end_time'] == 0 || $item_info['end_time'] > time())) {
|
||||
model('verify')->update(['is_verify' => Verify::STATUS_NOT_VERIFY, 'verify_time' => 0], [['id', '=', $item_info['member_verify_id']]]);
|
||||
}
|
||||
}
|
||||
model('member_goods_card')->setDec([['card_id', '=', $item_info['card_id']]], 'total_use_num', $item_info['num']);
|
||||
$records_ids[] = $item_info['id'];
|
||||
|
||||
// 判断卡是否为不可用状态
|
||||
if ($item_info['status'] == 0 && ($item_info['end_time'] == 0 || $item_info['end_time'] > time())) {
|
||||
model('member_goods_card')->update([
|
||||
'status' => 1
|
||||
], [['card_id', '=', $item_info['card_id']]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 删除使用记录
|
||||
if (!empty($records_ids)) model('member_goods_card_records')->delete([['id', 'in', $records_ids]]);
|
||||
|
||||
model('member_goods_card_item')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('member_goods_card_item')->rollback();
|
||||
Log::write('卡项退还错误,错误原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
|
||||
return $this->error('', '卡项退还失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询卡项活动信息
|
||||
* @param $condition
|
||||
*/
|
||||
public function getCardSelect($condition)
|
||||
{
|
||||
$info = model('goods_card')->getInfo($condition);
|
||||
return $this->success($info);
|
||||
}
|
||||
}
|
||||
653
addon/cardservice/model/Reserve.php
Executable file
653
addon/cardservice/model/Reserve.php
Executable file
@@ -0,0 +1,653 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\cardservice\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\member\Member;
|
||||
|
||||
/**
|
||||
* 预约
|
||||
*/
|
||||
class Reserve extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 预约状态
|
||||
* @var array
|
||||
*/
|
||||
public $reserve_state = [
|
||||
'wait_confirm' => [
|
||||
'state' => 'wait_confirm',
|
||||
'name' => '待确认',
|
||||
'color' => '#8558FA'
|
||||
],
|
||||
'wait_to_store' => [
|
||||
'state' => 'wait_to_store',
|
||||
'name' => '待到店',
|
||||
'color' => '#1475FA'
|
||||
],
|
||||
'arrived_store' => [
|
||||
'state' => 'arrived_store',
|
||||
'name' => '已到店',
|
||||
'color' => '#FA5B14'
|
||||
],
|
||||
'completed' => [
|
||||
'state' => 'completed',
|
||||
'name' => '已完成',
|
||||
'color' => '#10C610'
|
||||
],
|
||||
'cancelled' => [
|
||||
'state' => 'cancelled',
|
||||
'name' => '已取消',
|
||||
'color' => '#CCCCCC'
|
||||
]
|
||||
];
|
||||
|
||||
/**
|
||||
* 设置预约配置
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
* @param $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setReserveConfig($data, $site_id, $store_id)
|
||||
{
|
||||
$save_data[ 'config_desc' ] = '预约配置';
|
||||
$save_data[ 'is_use' ] = 1;
|
||||
$save_data[ 'value' ] = json_encode($data);
|
||||
|
||||
$config_model = model('reserve_config');
|
||||
$condition = [
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'store_id', '=', $store_id ],
|
||||
[ 'config_key', '=', 'RESERVE_CONFIG' ]
|
||||
];
|
||||
$info = $config_model->getInfo($condition, 'id');
|
||||
if (empty($info)) {
|
||||
$save_data[ 'create_time' ] = time();
|
||||
$save_data[ 'site_id' ] = $site_id;
|
||||
$save_data[ 'store_id' ] = $store_id;
|
||||
$save_data[ 'config_key' ] = 'RESERVE_CONFIG';
|
||||
$res = $config_model->add($save_data);
|
||||
} else {
|
||||
$save_data[ 'modify_time' ] = time();
|
||||
$res = $config_model->update($save_data, $condition);
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预约配置
|
||||
* @param $site_id
|
||||
* @param $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getReserveConfig($site_id, $store_id)
|
||||
{
|
||||
$key = 'RESERVE_CONFIG';
|
||||
$info = model('reserve_config')->getInfo([ [ 'site_id', '=', $site_id ], [ 'store_id', '=', $store_id ], [ 'config_key', '=', $key ] ], 'site_id, store_id, config_key, value, config_desc, is_use, create_time, modify_time');
|
||||
if (!empty($info)) {
|
||||
$info[ 'value' ] = json_decode($info[ 'value' ], true);
|
||||
} else {
|
||||
$info = [
|
||||
'site_id' => $site_id,
|
||||
'store_id' => $store_id,
|
||||
'config_key' => $key,
|
||||
'value' => [
|
||||
'week' => '[1,2,3,4,5]',
|
||||
'start' => 32400,
|
||||
'end' => 79200,
|
||||
'interval' => 30,
|
||||
'advance' => 1,
|
||||
'max' => 10
|
||||
],
|
||||
'config_desc' => '',
|
||||
'is_use' => 0,
|
||||
'create_time' => 0,
|
||||
'modify_time' => 0
|
||||
];
|
||||
}
|
||||
$info[ 'value' ][ 'week' ] = json_decode($info[ 'value' ][ 'week' ], true);
|
||||
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加预约
|
||||
* @param array $param
|
||||
* @return array
|
||||
*/
|
||||
public function addReserve(array $param)
|
||||
{
|
||||
|
||||
$member_info = ( new Member() )->getMemberInfo([ [ 'member_id', '=', $param[ 'member_id' ] ], [ 'site_id', '=', $param[ 'site_id' ] ] ])[ 'data' ];
|
||||
if (empty($member_info)) return $this->error('', '未查找到会员信息');
|
||||
|
||||
$store_info = model('store')->getInfo([ [ 'store_id', '=', $param[ 'store_id' ] ], [ 'site_id', '=', $param[ 'site_id' ] ] ], 'status');
|
||||
if (empty($store_info)) return $this->error('', '未查找到门店信息');
|
||||
|
||||
$check_res = $this->checkReserve($param);
|
||||
if ($check_res[ 'code' ] != 0) return $check_res;
|
||||
model('reserve')->startTrans();
|
||||
try {
|
||||
$data = [
|
||||
'site_id' => $param[ 'site_id' ],
|
||||
'member_id' => $param[ 'member_id' ],
|
||||
'reserve_name' => $member_info[ 'nickname' ],
|
||||
'reserve_time' => strtotime("{$param['date']} {$param['time']}"),
|
||||
'reserve_state' => $this->reserve_state[ 'wait_confirm' ][ 'state' ],
|
||||
'reserve_state_name' => $this->reserve_state[ 'wait_confirm' ][ 'name' ],
|
||||
'remark' => $param[ 'remark' ],
|
||||
'store_id' => $param[ 'store_id' ],
|
||||
'source' => $param[ 'source' ] ?? 'member',
|
||||
'create_time' => time()
|
||||
];
|
||||
$reserve_id = model('reserve')->add($data);
|
||||
|
||||
$reserve_item = [];
|
||||
$reserve_item_data = [];
|
||||
foreach ($param[ 'goods' ] as $item) {
|
||||
$goods_info = model('goods')->getInfo([ [ 'site_id', '=', $param[ 'site_id' ] ], [ 'sku_id', '=', $item[ 'sku_id' ] ], [ 'goods_state', '=', 1 ], [ 'is_delete', '=', 0 ] ], 'goods_name');
|
||||
|
||||
if (empty($goods_info)) {
|
||||
model('reserve')->rollback();
|
||||
return $this->error('', '未查找到所预约的服务');
|
||||
}
|
||||
|
||||
// 同一时间,一个服务人员只能预约一个客户
|
||||
if (!empty($item[ 'uid' ])) {
|
||||
$count = model('reserve_item')->getCount([
|
||||
[ 'site_id', '=', $param[ 'site_id' ] ],
|
||||
[ 'member_id', '=', $param[ 'member_id' ] ],
|
||||
[ 'reserve_user_id', '=', $item[ 'uid' ] ],
|
||||
[ 'reserve_goods_sku_id', '=', $item[ 'sku_id' ] ],
|
||||
[ 'store_id', '=', $param[ 'store_id' ] ],
|
||||
[ 'reserve_time', '=', strtotime("{$param['date']} {$param['time']}") ]
|
||||
]);
|
||||
if ($count) {
|
||||
model('reserve')->rollback();
|
||||
return $this->error('', '请勿重复预约服务');
|
||||
}
|
||||
$count = model('reserve_item')->getCount([
|
||||
[ 'site_id', '=', $param[ 'site_id' ] ],
|
||||
[ 'reserve_user_id', '=', $item[ 'uid' ] ],
|
||||
[ 'store_id', '=', $param[ 'store_id' ] ],
|
||||
[ 'reserve_time', '=', strtotime("{$param['date']} {$param['time']}") ]
|
||||
]);
|
||||
if ($count) {
|
||||
model('reserve')->rollback();
|
||||
return $this->error('', '同一时间,一个服务人员只能预约一个客户');
|
||||
}
|
||||
}
|
||||
|
||||
$reserve_item[] = $goods_info['goods_name'];
|
||||
$reserve_item_data[] = [
|
||||
'reserve_id' => $reserve_id,
|
||||
'site_id' => $param['site_id'],
|
||||
'member_id' => $param['member_id'],
|
||||
'reserve_name' => $member_info['nickname'],
|
||||
'reserve_time' => strtotime("{$param['date']} {$param['time']}"),
|
||||
'remark' => $param['remark'],
|
||||
'reserve_user_id' => $item['uid'] ?? 0,
|
||||
'reserve_goods_sku_id' => $item['sku_id'],
|
||||
'reserve_state' => $this->reserve_state['wait_confirm']['state'],
|
||||
'store_id' => $param['store_id'],
|
||||
];
|
||||
}
|
||||
model('reserve_item')->addList($reserve_item_data);
|
||||
model('reserve')->update([ 'reserve_item' => implode($reserve_item) ], [ [ 'reserve_id', '=', $reserve_id ] ]);
|
||||
|
||||
model('reserve')->commit();
|
||||
return $this->success($reserve_id);
|
||||
} catch (\Exception $e) {
|
||||
model('reserve')->rollback();
|
||||
return $this->error('添加失败' . $e->getMessage() . $e->getFile() . $e->getLine());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑预约
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
public function editReserve($param)
|
||||
{
|
||||
$condition = [
|
||||
[ 'reserve_id', '=', $param[ 'reserve_id' ] ],
|
||||
[ 'site_id', '=', $param[ 'site_id' ] ]
|
||||
];
|
||||
if (isset($param[ 'member_id' ])) $condition[] = [ 'member_id', '=', $param[ 'member_id' ] ];
|
||||
$info = model('reserve')->getInfo($condition);
|
||||
|
||||
if (empty($info)) return $this->error('', '未获取到预约信息');
|
||||
if (!in_array($info[ 'reserve_state' ], [ $this->reserve_state[ 'wait_confirm' ][ 'state' ], $this->reserve_state[ 'wait_to_store' ][ 'state' ] ]))
|
||||
return $this->error('', '该预约已不可更改');
|
||||
|
||||
$check_res = $this->checkReserve($param);
|
||||
if ($check_res[ 'code' ] != 0) return $check_res;
|
||||
|
||||
model('reserve')->startTrans();
|
||||
try {
|
||||
$res = model('reserve')->update([
|
||||
'reserve_time' => strtotime("{$param['date']} {$param['time']}"),
|
||||
'remark' => $param[ 'remark' ],
|
||||
], $condition);
|
||||
|
||||
// 删除原预约项
|
||||
|
||||
$reserve_item = [];
|
||||
$reserve_item_data = [];
|
||||
foreach ($param[ 'goods' ] as $item) {
|
||||
$goods_info = model('goods')->getInfo([ [ 'site_id', '=', $param[ 'site_id' ] ], [ 'sku_id', '=', $item[ 'sku_id' ] ], [ 'goods_state', '=', 1 ], [ 'is_delete', '=', 0 ] ], 'goods_name');
|
||||
if (empty($goods_info)) {
|
||||
model('reserve')->rollback();
|
||||
return $this->error('', '未查找到所预约的服务');
|
||||
}
|
||||
|
||||
// 同一时间,一个服务人员只能预约一个客户
|
||||
if (!empty($item[ 'uid' ])) {
|
||||
$count = model('reserve_item')->getCount([
|
||||
[ 'site_id', '=', $param[ 'site_id' ] ],
|
||||
[ 'member_id', '=', $info[ 'member_id' ] ],
|
||||
[ 'reserve_user_id', '=', $item[ 'uid' ] ],
|
||||
[ 'reserve_goods_sku_id', '=', $item[ 'sku_id' ] ],
|
||||
[ 'store_id', '=', $info[ 'store_id' ] ],
|
||||
[ 'reserve_time', '=', strtotime("{$param['date']} {$param['time']}") ]
|
||||
]);
|
||||
if ($count) {
|
||||
model('reserve')->rollback();
|
||||
return $this->error('', '请勿重复预约服务');
|
||||
}
|
||||
$count = model('reserve_item')->getCount([
|
||||
[ 'site_id', '=', $param[ 'site_id' ] ],
|
||||
[ 'reserve_user_id', '=', $item[ 'uid' ] ],
|
||||
[ 'store_id', '=', $info[ 'store_id' ] ],
|
||||
[ 'reserve_time', '=', strtotime("{$param['date']} {$param['time']}") ]
|
||||
]);
|
||||
if ($count) {
|
||||
model('reserve')->rollback();
|
||||
return $this->error('', '同一时间,一个服务人员只能预约一个客户');
|
||||
}
|
||||
}
|
||||
$reserve_item[] = $goods_info['goods_name'];
|
||||
$reserve_item_data[] = [
|
||||
'reserve_id' => $param['reserve_id'],
|
||||
'site_id' => $param['site_id'],
|
||||
'member_id' => $info['member_id'],
|
||||
'reserve_name' => $info['reserve_name'],
|
||||
'reserve_time' => strtotime("{$param['date']} {$param['time']}"),
|
||||
'remark' => $param['remark'],
|
||||
'reserve_user_id' => $item['uid'] ?? 0,
|
||||
'reserve_goods_sku_id' => $item['sku_id'],
|
||||
'reserve_state' => $info['reserve_state'],
|
||||
'store_id' => $info['store_id']
|
||||
];
|
||||
}
|
||||
|
||||
model('reserve_item')->delete([ [ 'reserve_id', '=', $param[ 'reserve_id' ] ] ]);
|
||||
model('reserve_item')->addList($reserve_item_data);
|
||||
model('reserve')->update([ 'reserve_item' => implode($reserve_item) ], [ [ 'reserve_id', '=', $param[ 'reserve_id' ] ] ]);
|
||||
|
||||
model('reserve')->commit();
|
||||
return $this->success($res);
|
||||
} catch (\Exception $e) {
|
||||
model('reserve')->rollback();
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验预约是否可添加
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
public function checkReserve($param)
|
||||
{
|
||||
$site_id = $param[ 'site_id' ] ?? 0;
|
||||
$date = $param[ 'date' ] ?? 0;
|
||||
$time = $param[ 'time' ] ?? 0;
|
||||
$store_id = $param[ 'store_id' ] ?? 0;
|
||||
$reserve_id = $param[ 'reserve_id' ] ?? 0;
|
||||
$app_module = $param[ 'app_module' ] ?? '';
|
||||
$config = $this->getReserveConfig($site_id, $store_id)[ 'data' ][ 'value' ];
|
||||
// 预约时间
|
||||
$reserve_time = strtotime($date . ' ' . $time);
|
||||
if (( $reserve_time - time() ) < ( $config[ 'advance' ] * 3600 )) return $this->error('', '需提前' . $config[ 'advance' ] . '小时预约');
|
||||
|
||||
$week = date('w', strtotime($date));
|
||||
|
||||
if (!in_array($week, $config[ 'week' ])) return $this->error('', '所选时间不在可预约时间内');
|
||||
|
||||
$time = strtotime(date('Y-m-d') . ' ' . $time) - strtotime(date('Y-m-d'));
|
||||
if ($time < $config[ 'start' ] || $time > $config[ 'end' ]) return $this->error('', '所选时间不在可预约时间内');
|
||||
|
||||
$max_condition = [
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'reserve_time', '=', $reserve_time ],
|
||||
[ 'reserve_state', '<>', $this->reserve_state[ 'cancelled' ][ 'state' ] ]
|
||||
];
|
||||
if ($reserve_id > 0) $max_condition[] = [ 'reserve_id', '<>', $reserve_id ];
|
||||
$max = model('reserve')->getCount($max_condition);
|
||||
if ($max > $config[ 'max' ]) return $this->error('', '所选时段内预约人数已达上限');
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预约列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getReserveList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('reserve')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预约数量
|
||||
* @param array $condition
|
||||
* @return array
|
||||
*/
|
||||
public function getReserveCount($condition = [], $field = '*')
|
||||
{
|
||||
$list = model('reserve')->getCount($condition, $field);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预约分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getReservePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$join = [
|
||||
[
|
||||
'member nm',
|
||||
'noy.member_id = nm.member_id',
|
||||
'left'
|
||||
],
|
||||
[
|
||||
'store os',
|
||||
'noy.store_id = os.store_id',
|
||||
'left'
|
||||
]
|
||||
];
|
||||
$list = model('reserve')->pageList($condition, $field, $order, $page, $page_size, 'noy', $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预约项列表
|
||||
* @param $condition
|
||||
* @param $field
|
||||
* @param $order
|
||||
* @param string $alias
|
||||
* @param null $join
|
||||
* @return array
|
||||
*/
|
||||
public function getReserveItemList($condition, $field, $order, $alias = 'oyi', $join = null)
|
||||
{
|
||||
$list = model('reserve_item')->getList($condition, $field, $order, $alias, $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预约详情
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @param string $alias
|
||||
* @param null $join
|
||||
* @return array
|
||||
*/
|
||||
public function getReserveInfo($condition, $field = '*', $alias = 'a', $join = null)
|
||||
{
|
||||
$res = model('reserve')->getInfo($condition, $field, $alias, $join);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认预约
|
||||
* @param $reserve_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function confirmReserve($reserve_id, $site_id)
|
||||
{
|
||||
$condition = [
|
||||
[ 'reserve_id', '=', $reserve_id ],
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'reserve_state', '=', $this->reserve_state[ 'wait_confirm' ][ 'state' ] ]
|
||||
];
|
||||
model('reserve')->update([
|
||||
'reserve_state' => $this->reserve_state[ 'wait_to_store' ][ 'state' ],
|
||||
'reserve_state_name' => $this->reserve_state[ 'wait_to_store' ][ 'name' ]
|
||||
], $condition);
|
||||
model('reserve_item')->update([ 'reserve_state' => $this->reserve_state[ 'wait_to_store' ][ 'state' ] ], $condition);
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消预约
|
||||
* @param $reserve_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function cancelReserve($reserve_id, $site_id, $member_id = 0)
|
||||
{
|
||||
model('reserve')->startTrans();
|
||||
try {
|
||||
$condition = [
|
||||
[ 'reserve_id', '=', $reserve_id ],
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'reserve_state', 'in', [ $this->reserve_state[ 'wait_confirm' ][ 'state' ], $this->reserve_state[ 'wait_to_store' ][ 'state' ] ] ]
|
||||
];
|
||||
if ($member_id) $condition[] = [ 'member_id', '=', $member_id ];
|
||||
model('reserve')->update([
|
||||
'reserve_state' => $this->reserve_state[ 'cancelled' ][ 'state' ],
|
||||
'reserve_state_name' => $this->reserve_state[ 'cancelled' ][ 'name' ],
|
||||
'cancel_time' => time()
|
||||
], $condition);
|
||||
model('reserve_item')->update([ 'reserve_state' => $this->reserve_state[ 'cancelled' ][ 'state' ] ], $condition);
|
||||
|
||||
model('reserve')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('reserve')->rollback();
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除预约
|
||||
* @param $reserve_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function deleteReserve($reserve_id, $site_id, $member_id = 0)
|
||||
{
|
||||
$condition = [
|
||||
[ 'reserve_id', '=', $reserve_id ],
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'reserve_state', '=', $this->reserve_state[ 'cancelled' ][ 'state' ] ]
|
||||
];
|
||||
if ($member_id) $condition[] = [ 'member_id', '=', $member_id ];
|
||||
model('reserve')->delete($condition);
|
||||
model('reserve_item')->delete($condition);
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认到店
|
||||
* @param $reserve_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function confirmToStore($reserve_id, $site_id)
|
||||
{
|
||||
$condition = [
|
||||
[ 'reserve_id', '=', $reserve_id ],
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'reserve_state', '=', $this->reserve_state[ 'wait_to_store' ][ 'state' ] ]
|
||||
];
|
||||
model('reserve')->update([
|
||||
'reserve_state' => $this->reserve_state[ 'arrived_store' ][ 'state' ],
|
||||
'reserve_state_name' => $this->reserve_state[ 'arrived_store' ][ 'name' ],
|
||||
'to_store_time' => time()
|
||||
], $condition);
|
||||
model('reserve_item')->update([ 'reserve_state' => $this->reserve_state[ 'arrived_store' ][ 'state' ] ], $condition);
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认完成
|
||||
* @param $reserve_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function confirmComplete($reserve_id, $site_id)
|
||||
{
|
||||
$condition = [
|
||||
[ 'reserve_id', '=', $reserve_id ],
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'reserve_state', '=', $this->reserve_state[ 'arrived_store' ][ 'state' ] ]
|
||||
];
|
||||
model('reserve')->update([
|
||||
'reserve_state' => $this->reserve_state[ 'completed' ][ 'state' ],
|
||||
'reserve_state_name' => $this->reserve_state[ 'completed' ][ 'name' ],
|
||||
'complete_time' => time()
|
||||
], $condition);
|
||||
model('reserve_item')->update([ 'reserve_state' => $this->reserve_state[ 'completed' ][ 'state' ] ], $condition);
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取周看板日期数据
|
||||
* @param $week_offset
|
||||
* @return array
|
||||
*/
|
||||
public function getWeekDays($week_offset)
|
||||
{
|
||||
$first_day = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
|
||||
$first_day = strtotime($week_offset . ' week', $first_day);
|
||||
|
||||
$week_names = [ '周日', '周一', '周二', '周三', '周四', '周五', '周六' ];
|
||||
$data = [];
|
||||
for ($i = 0; $i < 7; $i++) {
|
||||
$time = strtotime("+ {$i} day", $first_day);
|
||||
$data[] = [
|
||||
'start_time' => $time,
|
||||
'end_time' => strtotime(date('Y-m-d 23:59:59', $time)),
|
||||
'year' => date('Y', $time),
|
||||
'month' => date('m', $time),
|
||||
'day' => date('d', $time),
|
||||
'week' => date('w', $time),
|
||||
'week_name' => $week_names[ date('w', $time) ],
|
||||
'is_curr_day' => date('Y-m-d', $time) == date('Y-m-d') ? 1 : 0
|
||||
];
|
||||
}
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取月看板日期数据
|
||||
* @param $year
|
||||
* @param $month
|
||||
* @return array
|
||||
*/
|
||||
public function getMonthDays($year, $month)
|
||||
{
|
||||
$month_start_day = mktime(0, 0, 0, $month, 1, $year);
|
||||
//获取日历的第一天
|
||||
$first_day = mktime(0, 0, 0, $month, 1 - ( date("w", $month_start_day) - 1 ), $year);
|
||||
//获取日历的最后一天
|
||||
if ($month < 12) {
|
||||
$next_month = $month + 1;
|
||||
$next_month_year = $year;
|
||||
} else {
|
||||
$next_month = 1;
|
||||
$next_month_year = $year + 1;
|
||||
}
|
||||
$month_end_day = mktime(0, 0, 0, $next_month, 0, $next_month_year);
|
||||
$end_day = $month_end_day + ( 7 - date('w', $month_end_day) ) * 3600 * 24;
|
||||
|
||||
$data = [];
|
||||
for ($timestamp = $first_day; $timestamp <= $end_day; $timestamp += 3600 * 24) {
|
||||
$data_item = [
|
||||
'start_time' => $timestamp,
|
||||
'end_time' => $timestamp + 3600 * 24 - 1,
|
||||
'year' => date('Y', $timestamp),
|
||||
'month' => date('m', $timestamp),
|
||||
'day' => date('d', $timestamp),
|
||||
'week' => date('w', $timestamp),
|
||||
];
|
||||
$data_item[ 'is_curr_month' ] = ( $month == $data_item[ 'month' ] );
|
||||
$data[] = $data_item;
|
||||
}
|
||||
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过给定时间获取预约数据
|
||||
* @param $param
|
||||
*/
|
||||
public function getReserveDataByDays($param)
|
||||
{
|
||||
$days_data = $param[ 'days_data' ] ?? [];
|
||||
$query_num = $param[ 'query_num' ] ?? 10;
|
||||
$site_id = $param[ 'site_id' ] ?? 0;
|
||||
|
||||
$reserve_ids = [];
|
||||
foreach ($days_data as $key => $val) {
|
||||
$field = 'noy.reserve_id,noy.reserve_state,noy.reserve_time,nm.nickname';
|
||||
$list_data = $this->getReservePageList([
|
||||
[ 'noy.site_id', '=', $site_id ],
|
||||
[ 'noy.reserve_time', 'between', [ $val[ 'start_time' ], $val[ 'end_time' ] ] ]
|
||||
], 1, $query_num, 'noy.create_time desc', $field)[ 'data' ];
|
||||
$reserve_ids = array_merge($reserve_ids, array_column($list_data[ 'list' ], 'reserve_id'));
|
||||
$days_data[ $key ][ 'data' ] = $list_data;
|
||||
}
|
||||
|
||||
//查询所有预约项 并按照预约分配数据
|
||||
$reserve_item_list = $this->getReserveItemList([
|
||||
[ 'oyi.reserve_id', 'in', $reserve_ids ],
|
||||
], 'oyi.reserve_id,g.goods_name,g.goods_id,g.sku_id', 'reserve_item_id desc', 'oyi',
|
||||
[ [ 'goods g', 'g.sku_id = oyi.reserve_goods_sku_id', 'right' ] ])[ 'data' ];
|
||||
$reserve_data = [];
|
||||
foreach ($reserve_item_list as $val) {
|
||||
if (!isset($reserve_data[ $val[ 'reserve_id' ] ])) {
|
||||
$reserve_data[ $val[ 'reserve_id' ] ] = [];
|
||||
}
|
||||
$reserve_data[ $val[ 'reserve_id' ] ][] = $val;
|
||||
}
|
||||
|
||||
//预约项关联预约
|
||||
foreach ($days_data as $key => $val) {
|
||||
foreach ($val[ 'data' ][ 'list' ] as $k => $item) {
|
||||
$days_data[ $key ][ 'data' ][ 'list' ][ $k ][ 'item' ] = $reserve_data[ $item[ 'reserve_id' ] ] ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success($days_data);
|
||||
}
|
||||
}
|
||||
855
addon/cardservice/model/ServiceGoods.php
Executable file
855
addon/cardservice/model/ServiceGoods.php
Executable file
@@ -0,0 +1,855 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\cardservice\model;
|
||||
|
||||
use addon\discount\model\Discount;
|
||||
use app\model\BaseModel;
|
||||
use app\model\order\OrderCommon;
|
||||
use app\model\storegoods\StoreGoods;
|
||||
use app\model\system\Cron;
|
||||
use app\model\system\Stat;
|
||||
use app\model\goods\GoodsCommon;
|
||||
|
||||
/**
|
||||
* 服务项目商品
|
||||
*/
|
||||
class ServiceGoods extends GoodsCommon
|
||||
{
|
||||
private $goods_class = array ( 'id' => 4, 'name' => '服务项目' );
|
||||
|
||||
private $goods_state = array (
|
||||
1 => '销售中',
|
||||
0 => '仓库中'
|
||||
);
|
||||
|
||||
public function getGoodsState()
|
||||
{
|
||||
return $this->goods_state;
|
||||
}
|
||||
|
||||
public function getGoodsClass()
|
||||
{
|
||||
return $this->goods_class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品添加
|
||||
* @param $data
|
||||
*/
|
||||
public function addGoods($data)
|
||||
{
|
||||
model('goods')->startTrans();
|
||||
|
||||
try {
|
||||
if (!empty($data[ 'goods_attr_format' ])) {
|
||||
|
||||
$goods_attr_format = json_decode($data[ 'goods_attr_format' ], true);
|
||||
$keys = array_column($goods_attr_format, 'sort');
|
||||
if (!empty($keys)) {
|
||||
array_multisort($keys, SORT_ASC, SORT_NUMERIC, $goods_attr_format);
|
||||
$data[ 'goods_attr_format' ] = json_encode($goods_attr_format);
|
||||
}
|
||||
}
|
||||
|
||||
$goods_image = $data[ 'goods_image' ];
|
||||
$first_image = explode(",", $goods_image)[ 0 ];
|
||||
|
||||
//SKU商品数据
|
||||
if (!empty($data[ 'goods_sku_data' ])) {
|
||||
$data[ 'goods_sku_data' ] = json_decode($data[ 'goods_sku_data' ], true);
|
||||
}
|
||||
|
||||
//商品编码检测
|
||||
$sku_no_check = $this->checkSkuNoRepeat(['sku_list' => $data['goods_sku_data'], 'site_id' => $data['site_id'], 'goods_id' => $data['goods_id'] ?? 0]);
|
||||
if($sku_no_check['code'] < 0){
|
||||
model('goods')->rollback();
|
||||
return $sku_no_check;
|
||||
}
|
||||
|
||||
//检测商品核销日期
|
||||
$virtualDate_check = $this->checkVirtualDate($data[ 'verify_validity_type' ],$data[ 'virtual_indate' ]??'');
|
||||
if($virtualDate_check['code'] < 0){
|
||||
model('goods')->rollback();
|
||||
return $virtualDate_check;
|
||||
}
|
||||
|
||||
$goods_data = array (
|
||||
'goods_image' => $goods_image,
|
||||
'price' => $data[ 'goods_sku_data' ][ 0 ][ 'price' ],
|
||||
'market_price' => $data[ 'goods_sku_data' ][ 0 ][ 'market_price' ],
|
||||
'cost_price' => $data[ 'goods_sku_data' ][ 0 ][ 'cost_price' ],
|
||||
'goods_spec_format' => $data[ 'goods_spec_format' ],
|
||||
'category_id' => '',
|
||||
'category_json' => '',
|
||||
'label_id' => $data[ 'label_id' ],
|
||||
'label_name' => '',
|
||||
'timer_on' => $data[ 'timer_on' ],
|
||||
'timer_off' => $data[ 'timer_off' ],
|
||||
'sale_show' => $data[ 'sale_show' ] ?? 1,
|
||||
'stock_show' => $data[ 'stock_show' ] ?? 1,
|
||||
'market_price_show' => $data[ 'market_price_show' ] ?? 1,
|
||||
'barrage_show' => $data[ 'barrage_show' ] ?? 1,
|
||||
'is_consume_discount' => $data[ 'is_consume_discount' ],
|
||||
'service_category' => $data[ 'category_id' ],
|
||||
'is_need_verify' => 1,
|
||||
'is_reserve' => $data['is_reserve'],
|
||||
'service_mode' => $data['service_mode'],
|
||||
'service_price_way' => $data['service_price_way']
|
||||
);
|
||||
|
||||
$common_data = array (
|
||||
'goods_name' => $data[ 'goods_name' ],
|
||||
'goods_class' => $this->goods_class[ 'id' ],
|
||||
'goods_class_name' => $this->goods_class[ 'name' ],
|
||||
'goods_attr_class' => $data[ 'goods_attr_class' ],
|
||||
'goods_attr_name' => $data[ 'goods_attr_name' ],
|
||||
'is_limit' => $data[ 'is_limit' ] ?? 0,
|
||||
'limit_type' => $data[ 'limit_type' ] ?? 1,
|
||||
'site_id' => $data[ 'site_id' ],
|
||||
'goods_content' => $data[ 'goods_content' ],
|
||||
'goods_state' => $data[ 'goods_state' ],
|
||||
'goods_stock_alarm' => $data[ 'goods_stock_alarm' ],
|
||||
'is_virtual' => 1,
|
||||
'verify_validity_type' => $data[ 'verify_validity_type' ],
|
||||
'virtual_indate' => $data[ 'virtual_indate' ] ?? 0,
|
||||
'goods_attr_format' => $data[ 'goods_attr_format' ],
|
||||
'introduction' => $data[ 'introduction' ],
|
||||
'keywords' => $data[ 'keywords' ],
|
||||
'unit' => $data[ 'unit' ],
|
||||
'video_url' => $data[ 'video_url' ],
|
||||
'sort' => $data[ 'sort' ],
|
||||
'goods_service_ids' => $data[ 'goods_service_ids' ],
|
||||
'create_time' => time(),
|
||||
'virtual_sale' => $data[ 'virtual_sale' ],
|
||||
'max_buy' => $data[ 'max_buy' ],
|
||||
'min_buy' => $data[ 'min_buy' ],
|
||||
'recommend_way' => $data[ 'recommend_way' ],
|
||||
'qr_id' => $data[ 'qr_id' ] ?? 0,
|
||||
'template_id' => $data[ 'template_id' ] ?? 0,
|
||||
'form_id' => $data[ 'form_id' ] ?? 0,
|
||||
'sale_channel' => $data[ 'sale_channel' ] ?? 'all',
|
||||
'sale_store' => $data[ 'sale_store' ] ?? 'all',
|
||||
'is_unify_price' => $data[ 'is_unify_price' ] ?? 1,
|
||||
);
|
||||
|
||||
$goods_id = model('goods')->add(array_merge($goods_data, $common_data));
|
||||
|
||||
$goods_stock = 0;
|
||||
|
||||
$sku_arr = array ();
|
||||
$sku_stock_list = [];
|
||||
//添加sku商品
|
||||
foreach ($data[ 'goods_sku_data' ] as $item) {
|
||||
|
||||
$goods_stock += $item[ 'stock' ];
|
||||
|
||||
$sku_data = array (
|
||||
'sku_name' => $data[ 'goods_name' ] . ' ' . $item[ 'spec_name' ],
|
||||
'spec_name' => $item[ 'spec_name' ],
|
||||
'sku_no' => $item[ 'sku_no' ],
|
||||
'sku_spec_format' => !empty($item[ 'sku_spec_format' ]) ? json_encode($item[ 'sku_spec_format' ]) : "",
|
||||
'price' => $item[ 'price' ],
|
||||
'market_price' => $item[ 'market_price' ],
|
||||
'cost_price' => $item[ 'cost_price' ],
|
||||
'discount_price' => $item[ 'price' ],//sku折扣价(默认等于单价)
|
||||
'stock' => $item[ 'stock' ],
|
||||
'stock_alarm' => $item[ 'stock_alarm' ],
|
||||
'sku_image' => !empty($item[ 'sku_image' ]) ? $item[ 'sku_image' ] : $first_image,
|
||||
'sku_images' => $item[ 'sku_images' ],
|
||||
'goods_id' => $goods_id,
|
||||
'is_default' => $item[ 'is_default' ] ?? 0,
|
||||
'is_consume_discount' => $data[ 'is_consume_discount' ],
|
||||
'site_id' => $data[ 'site_id' ],
|
||||
'service_length' => $item[ 'service_length' ] ?? 0
|
||||
);
|
||||
|
||||
$sku_arr[] = array_merge($sku_data, $common_data);
|
||||
$sku_stock_list[] = [ 'stock' => $item[ 'stock' ], 'site_id' => $data[ 'site_id' ], 'goods_class' => $common_data[ 'goods_class' ] ];
|
||||
}
|
||||
model('goods_sku')->addList($sku_arr);
|
||||
|
||||
// 赋值第一个商品sku_id
|
||||
$first_info = model('goods_sku')->getFirstData([ 'goods_id' => $goods_id ], 'sku_id', 'is_default desc,sku_id asc');
|
||||
model('goods')->update([ 'sku_id' => $first_info[ 'sku_id' ] ], [ [ 'goods_id', '=', $goods_id ] ]);
|
||||
|
||||
if (!empty($data[ 'goods_spec_format' ])) {
|
||||
// 刷新SKU商品规格项 / 规格值JSON字符串
|
||||
$this->dealGoodsSkuSpecFormat($goods_id, $data[ 'goods_spec_format' ]);
|
||||
}
|
||||
|
||||
$cron = new Cron();
|
||||
//定时上下架
|
||||
if ($goods_data[ 'timer_on' ] > 0) {
|
||||
$cron->addCron(1, 0, "商品定时上架", "CronGoodsTimerOn", $goods_data[ 'timer_on' ], $goods_id);
|
||||
}
|
||||
if ($goods_data[ 'timer_off' ] > 0) {
|
||||
$cron->addCron(1, 0, "商品定时下架", "CronGoodsTimerOff", $goods_data[ 'timer_off' ], $goods_id);
|
||||
}
|
||||
|
||||
|
||||
//虚拟商品指定审核有效期后自动下架
|
||||
if ($data[ 'verify_validity_type' ] == 2 && $data['goods_state'] == 1) {
|
||||
$cron->addCron(1, 0, "虚拟商品定时下架", "CronVirtualGoodsVerifyOff", $data[ 'virtual_indate' ], $goods_id);
|
||||
}else{
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronVirtualGoodsVerifyOff' ], [ 'relate_id', '=', $goods_id ] ]);
|
||||
}
|
||||
|
||||
//添加统计
|
||||
$stat = new Stat();
|
||||
$stat->switchStat([ 'type' => 'add_goods', 'data' => [ 'add_goods_count' => 1, 'site_id' => $data[ 'site_id' ] ] ]);
|
||||
|
||||
if ($common_data[ 'sale_store' ] != 'all') {
|
||||
$sale_store = explode(',', $common_data[ 'sale_store' ]);
|
||||
$sale_store = array_filter($sale_store);
|
||||
foreach ($sale_store as $k => $v) {
|
||||
|
||||
// 同步商品成本价
|
||||
( new StoreGoods() )->setSkuPrice([ 'goods_id' => $goods_id, 'site_id' => $data[ 'site_id' ], 'store_id' => $v ]);
|
||||
}
|
||||
} else {
|
||||
|
||||
// 同步商品成本价
|
||||
( new StoreGoods() )->setSkuPrice([ 'goods_id' => $goods_id, 'site_id' => $data[ 'site_id' ] ]);
|
||||
|
||||
}
|
||||
|
||||
// 商品设置库存
|
||||
$goods_stock_model = new \app\model\stock\GoodsStock();
|
||||
$sku_list = model('goods_sku')->getList([ 'goods_id' => $goods_id ], 'sku_id');
|
||||
foreach ($sku_stock_list as $k => $v) {
|
||||
$v[ 'sku_id' ] = $sku_list[ $k ][ 'sku_id' ];
|
||||
$goods_stock_model->changeGoodsStock($v);
|
||||
}
|
||||
|
||||
$stat = new Stat();
|
||||
$stat->switchStat(['type' => 'add_goods', 'data' => ['add_goods_count' => 1, 'site_id' => $data['site_id']]]);
|
||||
$stat->switchStat(['type' => 'goods_on', 'data' => ['site_id' => $data['site_id']]]);
|
||||
|
||||
|
||||
model('goods')->commit();
|
||||
return $this->success($goods_id);
|
||||
} catch (\Exception $e) {
|
||||
model('goods')->rollback();
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品编辑
|
||||
* @param $data
|
||||
*/
|
||||
public function editGoods($data)
|
||||
{
|
||||
|
||||
model('goods')->startTrans();
|
||||
|
||||
try {
|
||||
|
||||
if (!empty($data[ 'goods_attr_format' ])) {
|
||||
|
||||
$goods_attr_format = json_decode($data[ 'goods_attr_format' ], true);
|
||||
$keys = array_column($goods_attr_format, 'sort');
|
||||
if (!empty($keys)) {
|
||||
array_multisort($keys, SORT_ASC, SORT_NUMERIC, $goods_attr_format);
|
||||
$data[ 'goods_attr_format' ] = json_encode($goods_attr_format);
|
||||
}
|
||||
}
|
||||
|
||||
$goods_id = $data[ 'goods_id' ];
|
||||
$goods_image = $data[ 'goods_image' ];
|
||||
$first_image = explode(",", $goods_image)[ 0 ];
|
||||
|
||||
//SKU商品数据
|
||||
if (!empty($data[ 'goods_sku_data' ])) {
|
||||
$data[ 'goods_sku_data' ] = json_decode($data[ 'goods_sku_data' ], true);
|
||||
}
|
||||
|
||||
//商品编码检测
|
||||
$sku_no_check = $this->checkSkuNoRepeat(['sku_list' => $data['goods_sku_data'], 'site_id' => $data['site_id'], 'goods_id' => $data['goods_id'] ?? 0]);
|
||||
if($sku_no_check['code'] < 0){
|
||||
model('goods')->rollback();
|
||||
return $sku_no_check;
|
||||
}
|
||||
|
||||
//检测商品核销日期
|
||||
$virtualDate_check = $this->checkVirtualDate($data[ 'verify_validity_type' ],$data[ 'virtual_indate' ] ?? '');
|
||||
if($virtualDate_check['code'] < 0){
|
||||
model('goods')->rollback();
|
||||
return $virtualDate_check;
|
||||
}
|
||||
|
||||
$goods_data = array (
|
||||
'goods_image' => $goods_image,
|
||||
'price' => $data[ 'goods_sku_data' ][ 0 ][ 'price' ],
|
||||
'market_price' => $data[ 'goods_sku_data' ][ 0 ][ 'market_price' ],
|
||||
'cost_price' => $data[ 'goods_sku_data' ][ 0 ][ 'cost_price' ],
|
||||
'goods_spec_format' => $data[ 'goods_spec_format' ],
|
||||
'category_id' => '',
|
||||
'category_json' => '',
|
||||
'label_id' => $data[ 'label_id' ],
|
||||
'label_name' => '',
|
||||
'timer_on' => $data[ 'timer_on' ],
|
||||
'timer_off' => $data[ 'timer_off' ],
|
||||
'sale_show' => $data[ 'sale_show' ],
|
||||
'stock_show' => $data[ 'stock_show' ],
|
||||
'market_price_show' => $data[ 'market_price_show' ],
|
||||
'barrage_show' => $data[ 'barrage_show' ],
|
||||
'is_consume_discount' => $data[ 'is_consume_discount' ],
|
||||
'service_category' => $data[ 'category_id' ],
|
||||
'is_need_verify' => 1,
|
||||
'is_reserve' => $data['is_reserve'],
|
||||
'service_mode' => $data['service_mode'],
|
||||
'service_price_way' => $data['service_price_way']
|
||||
);
|
||||
|
||||
$common_data = array (
|
||||
'goods_name' => $data[ 'goods_name' ],
|
||||
'goods_class' => $this->goods_class[ 'id' ],
|
||||
'goods_class_name' => $this->goods_class[ 'name' ],
|
||||
'goods_attr_class' => $data[ 'goods_attr_class' ],
|
||||
'goods_attr_name' => $data[ 'goods_attr_name' ],
|
||||
'is_limit' => $data[ 'is_limit' ] ?? 0,
|
||||
'limit_type' => $data[ 'limit_type' ] ?? 1,
|
||||
'site_id' => $data[ 'site_id' ],
|
||||
'goods_content' => $data[ 'goods_content' ],
|
||||
'goods_state' => $data[ 'goods_state' ],
|
||||
'goods_stock_alarm' => $data[ 'goods_stock_alarm' ],
|
||||
'is_virtual' => 1,
|
||||
'verify_validity_type' => $data[ 'verify_validity_type' ],
|
||||
'virtual_indate' => $data[ 'virtual_indate' ] ?? 0,
|
||||
'goods_attr_format' => $data[ 'goods_attr_format' ],
|
||||
'introduction' => $data[ 'introduction' ],
|
||||
'keywords' => $data[ 'keywords' ],
|
||||
'unit' => $data[ 'unit' ],
|
||||
'video_url' => $data[ 'video_url' ],
|
||||
'sort' => $data[ 'sort' ],
|
||||
'goods_service_ids' => $data[ 'goods_service_ids' ],
|
||||
'modify_time' => time(),
|
||||
'virtual_sale' => $data[ 'virtual_sale' ],
|
||||
'max_buy' => $data[ 'max_buy' ],
|
||||
'min_buy' => $data[ 'min_buy' ],
|
||||
'recommend_way' => $data[ 'recommend_way' ],
|
||||
'qr_id' => $data[ 'qr_id' ] ?? 0,
|
||||
'template_id' => $data[ 'template_id' ] ?? 0,
|
||||
'form_id' => $data[ 'form_id' ] ?? 0,
|
||||
'sale_channel' => $data[ 'sale_channel' ] ?? 'all',
|
||||
'sale_store' => $data[ 'sale_store' ] ?? 'all',
|
||||
'is_unify_price' => $data[ 'is_unify_price' ] ?? 1,
|
||||
);
|
||||
|
||||
model('goods')->update(array_merge($goods_data, $common_data), [ [ 'goods_id', '=', $goods_id ], [ 'goods_class', '=', $this->goods_class[ 'id' ] ] ]);
|
||||
|
||||
$goods_stock = 0;
|
||||
$goods_stock_model = new \app\model\stock\GoodsStock();
|
||||
$sku_stock_list = [];
|
||||
$is_off_store_goods = 0; // 是否下架门店商品
|
||||
|
||||
$discount_model = new Discount();
|
||||
$sku_id_arr = [];
|
||||
foreach ($data[ 'goods_sku_data' ] as $item) {
|
||||
$discount_info = [];
|
||||
if (!empty($item[ 'sku_id' ])) {
|
||||
$discount_info_result = $discount_model->getDiscountGoodsInfo([ [ 'pdg.sku_id', '=', $item[ 'sku_id' ] ], [ 'pd.status', '=', 1 ] ], 'id');
|
||||
$discount_info = $discount_info_result[ 'data' ];
|
||||
}
|
||||
$goods_stock += $item[ 'stock' ];
|
||||
$sku_data = array (
|
||||
'sku_name' => $data[ 'goods_name' ] . ' ' . $item[ 'spec_name' ],
|
||||
'spec_name' => $item[ 'spec_name' ],
|
||||
'sku_no' => $item[ 'sku_no' ],
|
||||
'sku_spec_format' => !empty($item[ 'sku_spec_format' ]) ? json_encode($item[ 'sku_spec_format' ]) : "",
|
||||
'goods_spec_format' => '',
|
||||
'price' => $item[ 'price' ],
|
||||
'market_price' => $item[ 'market_price' ],
|
||||
'cost_price' => $item[ 'cost_price' ],
|
||||
// 'stock' => $item[ 'stock' ],
|
||||
'stock_alarm' => $item[ 'stock_alarm' ],
|
||||
'sku_image' => !empty($item[ 'sku_image' ]) ? $item[ 'sku_image' ] : $first_image,
|
||||
'sku_images' => $item[ 'sku_images' ],
|
||||
'goods_id' => $goods_id,
|
||||
'is_default' => $item[ 'is_default' ] ?? 0,
|
||||
'service_length' => $item[ 'service_length' ] ?? 0,
|
||||
'is_consume_discount' => $data[ 'is_consume_discount' ]
|
||||
);
|
||||
if (empty($discount_info)) {
|
||||
$sku_data[ 'discount_price' ] = $item[ 'price' ];
|
||||
}
|
||||
if (!empty($item[ 'sku_id' ])) {
|
||||
$sku_id_arr[] = $item[ 'sku_id' ];
|
||||
model('goods_sku')->update(array_merge($sku_data, $common_data), [ [ 'sku_id', '=', $item[ 'sku_id' ] ], [ 'goods_class', '=', $this->goods_class[ 'id' ] ] ]);
|
||||
} else {
|
||||
$sku_id = model('goods_sku')->add(array_merge($sku_data, $common_data));
|
||||
$item[ 'sku_id' ] = $sku_id;
|
||||
$sku_id_arr[] = $sku_id;
|
||||
}
|
||||
$sku_stock_list[] = [ 'stock' => $item[ 'stock' ], 'sku_id' => $item[ 'sku_id' ], 'site_id' => $data[ 'site_id' ], 'goods_class' => $common_data[ 'goods_class' ] ];
|
||||
}
|
||||
|
||||
// 移除不存在的商品SKU
|
||||
$sku_id_list = model('goods_sku')->getList([ [ 'goods_id', '=', $goods_id ] ], 'sku_id');
|
||||
$sku_id_list = array_column($sku_id_list, 'sku_id');
|
||||
foreach ($sku_id_list as $k => $v) {
|
||||
foreach ($sku_id_arr as $ck => $cv) {
|
||||
if ($v == $cv) {
|
||||
unset($sku_id_list[ $k ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$sku_id_list = array_values($sku_id_list);
|
||||
if (!empty($sku_id_list)) {
|
||||
$check = $this->deleteGoodsSkuCheck($sku_id_list);
|
||||
if ($check[ 'code' ] < 0) {
|
||||
model('goods')->rollback();
|
||||
return $check;
|
||||
}
|
||||
$is_off_store_goods = 1;
|
||||
model('goods_sku')->delete([ [ 'sku_id', 'in', implode(",", $sku_id_list) ] ]);
|
||||
}
|
||||
|
||||
// 赋值第一个商品sku_id
|
||||
$first_info = model('goods_sku')->getFirstData([ 'goods_id' => $goods_id ], 'sku_id', 'is_default desc,sku_id asc');
|
||||
model('goods')->update([ 'sku_id' => $first_info[ 'sku_id' ] ], [ [ 'goods_id', '=', $goods_id ] ]);
|
||||
|
||||
if (!empty($data[ 'goods_spec_format' ])) {
|
||||
// 刷新SKU商品规格项 / 规格值JSON字符串
|
||||
$this->dealGoodsSkuSpecFormat($goods_id, $data[ 'goods_spec_format' ]);
|
||||
}
|
||||
|
||||
if($is_off_store_goods == 1) {
|
||||
(new StoreGoods())->modifyStoreGoodsState($goods_id, 0);
|
||||
}
|
||||
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronGoodsTimerOn' ], [ 'relate_id', '=', $goods_id ] ]);
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronGoodsTimerOff' ], [ 'relate_id', '=', $goods_id ] ]);
|
||||
//定时上下架
|
||||
if ($goods_data[ 'timer_on' ] > 0) {
|
||||
$cron->addCron(1, 0, "商品定时上架", "CronGoodsTimerOn", $goods_data[ 'timer_on' ], $goods_id);
|
||||
}
|
||||
if ($goods_data[ 'timer_off' ] > 0) {
|
||||
$cron->addCron(1, 0, "商品定时下架", "CronGoodsTimerOff", $goods_data[ 'timer_off' ], $goods_id);
|
||||
}
|
||||
|
||||
//虚拟商品指定审核有效期后自动下架
|
||||
if ($data[ 'verify_validity_type' ] == 2 && $data['goods_state'] == 1) {
|
||||
$cron->addCron(1, 0, "虚拟商品定时下架", "CronVirtualGoodsVerifyOff", $data[ 'virtual_indate' ], $goods_id);
|
||||
}else{
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronVirtualGoodsVerifyOff' ], [ 'relate_id', '=', $goods_id ] ]);
|
||||
}
|
||||
|
||||
// 商品设置库存
|
||||
foreach ($sku_stock_list as $k => $v) {
|
||||
$goods_stock_model->changeGoodsStock($v);
|
||||
}
|
||||
|
||||
if ($common_data[ 'sale_store' ] != 'all') {
|
||||
$sale_store = explode(',', $common_data[ 'sale_store' ]);
|
||||
$sale_store = array_filter($sale_store);
|
||||
foreach ($sale_store as $k => $v) {
|
||||
|
||||
// 同步商品成本价
|
||||
( new StoreGoods() )->setSkuPrice([ 'goods_id' => $goods_id, 'site_id' => $data[ 'site_id' ], 'store_id' => $v ]);
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
//同步商品成本价
|
||||
( new StoreGoods() )->setSkuPrice([ 'goods_id' => $goods_id, 'site_id' => $data[ 'site_id' ] ]);
|
||||
|
||||
}
|
||||
|
||||
$stat = new Stat();
|
||||
$stat->switchStat(['type' => 'goods_on', 'data' => ['site_id' => $data['site_id']]]);
|
||||
|
||||
model('goods')->commit();
|
||||
return $this->success($goods_id);
|
||||
} catch (\Exception $e) {
|
||||
model('goods')->rollback();
|
||||
return $this->error($e->getMessage() . $e->getFile() . $e->getLine());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
*/
|
||||
public function getGoodsInfo($condition, $field = 'goods_id,goods_name,goods_class,goods_class_name,goods_attr_class,goods_attr_name,goods_image,goods_content,goods_state,price,market_price,cost_price,goods_stock,goods_stock_alarm,goods_spec_format,goods_attr_format,introduction,keywords,unit,sort,video_url,evaluate,virtual_indate')
|
||||
{
|
||||
$info = model('goods')->getInfo($condition, $field);
|
||||
if (!empty($info)) {
|
||||
if (isset($info[ 'service_category' ])) {
|
||||
$service_category = explode(',', $info[ 'service_category' ]);
|
||||
$goods_category_data = [];
|
||||
foreach ($service_category as $k => $v) {
|
||||
if (!empty($v)) {
|
||||
$category_list = model('service_category')->getList([ [ 'category_id', 'in', $v ] ], 'category_id,category_name,level', 'level asc');
|
||||
$goods_category_data[] = $category_list;
|
||||
}
|
||||
}
|
||||
$info[ 'goods_category_data' ] = $goods_category_data;
|
||||
}
|
||||
if (isset($info[ 'goods_stock' ])) {
|
||||
$info[ 'goods_stock' ] = numberFormat($info[ 'goods_stock' ]);
|
||||
}
|
||||
if (isset($info[ 'sale_num' ])) {
|
||||
$info[ 'sale_num' ] = numberFormat($info[ 'sale_num' ]);
|
||||
}
|
||||
if (isset($info[ 'virtual_sale' ])) {
|
||||
$info[ 'virtual_sale' ] = numberFormat($info[ 'virtual_sale' ]);
|
||||
}
|
||||
if (isset($info[ 'real_stock' ])) {
|
||||
$info[ 'real_stock' ] = numberFormat($info[ 'real_stock' ]);
|
||||
}
|
||||
}
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品详情
|
||||
* @param $goods_id
|
||||
* @return array
|
||||
*/
|
||||
public function getGoodsDetail($goods_id, $site_id)
|
||||
{
|
||||
$info = model('goods')->getInfo([ [ 'is_delete', '=', 0 ], [ 'goods_id', '=', $goods_id ], [ 'site_id', '=', $site_id ], [ 'goods_class', '=', 3 ] ], "*");
|
||||
$field = 'sku_id, sku_name,spec_name, sku_no, sku_spec_format, price, market_price, cost_price, discount_price, stock,
|
||||
weight, volume, sku_image, sku_images, sort,member_price,fenxiao_price';
|
||||
$sku_data = model('goods_sku')->getList([ [ 'goods_id', '=', $goods_id ] ], $field);
|
||||
|
||||
if (!empty($sku_data)) {
|
||||
foreach ($sku_data as $k => $v) {
|
||||
$sku_data[ $k ][ 'member_price' ] = $v[ 'member_price' ] == '' ? '' : json_decode($v[ 'member_price' ], true);
|
||||
if (isset($v[ 'stock' ])) {
|
||||
$sku_data[ $k ][ 'stock' ] = numberFormat($sku_data[ $k ][ 'stock' ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($info)) {
|
||||
if (isset($info[ 'goods_stock' ])) {
|
||||
$info[ 'goods_stock' ] = numberFormat($info[ 'goods_stock' ]);
|
||||
}
|
||||
if (isset($info[ 'sale_num' ])) {
|
||||
$info[ 'sale_num' ] = numberFormat($info[ 'sale_num' ]);
|
||||
}
|
||||
if (isset($info[ 'virtual_sale' ])) {
|
||||
$info[ 'virtual_sale' ] = numberFormat($info[ 'virtual_sale' ]);
|
||||
}
|
||||
if (isset($info[ 'real_stock' ])) {
|
||||
$info[ 'real_stock' ] = numberFormat($info[ 'real_stock' ]);
|
||||
}
|
||||
$info[ 'sku_data' ] = $sku_data;
|
||||
}
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getGoodsList($condition = [], $field = 'goods_id,goods_class,goods_class_name,goods_attr_name,goods_name,site_id,,sort,goods_image,goods_content,goods_state,price,market_price,cost_price,goods_stock,goods_stock_alarm,is_virtual,goods_spec_format,goods_attr_format,create_time', $order = 'create_time desc', $limit = null)
|
||||
{
|
||||
$list = model('goods')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
if (!empty($list)) {
|
||||
foreach ($list as $k => $v) {
|
||||
if (isset($v[ 'goods_stock' ])) {
|
||||
$list[ $k ][ 'goods_stock' ] = numberFormat($list[ $k ][ 'goods_stock' ]);
|
||||
}
|
||||
if (isset($v[ 'sale_num' ])) {
|
||||
$list[ $k ][ 'sale_num' ] = numberFormat($list[ $k ][ 'sale_num' ]);
|
||||
}
|
||||
if (isset($v[ 'virtual_sale' ])) {
|
||||
$list[ $k ][ 'virtual_sale' ] = numberFormat($list[ $k ][ 'virtual_sale' ]);
|
||||
}
|
||||
if (isset($v[ 'real_stock' ])) {
|
||||
$list[ $k ][ 'real_stock' ] = numberFormat($list[ $k ][ 'real_stock' ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/************************************************************************* 购买的虚拟产品 start *******************************************************************/
|
||||
/**
|
||||
* 生成购买的虚拟产品
|
||||
* @param $site_id
|
||||
* @param $order_id
|
||||
* @param $order_no
|
||||
* @param $sku_id
|
||||
* @param $sku_name
|
||||
* @param $code
|
||||
* @param $member_id
|
||||
* @param $sku_image
|
||||
*/
|
||||
public function addGoodsVirtual($site_id, $goods_id, $sku_id, $data)
|
||||
{
|
||||
if (is_array($data) && count($data)) {
|
||||
$virtual_goods = [];
|
||||
foreach ($data as $carmichael_item) {
|
||||
$carmichael_item = htmlspecialchars(addslashes($carmichael_item));
|
||||
$card = explode(' ', $carmichael_item);
|
||||
$card_arr = [
|
||||
'cardno' => $card[ 0 ] ?? '',
|
||||
'password' => $card[ 1 ] ?? ''
|
||||
];
|
||||
$virtual_goods[] = [
|
||||
'site_id' => $site_id,
|
||||
'sku_id' => $sku_id,
|
||||
'card_info' => json_encode($card_arr),
|
||||
'goods_id' => $goods_id
|
||||
];
|
||||
}
|
||||
model("goods_virtual")->startTrans();
|
||||
try {
|
||||
$res = model("goods_virtual")->addList($virtual_goods);
|
||||
model('goods')->setInc([ [ 'goods_id', '=', $goods_id ], [ 'site_id', '=', $site_id ] ], 'goods_stock', count($virtual_goods)); // 增加商品总库存
|
||||
model('goods_sku')->setInc([ [ 'sku_id', '=', $sku_id ], [ 'site_id', '=', $site_id ] ], 'stock', count($virtual_goods)); // 增加sku库存
|
||||
model("goods_virtual")->commit();
|
||||
return $this->success($res);
|
||||
} catch (\Exception $e) {
|
||||
model("goods_virtual")->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
} else {
|
||||
return $this->error('', '请输入要添加的卡密数据');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param $condition
|
||||
*/
|
||||
public function deleteGoodsVirtual($condition)
|
||||
{
|
||||
$res = model("goods_virtual")->delete($condition);
|
||||
if ($res === false)
|
||||
return $this->error();
|
||||
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$goods_id = $check_condition[ 'goods_id' ] ?? 0;
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
/************************************************************************* 购买的虚拟产品 end *******************************************************************/
|
||||
|
||||
/**
|
||||
* 从excel中读取卡密数据
|
||||
* @param $path
|
||||
*/
|
||||
public function importData($path)
|
||||
{
|
||||
$PHPReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
||||
//载入文件
|
||||
$PHPExcel = $PHPReader->load($path);
|
||||
|
||||
//获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
|
||||
$currentSheet = $PHPExcel->getSheet(0);
|
||||
|
||||
//获取总行数
|
||||
$allRow = $currentSheet->getHighestRow();
|
||||
|
||||
if ($allRow < 2) {
|
||||
return $this->error('', '导入了一个空文件');
|
||||
}
|
||||
$data = [];
|
||||
for ($i = 2; $i <= $allRow; $i++) {
|
||||
//卡号
|
||||
$cardno = $PHPExcel->getActiveSheet()->getCell('A' . $i)->getValue();
|
||||
//卡号
|
||||
$password = $PHPExcel->getActiveSheet()->getCell('B' . $i)->getValue();
|
||||
$data[] = $cardno . ' ' . $password;
|
||||
}
|
||||
return $this->success(implode("\n", $data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品导入
|
||||
* @param $goods_data
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function importGoods($goods_data, $site_id)
|
||||
{
|
||||
try {
|
||||
if (empty($goods_data[ 'goods_name' ])) return $this->error('', '商品名称不能为空');
|
||||
if (empty($goods_data[ 'goods_image' ])) return $this->error('', '商品主图不能为空');
|
||||
if (empty($goods_data[ 'category_1' ]) && empty($goods_data[ 'category_2' ]) && empty($goods_data[ 'category_3' ])) return $this->error('', '商品分类不能为空');
|
||||
|
||||
// 处理商品分类
|
||||
$category_id = '';
|
||||
$category_json = [];
|
||||
if (!empty($goods_data[ 'category_3' ])) {
|
||||
$category_info = model('goods_category')->getInfo([ [ 'level', '=', 3 ], [ 'site_id', '=', $site_id ], [ 'category_full_name', '=', "{$goods_data['category_1']}/{$goods_data['category_2']}/{$goods_data['category_3']}" ] ], 'category_id_1,category_id_2,category_id_3');
|
||||
if (!empty($category_info)) {
|
||||
$category_id = "{$category_info['category_id_1']},{$category_info['category_id_2']},{$category_info['category_id_3']}";
|
||||
}
|
||||
}
|
||||
if (!empty($goods_data[ 'category_2' ]) && empty($category_id)) {
|
||||
$category_info = model('goods_category')->getInfo([ [ 'level', '=', 2 ], [ 'site_id', '=', $site_id ], [ 'category_full_name', '=', "{$goods_data['category_1']}/{$goods_data['category_2']}" ] ], 'category_id_1,category_id_2');
|
||||
if (!empty($category_info)) {
|
||||
$category_id = "{$category_info['category_id_1']},{$category_info['category_id_2']}";
|
||||
}
|
||||
}
|
||||
if (!empty($goods_data[ 'category_1' ]) && empty($category_id)) {
|
||||
$category_info = model('goods_category')->getInfo([ [ 'level', '=', 1 ], [ 'site_id', '=', $site_id ], [ 'category_name', '=', "{$goods_data['category_1']}" ] ], 'category_id_1');
|
||||
if (!empty($category_info)) {
|
||||
$category_id = "{$category_info['category_id_1']}";
|
||||
}
|
||||
}
|
||||
if (empty($category_id)) return $this->error('', '未找到所填商品分类');
|
||||
$category_json = [ $category_id ];
|
||||
|
||||
$sku_data = [];
|
||||
$goods_spec_format = [];
|
||||
$tag = 0;
|
||||
// 处理sku数据
|
||||
if (isset($goods_data[ 'sku' ])) {
|
||||
foreach ($goods_data[ 'sku' ] as $sku_item) {
|
||||
if (empty($sku_item[ 'sku_data' ])) return $this->error('', '规格数据不能为空');
|
||||
|
||||
$spec_name = '';
|
||||
$spec_data = explode(';', $sku_item[ 'sku_data' ]);
|
||||
|
||||
$sku_spec_format = [];
|
||||
foreach ($spec_data as $item) {
|
||||
$spec_item = explode(':', $item);
|
||||
$spec_name .= ' ' . $spec_item[ 1 ];
|
||||
|
||||
// 规格项
|
||||
$spec_index = array_search($spec_item[ 0 ], array_column($goods_spec_format, 'spec_name'));
|
||||
if (empty($goods_spec_format) || $spec_index === false) {
|
||||
$spec = [
|
||||
'spec_id' => -( $tag + getMillisecond() ),
|
||||
'spec_name' => $spec_item[ 0 ],
|
||||
'value' => []
|
||||
];
|
||||
$goods_spec_format[] = $spec;
|
||||
$tag++;
|
||||
} else {
|
||||
$spec = $goods_spec_format[ $spec_index ];
|
||||
}
|
||||
// 规格值
|
||||
$spec_index = array_search($spec_item[ 0 ], array_column($goods_spec_format, 'spec_name'));
|
||||
$spec_value_index = array_search($spec_item[ 1 ], array_column($spec[ 'value' ], 'spec_value_name'));
|
||||
if (empty($spec[ 'value' ]) || $spec_value_index === false) {
|
||||
$spec_value = [
|
||||
'spec_id' => $spec[ 'spec_id' ],
|
||||
'spec_name' => $spec[ 'spec_name' ],
|
||||
'spec_value_id' => -( $tag + getMillisecond() ),
|
||||
'spec_value_name' => $spec_item[ 1 ],
|
||||
'image' => '',
|
||||
];
|
||||
$goods_spec_format[ $spec_index ][ 'value' ][] = $spec_value;
|
||||
$tag++;
|
||||
} else {
|
||||
$spec_value = $spec[ 'value' ][ $spec_value_index ];
|
||||
}
|
||||
|
||||
$sku_spec_format[] = [
|
||||
'spec_id' => $spec[ 'spec_id' ],
|
||||
'spec_name' => $spec[ 'spec_name' ],
|
||||
'spec_value_id' => $spec_value[ 'spec_value_id' ],
|
||||
'spec_value_name' => $spec_value[ 'spec_value_name' ],
|
||||
'image' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$sku_images_arr = explode(',', $sku_item[ 'sku_image' ]);
|
||||
|
||||
$sku_temp = [
|
||||
'spec_name' => trim($spec_name),
|
||||
'sku_no' => $sku_item[ 'sku_code' ],
|
||||
'sku_spec_format' => $sku_spec_format,
|
||||
'price' => $sku_item[ 'price' ],
|
||||
'market_price' => $sku_item[ 'market_price' ],
|
||||
'cost_price' => $sku_item[ 'cost_price' ],
|
||||
'stock_alarm' => $sku_item[ 'stock_alarm' ],
|
||||
'sku_image' => empty($sku_item[ 'sku_image' ]) ? '' : $sku_images_arr[ 0 ],
|
||||
'sku_images' => empty($sku_item[ 'sku_image' ]) ? '' : $sku_item[ 'sku_image' ],
|
||||
'sku_images_arr' => empty($sku_item[ 'sku_image' ]) ? [] : $sku_images_arr,
|
||||
'is_default' => 0,
|
||||
'carmichael' => empty($sku_item[ 'carmichael' ]) ? [] : explode("\n", $sku_item[ 'carmichael' ])
|
||||
];
|
||||
|
||||
$sku_data[] = $sku_temp;
|
||||
}
|
||||
} else {
|
||||
$goods_img = explode(',', $goods_data[ 'goods_image' ]);
|
||||
$sku_data = [
|
||||
[
|
||||
'sku_id' => 0,
|
||||
'sku_name' => $goods_data[ 'goods_name' ],
|
||||
'spec_name' => '',
|
||||
'sku_spec_format' => '',
|
||||
'price' => empty($goods_data[ 'price' ]) ? 0 : $goods_data[ 'price' ],
|
||||
'market_price' => empty($goods_data[ 'market_price' ]) ? 0 : $goods_data[ 'market_price' ],
|
||||
'cost_price' => empty($goods_data[ 'cost_price' ]) ? 0 : $goods_data[ 'cost_price' ],
|
||||
'sku_no' => $goods_data[ 'goods_code' ],
|
||||
'stock_alarm' => empty($goods_data[ 'stock_alarm' ]) ? 0 : $goods_data[ 'stock_alarm' ],
|
||||
'sku_image' => $goods_img[ 0 ],
|
||||
'sku_images' => $goods_data[ 'goods_image' ],
|
||||
'carmichael' => empty($goods_data[ 'carmichael' ]) ? [] : explode("\n", $goods_data[ 'carmichael' ])
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
if (count($goods_spec_format) > 4) return $this->error('', '最多支持四种规格项');
|
||||
|
||||
$data = [
|
||||
'goods_name' => $goods_data[ 'goods_name' ],// 商品名称,
|
||||
'goods_attr_class' => '',// 商品类型id,
|
||||
'goods_attr_name' => '',// 商品类型名称,
|
||||
'site_id' => $site_id,
|
||||
'category_id' => ',' . $category_id . ',',
|
||||
'category_json' => json_encode($category_json),
|
||||
'goods_image' => $goods_data[ 'goods_image' ],// 商品主图路径
|
||||
'goods_content' => '',// 商品详情
|
||||
'goods_state' => 0, //$goods_data['goods_state'] == 1 || $goods_data['goods_state'] == '是' ? 1 : 0,// 商品状态(1.正常0下架)
|
||||
'price' => empty($goods_data[ 'price' ]) ? 0 : $goods_data[ 'price' ],// 商品价格(取第一个sku)
|
||||
'market_price' => empty($goods_data[ 'market_price' ]) ? 0 : $goods_data[ 'market_price' ],// 市场价格(取第一个sku)
|
||||
'cost_price' => empty($goods_data[ 'cost_price' ]) ? 0 : $goods_data[ 'cost_price' ],// 成本价(取第一个sku)
|
||||
'sku_no' => $goods_data[ 'goods_code' ],// 商品sku编码
|
||||
'goods_stock_alarm' => empty($goods_data[ 'goods_stock_alarm' ]) ? 0 : $goods_data[ 'goods_stock_alarm' ],// 库存预警
|
||||
'goods_spec_format' => empty($goods_spec_format) ? '' : json_encode($goods_spec_format, JSON_UNESCAPED_UNICODE),// 商品规格格式
|
||||
'goods_attr_format' => '',// 商品参数格式
|
||||
'introduction' => $goods_data[ 'introduction' ],// 促销语
|
||||
'keywords' => $goods_data[ 'keywords' ],// 关键词
|
||||
'unit' => $goods_data[ 'unit' ],// 单位
|
||||
'sort' => '',// 排序,
|
||||
'qr_id' => empty($goods_data[ 'qr_id' ]) ? 0 : $goods_data[ 'qr_id' ],// 社群二维码id
|
||||
'template_id' => empty($goods_data[ 'template_id' ]) ? 0 : $goods_data[ 'template_id' ],// 海报id
|
||||
'is_limit' => empty($goods_data[ 'is_limit' ]) ? 0 : $goods_data[ 'is_limit' ],// 是否限购
|
||||
'limit_type' => empty($goods_data[ 'limit_type' ]) ? 0 : $goods_data[ 'limit_type' ],// 限购类型
|
||||
'video_url' => '',// 视频
|
||||
'goods_sku_data' => json_encode($sku_data, JSON_UNESCAPED_UNICODE),// SKU商品数据
|
||||
'goods_service_ids' => '',// 商品服务id集合
|
||||
'label_id' => '',// 商品分组id
|
||||
'virtual_sale' => 0,// 虚拟销量
|
||||
'max_buy' => 0,// 限购
|
||||
'min_buy' => 0,// 起售
|
||||
'recommend_way' => 0, // 推荐方式,1:新品,2:精品,3;推荐
|
||||
'timer_on' => 0,//定时上架
|
||||
'timer_off' => 0,//定时下架
|
||||
'brand_id' => 0,
|
||||
'is_consume_discount' => $goods_data[ 'is_consume_discount' ] == 1 || $goods_data[ 'is_consume_discount' ] == '是' ? 1 : 0, //是否参与会员折扣
|
||||
];
|
||||
|
||||
$res = $this->addGoods($data);
|
||||
return $res;
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
303
addon/cardservice/model/order/MembercardOrderCreate.php
Executable file
303
addon/cardservice/model/order/MembercardOrderCreate.php
Executable file
@@ -0,0 +1,303 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\cardservice\model\order;
|
||||
|
||||
use addon\cardservice\model\MemberCard;
|
||||
use app\model\BaseModel;
|
||||
use app\model\order\Config;
|
||||
use app\model\order\OrderCreate;
|
||||
use app\model\order\OrderCreateTool;
|
||||
use app\model\store\Store;
|
||||
use app\model\express\Express;
|
||||
use app\model\system\Pay;
|
||||
use app\model\express\Config as ExpressConfig;
|
||||
use app\model\express\Local;
|
||||
use app\model\goods\Goods as GoodsModel;
|
||||
use extend\exception\OrderException;
|
||||
|
||||
/**
|
||||
* 订单创建(会员卡项提货)
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
class MembercardOrderCreate extends BaseModel
|
||||
{
|
||||
|
||||
use OrderCreateTool;
|
||||
public $member_card_id = 0;
|
||||
public $card_id = 0;
|
||||
public $card_info = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->promotion_type = 'cardservice';
|
||||
$this->promotion_type_name = '卡项提货';
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单创建
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//计算
|
||||
$this->confirm();
|
||||
if ($this->error > 0) {
|
||||
return $this->error([ 'error_code' => $this->error ], $this->error_msg);
|
||||
}
|
||||
//订单创建数据
|
||||
$order_insert_data = $this->getOrderInsertData([ 'discount' ], 'invert');
|
||||
$order_insert_data[ 'store_id' ] = $this->store_id;
|
||||
$order_insert_data[ 'create_time' ] = time();
|
||||
$order_insert_data[ 'is_enable_refund' ] = 0;
|
||||
//订单类型以及状态
|
||||
$this->orderType();
|
||||
$order_insert_data[ 'order_type' ] = $this->order_type[ 'order_type_id' ];
|
||||
$order_insert_data[ 'order_type_name' ] = $this->order_type[ 'order_type_name' ];
|
||||
$order_insert_data[ 'order_status_name' ] = $this->order_type[ 'order_status' ][ 'name' ];
|
||||
$order_insert_data[ 'order_status_action' ] = json_encode($this->order_type[ 'order_status' ], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
model('order')->startTrans();
|
||||
//循环生成多个订单
|
||||
try {
|
||||
$this->order_id = model('order')->add($order_insert_data);
|
||||
|
||||
$order_goods_insert_data = [];
|
||||
//订单项目表
|
||||
foreach ($this->goods_list as &$order_goods_v) {
|
||||
$item_order_goods_insert_data = $this->getOrderGoodsInsertData($order_goods_v);
|
||||
$item_order_goods_insert_data[ 'card_item_id' ] = $order_goods_v[ 'item_id' ];
|
||||
$order_goods_insert_data[] = $item_order_goods_insert_data;
|
||||
}
|
||||
model('order_goods')->addList($order_goods_insert_data);
|
||||
|
||||
//卡项使用记录
|
||||
$card_use_params = [];
|
||||
foreach ($this->getOrderGoodsList() as $k => $v) {
|
||||
$goods_item = $this->goods_list[ $k ];
|
||||
$card_use_params[] = [
|
||||
'item_id' => $goods_item[ 'item_id' ],
|
||||
'num' => $goods_item[ 'num' ],
|
||||
'type' => 'order',
|
||||
'relation_id' => $v[ 'order_goods_id' ],
|
||||
'store_id' => $this->store_id
|
||||
];
|
||||
}
|
||||
$member_card = new MemberCard();
|
||||
$use_result = $member_card->cardUse($card_use_params);
|
||||
if ($use_result[ 'code' ] != 0) {
|
||||
model('order')->rollback();
|
||||
return $use_result;
|
||||
}
|
||||
//批量库存处理(卡密商品支付后在扣出库存)//todo 可以再商品中设置扣除库存步骤
|
||||
$this->batchDecOrderGoodsStock();
|
||||
model('order')->commit();
|
||||
//订单创建后事件
|
||||
$this->orderCreateAfter();
|
||||
//生成整体支付单据
|
||||
$pay = new Pay();
|
||||
$pay->addPay($this->site_id, $this->out_trade_no, $this->pay_type, $this->order_name, $this->order_name, $this->pay_money, '', 'OrderPayNotify', '/pages/order/detail?order_id=' . $this->order_id, $this->order_id, $this->member_id);
|
||||
|
||||
return $this->success($this->out_trade_no);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
model('order')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算后的进一步计算(不存缓存,每次都是重新计算)
|
||||
* @return array
|
||||
*/
|
||||
public function confirm()
|
||||
{
|
||||
$order_key = $this->param[ 'order_key' ];
|
||||
$this->getOrderCache($order_key);
|
||||
//初始化地址
|
||||
$this->initMemberAddress();
|
||||
//初始化门店信息
|
||||
$this->initStore();
|
||||
//配送计算
|
||||
$this->calculateDelivery();
|
||||
//批量校验配送方式
|
||||
$this->batchCheckDeliveryType();
|
||||
//计算发票相关
|
||||
$this->calculateInvoice();
|
||||
$this->pay_money = $this->order_money - $this->balance_money;
|
||||
//设置过的商品项信息
|
||||
return get_object_vars($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单计算
|
||||
*/
|
||||
public function calculate()
|
||||
{
|
||||
//初始化会员收货地址
|
||||
$this->initMemberAddress();
|
||||
//初始化会员账户
|
||||
$this->initMemberAccount();
|
||||
//初始化门店信息
|
||||
$this->initStore();
|
||||
$this->member_card_id = $this->param[ 'member_card_id' ];
|
||||
$member_card_model = new MemberCard();
|
||||
|
||||
$condition = array (
|
||||
[ 'site_id', '=', $this->site_id ],
|
||||
[ 'card_id', '=', $this->member_card_id ],
|
||||
[ 'member_id', '=', $this->member_id ],
|
||||
[ 'status', '=', 1 ]
|
||||
);
|
||||
$this->card_info = $member_card_model->getCardInfo($condition)[ 'data' ] ?? [];
|
||||
if (empty($this->card_info)) throw new OrderException('该卡项不存在!');
|
||||
$this->card_id = $this->card_info[ 'card_id' ];
|
||||
//销售门店信息
|
||||
$goods_model = new GoodsModel();
|
||||
$card_goods_info = $goods_model->getGoodsInfo([['goods_id', '=', $this->card_info['goods_id']]], 'sale_store')['data'];
|
||||
if(empty($card_goods_info)) throw new OrderException('该卡项商品不存在!');
|
||||
if($card_goods_info['sale_store'] != 'all'){
|
||||
$this->available_store_ids = trim($card_goods_info['sale_store'], ',');
|
||||
}
|
||||
//商品列表信息
|
||||
$this->getOrderGoodsCalculate();
|
||||
//订单计算
|
||||
$this->shopOrderCalculate();
|
||||
|
||||
//获取发票相关
|
||||
$this->getInovice();
|
||||
$this->order_key = create_no();
|
||||
$this->setOrderCache(get_object_vars($this), $this->order_key);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 待付款订单
|
||||
*/
|
||||
public function orderPayment()
|
||||
{
|
||||
$this->calculate();
|
||||
//查询配送信息
|
||||
$this->getDeliveryData();
|
||||
//订单初始项
|
||||
event('OrderPayment', [ 'order_object' => $this ]);
|
||||
return get_object_vars($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品的计算信息
|
||||
*/
|
||||
public function getOrderGoodsCalculate()
|
||||
{
|
||||
$this->getCardGoodsList();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取组合套餐商品列表信息
|
||||
*/
|
||||
public function getCardGoodsList()
|
||||
{
|
||||
$card_info = $this->card_info;
|
||||
$card_type = $this->card_info[ 'card_type' ];
|
||||
$member_card_item = $this->param[ 'member_card_item' ] ?? [];
|
||||
if (!$member_card_item) throw new OrderException('商品不存在');
|
||||
foreach ($member_card_item as $item) {
|
||||
$join = [
|
||||
[ 'goods_sku ngs', 'mgci.sku_id = ngs.sku_id', 'inner' ],
|
||||
[ 'site ns', 'mgci.site_id = ns.site_id', 'inner' ]
|
||||
];
|
||||
$condition = [
|
||||
[ 'mgci.item_id', '=', $item[ 'item_id' ] ],
|
||||
[ 'mgci.site_id', '=', $this->site_id ],
|
||||
[ 'mgci.card_id', '=', $this->member_card_id ],
|
||||
[ 'ngs.goods_state', '=', 1 ],
|
||||
[ 'ngs.is_virtual', '=', 0 ]
|
||||
];
|
||||
$field = 'mgci.item_id,mgci.num as total_num,mgci.use_num,ngs.sku_id,ngs.sku_name, ngs.sku_no,ngs.price, ngs.discount_price, ngs.cost_price, ngs.stock, ngs.weight, ngs.volume, ngs.sku_image,ngs.site_id, ngs.goods_state, ngs.is_virtual,ngs.is_free_shipping, ngs.shipping_template, ngs.goods_class, ngs.goods_class_name, ngs.goods_id,ngs.sku_spec_format,ngs.goods_name,ns.site_name,ngs.support_trade_type,ngs.supplier_id';
|
||||
$goods_info = model('member_goods_card_item')->getInfo($condition, $field, 'mgci', $join);
|
||||
if (!empty($goods_info)) {
|
||||
$this->is_virtual = $goods_info[ 'is_virtual' ];
|
||||
if ($card_type == 'oncecard') {
|
||||
$item[ 'num' ] = min($item[ 'num' ], $goods_info[ 'total_num' ] - $goods_info[ 'use_num' ]);
|
||||
} else if ($card_type == 'commoncard') {
|
||||
$item[ 'num' ] = min($item[ 'num' ], $card_info[ 'total_num' ] - $card_info[ 'total_use_num' ]);
|
||||
$card_info[ 'total_use_num' ] += $item[ 'num' ];
|
||||
}
|
||||
if ($item[ 'num' ] < 1) continue;
|
||||
$goods_info[ 'num' ] = $item[ 'num' ];
|
||||
$price = $goods_info[ 'discount_price' ];
|
||||
$goods_info[ 'price' ] = $price;
|
||||
$goods_money = $price * $goods_info[ 'num' ];
|
||||
$goods_info[ 'goods_money' ] = $goods_money;
|
||||
$promotion_money = 0;
|
||||
$goods_info[ 'real_goods_money' ] = $goods_money;
|
||||
$goods_info[ 'coupon_money' ] = 0;//优惠券金额
|
||||
$goods_info[ 'promotion_money' ] = $promotion_money;//优惠金额
|
||||
|
||||
$this->site_name = $goods_info[ 'site_name' ];
|
||||
$this->goods_list[] = $goods_info;
|
||||
$order_name = $this->order_name ?? '';
|
||||
if ($order_name) {
|
||||
$len = strlen_mb($order_name);
|
||||
if ($len > 200) {
|
||||
$this->order_name = str_sub($order_name, 200);
|
||||
} else {
|
||||
$this->order_name = string_split($order_name, ',', $goods_info[ 'sku_name' ]);
|
||||
}
|
||||
} else {
|
||||
$this->order_name = string_split('', ',', $goods_info[ 'sku_name' ]);
|
||||
}
|
||||
$this->goods_num += $goods_info[ 'num' ];
|
||||
$this->goods_money += $goods_info[ 'goods_money' ];
|
||||
//以;隔开的商品项
|
||||
$goods_list_str = $this->goods_list_str ?? '';
|
||||
if ($goods_list_str) {
|
||||
$this->goods_list_str = $goods_list_str . ';' . $goods_info[ 'sku_id' ] . ':' . $goods_info[ 'num' ];
|
||||
} else {
|
||||
$this->goods_list_str = $goods_info[ 'sku_id' ] . ':' . $goods_info[ 'num' ];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$this->goods_list) throw new OrderException('卡项中没有可以提货的商品!');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单计算
|
||||
*/
|
||||
public function shopOrderCalculate()
|
||||
{
|
||||
$promotion_money = 0;
|
||||
foreach ($this->goods_list as &$v) {
|
||||
$item_goods_money = $v[ 'goods_money' ];
|
||||
$item_promotion_money = $item_goods_money;
|
||||
$real_goods_money = $item_goods_money - $item_promotion_money;
|
||||
$v[ 'real_goods_money' ] = $real_goods_money;
|
||||
$v[ 'promotion_money' ] = $item_promotion_money;
|
||||
$promotion_money += $item_promotion_money;
|
||||
}
|
||||
$this->promotion_money = $promotion_money;
|
||||
$this->is_free_delivery = false;//
|
||||
//重新计算订单总额
|
||||
$this->getOrderMoney();
|
||||
//理论上是多余的操作
|
||||
if ($this->order_money < 0) {
|
||||
$this->order_money = 0;
|
||||
}
|
||||
//总结计算
|
||||
$this->pay_money = $this->order_money;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user