PayTemplateController.php 14 KB

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