new-admin-api/app/controller/admin/user/ExchangeCode.php

181 lines
6.1 KiB
PHP

<?php
namespace app\controller\admin\user;
use app\common\repositories\store\service\StoreServiceRepository;
use app\common\repositories\system\groupData\GroupDataRepository;
use app\common\repositories\system\groupData\GroupRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use app\common\repositories\user\VipExchangeCodeRepository;
use crmeb\basic\BaseController;
use think\App;
use think\exception\ValidateException;
class ExchangeCode extends BaseController{
protected $repository;
public function __construct(App $app, VipExchangeCodeRepository $repository){
parent::__construct($app);
$this->repository = $repository;
}
/**
* Common: 兑换码列表
* Author: wu-hui
* Time: 2024/03/02 14:21
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getList(){
[$page, $limit] = $this->getPage();
$params = $this->request->params(['is_export','batch_unique','status','mer_id','staff_id','group_data_id']);
$params['mer_id'] = $this->request->merId();
$data = $this->repository->getList((array)$params,(int)$page,(int)$limit);
// 导出数据处理
if($params['is_export'] == 1){
// 处理其他导出信息
$data['title'] = [
'兑换码',
'生成时间:' . date('Y-m-d H:i:s',time())
];
// $data['header'] = ['批次名称','批次编号','兑换码', '小程序链接'];
$data['header'] = ['批次名称','批次编号','兑换码'];
$data['filename'] = '兑换码_'.time();
// 处理跳转路径
$data['list'] = array_map(function($item){
// $path = '/pages/annex/vip_center/index?ec='.$item['exchange_code'];
return [
$item['batch_title'] ?? '',
$item['batch_unique'] ?? '',
$item['exchange_code']
// $path,
];
}, $data['list']);
$data['list'] = array_reverse($data['list']);
}
return app('json')->success($data);
}
/**
* Common: 获取批次列表
* Author: wu-hui
* Time: 2024/03/02 14:45
* @return mixed
*/
public function getBatchList(){
$params['mer_id'] = $this->request->merId();
$list = $this->repository->getSearchModel($params)->column('batch_unique','batch_title');
return app('json')->success($list);
}
/**
* Common: 兑换码编辑表单
* Author: wu-hui
* Time: 2024/03/02 11:39
* @return mixed
* @throws \FormBuilder\Exception\FormBuilderException
*/
public function editForm(){
$data = $this->repository->getEditFormData();
return app('json')->success(formToData($data));
}
/**
* Common: 兑换码编辑提交信息
* Author: wu-hui
* Time: 2024/03/02 14:07
* @return mixed
*/
public function editInfo(){
$params = $this->request->params(['batch_title',['create_num', 0]]);
$this->repository->createExchangeCode($params);
return app('json')->success('添加成功');
}
/**
* Common: 分配商户 || 分配给员工
* Author: wu-hui
* Time: 2024/03/02 16:51
* @return mixed
* @throws \FormBuilder\Exception\FormBuilderException
*/
public function activateForm(){
$merId = $this->request->merId();
if($merId > 0){
// 商户操作 - 分配给员工并且激活
$data = $this->repository->getActivateFormData($merId);
}else{
// 总平台操作 - 分配给商户
$data = $this->repository->allocationMerchantFormData();
}
return app('json')->success(formToData($data));
}
/**
* Common: 提交表单
* Author: wu-hui
* Time: 2024/03/02 16:56
* @return mixed
*/
public function activateInfo(){
$merId = $this->request->merId();
if($merId > 0){
// 商户操作 - 分配给员工并且激活
$params = $this->request->params(['start_id','end_id',['staff_id', 0]]);
$this->repository->activateExchangeCode($params, $merId);
}else{
// 总平台操作 - 分配给商户
$params = $this->request->params(['start_id','end_id',['mer_id', 0],['group_data_id',0]]);
$this->repository->allocationMerchantExchangeCode($params);
}
return app('json')->success('激活成功');
}
/**
* Common: 获取搜索条件对象列表
* Author: wu-hui
* Time: 2024/03/03 10:40
* @return mixed
*/
public function getSearchData(){
$merId = $this->request->merId();
$groupId = app()->make(GroupRepository::class)->getSearch(['group_key' => 'svip_pay'])->value('group_id');
$data['vipList'] = app()->make(GroupDataRepository::class)
->getSearch([])
->field(['group_data_id','value'])
->withAttr('value', function ($val) {
$value = json_decode($val, true);
return $value['svip_name'] ?? '';
})
->where('mer_id', 0)
->where('status', 1)
->where('group_id', $groupId)
->order('sort DESC,group_data_id ASC')
->select()
->toArray();
if($merId > 0){
// 商户操作
$data['service'] = app()->make(StoreServiceRepository::class)
->getSearch([])
->field(['nickname','service_id'])
->where('mer_id', $merId)
->where('is_del', 0)
->select()
->toArray();
}else{
// 总平台操作
$data['mer_list'] = app()->make(MerchantRepository::class)
->search(['is_del' => 0,'merchant_type' => 0,'status' => 1])
->field(['mer_id','mer_name'])
->select()
->toArray();
}
return app('json')->success($data);
}
}