| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- <?php
- /**
- * @package merchant
- *
- * @author Qinii
- * @day 2020-06-15
- *
- *
- */
- namespace app\controller\admin\order;
- use crmeb\basic\BaseController;
- use app\common\repositories\store\ExcelRepository;
- use app\common\repositories\system\merchant\MerchantRepository;
- use app\common\repositories\store\order\StoreOrderRepository as repository;
- use crmeb\services\SpreadsheetExcelService;
- use think\App;
- use think\facade\Db;
- class Order extends BaseController
- {
- protected $repository;
- public function __construct(App $app, repository $repository)
- {
- parent::__construct($app);
- $this->repository = $repository;
- }
- public function lst($id)
- {
- [$page, $limit] = $this->getPage();
- $where = $this->request->params(['date','order_sn','order_type','keywords','username']);
- $where['reconciliation_type'] = $this->request->param('status', 1);
- $where['mer_id'] = $id;
- return app('json')->success($this->repository->adminGetList($where, $page, $limit));
- }
- public function markForm($id)
- {
- if (!$this->repository->getWhereCount([$this->repository->getPk() => $id]))
- return app('json')->fail('数据不存在');
- return app('json')->success(formToData($this->repository->adminMarkForm($id)));
- }
- public function mark($id)
- {
- if (!$this->repository->getWhereCount([$this->repository->getPk() => $id]))
- return app('json')->fail('数据不存在');
- $data = $this->request->params(['admin_mark']);
- $this->repository->update($id, $data);
- return app('json')->success('备注成功');
- }
- /**
- * TODO
- * @return mixed
- * @author Qinii
- * @day 2020-06-25
- */
- public function getAllList()
- {
- [$page, $limit] = $this->getPage();
- $where = $this->request->params(['type', 'date', 'mer_id','keywords','status','username','order_sn','order_ids','paid', 'zero_line_uid']);
- $pay_type=$this->request->param('pay_type');
- if($pay_type==1 || $pay_type==4){
- $where['pay_type']=$pay_type;
- }
- if($pay_type==6){
- //一壶茶红积分兑换的
- $where['mer_id']=496;
- $where['pay_type']=5;
- }
- // 导出
- $is_excel = request()->param('is_excel', 0);
- $result = $this->repository->merchantGetList($where, $page, $limit, $is_excel);
- if ($is_excel == 1) {
- $data = $result['list'];
- $arr = [];
- foreach ($data as $item) {
- $arr[] = [
- $item['order_sn'], // 订单编号
- $item['order_type'] == 0 ? "普通订单" : "核销订单", // 订单类型
- $item['user']['nickname'] ?? '', // 用户昵称
- $item['user']['phone'] ?? '', // 用户手机号
- $item['real_name'], // 收货人
- $item['merchant']['mer_name'] ?? '', // 商户名称
- // $item['orderProduct'][0]['cart_info']['product']['image'],
- ($item['orderProduct'][0]['cart_info']['product']['store_name'] ?? '') . ' | ' . ($item['orderProduct'][0]['cart_info']['productAttr']['sku'] ?? ''),
- $item['orderProduct'][0]['cart_info']['productAttr']['price'] ?? 0.00,
- $item['orderProduct'][0]['product_num'] ?? 0,
- $item['pay_price'], // 订单金额
- $item['paid'] == 0 ? "未支付" : "已支付", // 支付状态
- $item['fzone_pay_note'], // 支付方式
- $item['fzone_pay_jifen'], // 支付方式
- $item['fzone_pay_price'], // 支付方式
- $item['fzone_pay_jingdou'], // 支付方式
- $item['fzone_pay_vr'], // 支付方式
- $item['is_del'] == 0 ? ($item['status'] == 5 ? '待付款' : ($item['status'] == 0 ? '待发货' : ($item['status'] == 1 ? '待收货' : ($item['status'] == 2 ? '待评价' : ($item['status'] == 3 ? '已完成' : ($item['status'] == 4 ? '已申请退款' : ($item['status'] == -1 ? '已退款' : ($item['status'] == 6 ? '已取消' : '')))))))) : '已删除', // 订单状态
- $item['create_time'], // 下单时间
- ];
- }
- return app('json')->success($this->getStatisticsExcel([
- '订单编号',
- '订单类型',
- '用户昵称',
- '用户手机号',
- '收货人',
- '商户名称',
- '商品图片',
- // '商品名称',
- '商品单价',
- '商品数量',
- '订单金额',
- '支付状态',
- '支付方式',
- '支付积分',
- '支付现金',
- '支付京豆',
- '支付VR',
- '订单状态',
- '下单时间'
- ], $arr, '订单明细'));
- }
- return app('json')->success($result);
- }
- /**
- * 获取统计导出
- */
- public function getStatisticsExcel($header,$arr,$title)
- {
- $filename = $title.'_'.date('YmdHis').'_'.rand(10000,99999);
- $name = $filename;
- $info = '生成时间:' . date('Y-m-d H:i:s',time());
- $suffix='xlsx';
- $path='extract';
- unset($_instance);
- $_instance = SpreadsheetExcelService::instance_xin();
- $_path =$_instance
- ->createOrActive()
- ->setExcelHeader($header)
- ->setExcelTile($title, $name, $info)
- ->setExcelContent($arr)
- ->excelSave($filename, $suffix, $path);
- $domain = request()->domain();
- $data= [
- 'name' => $filename.'.'.$suffix,
- 'status' => 1,
- 'path' => $domain.'/'.$_path
- ];
- return $data;
- }
- /**
- * TODO 自提订单列表
- * @return mixed
- * @author Qinii
- * @day 2020-08-17
- */
- public function getTakeList()
- {
- [$page, $limit] = $this->getPage();
- $where = $this->request->params(['date','order_sn','keywords','username']);
- $where['take_order'] = 1;
- $where['status'] = -1;
- $where['verify_date'] = $where['date'];
- unset($where['date']);
- return app('json')->success($this->repository->merchantGetList($where, $page, $limit));
- }
- /**
- * TODO
- * @return mixed
- * @author Qinii
- * @day 2020-08-17
- */
- public function chart()
- {
- $where = $this->request->params(['type', 'date', 'mer_id','keywords','username','order_sn','order_ids','paid']);
- return app('json')->success($this->repository->adminOrderNumber(-1, $where));
- }
- /**
- * TODO 自提订单头部统计
- * @return mixed
- * @author Qinii
- * @day 2020-08-17
- */
- public function takeChart()
- {
- return app('json')->success($this->repository->adminOrderNumber(1));
- }
- /**
- * TODO 订单类型
- * @return mixed
- * @author Qinii
- * @day 2020-08-15
- */
- public function orderType()
- {
- return app('json')->success($this->repository->orderType([]));
- }
- public function detail($id)
- {
- $data = $this->repository->getOne($id, null);
- if (!$data)
- return app('json')->fail('数据不存在');
- return app('json')->success($data);
- }
- /**
- * TODO 快递查询
- * @param $id
- * @return mixed
- * @author Qinii
- * @day 2020-06-25
- */
- public function express($id)
- {
- if (!$this->repository->getWhereCount(['order_id' => $id, 'delivery_type' => 1]))
- return app('json')->fail('订单信息或状态错误');
- return app('json')->success($this->repository->express($id));
- }
- public function reList($id)
- {
- [$page, $limit] = $this->getPage();
- $where = ['reconciliation_id' => $id, 'type' => 0];
- return app('json')->success($this->repository->reconList($where, $page, $limit));
- }
- /**
- * TODO 导出文件
- * @author Qinii
- * @day 2020-07-30
- */
- public function excel()
- {
- $where = $this->request->params(['type', 'date', 'mer_id','keywords','status','username','order_sn']);
- app()->make(ExcelRepository::class)->create($where, $this->request->adminId(), 'order',0);
- return app('json')->success('开始导出数据');
- }
- /**
- * 养老金打款凭证 列表
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function order_red_list()
- {
- [$page, $limit] = $this->getPage();
- $where = $this->request->params(['status', 'date']);
- // $where['mer_id'] = $this->request->merId();
- $query = Db::name('enterprise_red_order_voucher')->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
- getModelTime($query, $where['date']);
- })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
- $query->where('status',$where['status']);
- });
- $data['count'] = $query->count();
- $data['list'] = $query->page($page, $limit)->order('id desc')->select();
- return app('json')->success('ok', $data);
- }
- /**
- * 养老金提交打款凭证 审核
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function order_red_status()
- {
- $post= $this->request->params(['id', 'status', 'refund_message']);
- $voucher=Db::name('enterprise_red_order_voucher')->find($post['id']);
- if(!$voucher){
- return app('json')->fail('订单信息错误');
- }
- if($voucher['status']!=0){
- return app('json')->fail('请勿重复操作');
- }
- if($post['status']==2){
- $update['status']=2;
- $update['refund_message']=$post['refund_message'];
- if($post['refund_message']==''){
- return app('json')->fail('请输入不通过原因');
- }
- $is_red_ylj=0;
- }else{
- $update['status']=1;
- $is_red_ylj=2;
- }
- $update['status_time']=date('Y-m-d H:i:s');
- $re=Db::name('enterprise_red_order_voucher')->where('id',$post['id'])->update($update);
- Db::name('store_order')->whereIn('order_id',$voucher['order_ids'])->where('is_red_ylj',1)->update(['is_red_ylj'=>$is_red_ylj]);
- $where['o.paid'] = 1;
- // $where['o.pay_type'] = 5;
- $where['u.commission_type'] = 5;
- $where['u.gzc'] = 3;
- $where['u.status'] = 0;
- $bill_id = Db::name('store_order')
- ->alias('o')
- ->join('user_bill u', 'u.order_sn=o.order_sn')
- ->where($where)
- ->whereIN('o.pay_type',[0,5])
- ->whereIn('o.order_id', $voucher['order_ids'])
- ->column('u.bill_id');
- Db::name('user_bill')->whereIn('bill_id',$bill_id)->update(['status'=>1,'gzc'=>0]);
- if($re){
- return app('json')->success('操作成功');
- }
- return app('json')->fail('操作失败');
- }
- /**
- * 撤回订单,并撤回订单奖励
- */
- public function killOrderRewards(){
- $group_order_id = request()->param('group_order_id');
- $order_id = request()->param('order_id');
- $uid = request()->param('uid');
- if (empty($order_id) || empty($group_order_id) || empty($uid)) {
- return app('json')->fail('参数缺失');
- }
- try {
- Db::startTrans();
- //查找是否有推荐人
- $spreadUid = Db::name('user')->where('uid', $uid)->value('spread_uid');
- //取出pv
- $pv = $this->getValue('user_pv_log', [['order_id', '=', $order_id], ['status', '=', 1],], 'pv');
- //取出 vr
- $payVr = $this->getValue('user_sign_vr', [['order_id', '=', $order_id], ['type', '=', 2],], 'number');
- //取出购买赠送积分 / 支出积分
- $score = $this->getValue('user_sign_score', [['order_id', '=', $order_id], ['status', '=', 1], ['source_type', '=', 3], ['uid', '=', $uid], ['pm', '=', 1],], 'reward_socre');
- $payScore = $this->getValue('user_sign_score', [['order_id', '=', $order_id], ['status', '=', 1], ['pm', '=', 2], ['uid', '=', $uid],], 'reward_socre');
- // 贡献值
- $gongXian = $this->getValue('user_sign_gongxian', [['order_id', '=', $order_id], ['status', '=', 1], ['type', '=', 1], ['uid', '=', $uid],], 'number');
- // 养老金
- $yangLaoJin = $this->getValue('user_bill', [['link_id', '=', $order_id], ['status', '=', 1], ['pm', '=', 1], ['uid', '=', $uid],], 'number');
- if($spreadUid){
- $spreadScore = 0;
- $spreadGongXian = 0;
- $spreadYlj = 0;
- $spreadYongJin = 0;
- $spreadScore = $this->getValue('user_sign_score', [['order_id', '=', $order_id], ['uid', '=', $spreadUid], ['status', '=', 1],], 'reward_socre');
- $spreadGongXian = $this->getValue('user_sign_gongxian', [['order_id', '=', $order_id], ['status', '=', 1], ['type', '=', 1], ['uid', '=', $spreadUid],], 'number');
- $spreadYlj = $this->getValue('user_bill', [['link_id', '=', $order_id], ['commission_type', '=', 5], ['uid', '=', $spreadUid],], 'number'); //推荐获得养老金
- $spreadYongJin = $this->getValue('user_bill', [['link_id', '=', $order_id], ['commission_type', '=', 3], ['uid', '=', $spreadUid],], 'number'); //推荐获得佣金
- $spreadUser = Db::name('user')->where('uid', $spreadUid)->find();
- Db::name('backup_user')->insert($spreadUser);
- Db::name('user')->where('uid', $spreadUid)
- ->update([
- 'score' => Db::raw("score - {$spreadScore}"),
- 'per_contribute' => Db::raw("per_contribute - {$spreadGongXian}"),
- 'brokerage_price' => Db::raw("brokerage_price - {$spreadYongJin}"),
- 'annuity' => Db::raw("annuity - {$spreadYlj}"),
- ]);
- $userhjm1 =[
- 'uid' => $spreadUid,
- 'order_id' => $order_id,
- 'score' => $spreadScore,
- 'per_contribute' => $spreadGongXian,
- 'brokerage_price' => $spreadYongJin,
- 'user' => 2,
- ];
- Db::name('backup_user_change')->insert($userhjm1);
- }
- //扫吗消费
- $smSpreadUid = $this->getValue('user_sign_score', [['order_id', '=', $order_id], ['mark','like','%扫码%'], ['status', '=', 1],], 'uid');
- $smSpreadScore = $this->getValue('user_sign_score', [['order_id', '=', $order_id], ['uid','=',$smSpreadUid], ['status', '=', 1],], 'reward_socre');
- //第二推荐
- $spreadYongJinTwoUid = $this->getValue('user_bill', [['link_id', '=', $order_id], ['commission_type', '=', 3], ['uid', 'not in', [$uid, $spreadUid]],], 'uid');
- $spreadYongJinTwo = $this->getValue('user_bill', [['link_id', '=', $order_id], ['commission_type', '=', 3], ['uid', '=', $spreadYongJinTwoUid],], 'number');
- //更新对应的user表
- $uscore = $payScore - $score;
- $user = Db::name('user')->where('uid', $uid)->find();
- Db::name('backup_user')->insert($user);
- Db::name('user')->where('uid', $uid)
- ->update([
- 'pv' => Db::raw("pv - {$pv}"),
- 'vr' => Db::raw("vr + {$payVr}"),
- 'score' => Db::raw("score + {$uscore}"),
- 'contribute' => Db::raw("contribute - {$gongXian}"),
- 'annuity' => Db::raw("annuity - {$yangLaoJin}"),
- ]);
- $userhjm =[
- 'uid' => $uid,
- 'order_id' => $order_id,
- 'pv' => $pv,
- 'vr' => $payVr,
- 'score' => $uscore,
- 'contribute' => $gongXian,
- 'annuity' => $yangLaoJin,
- 'user' => 1,
- ];
- Db::name('backup_user_change')->insert($userhjm);//具体修改插入对应表
- //第二推荐
- if($spreadYongJinTwoUid) {
- $spreadUserTwo = Db::name('user')->where('uid', $spreadYongJinTwoUid)->find();
- Db::name('backup_user')->insert($spreadUserTwo);
- Db::name('user')->where('uid', $spreadYongJinTwoUid)->dec('brokerage_price', $spreadYongJinTwo)->update();
- $userhjm2 =[
- 'uid' => $spreadYongJinTwoUid,
- 'order_id' => $order_id,
- 'brokerage_price' => $spreadYongJinTwo,
- 'user' => 3,
- ];
- Db::name('backup_user_change')->insert($userhjm2);
- }//扫码消费
- if($smSpreadUid){
- $smUserTwo = Db::name('user')->where('uid', $smSpreadUid)->find();
- Db::name('backup_user')->insert($smUserTwo);
- Db::name('user')->where('uid', $smSpreadUid)->dec('score', $smSpreadScore)->update();
- $userhjm3 =[
- 'uid' => $smSpreadUid,
- 'order_id' => $order_id,
- 'score' => $smSpreadScore,
- 'user' => 4,
- ];
- Db::name('backup_user_change')->insert($userhjm3);
- }
- // 删除订单相关表p;
- $this->backupAndDelete('store_order', ['group_order_id' => $group_order_id]);
- $this->backupAndDelete('store_group_order', ['group_order_id' => $group_order_id]);
- $this->backupAndDelete('user_pv_log', ['order_id' => $order_id]);
- $this->backupAndDelete('user_sign_vr', ['order_id' => $order_id]);
- $this->backupAndDelete('user_sign_score', ['order_id' => $order_id]);
- $this->backupAndDelete('user_sign_gongxian', ['order_id' => $order_id]);
- $this->backupAndDelete('user_bill', ['link_id' => $order_id]);
- $this->backupAndDelete('gzc_logs', ['link_id' => $order_id]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- return app('json')->fail('操作失败'. $e->getMessage());
- }
- return app('json')->success('操作成功');
- }
- private function backupAndDelete($table, array $where)//备份并删除
- {
- $backupTable = 'backup_' . $table;
- $rows = Db::name($table)->where($where)->select()->toArray();
- if ($rows) {
- Db::name($backupTable)->insertAll($rows);
- Db::name($table)->where($where)->delete();
- }
- }
- private function getValue($table, array $where, string $field, $default = 0)//查找
- {
- $value = Db::name($table)->where($where)->value($field);
- return $value ?? $default;
- }
- /**
- * 展示撤回订单列表
- */
- public function killOrderLst()
- {
- $query = Db::name('backup_store_order')->paginate(20)->toArray();
- return app('json')->success($query, '获取成功');
- }
- public function easterKillOrder(){
- $group_order_id = request()->param('group_order_id');
- $order_id = request()->param('order_id');
- if (empty($order_id) || empty($group_order_id)) {
- return app('json')->fail('参数缺失');
- }
- try {
- Db::startTrans();
- // 恢复订单相关表
- $this->easterOrder('store_order', ['group_order_id' => $group_order_id]);
- $this->easterOrder('store_group_order', ['group_order_id' => $group_order_id]);
- // 恢复 pv
- $this->easterOrder('user_pv_log', ['order_id' => $order_id]);
- // 恢复 vr
- $this->easterOrder('user_sign_vr', ['order_id' => $order_id]);
- // 恢复 积分
- $this->easterOrder('user_sign_score', ['order_id' => $order_id]);
- // 恢复 贡献值
- $this->easterOrder('user_sign_gongxian', ['order_id' => $order_id]);
- // 恢复 佣金/养老金等
- $this->easterOrder('user_bill', ['link_id' => $order_id]);
- // 恢复 公证处
- $this->easterOrder('gzc_logs', ['link_id' => $order_id]);
- //恢复用户奖励
- $this->easterOrder('user_change', ['order_id' => $order_id]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- return app('json')->fail('撤回失败: '.$e->getMessage());
- }
- return app('json')->success('撤回成功');
- }
- private function easterOrder($table, array $where)
- {
- $backupTable = 'backup_' . $table;
- $rows = Db::name($backupTable)->where($where)->select()->toArray();
- if ($rows) {
- foreach ($rows as &$row) {
- unset($row['backup_time']);
- }
- Db::name($table)->insertAll($rows);
- Db::name($backupTable)->where($where)->delete();
- }
- }
- private function easterUserAward($table, array $where){
- $backupTable = 'backup_' . $table;
- $row = Db::name($backupTable)->where($where)->where('user', 1)->field('pv, vr, score, contribute, annuity,uid')->find();
- $uid = $row['uid'];
- $pv = $row['pv'];
- $vr = $row['vr'];
- $score= $row['score'];
- $contribute = $row['contribute'];
- $annuity = $row['annuity'];
- Db::name('user') //更新订单用户
- ->where('uid', $uid)
- ->update([
- 'pv' => Db::raw("pv + {$pv}"),
- 'vr' => Db::raw("vr - {$vr}"),
- 'score' => Db::raw("score - {$score}"),
- 'contribute' => Db::raw("contribute + {$contribute}"),
- 'annuity' => Db::raw("annuity + {$annuity}"),
- ]);
- $row2 = Db::name($backupTable)->where($where)->where('user', 2)->field('brokerage_price, score, per_contribute, annuity,uid')->find();
- if ($row2) {
- $spreadUid = $row2['uid'];
- $spreadScore = $row2['score'];
- $per_contribute = $row2['per_contribute'];
- $spreadAnnuity = $row2['annuity'];
- $spreadYongJin = $row2['brokerage_price'];
- Db::name('user') //更新推荐人
- ->where('uid', $spreadUid)
- ->update([
- 'score' => Db::raw("score + {$spreadScore}"),
- 'contribute' => Db::raw("contribute + {$per_contribute}"),
- 'annuity' => Db::raw("annuity + {$spreadAnnuity}"),
- 'brokerage_price' => Db::raw("brokerage_price + {$spreadYongJin}"),
- ]);
- }
- $row3 = Db::name($backupTable)->where($where)->where('user', 3)->field('brokerage_price,uid')->find();
- if ($row3) {
- $spreadUidTwo = $row3['uid'];
- $spreadYongJin2 = $row3['brokerage_price'];
- Db::name('user')
- ->where('uid', $spreadUidTwo)
- ->update([
- 'brokerage_price' => Db::raw("brokerage_price + {$spreadYongJin2}"),
- ]);
- }
- $row4 = Db::name($backupTable)->where($where)->where('user', 4)->field('score,uid')->find();
- if ($row4) {
- $spreadUidThree = $row4['uid'];
- $spreadScore3 = $row4['score'];
- Db::name('user')
- ->where('uid', $spreadUidThree)
- ->update([
- 'score' => Db::raw("score + {$spreadScore3}"),
- ]);
- }
- }
- }
|