PayTemplateController.php 15 KB

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