PayTemplateController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. namespace Modules\Channel\Http\Controllers;
  3. use Carbon\Carbon;
  4. use Catch\Base\CatchController;
  5. use Illuminate\Foundation\Validation\ValidatesRequests;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\DB;
  8. use Modules\Channel\Exceptions\ChannelBusinessException;
  9. use Modules\Channel\Models\PayProduct;
  10. use Modules\Channel\Models\PayTemplate;
  11. use Modules\Channel\Models\PayTemplateItem;
  12. use Modules\Channel\Services\User\UserService;
  13. use Modules\Common\Errors\Errors;
  14. class PayTemplateController extends CatchController
  15. {
  16. use ValidatesRequests;
  17. public function __construct(protected readonly PayTemplate $payTemplate,protected readonly PayTemplateItem $payTemplateItem,protected readonly PayProduct $payProduct)
  18. {
  19. }
  20. private $sequence_map = [
  21. '','位置一','位置二','位置三','位置四','位置五','位置六','位置七','位置八'
  22. ];
  23. /**
  24. * 充值模板列表
  25. *
  26. * @param Request $request
  27. * @return void
  28. */
  29. public function index(Request $request)
  30. {
  31. $uid = $this->getLoginUser()->id;
  32. if(UserService::userHasRole($uid,'administrator')){
  33. $uid = 0;
  34. }
  35. if($uid) {
  36. $this->initPayTemplate($uid);
  37. }
  38. $name = $request->get('name');
  39. // 1-首充模板,2-非首充模板
  40. $type = $request->input('type');
  41. $where = [[
  42. 'uid','=',$uid,
  43. 'type', '=', $type,
  44. ]];
  45. if($name){
  46. $where[] = ['name','like','%'.$name.'%'];
  47. }
  48. $result = $this->payTemplate->orderBy('id','desc')->where($where)->paginate($request->input('limit', 20));
  49. foreach ($result as $item) {
  50. $item->status_str = $item->status ? '默认模板': '非默认模板';
  51. $item->type_str = 1 == $item->type ? '首充模板' : '非首充模板';
  52. }
  53. return $result;
  54. }
  55. /**
  56. * 如果优化是没有设置充值模板,那么就是用官方模板进行初始化
  57. * @param $uid
  58. */
  59. private function initPayTemplate($uid) {
  60. if(!$this->payTemplate->where('uid', $uid)->count()) {
  61. $systemPayTemplates = $this->payTemplate->where('uid', 0)->get();
  62. $now = date('Y-m-d H:i:s');
  63. foreach ($systemPayTemplates as $systemPayTemplate) {
  64. $payTemplate = $this->payTemplate->create([
  65. 'uid' => $uid,
  66. 'name' => $systemPayTemplate->name,
  67. 'status' => $systemPayTemplate->status,
  68. 'type' => $systemPayTemplate->type,
  69. ]);
  70. $payTemplateItems = $this->payTemplateItem->where([
  71. 'pay_template_id' => $systemPayTemplate->id, 'status' => 1,
  72. ])->get();
  73. $payTemplateItemDatas = [];
  74. foreach ($payTemplateItems as $item) {
  75. $payTemplateItemDatas[] = [
  76. 'pay_template_id' => $payTemplate->id,
  77. 'pay_product_id' => $item->pay_product_id,
  78. 'is_default' => $item->is_default,
  79. 'status' => $item->status,
  80. 'sequence' => $item->sequence,
  81. 'created_at' => $now,
  82. 'updated_at' => $now,
  83. ];
  84. }
  85. $this->payTemplateItem->insert($payTemplateItemDatas);
  86. }
  87. }
  88. }
  89. /**
  90. * 添加模板
  91. *
  92. * @param Request $request
  93. * @return void
  94. */
  95. public function store(Request $request)
  96. {
  97. $this->validate($request, [
  98. 'name' => 'required',
  99. 'options' => 'required',
  100. 'status' => 'required|in:0,1',
  101. 'type' => 'required|in:1,2'
  102. ]);
  103. $uid = $this->getLoginUser()->id;
  104. $name = $request->post('name');
  105. // 0-非默认模板,1-默认模板
  106. $status = $request->post('status');
  107. $options = $request->post('options');
  108. // 1-首充模板,2-非首充模板
  109. $type = $request->input('type');
  110. if(UserService::userHasRole($uid,'administrator')){
  111. $uid = 0;
  112. }
  113. $option_list = json_decode($options,1);
  114. $data = [];
  115. $default_optioin = 0;
  116. $option_cache = [];
  117. $type_list = collect($this->optionTypeList())->pluck('value');
  118. foreach($option_list as $option){
  119. if(!is_numeric($option['price']) || !is_numeric($option['given'])){
  120. ChannelBusinessException::throwError(Errors::PARAM_ERROR);
  121. }
  122. if($option['type'] == 'COIN' || $option['type'] == 'FIRST_COIN'){
  123. if($option['given'] > 3*$option['price']*100){
  124. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_GIVEN_TOO_MUCH);
  125. break;
  126. }
  127. }
  128. if(!$type_list->contains($option['type'])){
  129. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_OPTIONS_ERROR);
  130. }
  131. if(in_array($option['price'],$option_cache)){
  132. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_CONFLICT);
  133. }
  134. $option_cache[] = $option['price'];
  135. if(!$default_optioin && $option['is_default']){
  136. $default_optioin = $option['price'];
  137. }
  138. }
  139. $default_optioin = $default_optioin>0?$default_optioin:$option_list[0]['price'];
  140. if($status == 1 && 0 == $uid){
  141. $this->payTemplate->where([
  142. 'uid' => $uid, 'type' => $type
  143. ])->update(['status'=>0]);
  144. }
  145. $pay_template_info = $this->payTemplate->create(['name'=>$name,'uid'=>$uid,
  146. 'type' => $type, 'status' => $status]);
  147. foreach($option_list as $option){
  148. $type = $option['type'];
  149. if($option['type'] == 'COIN' || $option['type'] == 'FIRST_COIN'){
  150. $type = 'COIN';
  151. }
  152. $product_info = $this->getPayProduct($option['price'], $type,$option['given']);
  153. $data[] = [
  154. 'pay_template_id'=>$pay_template_info->id,'pay_product_id'=>$product_info->id,
  155. 'is_first_pay'=>$option['type'] == 'FIRST_COIN' ?1:0,'is_default'=>$option['price'] == $default_optioin ?1:0,
  156. 'status'=>1,'sequence'=>$option['sequence'],'created_at'=>Carbon::now(),'updated_at'=>Carbon::now()
  157. ];
  158. }
  159. $this->payTemplateItem->insert($data);
  160. return [];
  161. }
  162. /**
  163. * 模板详情
  164. *
  165. * @param int $id
  166. * @return void
  167. */
  168. public function show($id)
  169. {
  170. $uid = $this->getLoginUser()->id;
  171. if(UserService::userHasRole($uid,'administrator')){
  172. $uid = 0;
  173. }
  174. $exists = $this->payTemplate->where('uid',$uid)->where('id',$id)->first();
  175. if(!$exists){
  176. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_NOT_EXISTS_ERROR);
  177. }
  178. $pay_template_item = $this->payTemplateItem->join('pay_products','pay_products.id','=','pay_template_items.pay_product_id')
  179. ->where('pay_template_id',$id)
  180. ->select('pay_products.price','pay_products.type','pay_products.given','pay_template_id','pay_product_id','is_first_pay','is_default','sequence','pay_template_items.id as pay_template_item_id')
  181. ->where('pay_template_items.status',1)
  182. ->orderBy('pay_template_items.sequence')
  183. ->get();
  184. $type_list = collect($this->optionTypeList());
  185. foreach($pay_template_item as $item){
  186. if($item->type == 'COIN' && $item->is_first_pay == 1){
  187. $item->type == 'FIRST_COIN';
  188. }
  189. $item->type_name = $type_list->where('value',$item->type)->first()['name'];
  190. if($item->type == 'COIN' || $item->type == 'FIRST_COIN'){
  191. $item->charge_coin = (int)($item->price*100);
  192. }else{
  193. $item->charge_coin = $item->type_name;
  194. }
  195. $item->sequence_text = $this->sequence_map[$item->sequence];
  196. $item->default_text = $item->is_default? '默认项':"非默认项";
  197. }
  198. return [
  199. 'template_info'=>$exists,
  200. 'template_item_list'=>$pay_template_item
  201. ];
  202. }
  203. /**
  204. * 更新模板
  205. *
  206. * @param [type] $id
  207. * @param Request $request
  208. * @return void
  209. */
  210. public function update($id, Request $request)
  211. {
  212. $name = $request->post('name');
  213. $status = $request->post('status',-1);
  214. $options = $request->post('options');
  215. $uid = $this->getLoginUser()->id;
  216. if(UserService::userHasRole($uid,'administrator')){
  217. $uid = 0;
  218. }
  219. $exists = $this->payTemplate->where('uid',$uid)->where('id',$id)->first();
  220. if(!$exists){
  221. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_NOT_EXISTS_ERROR);
  222. }
  223. $template_info_update = false;
  224. if($name){
  225. $template_info_update = true;
  226. $exists->name = $name;
  227. }
  228. if($status != -1 && in_array($status,[1,0])){
  229. $template_info_update = true;
  230. $exists->status = $status;
  231. }
  232. if($status == 1){
  233. $this->payTemplate->where('uid',$uid)->update(['status'=>0]);
  234. $this->payTemplate->where('id',$id)->update(['status'=>1]);
  235. }
  236. if($template_info_update){
  237. $exists->save();
  238. }
  239. if($options){
  240. $option_list = json_decode($options,1);
  241. $default_optioin = 0;
  242. $option_cache = [];
  243. $type_list = collect($this->optionTypeList())->pluck('value');
  244. foreach($option_list as $option){
  245. if($option['type'] == 'COIN' || $option['type'] == 'FIRST_COIN'){
  246. if($option['given'] > 3*$option['price']*100){
  247. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_GIVEN_TOO_MUCH);
  248. break;
  249. }
  250. }
  251. if(!$type_list->contains($option['type'])){
  252. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_OPTIONS_ERROR);
  253. }
  254. if(in_array($option['price'],$option_cache)){
  255. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_CONFLICT);
  256. }
  257. $option_cache[] = $option['price'];
  258. if(!$default_optioin && $option['is_default']){
  259. $default_optioin = $option['price'];
  260. }
  261. }
  262. $this->payTemplateItem->where('pay_template_id',$id)->update(['status'=>0]);
  263. $default_optioin = $default_optioin>0?$default_optioin:$option_list[0]['price'];
  264. foreach($option_list as $option){
  265. $type = $option['type'];
  266. if($option['type'] == 'COIN' || $option['type'] == 'FIRST_COIN'){
  267. $type = 'COIN';
  268. }
  269. $product_info = $this->getPayProduct($option['price'], $type,$option['given']);
  270. $pay_template_item = $this->payTemplateItem->where('pay_template_id',$id)->where('pay_product_id',$product_info->id)->first();
  271. if($pay_template_item){
  272. $pay_template_item->status = 1;
  273. $pay_template_item->sequence = $option['sequence'];
  274. $pay_template_item->is_first_pay = $option['type'] == 'FIRST_COIN' ?1:0;
  275. $pay_template_item->is_default = $option['price'] == $default_optioin ?1:0;
  276. $pay_template_item->save();
  277. }else{
  278. $data = [
  279. 'pay_template_id'=>$id,'pay_product_id'=>$product_info->id,
  280. 'is_first_pay'=>$option['type'] == 'FIRST_COIN' ?1:0,'is_default'=>$option['price'] == $default_optioin ?1:0,
  281. 'status'=>1,'sequence'=>$option['sequence'],'created_at'=>Carbon::now(),'updated_at'=>Carbon::now()
  282. ];
  283. $this->payTemplateItem->insert($data);
  284. }
  285. }
  286. }
  287. return [];
  288. }
  289. /**
  290. * 编辑单条选项
  291. *
  292. * @param [type] $id
  293. * @param Request $request
  294. * @return void
  295. */
  296. public function updatePayTemplateItem($id,Request $request){
  297. $info = $this->payTemplateItem->find($id);
  298. $price = $request->post('price');
  299. $type = $request->post('type');
  300. if(empty($price) || empty($type)){
  301. ChannelBusinessException::throwError(Errors::PARAM_EMPTY);
  302. }
  303. $given = $request->post('given',0);
  304. if($given > 3*$price*100){
  305. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_GIVEN_TOO_MUCH);
  306. }
  307. $sequence = $request->post('sequence',0);
  308. $is_default = $request->post('is_default',0);
  309. $is_first_pay = 0;
  310. if($type == 'FIRST_COIN'){
  311. $type = 'COIN';
  312. $is_first_pay = 1;
  313. }
  314. $product_info = $this->getPayProduct($price,$type,$given);
  315. if($info->pay_product_id == $product_info->id){
  316. $info->is_first_pay = $is_first_pay;
  317. $info->is_default = $is_default;
  318. $info->save();
  319. }else{
  320. $info->status = 0;
  321. $info->save();
  322. $data = [
  323. 'pay_template_id'=>$info->pay_template_id,'pay_product_id'=>$product_info->id,
  324. 'is_first_pay'=>$is_first_pay,'is_default'=>$is_default,
  325. 'status'=>1,'sequence'=>$sequence,'created_at'=>Carbon::now(),'updated_at'=>Carbon::now()
  326. ];
  327. $this->payTemplateItem->insert($data);
  328. }
  329. return [];
  330. }
  331. /**
  332. * 删除单条选项
  333. *
  334. * @param [type] $id
  335. * @return void
  336. */
  337. public function deleteOneItem($id){
  338. $info = $this->payTemplateItem->find($id);
  339. $info->status = 0;
  340. $info->save();
  341. return [];
  342. }
  343. /**
  344. * 更新模板状态
  345. *
  346. * @param int $id
  347. * @param Request $request
  348. * @return void
  349. */
  350. public function updateStatus($id,Request $request){
  351. $uid = $this->getLoginUser()->id;
  352. if(UserService::userHasRole($uid,'administrator')){
  353. $uid = 0;
  354. }
  355. $exists = $this->payTemplate->where('uid',$uid)->where('id',$id)->first();
  356. $status = $request->post('status');
  357. if(!in_array($status,[1,0])){
  358. ChannelBusinessException::throwError(Errors::PARAM_EMPTY);
  359. }
  360. if(!$exists){
  361. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_NOT_EXISTS_ERROR);
  362. }
  363. if($status == 1){
  364. $this->payTemplate->where('uid',$uid)->update(['status'=>0]);
  365. }
  366. $exists->status = $status;
  367. $exists->save();
  368. return [];
  369. }
  370. private function getPayProduct($price,$type,$given){
  371. $where = [
  372. ['price','=',$price],
  373. ['type','=',$type],
  374. ['given','=',$given],
  375. ];
  376. $product_info = $this->payProduct->where($where)->first();
  377. if($product_info){
  378. return $product_info ;
  379. }
  380. return $this->payProduct->create(compact('price','type','given'));
  381. }
  382. public function optionTypeList(){
  383. return [
  384. ['name'=>'普通充值','value'=>'COIN'],
  385. ['name'=>'包月','value'=>'MONTH'],
  386. ['name'=>'包季','value'=>'QUARTER'],
  387. ['name'=>'包年','value'=>'YEAR']
  388. ];
  389. }
  390. public function optionTypeListOutPut(){
  391. return collect($this->optionTypeList())->map(function($item){
  392. return [
  393. 'type'=>$item['value'],'type_name'=>$item['name']
  394. ];
  395. })->all();
  396. }
  397. public function optionSequence(){
  398. return [
  399. ['sequence_text'=>'位置一','sequence'=>1],
  400. ['sequence_text'=>'位置二','sequence'=>2],
  401. ['sequence_text'=>'位置三','sequence'=>3],
  402. ['sequence_text'=>'位置四','sequence'=>4],
  403. ['sequence_text'=>'位置五','sequence'=>5],
  404. ['sequence_text'=>'位置六','sequence'=>6]
  405. ];
  406. }
  407. }