package com.ylx.web.controller.massage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.core.controller.BaseController;
import com.ylx.common.core.domain.R;
import com.ylx.common.exception.ServiceException;
import com.ylx.massage.domain.TTxRecord;
import com.ylx.massage.service.TTxRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import java.util.List;

/**
 * 提现表 前端控制器
 */
@RestController
@RequestMapping("api/tx/record")
@Api(tags = {"提现记录"})
@Slf4j
public class TTxRecordController extends BaseController {
    @Resource
    private TTxRecordService recordService;


    /**
     * 添加申请记录
     * cash
     * @return
     */
    @ApiOperation("申请提现")
    @RequestMapping(value = "wx/applicationCash", method = RequestMethod.POST)
    public R applicationCash(@RequestBody TTxRecord record) {
        try {
            return R.ok(recordService.applicationCash(record));
        } catch (ServiceException s) {
            log.error(s.getMessage());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            StackTraceElement stackTraceElement= e.getStackTrace()[0];
            log.error("系统出错，错误信息:"+ e +" at "+stackTraceElement.getClassName()+"."+stackTraceElement.getMethodName()+":"+stackTraceElement.getLineNumber());
            return R.fail("系统异常");
        }
    }
    @ApiOperation("查询上次提现记录")
    @RequestMapping(value = "wx/last", method = RequestMethod.POST)
    public R<TTxRecord> last(@RequestBody TTxRecord record) {
        try {
            LambdaQueryWrapper<TTxRecord> tTxRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
            tTxRecordLambdaQueryWrapper.eq(TTxRecord::getcOpenId,record.getcOpenId()).orderByDesc(TTxRecord::getcId).last("LIMIT 1");;
            return R.ok(recordService.getOne(tTxRecordLambdaQueryWrapper));
        } catch (ServiceException s) {
            log.error(s.getMessage());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            StackTraceElement stackTraceElement= e.getStackTrace()[0];
            log.error("系统出错，错误信息:"+ e +" at "+stackTraceElement.getClassName()+"."+stackTraceElement.getMethodName()+":"+stackTraceElement.getLineNumber());
            return R.fail("系统异常");
        }
    }

    @ApiOperation("审核通过/驳回")
    @RequestMapping(value = "wx/auditing", method = RequestMethod.POST)
    public R auditing(@RequestBody TTxRecord record) {
        try {
            return R.ok(recordService.auditing(record));
        } catch (ServiceException s) {
            log.error(s.getMessage());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            StackTraceElement stackTraceElement= e.getStackTrace()[0];
            log.error("系统出错，错误信息:"+ e +" at "+stackTraceElement.getClassName()+"."+stackTraceElement.getMethodName()+":"+stackTraceElement.getLineNumber());
            return R.fail("系统异常");
        }
    }

    /**
     * 管理员获取提现信息
     *
     * @return
     */
    @RequestMapping(value = "select", method = RequestMethod.GET)
    @ApiOperation("分页查询数据")
    public R<Page<TTxRecord>> select(Page<TTxRecord> page, TTxRecord txRecord) {
        // 获取页数
        LambdaQueryWrapper<TTxRecord> tTxRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tTxRecordLambdaQueryWrapper.eq(null != txRecord.getnStatus(), TTxRecord::getnStatus, txRecord.getnStatus()).
                eq(StringUtils.isNotBlank(txRecord.getcPhone()), TTxRecord::getcPhone, txRecord.getcPhone()).
                eq(StringUtils.isNotBlank(txRecord.getcName()), TTxRecord::getcName, txRecord.getcName()).
                eq(StringUtils.isNotBlank(txRecord.getcOpenId()), TTxRecord::getcOpenId, txRecord.getcOpenId()).
                orderByDesc(TTxRecord::getDtCreateTime);
        if(null != this.getDeptId() && this.getDeptId() != 100){
            tTxRecordLambdaQueryWrapper.eq(TTxRecord::getDeptId,this.getDeptId());
        }
        return R.ok(recordService.page(page, tTxRecordLambdaQueryWrapper));
    }


    /**
     * 商家获取提现记录
     *
     * @return
     */
    @ApiOperation("商家获取提现记录")
    @RequestMapping(value = "wx/select", method = RequestMethod.GET)
    public R<List<TTxRecord>> wxSelect(TTxRecord txRecord) {
        LambdaQueryWrapper<TTxRecord> tTxRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tTxRecordLambdaQueryWrapper.eq(null != txRecord.getcOpenId(), TTxRecord::getcOpenId, txRecord.getcOpenId()).
                orderByDesc(TTxRecord::getDtCreateTime);
        return R.ok(recordService.list(tTxRecordLambdaQueryWrapper));
    }
}
