PayTemplateController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. if(UserService::userHasRole($uid,'administrator')){
  113. $uid = 0;
  114. }
  115. $exists = $this->payTemplate->where('uid',$uid)->where('id',$id)->first();
  116. if(!$exists){
  117. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_NOT_EXISTS_ERROR);
  118. }
  119. $pay_template_item = $this->payTemplateItem->join('pay_products','pay_products.id','=','pay_template_items.pay_product_id')
  120. ->where('pay_template_id',$id)
  121. ->select('pay_products.price','pay_products.type','pay_products.given','pay_template_id','pay_product_id','is_first_pay','is_default','sequence')
  122. ->where('pay_template_items.status',1)
  123. ->orderBy('pay_template_items.sequence')
  124. ->get();
  125. $type_list = collect($this->optionTypeList());
  126. foreach($pay_template_item as $item){
  127. if($item->type == 'COIN' && $item->is_first_pay == 1){
  128. $item->type == 'FIRST_COIN';
  129. }
  130. $item->type_name = $type_list->where('value',$item->type)->first()['name'];
  131. if($item->type == 'COIN' || $item->type == 'FIRST_COIN'){
  132. $item->charge_coin = (int)($item->price*100);
  133. }else{
  134. $item->charge_coin = $item->type_name;
  135. }
  136. $item->sequence_text = $this->sequence_map[$item->sequence];
  137. $item->default_text = $item->is_default? '默认项':"非默认项";
  138. }
  139. return [
  140. 'template_info'=>$exists,
  141. 'template_item_list'=>$pay_template_item
  142. ];
  143. }
  144. /**
  145. * 更新模板
  146. *
  147. * @param [type] $id
  148. * @param Request $request
  149. * @return void
  150. */
  151. public function update($id, Request $request)
  152. {
  153. $name = $request->post('name');
  154. $status = $request->post('status',-1);
  155. $options = $request->post('options');
  156. $uid = $this->getLoginUser()->id;
  157. if(UserService::userHasRole($uid,'administrator')){
  158. $uid = 0;
  159. }
  160. $exists = $this->payTemplate->where('uid',$uid)->where('id',$id)->first();
  161. if(!$exists){
  162. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_NOT_EXISTS_ERROR);
  163. }
  164. $template_info_update = false;
  165. if($name){
  166. $template_info_update = true;
  167. $exists->name = $name;
  168. }
  169. if($status != -1 && in_array($status,[1,0])){
  170. $template_info_update = true;
  171. $exists->status = $status;
  172. }
  173. if($status == 1){
  174. $this->payTemplate->where('uid',$uid)->update(['status'=>0]);
  175. }
  176. if($template_info_update){
  177. $exists->save();
  178. }
  179. if($options){
  180. $option_list = json_decode($options,1);
  181. $default_optioin = 0;
  182. $option_cache = [];
  183. $type_list = collect($this->optionTypeList())->pluck('value');
  184. foreach($option_list as $option){
  185. if($option['type'] == 'COIN' || $option['type'] == 'FIRST_COIN'){
  186. if($option['given'] > 3*$option['price']*100){
  187. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_GIVEN_TOO_MUCH);
  188. break;
  189. }
  190. }
  191. if(!$type_list->contains($option['type'])){
  192. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_OPTIONS_ERROR);
  193. }
  194. if(in_array($option['price'],$option_cache)){
  195. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_CONFLICT);
  196. }
  197. $option_cache[] = $option['price'];
  198. if(!$default_optioin && $option['is_default']){
  199. $default_optioin = $option['price'];
  200. }
  201. }
  202. $this->payTemplateItem->where('pay_template_id',$id)->update(['status'=>0]);
  203. $default_optioin = $default_optioin>0?$default_optioin:$option_list[0]['price'];
  204. foreach($option_list as $option){
  205. $type = $option['type'];
  206. if($option['type'] == 'COIN' || $option['type'] == 'FIRST_COIN'){
  207. $type = 'COIN';
  208. }
  209. $product_info = $this->getPayProduct($option['price'], $type,$option['given']);
  210. $pay_template_item = $this->payTemplateItem->where('pay_template_id',$id)->where('pay_product_id',$product_info->id)->first();
  211. if($pay_template_item){
  212. $pay_template_item->status = 1;
  213. $pay_template_item->sequence = $option['sequence'];
  214. $pay_template_item->is_first_pay = $option['type'] == 'FIRST_COIN' ?1:0;
  215. $pay_template_item->is_default = $option['price'] == $default_optioin ?1:0;
  216. $pay_template_item->save();
  217. }else{
  218. $data = [
  219. 'pay_template_id'=>$id,'pay_product_id'=>$product_info->id,
  220. 'is_first_pay'=>$option['type'] == 'FIRST_COIN' ?1:0,'is_default'=>$option['price'] == $default_optioin ?1:0,
  221. 'status'=>1,'sequence'=>$option['sequence'],'created_at'=>Carbon::now(),'updated_at'=>Carbon::now()
  222. ];
  223. $this->payTemplateItem->insert($data);
  224. }
  225. }
  226. }
  227. return [];
  228. }
  229. /**
  230. * 更新模板状态
  231. *
  232. * @param int $id
  233. * @param Request $request
  234. * @return void
  235. */
  236. public function updateStatus($id,Request $request){
  237. $uid = $this->getLoginUser()->id;
  238. if(UserService::userHasRole($uid,'administrator')){
  239. $uid = 0;
  240. }
  241. $exists = $this->payTemplate->where('uid',$uid)->where('id',$id)->first();
  242. $status = $request->post('status');
  243. if(!in_array($status,[1,0])){
  244. ChannelBusinessException::throwError(Errors::PARAM_EMPTY);
  245. }
  246. if(!$exists){
  247. ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_NOT_EXISTS_ERROR);
  248. }
  249. if($status == 1){
  250. $this->payTemplate->where('uid',$uid)->update(['status'=>0]);
  251. }
  252. $exists->status = $status;
  253. $exists->save();
  254. return [];
  255. }
  256. private function getPayProduct($price,$type,$given){
  257. $where = [
  258. ['price','=',$price],
  259. ['type','=',$type],
  260. ['given','=',$given],
  261. ];
  262. $product_info = $this->payProduct->where($where)->first();
  263. if($product_info){
  264. return $product_info ;
  265. }
  266. return $this->payProduct->create(compact('price','type','given'));
  267. }
  268. public function optionTypeList(){
  269. return [
  270. ['name'=>'普通充值','value'=>'COIN'],
  271. ['name'=>'首充','value'=>'FIRST_COIN'],
  272. ['name'=>'包月','value'=>'MONTH'],
  273. ['name'=>'包季','value'=>'QUARTER'],
  274. ['name'=>'包年','value'=>'YEAR']
  275. ];
  276. }
  277. public function optionSequence(){
  278. return [
  279. ['name'=>'选项一','value'=>1],
  280. ['name'=>'选项二','value'=>2],
  281. ['name'=>'选项三','value'=>3],
  282. ['name'=>'选项四','value'=>4],
  283. ['name'=>'选项五','value'=>5],
  284. ['name'=>'选项六','value'=>6]
  285. ];
  286. }
  287. }