ChannelRechargeRatio.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * 渠道充值折扣接口
  4. *
  5. */
  6. namespace app\guildapi\controller;
  7. use app\Guildapi\controller\Guild;
  8. use app\common\model\ChannelRecharge as ChannelRechargeModel;
  9. use think\Db;
  10. use think\Config;
  11. use app\common\model\Setting;
  12. use think\Exception;
  13. class ChannelRechargeRatio extends Guild {
  14. protected $nowTime;
  15. protected $payRequestLimit = 5; //重复下单的限制时间
  16. protected $redis; //redis的句柄对象
  17. /**
  18. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  19. */
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->nowTime = NOW_TIMESTAMP;
  24. $this->redis = \think\Cache::store('default')->handler();
  25. }
  26. /**
  27. * 获取列表
  28. * @return [type] [description]
  29. */
  30. public function index()
  31. {
  32. //判断当前账号是否有充值功能
  33. if(!in_array($this->_channelLevel,[1])){
  34. $this->jsonResult('', 0, '您无权限使用该功能');
  35. }
  36. $list_rows = input('list_rows',10);
  37. $page = input('page',1);
  38. $account = $this->input('account','','trim');
  39. $where = [];
  40. $where['c.parent_id'] = $this->_channelId;
  41. $where['c.level'] = 2;
  42. if($account){
  43. $condition['r.channel_name'] = $account;
  44. }
  45. $ratioList = Db::name('nw_channel_recharge_ratio')->alias('r')
  46. ->join('nw_channel c', 'r.channel_id = c.id','inner')
  47. ->field('r.*')
  48. ->where($where)
  49. ->paginate(['list_rows'=>$list_rows,'page'=>$page])
  50. ->toArray();
  51. $this->jsonResult($ratioList, 20000, '获取列表成功');
  52. }
  53. /**
  54. * 批量添加处理
  55. */
  56. public function doAdd()
  57. {
  58. if ($this->request->isPost()) {
  59. $accounts = $this->input('accounts'); //多个账号用逗号分隔
  60. $ratio = $this->input('ratio'); //充值比例
  61. $begin_time = $this->input('begin_time'); //开始时间
  62. $end_time = $this->input('end_time'); //结束时间
  63. $remark = $this->input('remark'); //备注
  64. //判断当前账号是否有充值权限
  65. if(!in_array($this->_channelLevel,[1])){
  66. $this->jsonResult('', 0, '您不能进行充值比例设置');
  67. }
  68. if(strtotime($begin_time) > strtotime($end_time)){
  69. $this->jsonResult('', 0, '请选择正确的时间范围');
  70. }
  71. if(!preg_match("/^[1-9][0-9]*$/" ,$ratio) || $ratio<=0 || $ratio>=100){
  72. $this->jsonResult('', 0, '充值比例必须是0-100之间的整数');
  73. }
  74. //判断账号是否可以充值
  75. $ActiveAccounts = array();
  76. $flag = true;
  77. $error_msg = '';
  78. $accountArrs = explode(',',$accounts);
  79. while(list($key,$val)=@each($accountArrs)){
  80. if($val){
  81. $channelInfo = model('Channel')->where(['name' => trim($val)])->find();
  82. if(empty($channelInfo)){
  83. $flag = false;
  84. $error_msg .= trim($val).' 账号不存在或非您子公会;';
  85. }
  86. else if($channelInfo['level']<>2 || $channelInfo['parent_id']<>$this->_channelName){
  87. $flag = false;
  88. $error_msg .= trim($val).' 账号不存在或非您子公会;';
  89. }
  90. else{
  91. $existRatioYn = model('ChannelRechargeRatio')->where(['channel_id' => $channelInfo['id']])->count();
  92. if($existRatioYn){
  93. $flag = false;
  94. $error_msg .= trim($val).' 该账号已配置充值比例;';
  95. }
  96. }
  97. $ActiveAccounts[] = $channelInfo['id'];
  98. }
  99. }
  100. if(!$flag){
  101. $this->jsonResult('', 0, $error_msg);
  102. }
  103. if($ActiveAccounts){
  104. // 启动事务
  105. Db::startTrans();
  106. try{
  107. while(list($key,$val)=@each($ActiveAccounts)){
  108. $channelInfo = model('Channel')->where(['id'=>$val])->find();
  109. if(empty($channelInfo) || $channelInfo['parent_id']<>$this->_channelId || $channelInfo['level']<>2){
  110. throw new Exception("存在账号异常,请核对用户后再进行操作");
  111. }
  112. $ratioData = array();
  113. $ratioData['channel_id'] = $channelInfo['id'];
  114. $ratioData['channel_name'] = $channelInfo['name'];
  115. $ratioData['ratio'] = $ratio;
  116. $ratioData['begin_time'] = strtotime($begin_time);
  117. $ratioData['end_time'] = strtotime($end_time);
  118. $ratioData['remark'] = $remark;
  119. $ratioData['create_time'] = time();
  120. $ratioData['update_time'] = time();
  121. $ratioData['admin_type'] = '2';
  122. $ratioData['admin_name'] = $this->_channelName;
  123. //添加充值比例记录
  124. $insertRatioId = model('ChannelRechargeRatio')->insert($ratioData);
  125. if ( !$insertRatioId) {
  126. throw new Exception("添加充值比例失败");
  127. }
  128. $logData['ratio_id'] = $insertRatioId;
  129. $logData['channel_id'] = $channelInfo['id'];
  130. $logData['ratio'] = $ratio;
  131. $logData['begin_time'] = $begin_time;
  132. $logData['end_time'] = $end_time;
  133. $logData['remark'] = $remark;
  134. $logData['type'] = 1;
  135. $logData['create_time'] = time();
  136. $logData['admin_type'] = 2;
  137. $logData['admin_name'] = $this->_channelName;
  138. $insertLogId = model("ChannelRechargeRatioLog")->insert($logData);
  139. if(!$insertLogId){
  140. throw new Exception("创建充值比例日志失败! ");
  141. }
  142. }
  143. // 提交事务
  144. Db::commit();
  145. } catch (\Exception $e) {
  146. // 回滚事务
  147. Db::rollback();
  148. $this->jsonResult('', 0, '充值比例添加失败'.$e->getMessage());
  149. }
  150. }
  151. else{
  152. $this->jsonResult('', 0, '您正确输入账号');
  153. }
  154. $this->jsonResult('', 20000, '充值比例添加成功');
  155. exit;
  156. }
  157. else{
  158. $this->jsonResult('', 0, '非法请求');
  159. }
  160. }
  161. /**
  162. * 修改资料
  163. */
  164. public function edit()
  165. {
  166. if ($this->request->isPost()) {
  167. $id = intval($this->input('id')); //渠道ID
  168. //判断当前账号是否有充值权限
  169. if(!in_array($this->_channelLevel,[1])){
  170. $this->jsonResult('', 0, '您不能进行充值比例设置');
  171. }
  172. if(!$id){
  173. $this->jsonResult('', 0, '请选择要修改的记录');
  174. }
  175. $ratioInfo = model('ChannelRechargeRatio')->where(['id'=>$id])->find();
  176. if($ratioInfo){
  177. $channelInfo = model('Channel')->where(['id'=>$ratioInfo['channel_id']])->find();
  178. if(empty($channelInfo) || $channelInfo['parent_id']<>$this->_channelId || $channelInfo['level']<>2){
  179. $this->jsonResult('', 0, '您无权限操作');
  180. }
  181. $this->jsonResult($ratioInfo, 20000, '获取充值比例记录成功');
  182. }
  183. else{
  184. $this->jsonResult('', 0, '该充值比例记录不存在');
  185. }
  186. exit;
  187. }
  188. else{
  189. $this->jsonResult('', 0, '非法请求');
  190. }
  191. }
  192. /**
  193. * 修改保存
  194. */
  195. public function doEdit()
  196. {
  197. if ($this->request->isPost()) {
  198. $id = intval($this->input('id')); //记录ID
  199. $ratio = $this->input('ratio'); //充值比例
  200. $begin_time = $this->input('begin_time'); //开始时间
  201. $end_time = $this->input('end_time'); //结束时间
  202. $remark = $this->input('remark'); //备注
  203. //判断当前账号是否有充值权限
  204. if(!in_array($this->_channelLevel,[1])){
  205. $this->jsonResult('', 0, '您不能进行充值比例设置');
  206. }
  207. if(strtotime($begin_time) > strtotime($end_time)){
  208. $this->jsonResult('', 0, '请选择正确的时间范围');
  209. }
  210. if(!preg_match("/^[1-9][0-9]*$/" ,$ratio) || $ratio<=0 || $ratio>=100){
  211. $this->jsonResult('', 0, '充值比例必须是0-100之间的整数');
  212. }
  213. if(!$id){
  214. $this->jsonResult('', 0, '请选择要修改的记录');
  215. }
  216. $ratioInfo = model('ChannelRechargeRatio')->where(['id'=>$id])->find();
  217. if($ratioInfo){
  218. $channelInfo = model('Channel')->where(['id'=>$ratioInfo['channel_id']])->find();
  219. if(empty($channelInfo) || $channelInfo['parent_id']<>$this->_channelId || $channelInfo['level']<>2){
  220. $this->jsonResult('', 0, '无该记录或您无权限操作');
  221. }
  222. }
  223. // 启动事务
  224. Db::startTrans();
  225. try{
  226. $ratioData = array();
  227. $ratioData['ratio'] = $ratio;
  228. $ratioData['begin_time'] = strtotime($begin_time);
  229. $ratioData['end_time'] = strtotime($end_time);
  230. $ratioData['remark'] = $remark;
  231. $ratioData['update_time'] = time();
  232. $ratioData['admin_type'] = '2';
  233. $ratioData['admin_name'] = $this->_channelName;
  234. //添加充值比例记录
  235. $updRatioResult = model('ChannelRechargeRatio')->where(['id'=>$ratioInfo['id']])->update($ratioData);
  236. if ( !$updRatioResult) {
  237. throw new Exception("修改充值比例失败");
  238. }
  239. $logData['ratio_id'] = $ratioInfo['id'];
  240. $logData['channel_id'] = $channelInfo['id'];
  241. $logData['ratio'] = $ratio;
  242. $logData['begin_time'] = strtotime($begin_time);
  243. $logData['end_time'] = strtotime($end_time);
  244. $logData['remark'] = $remark;
  245. $logData['type'] = 1;
  246. $logData['create_time'] = time();
  247. $logData['admin_type'] = 2;
  248. $logData['admin_name'] = $this->_channelName;
  249. $insertLogId = model("ChannelRechargeRatioLog")->insert($logData);
  250. if(!$insertLogId){
  251. throw new Exception("新增充值比例日志失败! ");
  252. }
  253. // 提交事务
  254. Db::commit();
  255. } catch (\Exception $e) {
  256. // 回滚事务
  257. Db::rollback();
  258. $this->jsonResult('', 0, '充值比例修改失败'.$e->getMessage());
  259. }
  260. $this->jsonResult('', 20000, '充值比例修改成功');
  261. exit;
  262. }
  263. else{
  264. $this->jsonResult('', 0, '非法请求');
  265. }
  266. }
  267. }