package com.chaozhanggui.admin.system.controller;

import com.chaozhanggui.admin.system.annotation.OpLog;
import com.chaozhanggui.admin.system.model.LoginReq;
import com.chaozhanggui.admin.system.service.Userservice;
import com.chaozhanggui.common.system.config.RespBody;
import com.chaozhanggui.common.system.util.IPUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 登录相关接口
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {


    @Autowired
    Userservice userservice;

    /**
     * 登录
     * @param request
     * @param loginReq
     * @return
     */
    @OpLog(opName = "用户登录", opDetail = "用户登录", opCode = "USER-LOGIN")
    @RequestMapping(value = "doLogin",method = RequestMethod.POST)
    public RespBody doLogin(HttpServletRequest request, @RequestBody LoginReq loginReq){
        try {
            return userservice.doLogin(loginReq.getLoginName(),loginReq.getPassword(),loginReq.getUserType(), IPUtil.getIpAddress(request));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new RespBody("999998");
    }

    /**
     * 获取用户信息
     * @param request
     * @param loginName 用户名称
     * @param token token
     * @return
     */
    @RequestMapping(value = "getUserInfoByToken",method = RequestMethod.POST)
    public RespBody getUserInfoByToken(HttpServletRequest request,@RequestHeader("loginName") String loginName,@RequestHeader("token") String token,@RequestHeader("userId") String userId){
        try {
            return userservice.getUserInfoByToken(loginName,token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new RespBody("999997");
    }

    /**
     * 获取首页数据
     * @param loginName
     * @param token
     * @param userId
     * @return
     */
    @RequestMapping("getIndexData")
    public RespBody getIndexData(@RequestHeader("loginName") String loginName,@RequestHeader("token") String token,@RequestHeader("userId") String userId){
        return userservice.getIndexData(Integer.valueOf(userId));
    }


    /**
     * 获取近七日收单额
     * @param loginName
     * @param token
     * @param userId
     * @return
     */
    @GetMapping("getDayOrder")
    public RespBody getDayOrder(@RequestHeader("loginName") String loginName,@RequestHeader("token") String token,@RequestHeader("userId") String userId
                               , @RequestParam("reqUserId") String reqUserId
                                ){
        return userservice.getDayOrder(reqUserId);
    }

    /**
     * 获取近一年的收单额
     * @param loginName
     * @param token
     * @param userId
     * @return
     */

    @GetMapping("getMonthOrder")
    public RespBody getMonthOrder(@RequestHeader("loginName") String loginName,@RequestHeader("token") String token,@RequestHeader("userId") String userId,
                                  @RequestParam("year") String year, @RequestParam("reqUserId") String reqUserId
                                  ){
        return userservice.getMonthOrder(reqUserId,year);
    }

    /**
     * 获取近七日收益
     * @param loginName
     * @param token
     * @param userId
     * @return
     */
    @GetMapping("getDayProfit")
    public RespBody getDayProfit(@RequestHeader("loginName") String loginName,@RequestHeader("token") String token,@RequestHeader("userId") String userId, @RequestParam("reqUserId") String reqUserId){
        return userservice.getDayProfit(reqUserId);
    }




    /**
     * 获取近一年的收益数据
     * @param loginName
     * @param token
     * @param userId
     * @return
     */
    @GetMapping("getMonthProfit")
    public RespBody getMonthProfit(@RequestHeader("loginName") String loginName,@RequestHeader("token") String token,@RequestHeader("userId") String userId,
                                   @RequestParam("year") String year, @RequestParam("reqUserId") String reqUserId
                                   ){
        return userservice.getMonth(reqUserId,year);
    }

    /**
     * 退出
     * @param loginName
     * @param token
     * @param userId
     * @return
     */
    @OpLog(opName = "用户退出", opDetail = "用户退出", opCode = "USER-OUT")
    @RequestMapping("outlogin")
    public RespBody outLogin(@RequestHeader("loginName") String loginName,@RequestHeader("token") String token,@RequestHeader("userId") String userId){
        try {
            return  userservice.outLogin(loginName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new RespBody("999996");
    }


    /**
     * 提现申请查询
     * @param loginName
     * @param token
     * @param userId
     * @param userName 用户名称
     * @param merchantCode 商户号
     * @param status 状态
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getOutFlow")
    public RespBody getOutFlow(@RequestHeader("loginName") String loginName,@RequestHeader("token") String token,@RequestHeader("userId") String userId
        ,@RequestParam("userName") String userName,@RequestParam("merchantCode") String merchantCode,@RequestParam("status") String status,@RequestParam("pageNum") Integer pageNum
                               ,@RequestParam("pageSize") Integer pageSize
    ){
        return userservice.getOutFlow(userId, userName, merchantCode, status, pageNum, pageSize);
    }

    /**
     * 获取提现流水
     * @param loginName
     * @param token
     * @param userId
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getUserOutFlow")
    public RespBody getUserOutFlow(@RequestHeader("loginName") String loginName,@RequestHeader("token") String token,@RequestHeader("userId") String userId
            ,@RequestParam("status") String status,@RequestParam("pageNum") Integer pageNum
            ,@RequestParam("pageSize") Integer pageSize
    ){
        return userservice.getUserOutFlow(userId,status,pageNum,pageSize);
    }

    /**
     * 提现审核
     * @param loginName
     * @param token
     * @param userId
     * @param id 流水id
     * @param status 1 审核通过 2 审核驳回
     * @return
     */
    @OpLog(opName = "提现审核", opDetail = "提现审核", opCode = "OUT-AUDIT")
    @GetMapping("modifyOutFlow")
    public RespBody modifyOutFlow(@RequestHeader("loginName") String loginName,@RequestHeader("token") String token,@RequestHeader("userId") String userId
        ,@RequestParam("id") Integer id,@RequestParam("status") String status
    ){
        try {
            return userservice.modifyOutFlow(id,userId,status);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new RespBody("999995");
    }


    /**
     * 查询用户总收益和可提收益，冻结收益 ，已提收益
     * @param loginName
     * @param token
     * @param userId
     */
    @RequestMapping("getUserBalance")
    public RespBody getUserBalance(@RequestHeader("loginName") String loginName,@RequestHeader("token") String token,@RequestHeader("userId") String userId){
        return userservice.getUserBalance(userId);
    }

    /**
     * 提现
     * @param loginName
     * @param token
     * @param userId
     * @param amount
     * @return
     */
    @OpLog(opName = "提现申请", opDetail = "提现申请", opCode = "OUT-APPLY")
    @RequestMapping("withdrawalProfit")
    public RespBody withdrawalProfit(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId,
                                     @RequestParam("amount") BigDecimal amount
                                     ){
        try {
            return userservice.withdrawalProfit(userId, amount);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
