ChannelRechargeRatio.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. /**
  3. * 渠道充值折扣接口
  4. *
  5. */
  6. namespace app\mcpsapi\controller;
  7. use app\mcpsapi\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. $where['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. foreach ($accountArrs as $key => $val){
  81. if($val){
  82. $channelInfo = model('Channel')->where(['name' => trim($val)])->find();
  83. if(empty($channelInfo)){
  84. $flag = false;
  85. $error_msg .= trim($val).' 账号不存在或非您子公会;';
  86. }
  87. else if($channelInfo['level']<>2 || $channelInfo['parent_id']<>$this->_channelId){
  88. $flag = false;
  89. $error_msg .= trim($val).' 账号不存在或非您子公会;';
  90. }
  91. else{
  92. $existRatioYn = model('ChannelRechargeRatio')->where(['channel_id' => $channelInfo['id']])->count();
  93. if($existRatioYn){
  94. $flag = false;
  95. $error_msg .= trim($val).' 该账号已配置充值比例;';
  96. }
  97. }
  98. $ActiveAccounts[] = $channelInfo['id'];
  99. }
  100. }
  101. if(!$flag){
  102. $this->jsonResult('', 0, $error_msg);
  103. }
  104. if($ActiveAccounts){
  105. // 启动事务
  106. Db::startTrans();
  107. try{
  108. while(list($key,$val)=@each($ActiveAccounts)){
  109. $channelInfo = model('Channel')->where(['id'=>$val])->find();
  110. if(empty($channelInfo) || $channelInfo['parent_id']<>$this->_channelId || $channelInfo['level']<>2){
  111. throw new Exception("存在账号异常,请核对账号名称后再进行操作");
  112. }
  113. $ratioData = array();
  114. $ratioData['channel_id'] = $channelInfo['id'];
  115. $ratioData['channel_name'] = $channelInfo['name'];
  116. $ratioData['ratio'] = $ratio;
  117. $ratioData['begin_time'] = strtotime($begin_time);
  118. $ratioData['end_time'] = strtotime($end_time);
  119. $ratioData['remark'] = $remark;
  120. $ratioData['create_time'] = time();
  121. $ratioData['update_time'] = time();
  122. $ratioData['admin_type'] = '2';
  123. $ratioData['admin_name'] = $this->_channelName;
  124. //添加充值比例记录
  125. $insertRatioId = model('ChannelRechargeRatio')->insertGetId($ratioData);
  126. if ( !$insertRatioId) {
  127. throw new Exception("添加充值比例失败");
  128. }
  129. $logData['ratio_id'] = $insertRatioId;
  130. $logData['channel_id'] = $channelInfo['id'];
  131. $logData['ratio'] = $ratio;
  132. $logData['begin_time'] = strtotime($begin_time);
  133. $logData['end_time'] = strtotime($end_time);
  134. $logData['remark'] = $remark;
  135. $logData['type'] = 1;
  136. $logData['create_time'] = time();
  137. $logData['admin_type'] = 2;
  138. $logData['admin_name'] = $this->_channelName;
  139. $insertLogId = model("ChannelRechargeRatioLog")->insert($logData);
  140. if(!$insertLogId){
  141. throw new Exception("创建充值比例日志失败! ");
  142. }
  143. }
  144. // 提交事务
  145. Db::commit();
  146. } catch (\Exception $e) {
  147. // 回滚事务
  148. Db::rollback();
  149. $this->jsonResult('', 0, '充值比例添加失败'.$e->getMessage());
  150. }
  151. }
  152. else{
  153. $this->jsonResult('', 0, '您正确输入账号');
  154. }
  155. $this->jsonResult('', 20000, '充值比例添加成功');
  156. exit;
  157. }
  158. else{
  159. $this->jsonResult('', 0, '非法请求');
  160. }
  161. }
  162. /**
  163. * 修改资料
  164. */
  165. public function edit()
  166. {
  167. if ($this->request->isPost()) {
  168. $id = intval($this->input('id')); //渠道ID
  169. //判断当前账号是否有充值权限
  170. if(!in_array($this->_channelLevel,[1])){
  171. $this->jsonResult('', 0, '您不能进行充值比例设置');
  172. }
  173. if(!$id){
  174. $this->jsonResult('', 0, '请选择要修改的记录');
  175. }
  176. $ratioInfo = model('ChannelRechargeRatio')->where(['id'=>$id])->find();
  177. if($ratioInfo){
  178. $channelInfo = model('Channel')->where(['id'=>$ratioInfo['channel_id']])->find();
  179. if(empty($channelInfo) || $channelInfo['parent_id']<>$this->_channelId || $channelInfo['level']<>2){
  180. $this->jsonResult('', 0, '您无权限操作');
  181. }
  182. $this->jsonResult($ratioInfo, 20000, '获取充值比例记录成功');
  183. }
  184. else{
  185. $this->jsonResult('', 0, '该充值比例记录不存在');
  186. }
  187. exit;
  188. }
  189. else{
  190. $this->jsonResult('', 0, '非法请求');
  191. }
  192. }
  193. /**
  194. * 修改保存
  195. */
  196. public function doEdit()
  197. {
  198. if ($this->request->isPost()) {
  199. $id = intval($this->input('id')); //记录ID
  200. $ratio = $this->input('ratio'); //充值比例
  201. $begin_time = $this->input('begin_time'); //开始时间
  202. $end_time = $this->input('end_time'); //结束时间
  203. $remark = $this->input('remark'); //备注
  204. //判断当前账号是否有充值权限
  205. if(!in_array($this->_channelLevel,[1])){
  206. $this->jsonResult('', 0, '您不能进行充值比例设置');
  207. }
  208. if(strtotime($begin_time) > strtotime($end_time)){
  209. $this->jsonResult('', 0, '请选择正确的时间范围');
  210. }
  211. if(!preg_match("/^[1-9][0-9]*$/" ,$ratio) || $ratio<=0 || $ratio>100){
  212. $this->jsonResult('', 0, '充值比例必须是0-100之间的整数');
  213. }
  214. if(!$id){
  215. $this->jsonResult('', 0, '请选择要修改的记录');
  216. }
  217. $ratioInfo = model('ChannelRechargeRatio')->where(['id'=>$id])->find();
  218. if($ratioInfo){
  219. $channelInfo = model('Channel')->where(['id'=>$ratioInfo['channel_id']])->find();
  220. if(empty($channelInfo) || $channelInfo['parent_id']<>$this->_channelId || $channelInfo['level']<>2){
  221. $this->jsonResult('', 0, '无该记录或您无权限操作');
  222. }
  223. }
  224. else{
  225. $this->jsonResult('', 0, '该充值比例记录不存在记录或您无权限操作');
  226. }
  227. // 启动事务
  228. Db::startTrans();
  229. try{
  230. $ratioData = array();
  231. $ratioData['ratio'] = $ratio;
  232. $ratioData['begin_time'] = strtotime($begin_time);
  233. $ratioData['end_time'] = strtotime($end_time);
  234. $ratioData['remark'] = $remark;
  235. $ratioData['update_time'] = time();
  236. $ratioData['admin_type'] = '2';
  237. $ratioData['admin_name'] = $this->_channelName;
  238. //添加充值比例记录
  239. $updRatioResult = model('ChannelRechargeRatio')->where(['id'=>$ratioInfo['id']])->update($ratioData);
  240. if ( !$updRatioResult) {
  241. throw new Exception("修改充值比例失败");
  242. }
  243. $logData['ratio_id'] = $ratioInfo['id'];
  244. $logData['channel_id'] = $channelInfo['id'];
  245. $logData['ratio'] = $ratio;
  246. $logData['begin_time'] = strtotime($begin_time);
  247. $logData['end_time'] = strtotime($end_time);
  248. $logData['remark'] = $remark;
  249. $logData['type'] = 2;
  250. $logData['create_time'] = time();
  251. $logData['admin_type'] = 2;
  252. $logData['admin_name'] = $this->_channelName;
  253. $insertLogId = model("ChannelRechargeRatioLog")->insert($logData);
  254. if(!$insertLogId){
  255. throw new Exception("新增充值比例日志失败! ");
  256. }
  257. // 提交事务
  258. Db::commit();
  259. } catch (\Exception $e) {
  260. // 回滚事务
  261. Db::rollback();
  262. $this->jsonResult('', 0, '充值比例修改失败'.$e->getMessage());
  263. }
  264. $this->jsonResult('', 20000, '充值比例修改成功');
  265. exit;
  266. }
  267. else{
  268. $this->jsonResult('', 0, '非法请求');
  269. }
  270. }
  271. /**
  272. * 删除
  273. */
  274. public function delete()
  275. {
  276. if ($this->request->isPost()) {
  277. $id = intval($this->input('id')); //记录ID
  278. //判断当前账号是否有充值权限
  279. if(!in_array($this->_channelLevel,[1])){
  280. $this->jsonResult('', 0, '您不能进行充值比例设置');
  281. }
  282. if(!$id){
  283. $this->jsonResult('', 0, '请选择要删除的记录');
  284. }
  285. $ratioInfo = model('ChannelRechargeRatio')->where(['id'=>$id])->find();
  286. if($ratioInfo){
  287. $channelInfo = model('Channel')->where(['id'=>$ratioInfo['channel_id']])->find();
  288. if(empty($channelInfo) || $channelInfo['parent_id']<>$this->_channelId || $channelInfo['level']<>2){
  289. $this->jsonResult('', 0, '无该记录或您无权限操作');
  290. }
  291. }
  292. else{
  293. $this->jsonResult('', 0, '该充值比例记录不存在或您无权限操作');
  294. }
  295. // 启动事务
  296. Db::startTrans();
  297. try{
  298. //添加充值比例记录
  299. $delResult = model('ChannelRechargeRatio')->where(['id'=>$ratioInfo['id']])->delete();
  300. if ( !$delResult) {
  301. throw new Exception("删除充值比例失败");
  302. }
  303. $logData['ratio_id'] = $ratioInfo['id'];
  304. $logData['channel_id'] = $channelInfo['id'];
  305. $logData['ratio'] = $ratioInfo['ratio'];
  306. $logData['begin_time'] = $ratioInfo['begin_time'];
  307. $logData['end_time'] = $ratioInfo['end_time'];
  308. $logData['remark'] = '删除';
  309. $logData['type'] = 3;
  310. $logData['create_time'] = time();
  311. $logData['admin_type'] = 2;
  312. $logData['admin_name'] = $this->_channelName;
  313. $insertLogId = model("ChannelRechargeRatioLog")->insert($logData);
  314. if(!$insertLogId){
  315. throw new Exception("新增充值比例日志失败! ");
  316. }
  317. // 提交事务
  318. Db::commit();
  319. } catch (\Exception $e) {
  320. // 回滚事务
  321. Db::rollback();
  322. $this->jsonResult('', 0, '充值比例删除失败'.$e->getMessage());
  323. }
  324. $this->jsonResult('', 20000, '充值比例删除成功');
  325. exit;
  326. }
  327. else{
  328. $this->jsonResult('', 0, '非法请求');
  329. }
  330. }
  331. }