Package.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. User::where('uid',$to_uid)->inc('sys_pay',1)->update();
  64. }
  65. $data['to_uid'] = $to_uid;
  66. self::beginTrans();
  67. try{
  68. $data['use_integral'] = $integral;
  69. $res = self::create($data);
  70. User::where('uid',$uid)->dec('gold',$integral)->update();
  71. UserBill::expend("预约扣金豆",$uid,'gold','pay_money',$integral,$res['id'],bcsub($user['gold'],$integral,2),'预约扣除'.$integral."金豆");
  72. if($last_id==0) User::edit(['is_whole'=>1],$uid);
  73. if($last_id>0) self::where('id',$last_id)->update(['status'=>3]);
  74. self::commitTrans();
  75. return $res->toArray();
  76. }catch (Exception $e)
  77. {
  78. cache('reserve_'.$uid,null);
  79. Log::error('预约出错:' . $e->getLine().'--'.$e->getMessage());
  80. return self::setErrorInfo($e->getMessage(),true);
  81. }
  82. }
  83. /**
  84. * 生成订单唯一id
  85. * @param $uid 用户uid
  86. * @return string
  87. */
  88. public static function getNewOrderId()
  89. {
  90. list($msec, $sec) = explode(' ', microtime());
  91. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  92. $orderId = 'pk' . $msectime . mt_rand(10000, 99999);
  93. while (self::be(['order_id' => $orderId])) $orderId = 'pk' . $msectime . mt_rand(10000, 99999);
  94. return $orderId;
  95. }
  96. /**
  97. * 我的订单
  98. * @return bool|string
  99. */
  100. public static function getlist()
  101. {
  102. $rs = sys_data('package_manager');
  103. $am= explode("~",sys_config('start_time'));
  104. $pm= explode("~",sys_config('end_time'));
  105. foreach ($rs as &$v) {
  106. $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();
  107. $v['cts'] = bcadd($v['virtual'],$v['cts']);
  108. }
  109. return $rs;
  110. }
  111. /**
  112. * 收货确认
  113. * @param $id
  114. */
  115. public static function take($id)
  116. {
  117. $info = self::find($id);
  118. $data['status'] = 2;
  119. $data['paid'] = 1;
  120. self::beginTrans();
  121. try {
  122. $gold_bond = bcmul($info['price'],bcdiv(sys_config('gold_bond'),100,3),2);
  123. User::where('uid',$info['uid'])->inc('integral',$gold_bond)->update();
  124. $user = User::where('uid',$info['uid'])->find();
  125. UserBill::income('订单金券收益',$info['uid'],'integral','package',$gold_bond,$info['id'],$user['gold'],'订单金券收益'.$gold_bond);
  126. $res = self::edit($data,$id);
  127. /*
  128. User::where('uid',$info['to_uid'])->inc('brokerage_price',$confirm_receipt)->update();
  129. $to_user = User::where('uid',$info['to_uid'])->find();
  130. UserBill::income('订单确认收益',$info['to_uid'],'now_money','brokerage',$confirm_receipt,$info['id'],$to_user['brokerage_price'],'订单确认收益'.$confirm_receipt);
  131. */
  132. if($info['last_id']>0) self::edit(['status'=>4],$info['last_id']);
  133. self::commitTrans();
  134. }
  135. catch (Exception $e)
  136. {
  137. Log::error('收货错误:' .$e->getFile().'行'.$e->getLine()."原因:".$e->getMessage());
  138. self::setErrorInfo('收货确认错误',true);
  139. }
  140. return true;
  141. }
  142. /**
  143. * 订单无效
  144. * @param $id
  145. */
  146. public static function invalid($id,$re='')
  147. {
  148. $data['status'] = -1;
  149. $data['re'] = $re;
  150. $last_id = self::where('id',$id)->value('last_id');
  151. if($last_id>0) self::edit(['status'=>2],$last_id);
  152. return self::edit($data,$id);
  153. }
  154. /**
  155. * 支付超时
  156. */
  157. public static function time_out()
  158. {
  159. $list = self::where('status',0)->where('add_time','<',strtotime("-".sys_config('order_whole_time')." hour"))->select();
  160. foreach ($list as $v)
  161. {
  162. if($v['last_id']>0) self::edit(['status'=>2],$v['last_id']);
  163. self::edit(['status'=>-2],$v['id']);
  164. }
  165. return true;
  166. }
  167. /**
  168. * 收货确认超时
  169. * @return bool
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\DbException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. */
  174. public static function take_outs()
  175. {
  176. $data['status'] = 2;
  177. $data['paid'] = 1;
  178. $list = self::where('pay_time', '<', strtotime("-" . sys_config('order_whole_time') . " hour"))->where('status',1)->select()->toArray();
  179. self::beginTrans();
  180. try {
  181. foreach ($list as $info) {
  182. $gold_bond = bcmul($info['price'],bcdiv(sys_config('gold_bond'),100,3),2);
  183. User::where('uid',$info['uid'])->inc('integral',$gold_bond)->update();
  184. $user = User::where('uid',$info['uid'])->find();
  185. UserBill::income('订单金券收益',$info['uid'],'integral','package',$gold_bond,$info['id'],$user['gold'],'订单金券收益'.$gold_bond);
  186. $res = self::edit($data,$info['id']);
  187. $to_user = User::where('uid', $info['to_uid'])->find();
  188. if ($info['last_id'] > 0) self::edit(['status' => 4], $info['last_id']);
  189. self::edit($data, $info['id']);
  190. $package_income = bcmul($info['price'], bcdiv(sys_config('package_income'), 100, 3), 2);
  191. User::where('uid', $info['to_uid'])->dec('gold', $package_income)->update();
  192. if($to_user['gold']<=0)
  193. {
  194. User::where('uid', $info['to_uid'])->update(['gold'=>0]);
  195. continue;
  196. }
  197. UserBill::income('扣除收益', $info['to_uid'], 'gold', 'dec', $package_income, $info['id'], $to_user['gold'], '订单未确认收货系统确认,扣除收益' .$package_income );
  198. }
  199. } catch (Exception $e) {
  200. Log::error('收货错误:' . $e->getFile() . '行' . $e->getLine() . "原因:" . $e->getMessage());
  201. self::setErrorInfo('收货确认错误', true);
  202. }
  203. self::commitTrans();
  204. return true;
  205. }
  206. public static function take_out($id)
  207. {
  208. $info = self::where('pay_time','<',strtotime("-".sys_config('order_whole_time')." hour"))->where('id',$id)->find();
  209. if($info)
  210. {
  211. $data['status'] = 2;
  212. $data['paid'] = 1;
  213. self::beginTrans();
  214. try {
  215. $package_income = bcmul($info['price'], bcdiv(sys_config('package_income'), 100, 3), 2);
  216. User::where('uid', $info['to_uid'])->dec('gold', $package_income)->update();
  217. $to_user = User::where('uid', $info['to_uid'])->find();
  218. UserBill::income('扣除收益', $info['to_uid'], 'gold', 'dec', $package_income, $info['id'], $to_user['gold'], '订单未确认收货系统确认,扣除收益' .$package_income );
  219. if($to_user['gold']<=0)
  220. {
  221. User::where('uid', $info['to_uid'])->update(['gold'=>0]);
  222. }
  223. if ($info['last_id'] > 0) self::edit(['status' => 4], $info['last_id']);
  224. self::commitTrans();
  225. }
  226. catch (Exception $e)
  227. {
  228. Log::error('收货错误:' .$e->getFile().'行'.$e->getLine()."原因:".$e->getMessage());
  229. self::setErrorInfo('收货确认错误',true);
  230. }
  231. return true;
  232. }
  233. else
  234. {
  235. return self::setErrorInfo('未到'.sys_config('order_whole_time')."小时,不能后台确认");
  236. }
  237. }
  238. /**
  239. * 获取中奖用户编号
  240. * 先根据投入积分求出未中奖用户,然后在获取中奖用户
  241. * @param $user
  242. * @return array
  243. */
  244. public static function getrand($user, $prize_person_num = 1)
  245. {
  246. $user_arr = []; //随机数组
  247. $n = 0;
  248. $count = 0;$sum = 0;
  249. foreach ($user as $v)
  250. {
  251. $sum += bcadd(100,0,0);
  252. }
  253. foreach ($user as $v) {
  254. $user_arr [$n] = [
  255. 'id' => $v['id'],
  256. "start" => $count,
  257. "end" => $count + ($sum-bcadd(100,0,0)),
  258. ];
  259. $n++;
  260. $count +=($sum-bcadd(100,0,0))+1;
  261. }
  262. $prize_arr = [];
  263. $n = $prize_person_num; //中奖人数
  264. while (sizeof($prize_arr) < $n) {
  265. $random = mt_rand(0, $count); //随机数
  266. foreach ($user_arr as $item) {
  267. if ($random >= $item['start'] && $random <= $item['end'] && !in_array($item['id'],$prize_arr)) {
  268. $prize_arr[] = $item['id'];
  269. break;
  270. }
  271. }
  272. }
  273. return $prize_arr;
  274. }
  275. /**
  276. * 获取列表
  277. * @param $where
  278. */
  279. public static function lst($where)
  280. {
  281. $model = new self;
  282. if(isset($where['data']) && $where['data']!='')
  283. {
  284. $model = $model->getModelTime($where,$model);
  285. }
  286. else
  287. {
  288. $model = $model->where('pay_time','<',strtotime("-1 day"));
  289. }
  290. if(isset($where['uid']) && $where['uid']>0) $model = $model->where('uid',$where['uid']);
  291. if(isset($where['to_uid']) && $where['to_uid']>0) $model = $model->where('to_uid',$where['to_uid']);
  292. if(isset($where['status']) && $where['status']>-4) $model = $model->where('status',$where['status']);
  293. if(isset($where['time_id']) && $where['time_id']>-4) $model = $model->where('time_id',$where['time_id']);
  294. $count = $model->value('count(id)')?:0;
  295. $data = $model->order('id desc')->page($where['page'],$where['limit'])->select()->toarray();
  296. $whole = [];
  297. foreach (sys_data('whole_time') as $v)
  298. {
  299. $whole[$v['id']] = $v['time'];
  300. }
  301. foreach ($data as &$v)
  302. {
  303. $v['time_id_title'] = $whole[$v['time_id']];
  304. if($v['whole_id']) {
  305. $v['whole'] = StoreWholesale::find($v['whole_id']);
  306. }
  307. else
  308. {
  309. $v['whole'] = [];
  310. }
  311. $v['user'] = User::where('uid',$v['uid'])->field('real_name,avatar,phone')->find()->toArray();
  312. if($v['to_uid']>0) {
  313. $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();
  314. }
  315. }
  316. return compact('count','data');
  317. }
  318. /**
  319. * 获取会员升级订单
  320. */
  321. public static function orderlist($where)
  322. {
  323. $model = new self;
  324. if(isset($where['data']) && $where['data'] !='') $model = $model->getModelTime($where,$model,"add_time");
  325. if(isset($where['paid']) && $where['paid'] >-1) $model = $model->where('paid',$where['paid']);
  326. if(isset($where['status']) && $where['status'] >-4) $model = $model->where('status',$where['status']);
  327. if(isset($where['key']) && $where['key'] !='') $model = $model->where('order_id','like',"%".$where['key']."%");
  328. $model = $model->order('id desc');
  329. return self::page($model, function ($v) {
  330. $v['user'] = User::where('uid',$v['uid'])->field('nickname,real_name,avatar,phone')->find()->toArray();
  331. if($v['to_uid']>0) {
  332. $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();
  333. }
  334. else
  335. {
  336. $v['touser'] = null;
  337. }
  338. }, $where);
  339. }
  340. }