package com.muchinfo.mtp3century.controller;

import com.muchinfo.mtp3century.service.ITradingAccountService;
import com.muchinfo.mtp3century.utils.CenturyDecryptUtil;
import com.muchinfo.mtp3common.enumtype.Constants;
import com.muchinfo.mtp3common.enumtype.MessageType;
import com.muchinfo.mtp3common.utils.JsonUtils;
import com.muchinfo.mtp3century.utils.AjaxResult;
import com.muchinfo.mtp3common.vo.PageResult;
import com.muchinfo.mtp3pojos.entity.Loginaccount;
import com.muchinfo.mtp3pojos.vo.tradingacc.InitAddResult;
import com.muchinfo.mtp3pojos.vo.tradingacc.TradingAccountAddParam;
import com.muchinfo.mtp3pojos.vo.tradingacc.TradingAccountQueryParam;
import com.muchinfo.mtp3pojos.vo.tradingacc.TradingAccountQueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RestController
@RequestMapping("/tradingAcc")
@Api(value = "资金账户API", description = "交易账号管理")
public class TradingAccountController {

    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private ITradingAccountService iTradingAccountService;

    @ApiOperation("账号管理-->交易账号管理-->获取列表")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    @RequestMapping(method = RequestMethod.GET, value = "/query")
    public PageResult<TradingAccountQueryResult> query(TradingAccountQueryParam param, HttpServletRequest request) {
        return iTradingAccountService.query(request, param);
    }

    @ApiOperation("账号管理-->交易账号管理-->详情")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.GET, value = "/view")
    public AjaxResult<Loginaccount> view(Long loginid, HttpServletRequest request) {
        return iTradingAccountService.view(request, loginid);
    }

    @ApiOperation("账号管理-->交易账号管理-->重置密码")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.GET, value = "/resetPwd")
    public AjaxResult resetPwd(Long loginid, HttpServletRequest request) {
        return iTradingAccountService.resetPwd(request, loginid);
    }

    @ApiOperation("账号管理-->交易账号管理-->锁定")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.GET, value = "/lockAccount")
    public AjaxResult lockAccount(Long loginid, Integer status, HttpServletRequest request) {
        return iTradingAccountService.lockAccount(request, status, loginid);
    }

    @ApiOperation("账号管理-->交易账号管理-->注销")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.GET, value = "/delAccount")
    public AjaxResult delAccount(Long loginid, HttpServletRequest request) {
        return iTradingAccountService.delAccount(request, loginid);
    }

    @ApiOperation("账号管理-->交易账号管理-->恢复")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.GET, value = "/undelAccount")
    public AjaxResult undelAccount(Long loginid, HttpServletRequest request) {
        return iTradingAccountService.undelAccount(request, loginid);
    }

    @ApiOperation("账号管理-->交易账号管理-->新增")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.POST, value = "/add")
    public AjaxResult add(@RequestBody String param, HttpServletRequest request) {
        try {
            TradingAccountAddParam addParam = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), TradingAccountAddParam.class);
            return iTradingAccountService.add(request, addParam);
        } catch (Exception e) {
            log.error("交易商修改失败，失败原因：" + Arrays.toString(e.getStackTrace()));
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }

    @ApiOperation("账号管理-->交易账号管理-->新增-->初始化登录账户及授权资金账户")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.GET, value = "/initAdd")
    public AjaxResult<InitAddResult> initAdd(HttpServletRequest request) {
        return iTradingAccountService.initAdd(request);
    }


    @ApiOperation("账号管理-->交易账号管理-->修改")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.POST, value = "/edit")
    public AjaxResult edit(@RequestBody String param, HttpServletRequest request) {
        try {
            TradingAccountAddParam addParam = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), TradingAccountAddParam.class);
            return iTradingAccountService.edit(request, addParam);
        } catch (Exception e) {
            log.error("交易商修改失败，失败原因：" + Arrays.toString(e.getStackTrace()));
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }
}
