| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace app\shop\model\plus\agent;
- use app\common\library\easywechat\AppWx;
- use app\common\library\easywechat\AppMp;
- use app\common\service\message\MessageService;
- use app\common\service\order\OrderService;
- use app\common\library\easywechat\WxPay;
- use app\common\model\plus\agent\Cash as CashModel;
- use app\shop\model\user\User as UserModel;
- use app\shop\service\order\ExportService;
- /**
- * 分销商提现明细模型
- */
- class Cash extends CashModel
- {
- /**
- * 获取器:申请时间
- */
- public function getAuditTimeAttr($value)
- {
- return $value > 0 ? date('Y-m-d H:i:s', $value) : 0;
- }
- /**
- * 获取器:打款方式
- */
- public function getPayTypeAttr($value)
- {
- return ['text' => $this->payType[$value], 'value' => $value];
- }
- /**
- * 获取分销商提现列表
- */
- public function getList($user_id = null, $apply_status = -1, $pay_type = -1, $search = '')
- {
- $model = $this;
- // 构建查询规则
- $model = $model->alias('cash')
- ->with(['user'])
- ->field('cash.*, agent.real_name, agent.mobile, user.nickName, user.avatarUrl')
- ->join('user', 'user.user_id = cash.user_id')
- ->join('agent_user agent', 'agent.user_id = cash.user_id')
- ->order(['cash.create_time' => 'desc']);
- // 查询条件
- if ($user_id > 0) {
- $model = $model->where('cash.user_id', '=', $user_id);
- }
- if (!empty($search)) {
- $model = $model->where('agent.real_name|agent.mobile', 'like', '%' . $search . '%');
- }
- if ($apply_status > 0) {
- $model = $model->where('cash.apply_status', '=', $apply_status);
- }
- if ($pay_type > 0) {
- $model = $model->where('cash.pay_type', '=', $pay_type);
- }
- // 获取列表数据
- return $model->paginate(15);
- }
- /**
- * 分销商提现审核
- */
- public function submit($param)
- {
- $data = ['apply_status' => $param['apply_status']];
- if ($param['apply_status'] == 30) {
- $data['reject_reason'] = $param['reject_reason'];
- }
- // 更新申请记录
- $data['audit_time'] = time();
- self::update($data, ['id' => $param['id']]);
- // 提现驳回:解冻分销商资金
- if ($param['apply_status'] == 30) {
- User::backFreezeMoney($param['user_id'], $param['money']);
- }
- // 发送模板消息
- (new MessageService)->cash($this);
- return true;
- }
- /**
- * 确认已打款
- */
- public function money()
- {
- $this->startTrans();
- try {
- // 更新申请状态
- $data = ['apply_status' => 40, 'audit_time' => time()];
- self::update($data, ['id' => $this['id']]);
- // 更新分销商累积提现佣金
- User::totalMoney($this['user_id'], $this['money']);
- // 记录分销商资金明细
- Capital::add([
- 'user_id' => $this['user_id'],
- 'flow_type' => 20,
- 'money' => -$this['money'],
- 'describe' => '申请提现',
- ]);
- // 发送模板消息
- //(new Message)->withdraw($this);
- // 事务提交
- $this->commit();
- return true;
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- $this->rollback();
- return false;
- }
- }
- /**
- * 分销商提现:微信支付企业付款
- */
- public function wechatPay()
- {
- // 微信用户信息
- $user = UserModel::detail($this['user_id']);
- // 生成付款订单号
- $orderNO = OrderService::createOrderNo();
- // 付款描述
- $desc = '分销商提现付款';
- // 微信支付api:企业付款到零钱
- $open_id = '';
- $app = null;
- if ($user['reg_source'] == 'mp') {
- $app = AppMp::getWxPayApp($user['app_id']);
- $open_id = $user['mpopen_id'];
- } else if ($user['reg_source'] == 'wx') {
- $app = AppWx::getWxPayApp($user['app_id']);
- $open_id = $user['open_id'];
- }
- if ($open_id == '') {
- $this->error = '未找到用户open_id';
- return false;
- }
- $WxPay = new WxPay($app);
- // 请求付款api
- if ($WxPay->transfers($orderNO, $open_id, $this['money'], $desc)) {
- // 确认已打款
- $this->money();
- return true;
- }
- return false;
- }
- /*
- *统计提现总数量
- */
- public function getAgentCashTotal()
- {
- return $this->where('apply_status', '=', 10)->count();
- }
- /*
- *统计提现总数量
- */
- public function getAgentApplyTotal()
- {
- return $this->where('apply_status', '=', '10')->count();
- }
- /**
- * 导出分销商提现
- */
- public function exportList($user_id = null, $apply_status = -1, $pay_type = -1, $search = '')
- {
- $model = $this;
- // 构建查询规则
- $model = $model->alias('cash')
- ->with(['user'])
- ->field('cash.*, agent.real_name, agent.mobile, user.nickName, user.avatarUrl')
- ->join('user', 'user.user_id = cash.user_id')
- ->join('agent_user agent', 'agent.user_id = cash.user_id')
- ->order(['cash.create_time' => 'desc']);
- // 查询条件
- if ($user_id > 0) {
- $model = $model->where('cash.user_id', '=', $user_id);
- }
- if (!empty($search)) {
- $model = $model->where('agent.real_name|agent.mobile', 'like', '%' . $search . '%');
- }
- if ($apply_status > 0) {
- $model = $model->where('cash.apply_status', '=', $apply_status);
- }
- if ($pay_type > 0) {
- $model = $model->where('cash.pay_type', '=', $pay_type);
- }
- // 获取列表数据
- $list = $model->select();
- // 导出excel文件
- (new Exportservice)->cashList($list);
- }
- }
|