初始上传

This commit is contained in:
2026-04-04 17:27:12 +08:00
parent 4d80d28eb4
commit b7e11774ee
11191 changed files with 1588469 additions and 0 deletions

View File

@@ -0,0 +1,376 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\giftcard\model\card;
use addon\giftcard\model\giftcard\CardStat;
use addon\giftcard\model\giftcard\GiftCard;
use app\model\BaseModel;
use app\model\member\Member;
use think\facade\Cache;
/**
* 礼品卡工具类
*
* @author Administrator
*
*/
class Card extends BaseModel
{
public $source_list = array (
'order' => '购买',
'gift' => '赠送'
);
public function getStatusList($card_type)
{
switch ( $card_type ) {
case 'virtual'://电子卡
return array (
'to_use' => '待使用',
'used' => '已使用',
'expire' => '已过期'
);
break;
case 'real'://实体卡
return array (
'to_activate' => '待激活',
'to_use' => '待使用',
'used' => '已使用',
'expire' => '已过期',
'invalid' => '已失效'
);
break;
}
}
/**
* 生成礼品卡记录
* @param $params
* @return array
*/
public function addCardItem($params)
{
$insert_data = $params[ 'insert_data' ];//可能包含订单id
$site_id = $params[ 'site_id' ];
$member_id = $params[ 'member_id' ] ?? 0;
$giftcard_id = $params[ 'giftcard_id' ];
$card_type = $params[ 'card_type' ] ?? '';
$card_right_type = $params[ 'card_right_type' ];
//批量生成卡号
$giftcard_model = new Giftcard();
$card_no_res = $giftcard_model->createCardNo($params['giftcard_id'], 1);
if($card_no_res['code'] < 0) return $card_no_res;
$card_no = $card_no_res['data'][0];
if ($card_type == 'real') {
$count = model('giftcard_card')->getCount([ [ 'card_no', '=', $card_no ] ]);
if ($count > 0) {
return $this->error([], '当前卡密和编号已存在');
}
}
$data = array (
'site_id' => $params[ 'site_id' ],
'card_no' => $card_no,
'card_type' => $card_type,
'giftcard_id' => $giftcard_id,
'member_id' => $member_id,
'create_time' => time(),
'card_right_type' => $card_right_type,
'valid_time' => $this->getValidityTime($params),
'balance' => $params[ 'balance' ] ?? 0,
'card_right_goods_type' => $params[ 'card_right_goods_type' ] ?? '',
'card_right_goods_count' => $params[ 'card_right_goods_count' ] ?? '',
);
$card_id = model('giftcard_card')->add(array_merge($data, $insert_data));
if ($card_type == 'virtual') {
$goods_list = $params[ 'goods_list' ];
foreach ($goods_list as $k => $v) {
$v[ 'card_id' ] = $card_id;
$v[ 'giftcard_id' ] = $giftcard_id;
$v[ 'card_right_type' ] = $card_right_type;
$this->addCardItemGoods($v);
}
}
return $this->success($card_id);
}
/**
* 礼品卡项
* @param $params
* @return array
*/
public function addCardItemGoods($params)
{
$data = array (
'site_id' => $params[ 'site_id' ],
'giftcard_id' => $params[ 'giftcard_id' ],
'card_id' => $params[ 'card_id' ],
'sku_id' => $params[ 'sku_id' ] ?? 0,
'sku_name' => $params[ 'sku_name' ] ?? '',
'sku_image' => $params[ 'sku_image' ] ?? '',
'sku_no' => $params[ 'sku_no' ] ?? '',
'goods_id' => $params[ 'goods_id' ] ?? 0,
'goods_name' => $params[ 'goods_name' ] ?? '',
'balance' => $params[ 'balance' ] ?? 0,//储值余额
'total_balance' => $params[ 'total_balance' ] ?? 0,
'price' => $params[ 'price' ] ?? 0,
'total_num' => $params[ 'num' ] ?? 1,//购买数量
'order_id' => $params[ 'order_id' ] ?? 0,
'order_goods_id' => $params[ 'order_goods_id' ] ?? 0,
'card_right_type' => $params[ 'card_right_type' ] ?? '',
);
model('giftcard_card_goods')->add($data);
return $this->success();
}
/**
* 计算礼品卡有效期
* @param $params
* @return float|int
*/
public function getValidityTime($params)
{
$validity_type = $params[ 'validity_type' ];
$validity_time = $params[ 'validity_time' ];
$validity_day = $params[ 'validity_day' ];
switch ( $validity_type ) {
case 'forever':
$temp_time = 0;
break;
case 'day':
$temp_time = time() + 86400 * $validity_day;
break;
case 'date':
$temp_time = $validity_time;
break;
}
return $temp_time;
}
/**
* 一般认为这是针对单项的删除
* @param $params
* @return array
*/
public function delete($params)
{
$site_id = $params[ 'site_id' ] ?? 0;
$card_id = $params[ 'card_id' ] ?? 0;
$card_ids = $params[ 'card_ids' ] ?? '';
$card_import_id = $params[ 'card_import_id' ] ?? 0;
$card_import_ids = $params[ 'card_import_ids' ] ?? 0;
$condition = array (
[ 'status', '=', 'to_activate' ]
);
if ($card_id > 0) {
$condition[] = [ 'card_id', '=', $card_id ];
}
if (!empty($card_ids)) {
$condition[] = [ 'card_id', 'in', $card_ids ];
}
if ($card_id > 0) {
$condition[] = [ 'card_id', '=', $card_id ];
}
if ($site_id > 0) {
$condition[] = [ 'site_id', '=', $site_id ];
}
if ($card_import_id > 0) {
$condition[] = [ 'card_import_id', '=', $card_import_id ];
}
if (!empty($card_import_ids)) {
$condition[] = [ 'card_import_id', 'in', $card_import_ids ];
}
$card_list = $this->getCardList($condition)[ 'data' ] ?? [];
if (empty($card_list))
return $this->error();
// if($card_info['status'] == 'used')
// return $this->error('', '删除失败,不可删除已使用的礼品卡');
$result = model('giftcard_card')->delete($condition);
if ($result === false)
return $this->error();
$params[ 'list' ] = $card_list;
$this->deleteOperation($params);
return $this->success();
}
/**
* 删除卡项的后续事件
* @param $params
* @return array
*/
public function deleteOperation($params)
{
$list = $params[ 'list' ];
foreach ($list as $v) {
//数据统计
( new CardStat() )->stat(array_merge([ 'card_info' => $v, 'stat_type' => 'del' ]));
}
return $this->success();
}
/**
* 获取礼品卡记录信息
* @param $condition
* @param string $field
* @return array
*/
public function getCardInfo($condition, $field = '*', $alias = '', $join = [])
{
$info = model('giftcard_card')->getInfo($condition, $field, $alias, $join);
return $this->success($info);
}
/**
* 获取礼品卡记录列表
* @param array $condition
* @param string $field
* @param string $order
* @param null $limit
* @return array
*/
public function getCardList($condition = [], $field = '*', $order = '', $alias = '', $join = [], $limit = null)
{
$list = model('giftcard_card')->getList($condition, $field, $order, $alias, $join, '', $limit);
return $this->success($list);
}
/**
* 获取礼品卡记录分页列表
* @param array $condition
* @param int $page
* @param int $page_size
* @param string $order
* @param string $field
* @return array
*/
public function getCardPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = '', $join = [])
{
$list = model('giftcard_card')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
return $this->success($list);
}
/**
* 获取礼品卡记录项信息
* @param $condition
* @param string $field
* @return array
*/
public function getCardGoodsInfo($condition, $field = '*')
{
$info = model('giftcard_card_goods')->getInfo($condition, $field);
return $this->success($info);
}
/**
* 获取礼品卡记录项列表
* @param array $condition
* @param string $field
* @param string $order
* @param null $limit
* @return array
*/
public function getCardGoodsList($condition = [], $field = '*', $order = '', $limit = null, $alias = 'a', $join = [])
{
$list = model('giftcard_card_goods')->getList($condition, $field, $order, $alias, $join, '', $limit);
return $this->success($list);
}
/**
* 获取礼品卡记录项分页列表
* @param array $condition
* @param int $page
* @param int $page_size
* @param string $order
* @param string $field
* @return array
*/
public function getCardGoodsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = 'a', $join = [])
{
$list = model('giftcard_card_goods')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
return $this->success($list);
}
/**
* 卡券详情
* @param $params
* @return array
*/
public function getCardDetail($params)
{
$site_id = $params[ 'site_id' ] ?? 0;
$member_id = $params[ 'member_id' ] ?? 0;
$card_id = $params[ 'card_id' ];
$condition = array (
[ 'c.card_id', '=', $card_id ]
);
if ($site_id > 0) {
$condition[] = [ 'c.site_id', '=', $site_id ];
}
if ($member_id > 0) {
$condition[] = [ 'c.member_id', '=', $member_id ];
}
$info = $this->getCardInfo($condition, 'c.*,go.order_no', 'c', [
[ 'giftcard_order go', 'c.order_id=go.order_id', 'left' ]
])[ 'data' ] ?? [];
if (empty($info))
return $this->error();
$member_model = new Member();
$member_info = $member_model->getMemberInfo([ [ 'member_id', '=', $info[ 'member_id' ] ] ], 'nickname,headimg')[ 'data' ] ?? [];
$info[ 'member_nickname' ] = $member_info[ 'nickname' ] ?? '';
$info[ 'member_headimg' ] = $member_info[ 'headimg' ] ?? '';
$member_info = $member_model->getMemberInfo([ [ 'member_id', '=', $info[ 'init_member_id' ] ] ], 'nickname,headimg')[ 'data' ] ?? [];
$info[ 'init_member_nickname' ] = $member_info[ 'nickname' ] ?? '';
$info[ 'init_member_headimg' ] = $member_info[ 'headimg' ] ?? '';
$condition = array (
[ 'card_id', '=', $card_id ]
);
if ($site_id > 0) {
$condition[] = [ 'site_id', '=', $site_id ];
}
if ($member_id > 0) {
$condition[] = [ 'member_id', '=', $member_id ];
}
$list = $this->getCardGoodsList($condition)[ 'data' ] ?? [];
$info[ 'card_goods_list' ] = $list;
return $this->success($info);
}
public function tran($data)
{
$status = $data[ 'status' ] ?? '';
if (!empty($status)) {
$data[ 'status_name' ] = $this->getStatusList($data[ 'card_type' ])[ $status ] ?? '';
}
$source = $data[ 'source' ] ?? '';
if (!empty($source)) {
$data[ 'source_name' ] = $this->source_list[ $source ];
}
$member_id = $data[ 'member_id' ] ?? 0;
if ($member_id > 0) {
$member_model = new Member();
$member_info = $member_model->getMemberInfo([ [ 'member_id', '=', $member_id ] ])[ 'data' ] ?? [];
$data[ 'member_nickname' ] = $member_info[ 'nickname' ] ?? '';
}
return $data;
}
}

View File

@@ -0,0 +1,318 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\giftcard\model\card;
use addon\giftcard\model\giftcard\GiftCard;
use app\model\member\Member;
use think\facade\Cache;
use think\facade\Db;
/**
* 礼品卡导入类
*
* @author Administrator
*
*/
class CardImport extends Card
{
public $create_type_list = array (
'auto' => '在线制卡',
'import' => '导入制卡'
);
/**
* 创建导入记录单据
* @param $params
* @return array
*/
public function create($params)
{
$type = $params[ 'type' ];
$giftcard_id = $params[ 'giftcard_id' ];
$giftcard_model = new GiftCard();
$condition = [ [ 'giftcard_id', '=', $giftcard_id ] ];
$info = $giftcard_model->getGiftcardInfo($condition)[ 'data' ] ?? [];
if (empty($info))
return $this->error();
$info[ 'operator_data' ] = $params[ 'operator_data' ];
if ($info[ 'card_type' ] != 'real') {
return $this->error('', '该礼品不支持制卡');
}
$data = array (
'site_id' => $params[ 'site_id' ],
'name' => $params[ 'name' ] ?? date('YmdHis'),
'giftcard_id' => $params[ 'giftcard_id' ],
'type' => $params[ 'type' ],
'card_type' => $info[ 'card_type' ],
'create_time' => time()
);
switch ( $type ) {
case 'auto':
$data[ 'total_count' ] = $params[ 'num' ] ?? 0;
break;
case 'manual':
$data[ 'total_count' ] = 1;
break;
case 'import':
$file = request()->file('file');
$tmp_name = $file->getPathname();//获取上传缓存文件
$fp = file($tmp_name);
$data[ 'total_count' ] = count($fp) - 1;
break;
}
$import_id = $this->add($data)[ 'data' ] ?? 0;
if ($type == 'import') {
$path = 'upload/giftcard/';
if (file_exists($path) || mkdir($path, 0755, true)) {
if (move_uploaded_file($tmp_name, $path . 'giftcard_card_import' . $import_id . '.csv')) {
Cache::set('giftcard/giftcard_card_import_name' . $import_id, $file->getOriginalName());
} else {
return $this->error([], '导入失败');
}
} else {
return $this->error([], '导入失败');
}
}
return $this->success($import_id);
}
/**
* 添加
* @param $data
* @return array
*/
public function add($data)
{
$data[ 'create_time' ] = time();
$id = model('giftcard_card_import')->add($data);
return $this->success($id);
}
/**
* 编辑
* @param $data
* @param $condition
* @return array
*/
public function update($data, $condition)
{
$id = model('giftcard_card_import')->update($data, $condition);
return $this->success($id);
}
/**
* 删除导入记录(todo 能做真删吗)
* @param $condition
* @return array
*/
public function delete($params)
{
$site_id = $params[ 'site_id' ] ?? 0;
$import_id = $params[ 'import_id' ] ?? 0;
$import_ids = $params[ 'import_ids' ] ?? '';
$condition = array ();
if ($site_id > 0) {
$condition[] = [ 'site_id', '=', $site_id ];
}
if ($import_id > 0) {
$condition[] = [ 'import_id', '=', $import_id ];
}
if (!empty($import_ids)) {
$condition[] = [ 'import_id', 'in', $import_ids ];
}
$list = $this->getCardImportList($condition)[ 'data' ] ?? [];
if (empty($list))
return $this->error();
foreach ($list as $k => $v) {
$item_condition = array (
[ 'card_import_id', '=', $v[ 'import_id' ] ],
[ 'status', '=', 'used' ]
);
$count = model('giftcard_card')->getCount($item_condition);
if ($count > 0) {
return $this->error([], '存在已使用的卡项,当前记录不允许删除');
}
}
//删除制卡记录
$res = model('giftcard_card_import')->delete($condition);
$this->deleteOperation($params);
return $this->success($res);
}
/**
* 删除导入卡记录后续
* @param $params
* @return array
*/
public function deleteOperation($params)
{
// $list = $params['list'];
//会将这个记录之下的卡项全部删除
$card_model = new Card();
$import_id = $params[ 'import_id' ] ?? 0;
$import_ids = $params[ 'import_ids' ] ?? 0;
if ($import_id > 0) {
$params[ 'card_import_id' ] = $import_id;
}
if (!empty($import_ids)) {
$params[ 'card_import_ids' ] = $import_ids;
}
$result = $card_model->delete($params);
return $result;
}
/**
* 获取礼品卡导入记录信息
* @param $condition
* @param string $field
* @return array
*/
public function getCardImportInfo($condition, $field = '*')
{
$info = model('giftcard_card_import')->getInfo($condition, $field);
return $this->success($info);
}
/**
* 获取礼品卡导入记录列表
* @param array $condition
* @param string $field
* @param string $order
* @param null $limit
* @return array
*/
public function getCardImportList($condition = [], $field = '*', $order = '', $limit = null)
{
$list = model('giftcard_card_import')->getList($condition, $field, $order, '', '', '', $limit);
return $this->success($list);
}
/**
* 获取礼品卡导入记录分页列表
* @param array $condition
* @param int $page
* @param int $page_size
* @param string $order
* @param string $field
* @return array
*/
public function getCardImportPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
{
$list = model('giftcard_card_import')->pageList($condition, $field, $order, $page, $page_size);
return $this->success($list);
}
public function getCardImportColumn($condition, $field = '*')
{
$info = model('giftcard_card_import')->getColumn($condition, $field);
return $this->success($info);
}
/**
* 导出
* @param $condition
*/
public function export($condition)
{
try {
$file_name = date('Y年m月d日-礼品卡', time()) . '.csv';
// $file_name = date('YmdHis').'.csv';//csv文件名
//通过分批次执行数据导出(防止内存超出配置设置的)
set_time_limit(0);
ini_set('memory_limit', '256M');
//设置header头
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="' . $file_name . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//打开php数据输入缓冲区
$fp = fopen('php://output', 'a');
// fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); // 添加 BOM
$heade = [ '编号', '卡密', '状态', '生成时间', '激活会员', '激活时间' ];
//将数据编码转换成GBK格式
mb_convert_variables('GBK', 'UTF-8', $heade);
//将数据格式化为CSV格式并写入到output流中
fputcsv($fp, $heade);
$member_model = new Member();
//写入第一行表头
Db::name('giftcard_card')->where($condition)->chunk(500, function($item_list) use ($fp, $member_model) {
//写入导出信息
foreach ($item_list as $k => $item_v) {
$item_member_id = $item_v[ 'member_id' ];
$item_nickname = '';
if ($item_member_id > 0) {
$item_member_info = $member_model->getMemberInfo([ [ 'member_id', '=', $item_member_id ] ])[ 'data' ] ?? [];
$item_nickname = $item_member_info[ 'nickname' ];
}
$item_v = $this->tran($item_v);
$temp_data = [
(string) $item_v[ 'card_no' ] . "\t",
(string) $item_v[ 'card_cdk' ] . "\t",
(string) $item_v[ 'status_name' ] . "\t",
time_to_date($item_v[ 'create_time' ]) . "\t",
$item_nickname . "\t",
time_to_date($item_v[ 'activate_time' ]) . "\t",
];
mb_convert_variables('GBK', 'UTF-8', $temp_data);
fputcsv($fp, $temp_data);
//将已经存储到csv中的变量数据销毁释放内存
unset($item_v);
}
unset($item_list);
});
//关闭句柄
fclose($fp);
die;
} catch (\Exception $e) {
return $this->error([], $e->getMessage() . $e->getFile() . $e->getLine());
}
}
/**
* 作废
* @param $params
* @return array
*/
public function invalid($params)
{
$import_id = $params[ 'import_id' ];
$condition = array (
[ 'import_id', '=', $import_id ],
[ 'status', '=', '1' ]
);
$data = array (
'status' => 2,
'invalid_time' => time(),
);
model('giftcard_card')->update($data, $condition);
//应该是批量设置
//查询未激活的卡密有多少
$card_operation_model = new CardOperation();
$params[ 'card_import_id' ] = $import_id;
//批量使卡失效作废
$card_operation_model->cardInvalid($params);
return $this->success();
}
}

View File

@@ -0,0 +1,197 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\giftcard\model\card;
use addon\giftcard\model\membercard\MemberCard;
use app\model\BaseModel;
use app\model\member\Member;
/**
* 礼品卡操作日志
*
* @author Administrator
*
*/
class CardLog extends BaseModel
{
public function add($params)
{
$type = $params[ 'type' ];//操作类型
$card_id = $params[ 'card_id' ];
$type_id = $params[ 'type_id' ] ?? 0;
$card_model = new Card();
$card_condition = array (
[ 'card_id', '=', $card_id ]
);
$card_info = $card_model->getCardInfo($card_condition)[ 'data' ] ?? [];
if (empty($card_info))
return $this->error();
$site_id = $card_info[ 'site_id' ];
$operator_type = $params[ 'operator_type' ] ?? '';
$operator = '';
$operator_name = '';
$member_model = new Member();
switch ( $operator_type ) {
case 'member':
$operator = $params[ 'operator' ] ?? 0;
$member_condition = array (
[ 'member_id', '=', $operator ]
);
$operator_name = $member_model->getMemberInfo($member_condition)[ 'data' ][ 'nickname' ] ?? '';
break;
case 'shop':
$operator_info = $params[ 'operator_data' ] ?? [];
$operator = $operator_info[ 'uid' ] ?? 0;
$operator_name = $operator_info[ 'username' ] ?? '';
break;
case 'system':
$operator = 0;
$operator_name = '系统任务';
break;
}
$data = array (
'card_id' => $card_id,
'site_id' => $site_id,
'type' => $type,
'type_id' => $type_id,
'giftcard_id' => $card_info[ 'giftcard_id' ],
'member_id' => $card_info[ 'member_id' ],
'create_time' => time(),
'operator_type' => $operator_type,
'operator' => $operator,
'operator_name' => $operator_name,
);
$remark = '';
switch ( $type ) {
case 'create'://制卡
$remark = '店铺管理员' . $operator_name . '制成礼品卡';
break;
case 'buy'://购买卡
$init_member_id = $card_info[ 'init_member_id' ];
$member_condition = array (
[ 'member_id', '=', $init_member_id ]
);
$init_member_name = $member_model->getMemberInfo($member_condition)[ 'data' ][ 'nickname' ] ?? '';
$remark = '会员' . $init_member_name . '购买礼品卡';
break;
case 'transfer'://赠送
$member_card_id = $type_id;
$member_card_model = new MemberCard();
$member_card_condition = array (
[ 'member_card_id', '=', $member_card_id ]
);
$member_card_info = $member_card_model->getMemberCardInfo($member_card_condition)[ 'data' ] ?? [];
if (empty($member_card_info)) {
return $this->error();
}
$member_condition = array (
[ 'member_id', '=', $member_card_info[ 'from_member_id' ] ]
);
$from_member_name = $member_model->getMemberInfo($member_condition)[ 'data' ][ 'nickname' ] ?? '';
$member_condition = array (
[ 'member_id', '=', $member_card_info[ 'member_id' ] ]
);
$member_name = $member_model->getMemberInfo($member_condition)[ 'data' ][ 'nickname' ] ?? '';
$remark = '会员' . $from_member_name . '将礼品卡赠送给会员' . $member_name;
break;
case 'use'://使用
$records_id = $type_id;
$card_use_model = new CardUse();
$use_condition = array (
[ 'records_id', '=', $records_id ]
);
$card_use_info = $card_use_model->getCardUseRecordsInfo($use_condition)[ 'data' ] ?? [];
if (empty($card_use_info))
return $this->error();
$use_member_id = $card_use_info[ 'member_id' ];
$member_condition = array (
[ 'member_id', '=', $use_member_id ]
);
$use_member_name = $member_model->getMemberInfo($member_condition)[ 'data' ][ 'nickname' ] ?? '';
$remark = '会员' . $use_member_name . '使用礼品卡购买了';
$card_use_list = $card_use_model->getCardUseRecordsGoodsList($use_condition)[ 'data' ] ?? [];
$card_use_goods_array = [];
foreach ($card_use_list as $v) {
$card_use_goods_array[] = $v[ 'sku_name' ] . $v[ 'use_num' ] . '件';
}
$remark .= implode('、', $card_use_goods_array);
break;
case 'used':
$remark = '礼品卡次数使用完毕,礼品卡已使用';
break;
case 'expire':
$remark = '礼品卡过期';
break;
case 'invalid':
$remark = '店铺管理员' . $operator_name . '将礼品卡作废';
break;
}
$data[ 'remark' ] = $remark;
$data[ 'extend' ] = json_encode($extend ?? []);
model('giftcard_card_log')->add($data);
return $this->success();
}
/**
* 获取礼品卡日志记录信息
* @param $condition
* @param string $field
* @param string $alias
* @param array $join
* @return array
*/
public function getCardLogInfo($condition, $field = '*', $alias = '', $join = [])
{
$info = model('giftcard_card_log')->getInfo($condition, $field, $alias, $join);
return $this->success($info);
}
/**
* 获取礼品卡日志记录列表
* @param array $condition
* @param string $field
* @param string $order
* @param string $alias
* @param array $join
* @param null $limit
* @return array
*/
public function getCardLogList($condition = [], $field = '*', $order = '', $alias = '', $join = [], $limit = null)
{
$list = model('giftcard_card_log')->getList($condition, $field, $order, $alias, $join, '', $limit);
return $this->success($list);
}
/**
* 获取礼品卡日志记录分页列表
* @param array $condition
* @param int $page
* @param int $page_size
* @param string $order
* @param string $field
* @param string $alias
* @param array $join
* @return array
*/
public function getCardLogPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = '', $join = [])
{
$list = model('giftcard_card_log')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
return $this->success($list);
}
}

View File

@@ -0,0 +1,341 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\giftcard\model\card;
use addon\giftcard\model\giftcard\CardStat;
use addon\giftcard\model\membercard\MemberCard;
use app\dict\member_account\AccountDict;
use app\model\BaseModel;
use app\model\member\MemberAccount;
use think\facade\Db;
/**
* 礼品卡工具类
*
* @author Administrator
*
*/
class CardOperation extends BaseModel
{
/**
* 礼品卡使用吧公共函数
* @param $params
* @return array
* @throws \think\db\exception\DbException
*/
public function cardUseOperation($params)
{
$card_id = $params[ 'card_id' ];
$card_goods_list = $params[ 'goods_list' ];
$condition = array (
[ 'card_id', '=', $card_id ]
);
$card_right_type = $params[ 'card_right_type' ];
$card_right_goods_type = $params[ 'card_right_goods_type' ];
$card_right_goods_count = $params[ 'card_right_goods_count' ];
$card_use_count = $params[ 'use_count' ];
$card_right_goods_count -= $card_use_count;
foreach ($card_goods_list as $k => $v) {
$use_num = $v[ 'use_num' ];
$total_num = $v[ 'total_num' ];
$temp_use_num = $v[ 'temp_use_num' ];
if ($card_right_goods_type != 'all') {
if (( $use_num + $temp_use_num ) > $total_num) {
return $this->error([], '使用次数超出可使用次数');
}
} else {
if ($temp_use_num > $card_right_goods_count) {
return $this->error([], '使用次数超出可使用次数');
}
}
$item_condition = array (
[ 'id', '=', $v[ 'id' ] ]
);
model('giftcard_card_goods')->setInc($item_condition, 'use_num', $temp_use_num);
model('giftcard_card')->setInc($condition, 'use_count', $temp_use_num);//卡整体使用次数
$card_right_goods_count -= $temp_use_num;
}
//写入使用记录
$card_use_records_model = new CardUse();
$result = $card_use_records_model->addCardUseRecords($params);
$records_id = $result[ 'data' ];
//核验礼品卡还是否存在可使用次数
if ($card_right_goods_type != 'all') {
$surplus_num = model('giftcard_card_goods')->getSum($condition, Db::raw('(total_num - use_num)'));
} else {
$card_model = new Card();
$card_info = $card_model->getCardInfo($condition)[ 'data' ] ?? [];
$surplus_num = $card_info[ 'card_right_goods_count' ] - $card_info[ 'use_count' ];
}
( new CardLog() )->add([
'card_id' => $card_id,
'type_id' => $records_id,
'type' => 'use',
'operator_type' => 'member',//todo 暂时是确定的
'operator' => $params[ 'member_id' ],
]);
if ($surplus_num == 0) {
//使用
$this->used($params);
}
return $this->success();
}
/**
* 储值礼品卡使用
* @param $params
*/
public function cardUse($params)
{
$member_id = $params[ 'member_id' ] ?? 0;
$site_id = $params[ 'site_id' ] ?? 0;
$member_card_id = $params[ 'member_card_id' ];
$order_id = $params[ 'order_id' ] ?? 0;
$member_card_model = new MemberCard();
$member_card_condition = array (
[ 'member_card_id', '=', $member_card_id ],
[ 'member_id', '=', $member_id ],
[ 'is_transfer', '=', 0 ]
);
if ($site_id > 0) {
$member_card_condition[] = [ 'site_id', '=', $site_id ];
}
$member_card_info = $member_card_model->getMemberCardInfo($member_card_condition)[ 'data' ] ?? [];
if (empty($member_card_info))
return $this->error([], '礼品卡不存在或已转赠');
$member_id = $member_card_info[ 'member_id' ];
$card_id = $member_card_info[ 'card_id' ];
$card_model = new Card();
$card_condition = array (
[ 'card_id', '=', $card_id ],
);
$card_info = $card_model->getCardInfo($card_condition)[ 'data' ] ?? [];
if (empty($card_info))
return $this->error([], '礼品卡不存在或已转赠');
$card_info[ 'use_order_id' ] = $order_id;
$status = $card_info[ 'status' ];
//todo 加入队列概念,队列
if ($status != 'to_use')
return $this->error([], '当前礼品卡不可以使用');
$card_right_type = $card_info[ 'card_right_type' ];
if ($card_right_type == 'balance') {
$card_goods_list = $card_model->getCardGoodsList($card_condition)[ 'data' ] ?? [];
foreach ($card_goods_list as $k => $v) {
$card_goods_list[ $k ][ 'temp_use_num' ] = $v[ 'total_num' ];
}
} else {
$card_goods_array = $params[ 'card_goods_json' ];//[{'card_goods_id':15,'order_goods_id':1, 'num': 2}]
if (empty($card_goods_array))
return $this->error([], '礼品卡使用参数有误');
$card_goods_ids = array_column($card_goods_array, 'card_goods_id');
$card_goods_column = array_column($card_goods_array, null, 'card_goods_id');
$temp_card_condition = $card_condition;
$temp_card_condition[] = [ 'id', 'in', $card_goods_ids ];
$card_goods_list = $card_model->getCardGoodsList($temp_card_condition)[ 'data' ] ?? [];
foreach ($card_goods_list as $k => $v) {
$item_column = $card_goods_column[ $v[ 'id' ] ];
$card_goods_list[ $k ][ 'temp_use_num' ] = $item_column[ 'num' ];
$card_goods_list[ $k ][ 'use_order_goods_id' ] = $item_column[ 'order_goods_id' ];
}
}
model('giftcard_card')->startTrans();
try {
$user_params = array_merge($member_card_info, $card_info);
$user_params[ 'goods_list' ] = $card_goods_list;
$result = $this->cardUseOperation($user_params);
if ($result[ 'code' ] < 0) {
model('giftcard_card')->rollback();
return $result;
}
$result = $this->balanceCardUse($user_params);
if ($result[ 'code' ] < 0) {
model('giftcard_card')->rollback();
return $result;
}
model('giftcard_card')->commit();
return $this->success();
} catch (\Exception $e) {
model('giftcard_card')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 礼品卡兑换
* @param $params
* @return array
*/
public function balanceCardUse($params)
{
$card_goods_list = $params[ 'goods_list' ];
//将余额给会员
$member_account_model = new MemberAccount();
foreach ($card_goods_list as $k => $v) {
$item_balance = $v[ 'total_balance' ];//总的余额
if ($item_balance > 0) {
$member_account_model->addMemberAccount($params[ 'site_id' ], $params[ 'member_id' ], AccountDict::balance, $item_balance, 'giftcard', '礼品卡兑换' . $item_balance, '储值礼品卡兑换');
}
$card_goods_list[ $k ][ 'temp_use_num' ] = $v[ 'total_num' ];
}
return $this->success();
}
public function cardExpire($params)
{
$site_id = $params[ 'site_id' ] ?? 0;
$card_ids = $params[ 'card_ids' ];
$condition = array (
[ 'card_id', 'in', $card_ids ],
[ 'status', 'in', [ 'to_activate', 'to_use' ] ]//暂时认为只有待使用和待激活的卡项会过期
);
if ($site_id > 0) {
$condition[] = [ 'site_id', '=', $site_id ];
}
$card_model = new Card();
$list = $card_model->getCardList($condition)[ 'data' ] ?? [];
if (empty($list)){
return $this->success();
}
$data = array (
'status' => 'expire'
);
model('giftcard_card')->update($data, $condition);
$this->cardExpireOperation($list);
return $this->success();
}
/**
* 卡过期后续任务
* @param $list
* @return array
*/
public function cardExpireOperation($list)
{
$card_log_model = new CardLog();
foreach ($list as $k => $v) {
//将贺卡全部作废
$card_blessing_condition = array (
[ 'card_id', '=', $v[ 'card_id' ] ],
[ 'status', '=', 0 ]
);
model('giftcard_card_blessing')->update([ 'status' => 1 ], $card_blessing_condition);
$card_log_model->add([
'card_id' => $v[ 'card_id' ],
'type' => 'expire',
'operator_type' => 'system',//todo 暂时是确定的
]);
}
return $this->success();
}
/**
* 卡使用
* @param $params
*/
public function used($params)
{
$condition = array (
[ 'card_id', '=', $params[ 'card_id' ] ]
);
$data = array (
'status' => 'used',
'use_time' => time(),
);
model('giftcard_card')->update($data, $condition);
//数据统计
( new CardStat() )->stat(array_merge($params, [ 'stat_type' => 'use' ]));
( new CardLog() )->add([
'card_id' => $params[ 'card_id' ],
'type' => 'used',
'operator_type' => 'member',//todo 暂时是确定的
'operator' => $params[ 'member_id' ],
]);
return $this->success();
}
/**
* 批量作废
* @param $params
* @return array
*/
public function cardInvalid($params)
{
$site_id = $params[ 'site_id' ] ?? 0;
$card_import_id = $params[ 'card_import_id' ] ?? 0;
$card_id = $params[ 'card_id' ] ?? 0;
$condition = [
[ 'status', '=', 'to_activate' ]
];
if ($site_id > 0) {
$condition[] = [ 'site_id', '=', $site_id ];
}
if ($card_import_id > 0) {
$condition[] = [ 'card_import_id', '=', $card_import_id ];
}
if ($card_id > 0) {
$condition[] = [ 'card_id', '=', $card_id ];
}
$data = array (
'status' => 'invalid',
'invalid_time' => time(),
);
$card_model = new Card();
$list = $card_model->getCardList($condition)[ 'data' ] ?? [];
if (empty($list))
return $this->error();
model('giftcard_card')->update($data, $condition);
$params[ 'list' ] = $list;
//作废后的操作
$this->cardInvalidOperation($params);
return $this->success();
}
/**
* 作废后操作
* @param $params
* @return array
*/
public function cardInvalidOperation($params)
{
$list = $params[ 'list' ];
$card_log_model = new CardLog();
foreach ($list as $k => $v) {
//添加日志
$card_log_model->add([
'card_id' => $v[ 'card_id' ],
'type' => 'used',
'operator_type' => 'member',//todo 暂时是确定的
'operator_data' => $params[ 'operator_data' ],
]);
}
( new CardStat() )->stat(array_merge($params, [ 'stat_type' => 'invalid', 'num' => count($list) ]));
return $this->success();
}
}

View File

@@ -0,0 +1,165 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\giftcard\model\card;
use app\model\BaseModel;
/**
* 礼品卡使用记录工具类
* Class CardUse
* @package addon\giftcard\model\card
*/
class CardUse extends BaseModel
{
/**
* 生成礼品卡记录
* @param $params
* @return array
*/
public function addCardUseRecords($params)
{
$member_id = $params[ 'member_id' ] ?? 0;
$giftcard_id = $params[ 'giftcard_id' ];
$card_right_type = $params[ 'card_right_type' ];
$use_order_id = $params[ 'use_order_id' ] ?? 0;
$data = array (
'site_id' => $params[ 'site_id' ],
'card_id' => $params[ 'card_id' ],
'member_card_id' => $params[ 'member_card_id' ],
'giftcard_id' => $giftcard_id,
'member_id' => $member_id,
'use_time' => time(),
'card_right_type' => $card_right_type,
'order_id' => $use_order_id
);
$records_id = model('giftcard_card_use_records')->add($data);
$goods_list = $params[ 'goods_list' ];
foreach ($goods_list as $k => $v) {
$v[ 'order_id' ] = $use_order_id;
$v[ 'records_id' ] = $records_id;
$this->addCardUseRecordsGoods($v);
}
return $this->success($records_id);
}
/**
* 礼品卡项使用记录
* @param $params
* @return array
*/
public function addCardUseRecordsGoods($params)
{
$data = array (
'site_id' => $params[ 'site_id' ],
'records_id' => $params[ 'records_id' ],//使用记录id
'card_goods_id' => $params[ 'id' ],
'sku_id' => $params[ 'sku_id' ] ?? 0,
'sku_name' => $params[ 'sku_name' ] ?? '',
'sku_image' => $params[ 'sku_image' ] ?? '',
'sku_no' => $params[ 'sku_no' ] ?? '',
'goods_id' => $params[ 'goods_id' ] ?? 0,
'goods_name' => $params[ 'goods_name' ] ?? '',
'balance' => $params[ 'balance' ] ?? 0,//储值余额
'use_num' => $params[ 'temp_use_num' ] ?? '',
'order_id' => $params[ 'order_id' ] ?? 0,
'order_goods_id' => $params[ 'use_order_goods_id' ] ?? 0,
);
model('giftcard_card_use_records_goods')->add($data);
return $this->success();
}
/**
* 获取礼品卡使用记录信息
* @param $condition
* @param string $field
* @return array
*/
public function getCardUseRecordsInfo($condition, $field = '*')
{
$info = model('giftcard_card_use_records')->getInfo($condition, $field);
return $this->success($info);
}
/**
* 获取礼品卡使用记录列表
* @param array $condition
* @param string $field
* @param string $order
* @param null $limit
* @return array
*/
public function getCardUseRecordsList($condition = [], $field = '*', $order = '', $limit = null)
{
$list = model('giftcard_card_use_records')->getList($condition, $field, $order, '', '', '', $limit);
return $this->success($list);
}
/**
* 获取礼品卡使用记录分页列表
* @param array $condition
* @param int $page
* @param int $page_size
* @param string $order
* @param string $field
* @return array
*/
public function getCardUseRecordsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
{
$list = model('giftcard_card_use_records')->pageList($condition, $field, $order, $page, $page_size);
return $this->success($list);
}
/**
* 获取礼品卡使用记录项信息
* @param $condition
* @param string $field
* @return array
*/
public function getCardUseRecordsGoodsInfo($condition, $field = '*')
{
$info = model('giftcard_card_use_records_goods')->getInfo($condition, $field);
return $this->success($info);
}
/**
* 获取礼品卡使用记录项列表
* @param array $condition
* @param string $field
* @param string $order
* @param null $limit
* @return array
*/
public function getCardUseRecordsGoodsList($condition = [], $field = '*', $order = '', $limit = null)
{
$list = model('giftcard_card_use_records_goods')->getList($condition, $field, $order, '', '', '', $limit);
return $this->success($list);
}
/**
* 获取礼品卡使用记录项分页列表
* @param array $condition
* @param int $page
* @param int $page_size
* @param string $order
* @param string $field
* @return array
*/
public function getCardUseRecordsGoodsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
{
$list = model('giftcard_card_use_records_goods')->pageList($condition, $field, $order, $page, $page_size);
return $this->success($list);
}
}

View File

@@ -0,0 +1,470 @@
<?php
namespace addon\giftcard\model\card;
use addon\giftcard\model\giftcard\CardStat;
use addon\giftcard\model\giftcard\GiftCard;
use addon\giftcard\model\membercard\MemberCard;
use think\facade\Cache;
/**
* 实体卡(线下)
* Class GiftCardRecords
* @package addon\giftcard\model\records
*/
class RealCard extends Card
{
public function addCard($params)
{
$source = $params[ 'source' ] ?? '';
$card_right_goods_type = $params[ 'card_right_goods_type' ];
$card_right_goods_count = $params[ 'card_right_goods_count' ];
$insert_data = array (
'card_name' => $params[ 'card_name' ] ?? '',
'card_cover' => $params[ 'card_cover' ] ?? '',
'card_right_goods_type' => $card_right_goods_type,
'card_right_goods_count' => $card_right_goods_count,
'card_cdk' => $params[ 'card_cdk' ],
'status' => 'to_activate',
'card_import_id' => $params[ 'import_id' ]
);
$params[ 'card_type' ] = 'real';
$params[ 'insert_data' ] = $insert_data;
//批量生成卡号
$giftcard_model = new Giftcard();
$card_no_res = $giftcard_model->createCardNo($params['giftcard_id'], 1);
if($card_no_res['code'] < 0) return $card_no_res;
$card_no = $card_no_res['data'][0];
$params[ 'card_no' ] = $card_no;
$result = $this->addCardItem($params);
if ($result[ 'code' ] >= 0) {
$card_id = $result[ 'data' ];
( new CardLog() )->add([
'card_id' => $card_id,
'type' => 'create',
'operator_type' => 'shop',//todo 暂时是确定的
'operator_data' => $params[ 'operator_data' ],
'type_id' => $params[ 'import_id' ]
]);
}
return $result;
}
/**
* 制卡
* @param $params
* @return array
*/
public function cdkLog($params)
{
set_time_limit(0);
ini_set('memory_limit', '512M');
$import_id = $params[ 'import_id' ];
$site_id = $params[ 'site_id' ] ?? 0;
$card_import_model = new CardImport();
$import_condition = array (
[ 'import_id', '=', $import_id ],
);
if ($site_id > 0) {
$import_condition[] = [ 'site_id', '=', $site_id ];
}
$import_info = $card_import_model->getCardImportInfo($import_condition)[ 'data' ] ?? [];
if (empty($import_info))
return $this->error();
$type = $import_info[ 'type' ];
$giftcard_id = $import_info[ 'giftcard_id' ];
$card_type = $import_info[ 'card_type' ];
$giftcard_model = new GiftCard();
$condition = [ [ 'giftcard_id', '=', $giftcard_id ] ];
$info = $giftcard_model->getGiftcardInfo($condition)[ 'data' ] ?? [];
if (empty($info))
return $this->error();
$info[ 'operator_data' ] = $params[ 'operator_data' ];
if ($card_type != 'real') {
return $this->error('', '该礼品不支持制卡');
}
if (empty($type) && !in_array($type, [ 'auto', 'manual', 'import' ]))
return $this->error();
$data = array (
'site_id' => $info[ 'site_id' ],
'card_type' => $info[ 'card_type' ],
'giftcard_id' => $info[ 'giftcard_id' ],
'create_time' => time(),
'card_right_type' => $info[ 'card_right_type' ],
'valid_time' => $this->getValidityTime($info),
'balance' => $info[ 'balance' ] ?? 0,
'card_right_goods_type' => $info[ 'card_right_goods_type' ] ?? '',
'card_right_goods_count' => $info[ 'card_right_goods_count' ] ?? '',
'card_name' => $info[ 'card_name' ] ?? '',
'card_cover' => $info[ 'card_cover' ] ?? '',
'status' => 'to_activate',
'card_import_id' => $import_id
);
$info[ 'card_data' ] = $data;
$info[ 'import_id' ] = $import_id;
switch ( $type ) {
case 'auto':
$info[ 'num' ] = $import_info[ 'total_count' ] ?? 0;
$result = $this->createCdk($info);
break;
case 'manual':
$card_cdk = $params[ 'card_cdk' ];
$info[ 'card_cdk' ] = $card_cdk;
$result = $this->addCdk($info);
break;
case 'import':
$info[ 'num' ] = $import_info[ 'total_count' ];
$result = $this->importCdk($info);
break;
}
//制卡统计
//( new CardStat() )->stat([ 'stat_type' => 'create', 'giftcard_id' => $giftcard_id, 'num' => $result[ 'data' ][ 'success_count' ] ?? 0 ]);
return $result;
}
public function addCdk($params)
{
$result = $this->addCard($params);
$total_count = 1;
if ($result[ 'code' ] < 0) {
//生成卡密失败后,继续还是退出
$fail_count = 1;
$success_count = 0;
$error = $result[ 'message' ];
} else {
$fail_count = 0;
$success_count = 1;
}
$import_id = $params[ 'import_id' ] ?? 0;
if ($import_id > 0) {
model('giftcard_card_import')->setInc([ [ 'import_id', '=', $import_id ] ], 'imported_count', 1);
$card_import_model = new CardImport();
$card_import_model->update([
'import_time' => time(),
// 'total_count' => $total_count,
'fail_count' => $fail_count,
'success_count' => $success_count,
'card_cdk' => $params[ 'card_cdk' ],
'error' => $error ?? ''
], [ [ 'import_id', '=', $import_id ] ]);
}
return $this->success([ 'total_count' => $total_count, 'fail_count' => $fail_count, 'success_count' => $success_count, 'error' => $error ?? '' ]);
}
public function createCdk($params)
{
$num = $params[ 'num' ];//生成卡密数量(一般上限一次1000个)
$num_dict = '0123456789';
$latter_dict = 'abcdefghijklmnopqrstuvwxyz';
$big_latter_dict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$cdk_length = $params[ 'cdk_length' ];
$card_prefix = $params[ 'card_prefix' ];
$card_suffix = $params[ 'card_suffix' ];
// $cdk_prefix_length = strlen($cdk_prefix);
// $cdk_suffix_length = strlen($cdk_suffix);
// $length = $cdk_length - $cdk_prefix_length - $cdk_suffix_length;
$length = $cdk_length;
$cdk_type = $params[ 'cdk_type' ];
$dict = '';
if (strstr($cdk_type, '0-9')) {
$dict .= $num_dict;
}
if (strstr($cdk_type, 'a-z')) {
$dict .= $latter_dict;
}
if (strstr($cdk_type, 'A-Z')) {
$dict .= $big_latter_dict;
}
$dict_len = strlen($dict) - 1;
$start_num = 1;
$total_count = $num;
$fail_count = 0;
$success_count = 0;
$import_id = $params[ 'import_id' ] ?? 0;
$import_info = model('giftcard_card_import')->getInfo([ [ 'import_id', '=', $import_id ] ], "*");
Cache::set("card_import_log_" . $import_id, $import_info);
$common_data = $params[ 'card_data' ];
//批量生成卡号
$giftcard_model = new Giftcard();
$card_no_res = $giftcard_model->createCardNo($params['giftcard_id'], $num);
if($card_no_res['code'] < 0) return $card_no_res;
$card_no_arr = $card_no_res['data'];
$insert_data = array ();
while ($start_num <= $num) {
$randstr = '';
for ($i = 0; $i < $length; $i++) {
$temp_num = mt_rand(0, $dict_len);
$randstr .= $dict[ $temp_num ];
}
$card_cdk = $randstr;
$success_count++;
$item_data = $common_data;
$item_data[ 'card_no' ] = array_shift($card_no_arr);
$item_data[ 'card_cdk' ] = $card_cdk;
$insert_data[] = $item_data;
if (( $start_num % 100 ) == 0) {
model('giftcard_card')->addList($insert_data);
model('giftcard_card_import')->update([ 'imported_count' => $start_num ], [ [ 'import_id', '=', $import_id ] ]);
$insert_data = [];
} else if ($start_num >= $num) {
model('giftcard_card')->addList($insert_data);
model('giftcard_card_import')->update([ 'imported_count' => $start_num ], [ [ 'import_id', '=', $import_id ] ]);
$insert_data = [];
}
$start_num++;
}
model('giftcard_card_import')->update([ 'imported_count' => $import_info[ 'total_count' ] ], [ [ 'import_id', '=', $import_id ] ]);
if ($import_id > 0) {
$card_import_model = new CardImport();
$card_import_model->update([
'cdk_length' => $cdk_length,
'card_prefix' => $card_prefix,
'card_suffix' => $card_suffix,
'cdk_type' => $cdk_type,
'import_time' => time(),
// 'total_count' => $total_count,
'fail_count' => $fail_count,
'success_count' => $success_count,
], [ [ 'import_id', '=', $import_id ] ]);
}
return $this->success([ 'total_count' => $total_count, 'fail_count' => $fail_count, 'success_count' => $success_count ]);
}
public function importCdk($params)
{
//之后可以配合匿名函数封装公共函数
$total_count = 0;
$fail_count = 0;
$success_count = 0;
$import_id = $params[ 'import_id' ] ?? 0;
$file_path = 'upload/giftcard/giftcard_card_import' . $import_id . '.csv';
$import_info = model('giftcard_card_import')->getInfo([ [ 'import_id', '=', $import_id ] ], "*");
Cache::set("card_import_log_" . $import_id, $import_info);
$common_data = $params[ 'card_data' ];
$card_no_array = [];
foreach (getCsvRow($file_path) as $row) {
if (!empty($row)) {
$total_count++;
if ($total_count > 1) {
$card_cdk = $row[ 1 ] ?? '';
$card_no = $row[ 0 ] ?? '';
$card_cdk = trim($card_cdk);
$card_no = trim($card_no);
if (!empty($card_no) && !empty($card_cdk)) {
$params[ 'card_cdk' ] = $card_cdk;
$params[ 'card_no' ] = $card_no;
// $result = $this->addCard($params);
// if ($result['code'] < 0) {
// //生成卡密失败后,继续还是退出
// $fail_count++;
// } else {
// $success_count++;
// }
$item_data = $common_data;
$item_data[ 'card_no' ] = $card_no;
$card_no_array[] = $card_no;
$item_data[ 'card_cdk' ] = $card_cdk;
$insert_data[] = $item_data;
if (( $total_count % 100 ) == 0) {
$column = model('giftcard_card')->getColumn([ [ 'card_no', 'in', $card_no_array ] ], 'card_no');
$column = array_unique($column);
foreach ($insert_data as $k => $v) {
if (in_array($v[ 'card_no' ], $column)) {
unset($insert_data[ $k ]);
$fail_count++;
}
}
model('giftcard_card')->addList($insert_data);
model('giftcard_card_import')->update([ 'imported_count' => $total_count ], [ [ 'import_id', '=', $import_id ] ]);
$success_count += count($insert_data);
$insert_data = [];
$card_no_array = [];
}
} else {
$fail_count++;
}
}
}
}
$column = model('giftcard_card')->getColumn([ [ 'card_no', 'in', $card_no_array ] ], 'card_no');
$column = array_unique($column);
foreach ($insert_data as $k => $v) {
if (in_array($v[ 'card_no' ], $column)) {
unset($insert_data[ $k ]);
$fail_count++;
}
}
$success_count += count($insert_data);
//最后一次补充提交
model('giftcard_card')->addList($insert_data);
model('giftcard_card_import')->update([ 'imported_count' => $import_info[ 'total_count' ] ], [ [ 'import_id', '=', $import_id ] ]);
if ($import_id > 0) {
$original_name = Cache::get('giftcard/giftcard_card_import_name' . $import_id);//文件原名
$card_import_model = new CardImport();
$card_import_model->update([
'import_time' => time(),
// 'total_count' => $total_count,
'fail_count' => $fail_count,
'success_count' => $success_count,
'file_name' => $original_name
], [ [ 'import_id', '=', $import_id ] ]);
}
return $this->success([ 'total_count' => $total_count, 'fail_count' => $fail_count, 'success_count' => $success_count ]);
}
/**
* 会员激活卡密
* @param $params
* @return array
*/
public function memberCardActivate($params)
{
$card_no = $params[ 'card_no' ];
$card_cdk = $params[ 'card_cdk' ];
$site_id = $params[ 'site_id' ] ?? 0;
$member_id = $params[ 'member_id' ];
$card_condition = array (
[ 'gc.card_no', '=', $card_no ],
[ 'gc.card_cdk', '=', $card_cdk ],
[ 'gc.member_id', '=', 0 ],
[ 'g.is_delete', '=', 0 ],
);
if ($site_id > 0) {
$card_condition[] = [ 'gc.site_id', '=', $site_id ];
}
$card_info = $this->getCardInfo($card_condition, 'gc.*,g.is_delete,g.status as giftcard_status', 'gc', [
[ 'giftcard g', 'gc.giftcard_id = g.giftcard_id', 'inner' ]
])[ 'data' ] ?? [];
if (empty($card_info))
return $this->error([], '当前卡密无效或已被激活');
if (empty($card_info[ 'giftcard_status' ]))
return $this->error([], '当前礼品卡已下架');
$card_id = $card_info[ 'card_id' ];
$result = $this->activate([ 'card_id' => $card_id, 'member_id' => $member_id, 'site_id' => $site_id ]);
return $result;
}
/**
* 激活卡密
* @param $params
*/
public function activate($params)
{
$site_id = $params[ 'site_id' ] ?? 0;
$member_id = $params[ 'member_id' ];
$card_id = $params[ 'card_id' ];
$card_condition = array (
[ 'card_id', '=', $card_id ],
[ 'member_id', '=', 0 ]
);
if ($site_id > 0) {
$card_condition[] = [ 'site_id', '=', $site_id ];
}
$card_model = new Card();
$card_info = $card_model->getCardInfo($card_condition)[ 'data' ] ?? [];
if (empty($card_info))
return $this->error();
$giftcard_model = new GiftCard();
$giftcard_id = $card_info[ 'giftcard_id' ];
$condition = [
[ 'giftcard_id', '=', $giftcard_id ]
];
$info = $giftcard_model->getGiftcardInfo($condition)[ 'data' ] ?? [];
if (empty($info))
return $this->error();
$data = array (
'card_type' => $info[ 'card_type' ],
'card_right_type' => $info[ 'card_right_type' ],
'valid_time' => $this->getValidityTime($info),
'balance' => $info[ 'balance' ] ?? 0,
'card_right_goods_type' => $info[ 'card_right_goods_type' ] ?? '',
'card_right_goods_count' => $info[ 'card_right_goods_count' ] ?? '',
'card_name' => $info[ 'card_name' ] ?? '',
'card_cover' => $info[ 'card_cover' ] ?? ''
);
model('giftcard_card')->update($data, $card_condition);
//需要更新卡项数据的
if ($info[ 'card_right_type' ] == 'goods') {
$goods_list = $giftcard_model->getGiftcardGoodsList([ [ 'giftcard_id', '=', $giftcard_id ] ], 'cg.*,gs.sku_name,gs.sku_image,gs.goods_name,gs.sku_no,gs.price', '', 'cg', [
[ 'goods_sku gs', 'cg.sku_id=gs.sku_id', 'inner' ]
])[ 'data' ] ?? [];
foreach ($goods_list as $k => $v) {
if ($info[ 'card_right_goods_type' ] == 'all') {
$goods_list[ $k ][ 'num' ] = 0;
} else {
$goods_list[ $k ][ 'num' ] = $v[ 'goods_num' ];
}
}
} else {
$goods_list = array (
[
'site_id' => $info[ 'site_id' ],
'giftcard_id' => $giftcard_id,
'sku_name' => $info[ 'card_name' ],
'sku_image' => $info[ 'card_cover' ],
'goods_name' => $info[ 'card_name' ],
'balance' => $info[ 'balance' ],//储值余额
'total_balance' => $info[ 'balance' ],
'total_num' => 1,//购买数量
]
);
}
foreach ($goods_list as $k => $v) {
$v[ 'card_id' ] = $card_id;
$v[ 'giftcard_id' ] = $giftcard_id;
$v[ 'card_right_type' ] = $info[ 'card_right_type' ];
$this->addCardItemGoods($v);
}
$data = array (
'status' => 'to_use',
'init_member_id' => $member_id,
'member_id' => $member_id,
'activate_time' => time()
);
model('giftcard_card')->update($data, $card_condition);
//生成会员所属记录
$member_card_model = new MemberCard();
$card_params = array (
'site_id' => $site_id,
'form_member_id' => 0,
'member_id' => $member_id,
'card_id' => $card_id,
'source' => '',
);
$result = $member_card_model->addMemberCard($card_params);
//数据统计
( new CardStat() )->stat(array_merge($params, [ 'stat_type' => 'activate' ]));
return $result;
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace addon\giftcard\model\card;
use addon\giftcard\model\membercard\MemberCard;
/**
* 电子卡(线上)
* Class GiftCardRecords
* @package addon\giftcard\model\records
*/
class VirtualCard extends Card
{
public function addCard($params)
{
$source = $params[ 'source' ];
$insert_data = array (
'order_id' => $params[ 'order_id' ],
'source' => $source,
'is_allow_transfer' => $params[ 'is_allow_transfer' ],
'init_member_id' => $params[ 'member_id' ],
'card_name' => $params[ 'order_name' ] ?? '',
'card_cover' => $params[ 'card_cover' ] ?? '',
'status' => 'to_use',
);
$params[ 'insert_data' ] = $insert_data;
$params[ 'card_type' ] = 'virtual';
$result = $this->addCardItem($params);
$card_id = $result[ 'data' ];
//生成会员所属记录
$member_card_model = new MemberCard();
$card_params = array (
'site_id' => $params[ 'site_id' ],
'form_member_id' => 0,
'member_id' => $params[ 'member_id' ],
'card_id' => $card_id,
'source' => $source,
);
$member_card_model->addMemberCard($card_params);
( new CardLog() )->add([
'card_id' => $card_id,
'type' => 'buy',
'operator_type' => 'member',//todo 暂时是确定的
'operator' => $params[ 'member_id' ],
'type_id' => $params[ 'order_id' ]
]);
return $result;
}
}