package com.lightinit.hsdataportal.controller.open;


import com.lightinit.hsdataportal.entity.Order;
import com.lightinit.hsdataportal.model.DicAccountManageTransactionTransType;
import com.lightinit.hsdataportal.model.LoginUser;
import com.lightinit.hsdataportal.model.MyAccountManageRechargeVOModel;
import com.lightinit.hsdataportal.model.ResultState;
import com.lightinit.hsdataportal.service.IOrderService;
import com.lightinit.hsdataportal.service.open.IMyAccountService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping(value = "/open/myAccount")
public class MyAccountController {

    @Autowired
    private IMyAccountService myAccountService ;
    @Autowired
    private IOrderService iOrderService;

    @RequiresAuthentication
    @RequestMapping(value = "index.htm")
    public ModelAndView index(HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ModelAndView modelAndView = new ModelAndView("/open/myAccount/index") ;
        List<MyAccountManageRechargeVOModel> outputModel = myAccountService.queryRechargeList(loginUser.getId(), DicAccountManageTransactionTransType.RECHARGE);
        modelAndView.addObject("recharges",outputModel) ;
        List<MyAccountManageRechargeVOModel> outputModel1 = myAccountService.queryTransactionList(loginUser.getId(), DicAccountManageTransactionTransType.TRANSACTION);
        modelAndView.addObject("transactions",outputModel1) ;
        BigDecimal balance = myAccountService.getMyBalance(loginUser.getId()) ;
        modelAndView.addObject("balance",balance) ;
        return modelAndView ;
    }

    /**
     * 跳转充值页面
     * @param balance
     * @return
     */
    @RequestMapping(value = "recharge.htm")
    public ModelAndView recharge(BigDecimal balance) {
        BigDecimal b = BigDecimal.valueOf(0.0);
        if(balance==null) {
            balance=b;
        }
        ModelAndView modelAndView = new ModelAndView("/open/myAccount/recharge");
        modelAndView.addObject("balance",balance);
        return modelAndView;
    }

    /**
     * 生成充值订单
     * @param session
     * @return
     */
    @RequestMapping(value = "toRecharge.action")
    @ResponseBody
    public ResultState toRecharge(HttpSession session,Order order) {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ResultState resultState = iOrderService.generateOrder(loginUser.getId(),order);
        return resultState;
    }

    /**
     * 跳转支付页面
     * @param orderAmount
     * @return
     */
    @RequestMapping(value = "payment.htm")
    public ModelAndView payment(BigDecimal orderAmount,String orderNumber) {
    ModelAndView modelAndView = new ModelAndView("/open/myAccount/pay");
    modelAndView.addObject("transAmount",orderAmount);
    modelAndView.addObject("orderNumber",orderNumber);

    return modelAndView;
    }

}
