PayTemplateController.php 11 KB

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