Distribution.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Method;
  4. use app\admin\model\Order;
  5. use app\common\controller\Api;
  6. use think\Db;
  7. use think\Exception;
  8. class Distribution extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. public function dist_list(){
  13. $dis_type = input('dis_type',1);
  14. $method_model = new Method();
  15. $user = $this->auth->getUser();
  16. $admin_id=$user->id;
  17. if($user->pid>0){
  18. $admin_id = $user->pid;
  19. }
  20. $list = $method_model->where(array('user_id'=>$admin_id,'type'=>2,'status'=>1,'dis_type'=>$dis_type))->whereIn('moshi','2,3')->paginate();
  21. $this->success('选择模式列表',$list);
  22. }
  23. public function order_list(){
  24. $method_id = input('method_id');
  25. $dis_model = new \app\admin\model\Distribution();
  26. $list = $dis_model->where('method_id',$method_id)->order('id,number','desc')->paginate();
  27. $this->success('列表',$list);
  28. }
  29. public function add_list(){
  30. $data = input();
  31. $dis_model = new \app\admin\model\Distribution();
  32. $dis_data= [
  33. 'method_id'=>$data['method_id'],
  34. 'title'=>'线下订单',
  35. 'phone'=>$data['phone'],
  36. 'type'=>2,
  37. ];
  38. $dis_model->insert($dis_data);
  39. $this->success('添加成功');
  40. }
  41. public function del_list(){
  42. $list_id = input('list_id');
  43. $dis_model = new \app\admin\model\Distribution();
  44. $dis_model->where('id',$list_id)->delete();
  45. $this->success('删除成功');
  46. }
  47. public function number_list(){
  48. $method_id = input('method_id');
  49. $dis_model = new \app\admin\model\Distribution();
  50. $list = $dis_model->where('method_id',$method_id)->order('id')->select();
  51. $array = $this->shuffle_assoc($list);
  52. Db::startTrans();
  53. try {
  54. $i = 1;
  55. foreach ($array as $k=>$v){
  56. $dis_model->isUpdate('true',['id'=>$array[$k]['id']])->save(['number'=>$i]);
  57. $i++;
  58. }
  59. $method_model = new Method();
  60. $method_model->isUpdate('true',['id'=>$method_id])->save(['dis_type'=>2]);
  61. Db::commit();
  62. $this->success('生成座位表');
  63. }catch (Exception $e){
  64. Db::rollback();
  65. $this->error('座位表生成失败');
  66. }
  67. }
  68. public function shuffle_assoc($list) {
  69. if (!is_array($list)) return $list;
  70. $keys = array_keys($list);
  71. shuffle($keys);
  72. $random = array();
  73. foreach ($keys as $key)
  74. $random[$key] = $this->shuffle_assoc($list[$key]);
  75. return $random;
  76. }
  77. public function zuowei_tongzhi(){
  78. $user = $this->auth->getUser();
  79. $method_id = input('method_id');
  80. $model = new \app\admin\model\Distribution();
  81. $method = $model
  82. ->alias('a')
  83. ->join('user b', 'a.phone = b.mobile', 'LEFT')
  84. ->join('method c', 'c.id = a.method_id', 'LEFT')
  85. ->field('a.phone,a.number,b.id as user_id,b.nickname,c.show_images')
  86. ->where("a.method_id",$method_id)
  87. ->select();
  88. if(empty($method)){
  89. $this->error('没有数据');
  90. }
  91. if($user->zhannei == 1){
  92. $message_model = new \app\admin\model\Message();
  93. foreach($method as $val){
  94. $user_name = $val->nickname??'';
  95. $message_data = [
  96. 'category_id'=>1,
  97. 'user_id'=>$val->user_id??0,
  98. 'title'=>'座位表生成通知',
  99. 'name'=>$user_name,
  100. 'image'=>$val->show_images??'',
  101. 'description'=>$user_name.'通知您座位表已经生成了',
  102. 'is_read'=>0,
  103. 'createtime'=>time(),
  104. ];
  105. $message_model->insert($message_data);
  106. }
  107. if($user->duanxin == 1){
  108. if(count($method) > $user->sms_number){
  109. $this->error('短信数量不足');
  110. }
  111. $user->sms_number -=count($method);
  112. $user->save();
  113. $sms= new Sms();
  114. $res = $sms->zuowei_tongzhi($method,$this->auth->id);
  115. if ($res) {
  116. $this->success('座位生成通知已发出');
  117. }
  118. }
  119. }
  120. }
  121. }