PayTemplateController.php 14 KB

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