package com.izouma.awesomeadmin.web;

import java.util.*;

import com.izouma.awesomeadmin.util.ExportExcelUtil;
import org.apache.commons.lang.StringUtils;
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.*;
import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.model.WxpayTemp;
import com.izouma.awesomeadmin.service.WxpayTempService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*  controller类
*/
@Controller
@RequestMapping("/wxpayTemp")
public class WxpayTempController {

    @Autowired
    private WxpayTempService wxpayTempService;

    /**
    * <p>获取全部记录。</p>
    */
    @RequiresAuthentication
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public Result all(WxpayTemp record) {
        List<WxpayTemp> pp = wxpayTempService.getWxpayTempList(record);
        return new Result(true, pp);
    }

    /**
    * <p>根据Id。</p>
    */
    @RequestMapping(value = "/getWxpayTemp", method = RequestMethod.GET)
    @ResponseBody
    public Result getWxpayTemp(@RequestParam(required = false, value = "id") String id) {
        WxpayTemp data = wxpayTempService.getWxpayTempById(id);
        return new Result(true, data);
    }

    /**
    * <p>根据条件获取。</p>
    */
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public Result getOne(WxpayTemp record) {
        WxpayTemp data = wxpayTempService.getWxpayTemp(record);
        return new Result(true, data);
    }


    /**
    * <p>分页查询。</p>
    */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Result page(Page page, WxpayTemp record) {
        Map<String, Object> result = new HashMap<>();

        List<WxpayTemp> pp =wxpayTempService.getWxpayTempByPage(page, record);

        result.put(AppConstant.PAGE, page);
        result.put("pp", pp);
        return new Result(true, result);
    }


    /**
    * <p>保存。</p>
    */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(WxpayTemp record) {
        boolean num = wxpayTempService.createWxpayTemp(record);
        if (num) {
        return new Result(true, record.getId());
        }
        return new Result(false, "保存异常");
    }

    /**
    * <p>更新信息。</p>
    */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result updateWxpayTemp(WxpayTemp record) {
        boolean num = wxpayTempService.updateWxpayTemp(record);
        if (num) {
        return new Result(true, "保存成功");
        }
        return new Result(false, "保存异常");
    }

    /**
    * <p>删除。</p>
    */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteWxpayTemp(@RequestParam(required = true, value = "id") String id) {

        boolean num = wxpayTempService.deleteWxpayTemp(id);
        if (num) {
        return new Result(true, "删除成功");
        }
        return new Result(false, "删除异常");
    }

    /**
    * 导出Excel
    * @param request
    * @param response
    * @param record
    * @throws Exception
    */
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    @ResponseBody
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, WxpayTemp record) throws Exception {

    List<WxpayTemp> wxpayTemps = wxpayTempService.getWxpayTempList(record);


        String sheetName = "wxpay_temp";
        String titleName = "微信支付记录数据表";
        String fileName = "微信支付记录表";
        int columnNumber = 18;
        int[] columnWidth = { 20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20 };
        String[] columnName = {  "" ,   "删除标识" ,   "更新时间" ,   "更新人" ,   "创建时间" ,   "创建人" ,   "订单" ,   "用户" ,   "金额" ,   "订单" ,   "微信订单" ,   "类型" ,   "退款单" ,   "退款数量" ,   "余额" ,   "积分" ,   "现金" ,   "openId"  };
        String[][] dataList = new String[wxpayTemps.size()][18];

        for (int i = 0; i < wxpayTemps.size(); i++) {

                        dataList[i][0] = String.valueOf(wxpayTemps.get(i).getId());
                        dataList[i][1] = String.valueOf(wxpayTemps.get(i).getDelFlag());
                        dataList[i][2] = String.valueOf(wxpayTemps.get(i).getUpdateTime());
                        dataList[i][3] = String.valueOf(wxpayTemps.get(i).getUpdateUser());
                        dataList[i][4] = String.valueOf(wxpayTemps.get(i).getCreateTime());
                        dataList[i][5] = String.valueOf(wxpayTemps.get(i).getCreateUser());
                        dataList[i][6] = String.valueOf(wxpayTemps.get(i).getOrderId());
                        dataList[i][7] = String.valueOf(wxpayTemps.get(i).getUserId());
                        dataList[i][8] = String.valueOf(wxpayTemps.get(i).getTotalAmount());
                        dataList[i][9] = String.valueOf(wxpayTemps.get(i).getOutTradeNo());
                        dataList[i][10] = String.valueOf(wxpayTemps.get(i).getTransactionId());
                        dataList[i][11] = String.valueOf(wxpayTemps.get(i).getTypeFlag());
                        dataList[i][12] = String.valueOf(wxpayTemps.get(i).getOutRefundNo());
                        dataList[i][13] = String.valueOf(wxpayTemps.get(i).getRefundAmount());
                        dataList[i][14] = String.valueOf(wxpayTemps.get(i).getCoin());
                        dataList[i][15] = String.valueOf(wxpayTemps.get(i).getPoint());
                        dataList[i][16] = String.valueOf(wxpayTemps.get(i).getCash());
                        dataList[i][17] = String.valueOf(wxpayTemps.get(i).getOpenId());
                    }


        ExportExcelUtil.ExportWithResponse(sheetName, titleName, fileName,
        columnNumber, columnWidth, columnName, dataList, response);


        }
    }

