package com.sifang.modules.rechargewallet.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.rechargewallet.service.RechargeWalletService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.persistence.Page;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author KongRan-WANG
 * 账户总额-充值记录-冻结金额 Controller
 * @date 2019/4/12 21:33
 */
@Controller
@RequestMapping("${adminPath}/recharge/wallet")
public class RechargeWalletController extends BaseController {

    @Autowired
    private RechargeWalletService reChargeService;

    @RequestMapping("toWalletPage")
    public String toTotalMoney(Page<Record> page, Model model) {
        if (page.getPageSize() < 20) {
            page.setPageSize(20);
        }
        Record re = reChargeService.getRechargeList();
        model.addAttribute("page", re);
        return "modules/factory/wallet/totalMoney";
    }

    //充值记录页面
    @RequestMapping("rechargeRecord")
    public String toWalletPage(Page<Record> page, HttpServletRequest request, Model model) {
        String month = request.getParameter("month");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String defaultStartTime = "";
        String defaultEndTime = "";
        if (StringUtils.isNotBlank(month)) {// 此时是从-月结账单列表-点击列表充值金额跳转过来的
            month = month + "-01 00:00:00";
            Date startTime = new Date();
            try {
                startTime = sdf.parse(month);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Calendar ca = Calendar.getInstance();
            ca.setTime(startTime);
            ca.add(Calendar.MONTH, 1);
            ca.add(Calendar.DATE, -1);
            Date endTime = ca.getTime();
            defaultStartTime = sdf.format(startTime);
            defaultEndTime = sdf.format(endTime);
        }
        model.addAttribute("defaultStartTime", defaultStartTime);
        model.addAttribute("defaultEndTime", defaultEndTime);
        return "modules/factory/wallet/rechargeInfo";
    }


    @ResponseBody
    @RequestMapping("frozenMoneyData")
    public Object getfrozenList(HttpServletRequest request, Page<Record> page) {
        if (page.getPageSize() < 20) {
            page.setPageSize(20);
        }
        User user = UserUtils.getUser();
        Map<String, String> map = getParams(request);
        List<Record> list = page.getList();
        LayuiPage pa = new LayuiPage(list, page.getCount());
        return pa.renderJson();
    }

}
