123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- /**
- * OrderModel.php UTF-8
- * 订单
- *
- * @date : 2018/1/19 21:31
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huo\model\order;
- use huo\model\common\CommonModel;
- use huo\model\member\MemberModel;
- use huolib\constant\CacheConst;
- use huolib\constant\CommonConst;
- use huolib\constant\OrderConst;
- use think\Cache;
- class OrderModel extends CommonModel {
- protected $name = 'pay';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = true;
- protected $cache_tag = CacheConst::TAG_CACHE_SERVER_ROLE;
- /**
- * 关联订单扩展
- *
- * @return \think\model\relation\HasOne
- */
- public function payext() {
- return $this->hasOne('huo\model\order\OrderExtModel', 'pay_id', 'id', [], 'left');
- }
- /**
- * 关联订单扩展
- *
- * @return \think\model\relation\HasOne
- */
- public function leftpayext() {
- return $this->hasOne('huo\model\order\OrderExtModel', 'pay_id', 'id', [], 'left')->setEagerlyType(0);
- }
- public function mem() {
- return $this->belongsTo(MemberModel::className(), 'mem_id', 'id', [], 'left')->setEagerlyType(0);
- }
- public function agent() {
- return $this->belongsTo('huo\model\user\UserModel', 'agent_id', 'id')->field('id, user_login');
- }
- public function game() {
- return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id, name');
- }
- /**
- * @param array $data
- *
- * @return bool|array
- */
- public function createOrder($data) {
- $_data['order_id'] = get_val($data, 'order_id', '');
- $_data['cp_order_id'] = get_val($data, 'cp_order_id', '');
- $_data['mem_id'] = get_val($data, 'mem_id', 0);
- $_data['mg_mem_id'] = get_val($data, 'mg_mem_id', 0);
- $_data['agent_id'] = get_val($data, 'agent_id', 0);
- $_data['app_id'] = get_val($data, 'app_id', 0);
- $_data['vb_id'] = get_val($data, 'vb_id', 0);
- $_data['amount'] = get_val($data, 'amount', 0.00);
- $_data['real_amount'] = get_val($data, 'real_amount', 0.00);
- $_data['product_id'] = get_val($data, 'product_id', '');
- $_data['product_cnt'] = get_val($data, 'product_cnt', 1);
- $_data['product_name'] = get_val($data, 'product_name', '');
- $_data['ext'] = get_val($data, 'ext', '');
- $_data['coupon_amount'] = get_val($data, 'coupon_amount', 0.00);
- $_data['ptb_amount'] = get_val($data, 'ptb_amount', 0.00);
- $_data['gm_amount'] = get_val($data, 'gm_amount', 0.00);
- $_data['integral'] = get_val($data, 'integral', 0.00);
- $_data['integral_money'] = get_val($data, 'integral_money', 0.00);
- $_data['rebate_amount'] = get_val($data, 'rebate_amount', 0.00);
- $_data['rate'] = get_val($data, 'rate', 1);
- $_data['status'] = get_val($data, 'status', OrderConst::PAY_STATUS_NOT);
- $_data['is_switch'] = get_val($data, 'is_switch', OrderConst::PAY_SWITCH_NO);
- $_data['cp_status'] = get_val($data, 'cp_status', OrderConst::CP_STATUS_NOT);
- $_data['payway'] = get_val($data, 'payway', '');
- $_data['currency'] = get_val($data, 'currency', 'CNY');
- $_data['is_distribute'] = get_val($data, 'is_distribute', OrderConst::DTB_STATUS_NOT);
- if ($_obj = self::create($_data, true)) {
- $_data['id'] = $_obj->id;
- return $_data;
- } else {
- return false;
- }
- }
- /**
- * @param array $order_data
- * @param string $pay_id
- *
- * @return bool
- */
- public function updateOrder($order_data, $pay_id) {
- $_map['id'] = $pay_id;
- $_data = $order_data;
- $_rs = self::update($_data, $_map, true);
- if (false == $_rs) {
- return false;
- } else {
- Cache::clear($this->cache_tag);
- return true;
- }
- }
- public function getStatus($order_id) {
- $_map['order_id'] = $order_id;
- $_status = self::where($_map)->field('cp_status, status')->find();
- if (false == $_status) {
- return false;
- }
- $_rdata['status'] = $_status['status'];
- $_rdata['cp_status'] = $_status['cp_status'];
- return $_rdata;
- }
- public function getDetail($order_id) {
- $_map['order_id'] = $order_id;
- $_data = self::where($_map)->find();
- if (false == $_data) {
- return false;
- }
- $_data = $_data->toArray();
- return $_data;
- }
- /**
- * 获取最近的订单ID
- *
- * @param INT $mem_id 玩家ID
- * @param INT $app_id 游戏ID
- *
- * @return mixed|string
- */
- public function getLastOrderId($mem_id, $app_id) {
- $_map['mem_id'] = $mem_id;
- $_map['app_id'] = $app_id;
- $_max_id = $this->where($_map)->max('id');
- $_o_map['id'] = $_max_id;
- $_order_id = $this->where($_o_map)->value('order_id');
- if (empty($_order_id)) {
- return '';
- }
- return $_order_id;
- }
- /**
- * 根据条件获取金额
- *
- * @param $map
- *
- * @return float|int
- */
- public function getTotalAmount($map) {
- $_tag = $this->cache_tag;
- $_cache_key = md5($_tag.json_encode($map));
- $_sum_money = $this->where($map)
- ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag)
- ->sum('amount');
- return $_sum_money;
- }
- /**
- * 总金额
- *
- * @param array $where
- *
- * @return double
- */
- public function totalMoney($where = []) {
- $where['status'] = 2;
- return $this->where($where)->sum('amount');
- }
- /**
- * 昨日金额
- *
- *
- * @param array $map
- *
- * @return float
- */
- public function yesterdayMoney($map = []) {
- return $this->getSumMoney(strtotime('yesterday'), strtotime('today') - 1, $map);
- }
- /**
- * 今日金额
- *
- * @param array $map
- *
- * @return float
- */
- public function todayMoney($map = []) {
- return $this->getSumMoney(strtotime('today'), time(), $map);
- }
- /**
- * 本周金额
- *
- * @param array $map
- *
- * @return float|int
- */
- public function thisWeekMoney($map = []) {
- return $this->getSumMoney(strtotime(date('Y-m-d 00:00:00', strtotime('this week'))), time(), $map);
- }
- /**
- * 本月金额
- *
- * @param array $map
- *
- * @return float|int
- */
- public function thisMonthMoney($map = []) {
- return $this->getSumMoney(strtotime(date('Y-m-01 00:00:00')), time(), $map);
- }
- /**
- * 获取金额
- *
- * @param int $start_time 开始时间
- * @param int $end_time 结束时间
- *
- * @param array $map
- *
- * @return float|int
- */
- public function getSumMoney($start_time, $end_time, $map = []) {
- $where['status'] = OrderConst::PAY_STATUS_SUC;
- $where['create_time'] = ['between', [$start_time, $end_time]];
- return $this->where($map)->where($where)->sum('amount');
- }
- /**
- * 按天统计付费用户数
- *
- * @param int $start_time 开始时间
- * @param int $end_time 结束时间
- *
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function statDatePaidUser($start_time, $end_time) {
- $end_time += CommonConst::CONST_DAY_SECONDS - 1;
- $_rows = $this->where('create_time', 'between', [$start_time, $end_time])
- ->group('DATE(FROM_UNIXTIME(create_time))')
- ->field('COUNT(DISTINCT mem_id) count, DATE(FROM_UNIXTIME(create_time)) date')
- ->select();
- $_data = [];
- foreach ($_rows as $_row) {
- $_data[$_row['date']] = $_row['count'];
- }
- $_result = [];
- for ($_i = $start_time; $_i <= $end_time; $_i += CommonConst::CONST_DAY_SECONDS) {
- $_date = date('Y-m-d', $_i);
- $_result[$_date] = isset($_data[$_date]) ? $_data[$_date] : 0;
- }
- return $_result;
- }
- /**
- * 获取某玩家最后一个订单order_id
- *
- * @param $mem_id
- *
- * @return mixed
- */
- public function getMemLastOrderId($mem_id) {
- $_map = ['mem_id' => $mem_id];
- $_order_id = $this->where($_map)->order('create_time desc')->limit(1)->value('order_id');
- return $_order_id;
- }
- }
|