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.TableNiuseRongliang;
import com.izouma.awesomeadmin.service.TableNiuseRongliangService;

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

/**
*  controller类
*/
@Controller
@RequestMapping("/tableNiuseRongliang")
public class TableNiuseRongliangController {

    @Autowired
    private TableNiuseRongliangService tableNiuseRongliangService;

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

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

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


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

        List<TableNiuseRongliang> pp =tableNiuseRongliangService.getTableNiuseRongliangByPage(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(TableNiuseRongliang record) {
        boolean num = tableNiuseRongliangService.createTableNiuseRongliang(record);
        if (num) {
        return new Result(true, record.getId());
        }
        return new Result(false, "保存异常");
    }

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

    /**
    * <p>删除。</p>
    */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteTableNiuseRongliang(TableNiuseRongliang record) {

        boolean num = tableNiuseRongliangService.deleteTableNiuseRongliang(record);
        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, TableNiuseRongliang record) throws Exception {

    List<TableNiuseRongliang> tableNiuseRongliangs = tableNiuseRongliangService.getTableNiuseRongliangList(record);


        String sheetName = "Table_牛只_牛舍信息";
        String titleName = "牛舍容量定额配置数据表";
        String fileName = "牛舍容量定额配置表";
        int columnNumber = 20;
        int[] columnWidth = { 20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20 };
        String[] columnName = {  "ID" ,   "牛舍编号" ,   "牧场编号" ,   "牛舍名称" ,   "牛舍类别" ,   "实际存放牛只分类" ,   "管理员" ,   "建筑面积" ,   "设计容量" ,   "颈枷数" ,   "制度要求使用数_颈枷" ,   "卧床数" ,   "制度要求使用数_卧床" ,   "核定容量" ,   "综合核定的合理承载数" ,   "是否青年围产舍" ,   "FlagColumn" ,   "备注" ,   "修改人" ,   "修改时间"  };
        String[][] dataList = new String[tableNiuseRongliangs.size()][20];

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

                        dataList[i][0] = String.valueOf(tableNiuseRongliangs.get(i).getId());
                        dataList[i][1] = String.valueOf(tableNiuseRongliangs.get(i).get牛舍编号());
                        dataList[i][2] = String.valueOf(tableNiuseRongliangs.get(i).get牧场编号());
                        dataList[i][3] = String.valueOf(tableNiuseRongliangs.get(i).get牛舍名称());
                        dataList[i][4] = String.valueOf(tableNiuseRongliangs.get(i).get牛舍类别());
                        dataList[i][5] = String.valueOf(tableNiuseRongliangs.get(i).get实际存放牛只分类());
                        dataList[i][6] = String.valueOf(tableNiuseRongliangs.get(i).get管理员());
                        dataList[i][7] = String.valueOf(tableNiuseRongliangs.get(i).get建筑面积());
                        dataList[i][8] = String.valueOf(tableNiuseRongliangs.get(i).get设计容量());
                        dataList[i][9] = String.valueOf(tableNiuseRongliangs.get(i).get颈枷数());
                        dataList[i][10] = String.valueOf(tableNiuseRongliangs.get(i).get制度要求使用数颈枷());
                        dataList[i][11] = String.valueOf(tableNiuseRongliangs.get(i).get卧床数());
                        dataList[i][12] = String.valueOf(tableNiuseRongliangs.get(i).get制度要求使用数卧床());
                        dataList[i][13] = String.valueOf(tableNiuseRongliangs.get(i).get核定容量());
                        dataList[i][14] = String.valueOf(tableNiuseRongliangs.get(i).get综合核定的合理承载数());
                        dataList[i][15] = String.valueOf(tableNiuseRongliangs.get(i).get是否青年围产舍());
                        dataList[i][16] = String.valueOf(tableNiuseRongliangs.get(i).getFlagcolumn());
                        dataList[i][17] = String.valueOf(tableNiuseRongliangs.get(i).get备注());
                        dataList[i][18] = String.valueOf(tableNiuseRongliangs.get(i).get修改人());
                        dataList[i][19] = String.valueOf(tableNiuseRongliangs.get(i).get修改时间());
                    }


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


        }
    }

