PayTemplateController.php 16 KB

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