PayTemplateController.php 14 KB

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