PayTemplateController.php 13 KB

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