Package.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace app\models\store;
  3. use app\models\system\SystemUserLevel;
  4. use app\models\user\User;
  5. use app\models\user\UserBill;
  6. use crmeb\basic\BaseModel;
  7. use crmeb\services\UtilService;
  8. use crmeb\traits\ModelTrait;
  9. use think\Exception;
  10. use think\facade\Log;
  11. use think\Model;
  12. class Package extends BaseModel
  13. {
  14. use ModelTrait;
  15. /**
  16. * 预约
  17. * @param $uid
  18. * @param $package_manager
  19. * @param $price
  20. * @param $day
  21. * @param $proportion
  22. * @return Package|bool|Model
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. */
  27. public static function reserve($uid,$whole_id,$time_id,$to_uid,$price,$first_price,$last_id)
  28. {
  29. $user = User::find($uid);
  30. $data['uid'] = $uid;
  31. $data['whole_id'] = $whole_id;
  32. $data['time_id'] = $time_id;
  33. $data['first_price'] = $first_price;
  34. $data['last_id'] = $last_id;
  35. $data['price'] = $price;
  36. if(cache('reserve_'.$uid,1) && false) return self::setErrorInfo('你已报名,无需重复报名');
  37. if($last_id>0 && self::where('last_id',$last_id)->where('uid',$uid)->find())
  38. {
  39. return self::setErrorInfo('你已报名,无需重复报名');
  40. }elseif($last_id>0 && self::where('last_id',$last_id)->where('status','>',-1)->find())
  41. {
  42. return self::setErrorInfo('该订单已有预约');
  43. }
  44. else
  45. {
  46. cache('reserve_'.$uid,1,60);
  47. }
  48. $integral = bcmul($price,bcdiv(sys_config('reserve_integral'),100,2),2);
  49. if($integral>$user['gold'])
  50. {
  51. cache('reserve_'.$uid,null);
  52. return self::setErrorInfo('金豆不足,请先买金豆专区');
  53. }
  54. if($last_id>0 && self::where('id',$last_id)->value('status')==3) {
  55. cache('reserve_'.$uid,null);
  56. return self::setErrorInfo('已有用户预约,换个商品预约');
  57. }
  58. $data['add_time'] = time();
  59. $data['order_id'] = self::getNewOrderId();
  60. if($to_uid==0)
  61. {
  62. $to_uid = User::where('is_sys', 1)->order('sys_pay asc,uid asc')->value('uid');
  63. }
  64. $data['to_uid'] = $to_uid;
  65. self::beginTrans();
  66. try{
  67. $data['use_integral'] = $integral;
  68. $res = self::create($data);
  69. User::where('uid',$uid)->dec('gold',$integral)->update();
  70. UserBill::expend("预约扣金豆",$uid,'gold','pay_money',$integral,$res['id'],bcsub($user['gold'],$integral,2),'预约扣除'.$integral."金豆");
  71. if($last_id==0) User::edit(['is_whole'=>1],$uid);
  72. if($last_id>0) self::where('id',$last_id)->update(['status'=>3]);
  73. self::commitTrans();
  74. return $res->toArray();
  75. }catch (Exception $e)
  76. {
  77. cache('reserve_'.$uid,null);
  78. Log::error('预约出错:' . $e->getLine().'--'.$e->getMessage());
  79. return self::setErrorInfo($e->getMessage(),true);
  80. }
  81. }
  82. /**
  83. * 生成订单唯一id
  84. * @param $uid 用户uid
  85. * @return string
  86. */
  87. public static function getNewOrderId()
  88. {
  89. list($msec, $sec) = explode(' ', microtime());
  90. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  91. $orderId = 'pk' . $msectime . mt_rand(10000, 99999);
  92. while (self::be(['order_id' => $orderId])) $orderId = 'pk' . $msectime . mt_rand(10000, 99999);
  93. return $orderId;
  94. }
  95. /**
  96. * 我的订单
  97. * @return bool|string
  98. */
  99. public static function getlist()
  100. {
  101. $rs = sys_data('package_manager');
  102. $am= explode("~",sys_config('start_time'));
  103. $pm= explode("~",sys_config('end_time'));
  104. foreach ($rs as &$v) {
  105. $v['cts'] =self::where('package_manager',$v['id'])->where('add_time','>=',strtotime(date("Y-m-d ")))->where('add_time','<',strtotime(date("Y-m-d ").$v['end']))->count();
  106. $v['cts'] = bcadd($v['virtual'],$v['cts']);
  107. }
  108. return $rs;
  109. }
  110. /**
  111. * 收货确认
  112. * @param $id
  113. */
  114. public static function take($id)
  115. {
  116. $info = self::find($id);
  117. $data['status'] = 2;
  118. $data['paid'] = 1;
  119. self::beginTrans();
  120. try {
  121. $package_income = bcmul($info['price'],bcdiv(sys_config('package_income'),100,3),2);
  122. $confirm_receipt = bcmul($info['price'],bcdiv(sys_config('confirm_receipt'),100,3),2);
  123. $gold_bond = bcmul($info['price'],bcdiv(sys_config('gold_bond'),100,3),2);
  124. User::where('uid',$info['uid'])->inc('brokerage_price',$package_income)->inc('integral',$gold_bond)->update();
  125. $user = User::where('uid',$info['uid'])->find();
  126. UserBill::income('订单收益',$info['uid'],'now_money','brokerage',$package_income,$info['id'],$user['brokerage_price'],'订单收益'.$package_income);
  127. UserBill::income('订单金券收益',$info['uid'],'integral','package',$gold_bond,$info['id'],$user['gold'],'订单金券收益'.$gold_bond);
  128. /*
  129. $res = self::edit($data,$id);
  130. User::where('uid',$info['to_uid'])->inc('brokerage_price',$confirm_receipt)->update();
  131. $to_user = User::where('uid',$info['to_uid'])->find();
  132. UserBill::income('订单确认收益',$info['to_uid'],'now_money','brokerage',$confirm_receipt,$info['id'],$to_user['brokerage_price'],'订单确认收益'.$confirm_receipt);
  133. */
  134. if($info['last_id']>0) self::edit(['status'=>4],$info['last_id']);
  135. self::commitTrans();
  136. }
  137. catch (Exception $e)
  138. {
  139. Log::error('收货错误:' .$e->getFile().'行'.$e->getLine()."原因:".$e->getMessage());
  140. self::setErrorInfo('收货确认错误',true);
  141. }
  142. return true;
  143. }
  144. /**
  145. * 订单无效
  146. * @param $id
  147. */
  148. public static function invalid($id,$re='')
  149. {
  150. $data['status'] = -1;
  151. $data['re'] = $re;
  152. $last_id = self::where('id',$id)->value('last_id');
  153. if($last_id>0) self::edit(['status'=>2],$last_id);
  154. return self::edit($data,$id);
  155. }
  156. /**
  157. * 支付超时
  158. */
  159. public static function time_out()
  160. {
  161. $list = self::where('status',1)->where('add_time','<',strtotime("-".sys_config('order_whole_time')." hour"))->select();
  162. foreach ($list as $v)
  163. {
  164. if($v['last_id']>0) self::edit(['status'=>2],$v['last_id']);
  165. self::edit(['status'=>-2],$v['id']);
  166. }
  167. return true;
  168. }
  169. /**
  170. * 收货确认超时
  171. * @return bool
  172. * @throws \think\db\exception\DataNotFoundException
  173. * @throws \think\db\exception\DbException
  174. * @throws \think\db\exception\ModelNotFoundException
  175. */
  176. public static function take_outs()
  177. {
  178. $data['status'] = 2;
  179. $data['paid'] = 1;
  180. $list = self::where('pay_time', '<', strtotime("-" . sys_config('order_whole_time') . " hour"))->select()->toArray();
  181. self::beginTrans();
  182. try {
  183. foreach ($list as $info) {
  184. $package_income = bcmul($info['price'], bcdiv(sys_config('package_income'), 100, 3), 2);
  185. User::where('uid', $info['to_uid'])->dec('gold', $package_income)->update();
  186. $to_user = User::where('uid', $info['to_uid'])->find();
  187. UserBill::income('扣除收益', $info['to_uid'], 'gold', 'dec', $package_income, $info['id'], $to_user['gold'], '订单未确认收货系统确认,扣除收益' .$package_income );
  188. if ($info['last_id'] > 0) self::edit(['status' => 4], $info['last_id']);
  189. }
  190. } catch (Exception $e) {
  191. Log::error('收货错误:' . $e->getFile() . '行' . $e->getLine() . "原因:" . $e->getMessage());
  192. self::setErrorInfo('收货确认错误', true);
  193. }
  194. self::commitTrans();
  195. return true;
  196. }
  197. public static function take_out($id)
  198. {
  199. $info = self::where('pay_time','<',strtotime("-".sys_config('order_whole_time')." hour"))->where('id',$id)->find();
  200. if($info)
  201. {
  202. $data['status'] = 2;
  203. $data['paid'] = 1;
  204. self::beginTrans();
  205. try {
  206. $package_income = bcmul($info['price'], bcdiv(sys_config('package_income'), 100, 3), 2);
  207. User::where('uid', $info['to_uid'])->dec('gold', $package_income)->update();
  208. $to_user = User::where('uid', $info['to_uid'])->find();
  209. UserBill::income('扣除收益', $info['to_uid'], 'gold', 'dec', $package_income, $info['id'], $to_user['gold'], '订单未确认收货系统确认,扣除收益' .$package_income );
  210. if ($info['last_id'] > 0) self::edit(['status' => 4], $info['last_id']);
  211. self::commitTrans();
  212. }
  213. catch (Exception $e)
  214. {
  215. Log::error('收货错误:' .$e->getFile().'行'.$e->getLine()."原因:".$e->getMessage());
  216. self::setErrorInfo('收货确认错误',true);
  217. }
  218. return true;
  219. }
  220. else
  221. {
  222. return self::setErrorInfo('未到'.sys_config('order_whole_time')."小时,不能后台确认");
  223. }
  224. }
  225. /**
  226. * 获取中奖用户编号
  227. * 先根据投入积分求出未中奖用户,然后在获取中奖用户
  228. * @param $user
  229. * @return array
  230. */
  231. public static function getrand($user, $prize_person_num = 1)
  232. {
  233. $user_arr = []; //随机数组
  234. $n = 0;
  235. $count = 0;$sum = 0;
  236. foreach ($user as $v)
  237. {
  238. $sum += bcadd(100,0,0);
  239. }
  240. foreach ($user as $v) {
  241. $user_arr [$n] = [
  242. 'id' => $v['id'],
  243. "start" => $count,
  244. "end" => $count + ($sum-bcadd(100,0,0)),
  245. ];
  246. $n++;
  247. $count +=($sum-bcadd(100,0,0))+1;
  248. }
  249. $prize_arr = [];
  250. $n = $prize_person_num; //中奖人数
  251. while (sizeof($prize_arr) < $n) {
  252. $random = mt_rand(0, $count); //随机数
  253. foreach ($user_arr as $item) {
  254. if ($random >= $item['start'] && $random <= $item['end'] && !in_array($item['id'],$prize_arr)) {
  255. $prize_arr[] = $item['id'];
  256. break;
  257. }
  258. }
  259. }
  260. return $prize_arr;
  261. }
  262. /**
  263. * 获取列表
  264. * @param $where
  265. */
  266. public static function lst($where)
  267. {
  268. $model = new self;
  269. if(isset($where['data']) && $where['data']!='')
  270. {
  271. $model = $model->getModelTime($where,$model);
  272. }
  273. else
  274. {
  275. $model = $model->where('pay_time','<',strtotime("-1 day"));
  276. }
  277. if(isset($where['uid']) && $where['uid']>0) $model = $model->where('uid',$where['uid']);
  278. if(isset($where['to_uid']) && $where['to_uid']>0) $model = $model->where('to_uid',$where['to_uid']);
  279. if(isset($where['status']) && $where['status']>-4) $model = $model->where('status',$where['status']);
  280. if(isset($where['time_id']) && $where['time_id']>-4) $model = $model->where('time_id',$where['time_id']);
  281. $count = $model->value('count(id)')?:0;
  282. $data = $model->order('id desc')->page($where['page'],$where['limit'])->select()->toarray();
  283. $whole = [];
  284. foreach (sys_data('whole_time') as $v)
  285. {
  286. $whole[$v['id']] = $v['time'];
  287. }
  288. foreach ($data as &$v)
  289. {
  290. $v['time_id_title'] = $whole[$v['time_id']];
  291. if($v['whole_id']) {
  292. $v['whole'] = StoreWholesale::find($v['whole_id']);
  293. }
  294. else
  295. {
  296. $v['whole'] = [];
  297. }
  298. $v['user'] = User::where('uid',$v['uid'])->field('real_name,avatar,phone')->find()->toArray();
  299. if($v['to_uid']>0) {
  300. $v['touser'] = User::where('uid', $v['to_uid'])->field('real_name,avatar,phone,wx_qr,wx_no,alipay_no,alipay_name,account_Bank,bank_card,bank_name,bank_branch')->find()->toArray();
  301. }
  302. }
  303. return compact('count','data');
  304. }
  305. /**
  306. * 获取会员升级订单
  307. */
  308. public static function orderlist($where)
  309. {
  310. $model = new self;
  311. if(isset($where['data']) && $where['data'] !='') $model = $model->getModelTime($where,$model,"add_time");
  312. if(isset($where['paid']) && $where['paid'] >-1) $model = $model->where('paid',$where['paid']);
  313. if(isset($where['status']) && $where['status'] >-4) $model = $model->where('status',$where['status']);
  314. if(isset($where['key']) && $where['key'] !='') $model = $model->where('order_id','like',"%".$where['key']."%");
  315. $model = $model->order('id desc');
  316. return self::page($model, function ($v) {
  317. $v['user'] = User::where('uid',$v['uid'])->field('nickname,real_name,avatar,phone')->find()->toArray();
  318. if($v['to_uid']>0) {
  319. $v['touser'] = User::where('uid', $v['to_uid'])->field('real_name,real_name,avatar,phone,wx_qr,wx_no,alipay_no,alipay_name,account_Bank,bank_card,bank_name,bank_branch')->find()->toArray();
  320. }
  321. else
  322. {
  323. $v['touser'] = null;
  324. }
  325. }, $where);
  326. }
  327. }