package com.izouma.awesomeadmin.web;

#foreach($import in $imports)
$import
#end
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.${model.className};
import com.izouma.awesomeadmin.service.${model.className}Service;

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

/**
*  controller类
*/
@Controller
@RequestMapping("/$display.uncapitalize($model.className)")
public class ${model.className}Controller extends BaseController{

    @Autowired
    private ${model.className}Service $display.uncapitalize(${model.className})Service;

    /**
    * <p>获取全部记录。</p>
    */
    @RequiresAuthentication
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public Result all($model.className record) {
        List<$model.className> pp = ${display.uncapitalize(${model.className})}Service.get${model.className}List(record);
        return new Result(true, pp);
    }

    /**
    * <p>根据Id。</p>
    */
    @RequestMapping(value = "/get${model.className}", method = RequestMethod.GET)
    @ResponseBody
    public Result get${model.className}(@RequestParam(required = false, value = "id") String id) {
        ${model.className} data = ${display.uncapitalize(${model.className})}Service.get${model.className}ById(id);
        return new Result(true, data);
    }

    /**
    * <p>根据条件获取。</p>
    */
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public Result getOne(${model.className} record) {
        ${model.className} data = ${display.uncapitalize(${model.className})}Service.get${model.className}(record);
        return new Result(true, data);
    }


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

        List<${model.className}> pp =${display.uncapitalize(${model.className})}Service.get${model.className}ByPage(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(${model.className} record) {
        boolean num = ${display.uncapitalize(${model.className})}Service.create${model.className}(record);
        if (num) {
        return new Result(true, record.getId());
        }
        return new Result(false, "保存异常");
    }

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

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

        boolean num = ${display.uncapitalize(${model.className})}Service.delete${model.className}(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, ${model.className} record) throws Exception {

    List<${model.className}> ${display.uncapitalize(${model.className})}s = ${display.uncapitalize(${model.className})}Service.get${model.className}List(record);


        String sheetName = "${model.tableName}";
        String titleName = "${model.remark}数据表";
        String fileName = "${model.remark}表";
        int columnNumber = ${model.fields.size()};
        int[] columnWidth = {#foreach(${field} in ${model.fields}) 20#if($foreach.count != ${model.fields.size()}),#end #end};
        String[] columnName = {#foreach(${field} in ${model.fields}) #if(${field.remark}) "${field.remark}" #else "${field.modelName}"#end#if($foreach.count != ${model.fields.size()}),#end #end};
        String[][] dataList = new String[${display.uncapitalize(${model.className})}s.size()][${model.fields.size()}];

        for (int i = 0; i < ${display.uncapitalize(${model.className})}s.size(); i++) {

            #foreach(${field} in ${model.fields})
                #set($fund=$foreach.count - 1)
            dataList[i][$fund] = String.valueOf(${display.uncapitalize(${model.className})}s.get(i).get${display.capitalize($field.modelName)}());
            #end
        }


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


        }
    }

