PayTemplateController.php 14 KB

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