448 lines
14 KiB
PHP
448 lines
14 KiB
PHP
<?php
|
|
|
|
|
|
namespace Yunshop\NewMemberPrize\admin\controllers;
|
|
|
|
|
|
use app\common\components\BaseController;
|
|
use app\common\models\Coupon;
|
|
use app\common\models\Goods;
|
|
use app\common\models\MemberCoupon;
|
|
use app\common\services\ExportService;
|
|
use Yunshop\Love\Common\Services\SetService;
|
|
use Yunshop\MemberTags\Common\models\MemberTagsModel;
|
|
use Yunshop\NewMemberPrize\common\models\ActivityModel;
|
|
use Yunshop\NewMemberPrize\common\models\CouponModel;
|
|
use Yunshop\NewMemberPrize\common\models\GoodsModel;
|
|
use Yunshop\NewMemberPrize\common\models\PrizeModel;
|
|
use Yunshop\NewMemberPrize\common\models\RecordModel;
|
|
|
|
class ActivityController extends BaseController
|
|
{
|
|
public function index()
|
|
{
|
|
return view('Yunshop\NewMemberPrize::admin.index');
|
|
}
|
|
|
|
public function search()
|
|
{
|
|
$search = request()->search;
|
|
|
|
$list = ActivityModel::getActivity($search)->paginate(20)->toArray();
|
|
|
|
$list['data'] = collect($list['data'])->map(function ($item) {
|
|
$prize_list = PrizeModel::uniacid()->select('id', 'name')->whereIn('id', $item['prize_id'])->get();
|
|
$item = collect($item)->put('prize_list', $prize_list);
|
|
|
|
return $item->toArray();
|
|
});
|
|
|
|
$list['data'] = $list['data']->toArray();
|
|
|
|
return $this->successJson('ok', [
|
|
'list' => $list,
|
|
]);
|
|
}
|
|
|
|
public function addActivity()
|
|
{
|
|
$postData = request()->post_data;
|
|
if ($postData['love_use']) {
|
|
$postData['love'] = $postData['love_use'];
|
|
}
|
|
if ($postData['love_froze']) {
|
|
$postData['love'] = $postData['love_froze'];
|
|
}
|
|
unset($postData['love_froze']);
|
|
unset($postData['love_use']);
|
|
if ($postData) {
|
|
$activitys = ActivityModel::uniacid()
|
|
->where('end_time', '>=', $postData['start_time'])
|
|
->where('start_time', '<=', $postData['end_time'])
|
|
->get()
|
|
->toArray();
|
|
|
|
$error = false;
|
|
|
|
foreach ($activitys as $activity) {
|
|
if (
|
|
($activity['receive_type'] == 1 || $activity['receive_type'] == 2 || $activity['receive_type'] == 3) &&
|
|
($postData['receive_type'] == 1 || $postData['receive_type'] == 2 || $postData['receive_type'] == 3)
|
|
) {
|
|
$error = true;
|
|
}
|
|
if ($activity['receive_type'] == $postData['receive_type']) {
|
|
$error = true;
|
|
}
|
|
}
|
|
|
|
if ($error == true) {
|
|
return $this->errorJson('创建活动条件有误');
|
|
}
|
|
|
|
$model = new ActivityModel();
|
|
$model->fill($postData);
|
|
$model->uniacid = $this->getUniacid();
|
|
|
|
if ($model->save()) {
|
|
return $this->successJson('添加成功');
|
|
}
|
|
}
|
|
|
|
$love_status = 0;
|
|
|
|
$love_name = '爱心值';
|
|
if (app('plugins')->isEnabled('love')) {
|
|
$love_name = SetService::getLoveName();
|
|
$love_status = 1;
|
|
}
|
|
|
|
return view('Yunshop\NewMemberPrize::admin.add', [
|
|
'love_status' => $love_status,
|
|
'love_name' => $love_name,
|
|
]);
|
|
}
|
|
|
|
public function editActivity()
|
|
{
|
|
$id = request()->id;
|
|
$postData = request()->post_data;
|
|
|
|
if ($postData) {
|
|
$activitys = ActivityModel::uniacid()
|
|
->where('end_time', '>=', $postData['start_time'])
|
|
->where('start_time', '<=', $postData['end_time'])
|
|
->where('id', '!=', $id)
|
|
->get()
|
|
->toArray();
|
|
|
|
$error = false;
|
|
|
|
foreach ($activitys as $activity) {
|
|
if (
|
|
($activity['receive_type'] == 1 || $activity['receive_type'] == 2 || $activity['receive_type'] == 3) &&
|
|
($postData['receive_type'] == 1 || $postData['receive_type'] == 2 || $postData['receive_type'] == 3)
|
|
) {
|
|
$error = true;
|
|
}
|
|
if ($activity['receive_type'] == $postData['receive_type']) {
|
|
$error = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($error == true) {
|
|
return $this->errorJson('编辑活动条件有误');
|
|
}
|
|
|
|
$model = ActivityModel::uniacid()->find($id);
|
|
$model->fill($postData);
|
|
|
|
if ($model->save()) {
|
|
return $this->successJson('编辑成功');
|
|
}
|
|
}
|
|
|
|
$love_status = 0;
|
|
|
|
$love_name = '爱心值';
|
|
if (app('plugins')->isEnabled('love')) {
|
|
$love_name = SetService::getLoveName();
|
|
$love_status = 1;
|
|
}
|
|
|
|
$activity = ActivityModel::uniacid()->find($id)->toArray();
|
|
$goods_id = $activity['receive_content']['goods_id'];
|
|
$goods = Goods::select(['id', 'title', 'thumb'])->whereIn('id', $goods_id)->get();
|
|
foreach ($goods as &$good) {
|
|
$good['thumb'] = yz_tomedia($good['thumb']);
|
|
}
|
|
|
|
$tags = [];
|
|
if($activity['receive_content']['tags'] && app('plugins')->isEnabled('member-tags')){
|
|
foreach ($activity['receive_content']['tags'] as $v){
|
|
$finder = MemberTagsModel::find($v['id']);
|
|
if($finder) $tags[] = ['id'=>$finder->id,'title'=>$finder->title];
|
|
}
|
|
}
|
|
$activity['diy_pic'] = '';
|
|
if($activity['style_pic']) $activity['diy_pic'] = yz_tomedia($activity['style_pic']);
|
|
|
|
return view('Yunshop\NewMemberPrize::admin.edit', [
|
|
'love_status' => $love_status,
|
|
'love_name' => $love_name,
|
|
'activity' => json_encode($activity),
|
|
'goods' => json_encode($goods),
|
|
'tags' => json_encode($tags),
|
|
]);
|
|
}
|
|
|
|
public function delActivity()
|
|
{
|
|
$id = request()->id;
|
|
|
|
if (!$id) {
|
|
return $this->errorJson('请传入正常参数');
|
|
}
|
|
|
|
$model = ActivityModel::uniacid()->find($id);
|
|
|
|
if (!$model) {
|
|
return $this->errorJson('活动不存在');
|
|
}
|
|
|
|
if ($model->delete()) {
|
|
return $this->successJson('删除成功');
|
|
}
|
|
}
|
|
|
|
public function getPrizeList()
|
|
{
|
|
$prize_id = request()->prize_id;
|
|
|
|
if (!$prize_id) {
|
|
return $this->errorJson('请传入正常参数');
|
|
}
|
|
|
|
$prizeList = PrizeModel::uniacid()->with(['hasOneCoupon' => function ($q) {
|
|
$q->select(['id', 'name', 'total']);
|
|
}])->whereIn('id', $prize_id)->get();
|
|
|
|
return $this->successJson('ok', [
|
|
'prize_list' => $prizeList,
|
|
]);
|
|
}
|
|
|
|
public function addPrize()
|
|
{
|
|
$postData = request()->post_data;
|
|
$id = request()->id;
|
|
|
|
if ($postData) {
|
|
$model = new PrizeModel();
|
|
if ($postData['love_use']) {
|
|
$postData['love'] = $postData['love_use'];
|
|
}
|
|
if ($postData['love_froze']) {
|
|
$postData['love'] = $postData['love_froze'];
|
|
}
|
|
unset($postData['love_froze']);
|
|
unset($postData['love_use']);
|
|
$model->fill($postData);
|
|
$model->uniacid = $this->getUniacid();
|
|
if ($model->save()) {
|
|
if (!$id) {
|
|
return $this->successJson('添加成功', [
|
|
'id' => $model->id,
|
|
]);
|
|
}
|
|
$activity = ActivityModel::uniacid()->where('id' ,$id)->first();
|
|
$prizeList = $activity->prize_id;
|
|
$prizeList[] = $model->id;
|
|
$activity->prize_id = $prizeList;
|
|
if ($activity->save()) {
|
|
return $this->successJson('添加成功', [
|
|
'id' => $model->id,
|
|
]);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public function editPrize()
|
|
{
|
|
$id = request()->id;
|
|
$postData = request()->post_data;
|
|
if (!$id) {
|
|
return $this->errorJson('请传入正确参数');
|
|
}
|
|
if ($postData) {
|
|
if ($postData['love_use']) {
|
|
$postData['love'] = $postData['love_use'];
|
|
}
|
|
if ($postData['love_froze']) {
|
|
$postData['love'] = $postData['love_froze'];
|
|
}
|
|
unset($postData['love_froze']);
|
|
unset($postData['love_use']);
|
|
$model = PrizeModel::uniacid()->find($id);
|
|
$model->fill($postData);
|
|
if ($model->save()) {
|
|
return $this->successJson('编辑成功', [
|
|
'id' => $model->id,
|
|
]);
|
|
}
|
|
}
|
|
$prize = PrizeModel::uniacid()->find($id)->toArray();
|
|
if ($prize['coupon_id']) {
|
|
$coupon = Coupon::select(['id', 'name', 'total'])->find($prize['coupon_id']);
|
|
$coupon['gettotal'] = MemberCoupon::uniacid()->where('coupon_id', $coupon['id'])->count();
|
|
$lasttotal = $coupon['total'] - $coupon['gettotal'];
|
|
$coupon['lasttotal'] = ($lasttotal > 0) ? $lasttotal : 0; //考虑到可领取总数修改成比之前的设置小, 则会变成负数
|
|
$prize['coupon'] = $coupon;
|
|
} else {
|
|
$prize['coupon'] = [];
|
|
}
|
|
return $this->successJson('ok', [
|
|
'prize' => $prize,
|
|
]);
|
|
}
|
|
|
|
public function delPrize()
|
|
{
|
|
$id = request()->id;
|
|
$activity_id = request()->activity_id;
|
|
|
|
if (!$id) {
|
|
return $this->errorJson('请传入正确参数');
|
|
}
|
|
|
|
$model = PrizeModel::uniacid()->find($id);
|
|
|
|
if (!$model) {
|
|
return $this->errorJson('奖品不存在');
|
|
}
|
|
|
|
if ($model->delete()) {
|
|
if (!$activity_id) {
|
|
return $this->successJson('删除成功');
|
|
}
|
|
$activity = ActivityModel::uniacid()->where('id', $activity_id)->first();
|
|
$arr = $activity->prize_id;
|
|
if (($key = array_search($id, $arr))) {
|
|
unset($arr[$key]);
|
|
$activity->prize_id = $arr;
|
|
}
|
|
if ($activity->save()) {
|
|
return $this->successJson('删除成功');
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public function getCoupons()
|
|
{
|
|
$kwd = request()->kwd;
|
|
$coupons = CouponModel::getCouponByKwd($kwd)->toArray();
|
|
|
|
if (empty($coupons)) {
|
|
return $this->successJson('ok', [
|
|
'coupons' => [],
|
|
]);
|
|
}
|
|
|
|
foreach ($coupons as &$coupon) {
|
|
$coupon['gettotal'] = MemberCoupon::uniacid()->where('coupon_id', $coupon['id'])->count();
|
|
$lasttotal = $coupon['total'] - $coupon['gettotal'];
|
|
$coupon['lasttotal'] = ($lasttotal > 0) ? $lasttotal : 0; //考虑到可领取总数修改成比之前的设置小, 则会变成负数
|
|
}
|
|
|
|
return $this->successJson('ok', [
|
|
'coupons' => $coupons,
|
|
]);
|
|
}
|
|
|
|
public function record()
|
|
{
|
|
return view('Yunshop\NewMemberPrize::admin.record');
|
|
}
|
|
|
|
public function searchRecord()
|
|
{
|
|
$id = (int)request()->id;
|
|
$search_form = request()->search;
|
|
|
|
$love_name = '爱心值';
|
|
if (app('plugins')->isEnabled('love')) {
|
|
$love_name = SetService::getLoveName();
|
|
}
|
|
|
|
$activity = ActivityModel::uniacid()->select('id', 'name', 'start_time', 'end_time')->find($id);
|
|
$logs = RecordModel::search($search_form)->where('activity_id', $id)->orderBy('id', 'desc')->paginate()->toArray();
|
|
|
|
return $this->successJson('ok', [
|
|
'logs' => $logs,
|
|
'activity' => $activity,
|
|
'love_name' => $love_name,
|
|
]);
|
|
}
|
|
|
|
public function export()
|
|
{
|
|
$id = request()->id;
|
|
$love_name = '爱心值';
|
|
if (app('plugins')->isEnabled('love')) {
|
|
$love_name = SetService::getLoveName();
|
|
}
|
|
$file_name = date('Ymdhis', time()) . '新人奖记录导出';
|
|
|
|
$search = request()->search;
|
|
if (!empty($search['activity_time']) && $search['activity_time'] !== 'undefined' && $search['activity_time'] != '') {
|
|
$search['activity_time'] = explode(',', $search['activity_time']);
|
|
}
|
|
|
|
$list = RecordModel::search($search)->where('activity_id', $id)->orderBy('id', 'desc');
|
|
|
|
$export_page = request()->export_page ? request()->export_page : 1;
|
|
$export_model = new ExportService($list, $export_page);
|
|
|
|
$export_data[0] = [
|
|
'昵称',
|
|
'会员id',
|
|
'手机号',
|
|
'抽奖时间',
|
|
'奖品名称',
|
|
'奖品信息',
|
|
];
|
|
|
|
foreach ($export_model->builder_model as $key => $item) {
|
|
$detail = '';
|
|
switch ($item['prize_type']) {
|
|
case 1 :
|
|
$detail = $item['coupon_name'];
|
|
break;
|
|
case 2 :
|
|
$detail = $item['point'] . '积分';
|
|
break;
|
|
case 3 :
|
|
$detail = $item['love'] . $love_name;
|
|
break;
|
|
case 4 :
|
|
$detail = $item['amount'] . '余额';
|
|
break;
|
|
}
|
|
$export_data[$key + 1] = [
|
|
$item['hasOneMember']['nickname'],
|
|
$item['hasOneMember']['uid'],
|
|
$item['hasOneMember']['mobile'],
|
|
date('Y-m-d H:i:s', $item['created_at']->timestamp),
|
|
$item['prize_name'],
|
|
$detail,
|
|
];
|
|
}
|
|
|
|
$export_model->export($file_name, $export_data, \Request::query('route'));
|
|
}
|
|
|
|
public function getGoods()
|
|
{
|
|
$kwd = request()->kwd;
|
|
|
|
if (!$kwd) {
|
|
return $this->successJson('请传入正确参数');
|
|
}
|
|
|
|
$goodsModels = GoodsModel::getGoodsByName($kwd)->toArray();
|
|
foreach ($goodsModels as &$goodsModel) {
|
|
$goodsModel['thumb'] = yz_tomedia($goodsModel['thumb']);
|
|
}
|
|
|
|
return $this->successJson('ok', $goodsModels);
|
|
}
|
|
|
|
public function getUniacid()
|
|
{
|
|
return \YunShop::app()->uniacid;
|
|
}
|
|
} |