| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- /**
- * @package merchant
- *
- * @author xaboy
- * @day 2020/6/23
- *
- *
- */
- namespace app\controller\admin\user;
- use app\common\repositories\finance\FinanceRepository;
- use crmeb\basic\BaseController;
- use crmeb\services\SpreadsheetExcelService;
- use think\App;
- use think\facade\Db;
- use think\facade\Log;
- class UserScore extends BaseController
- {
- protected $repository;
- public function __construct(App $app)
- {
- parent::__construct($app);
- }
- public function deduct($uid)
- {
- $user = Db::name('user')->where('uid', $uid)->find();
- if (empty($user)) {
- return app('json')->fail('用户不存在');
- }
- $user_score = $user['score'];
- $deduct_score = $this->request->param('deduct_score', 0);
- $desc = $this->request->param('mark', '');
- if (empty($desc)) {
- return app('json')->fail('请填写扣减原因~');
- }
- if (!$deduct_score) {
- return app('json')->fail('扣减积分数不能为0~');
- }
- if ($deduct_score < 0) {
- return app('json')->fail('扣减积分数不能为负数');
- }
- if ($deduct_score > $user_score) {
- return app('json')->fail('扣减积分数不得大于可用积分数~');
- }
- //开启事务
- Db::startTrans();
- try {
- $ins_data['uid'] = $uid;
- $ins_data['reward_socre'] = $deduct_score;
- $ins_data['addtime'] = time();
- $ins_data['status'] = 1;
- $ins_data['mark'] = "线下积分兑换扣减积分";
- $ins_data['desc'] = $desc;
- $ins_data['surplus'] = $user_score - $deduct_score;
- $ins_data['type'] = 3;
- $ins_data['pm'] = 2;
- Db::name('user_sign_score')->insertGetId($ins_data);
- Db::name('user')->where('uid', $uid)->dec('score', $deduct_score)->update();
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- return app('json')->fail($e->getMessage());
- }
- return app('json')->success("成功");
- }
- public function excel()
- {
- $where = request()->params(['date']);
- $params = request()->params(['type', 'uid', 'pm', 'phone', 'status']);
- //商户ID
- $query = Db::name('user_sign_score')
- ->alias('uss')
- ->leftJoin('user u', 'u.uid=uss.uid')
- ->leftJoin('store_order o', 'o.order_id=uss.order_id')
- ->when(isset($params['uid']) && $params['uid'] != '', function ($query) use ($params) {
- $query->where('uss.uid', $params['uid']);
- })
- ->when(isset($params['pm']) && $params['pm'] != '', function ($query) use ($params) {
- $query->where('uss.pm', $params['pm']);
- })
- ->when(isset($params['status']) && $params['status'] != '', function ($query) use ($params) {
- $query->where('uss.status', $params['status']);
- })
- ->when(isset($params['phone']) && $params['phone'] != '', function ($query) use ($params) {
- $query->whereLike('u.phone', '%' . $params['phone'] . '%');
- })
- ->when(isset($params['type']) && $params['type'] != '', function ($query) use ($params) {
- $query->where('type', $params['type']);
- });
- $financeRepository = app()->make(FinanceRepository::class);
- $list = $financeRepository->date_where($query, $where['date'], 'uss,addtime', '-', 2)
- ->field('uss.id,uss.addtime,u.nickname,u.phone,uss.pm,uss.reward_socre,o.order_sn,uss.status,uss.mark,uss.desc')
- ->order('uss.id', 'desc')
- ->select()
- ->each(function ($item) {
- $item['mark'] = $item['mark'] . $item['desc'];
- $item['addtime'] = date('Y-m-d H:i:s', $item['addtime']);
- return $item;
- });
- foreach ($list as $k => $v) {
- $temp = array(
- $v['id'],
- $v['addtime'],
- $v['nickname'],
- $v['phone'],
- $v['reward_socre'],
- );
- $temp[] = $v['status'] == 1 ? '已结算': '未结算';
- $temp[] = $v['pm'] == 1 ? '增加': '扣减';
- $temp[] = $v['order_sn'] ?? '';
- $temp[] = $v['mark'] . $v['desc'];
- $arr[] = $temp;
- }
- $header = [
- 'ID', '创建时间', '用户昵称', '用户账号(手机号)', '积分数', '结算状态', '积分变动', '关联订单', '说明'
- ];
- $filename = '积分统计' . date('YmdHis') . '_' . rand(10000, 99999);
- $title = '积分统计';
- $name = '积分统计';
- $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);
- $data = [
- 'name' => $filename . '.' . $suffix,
- 'status' => 1,
- 'path' => '/' . $_path
- ];
- return app('json')->success('ok', $data);
- }
- }
|