package com.izouma.awesomeadmin.web;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.model.CommonQuery;
import com.izouma.awesomeadmin.service.CommonQueryService;
import com.izouma.awesomeadmin.util.ExportExcelUtil;
import net.sf.json.JSONObject;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * controller类
 */
@Controller
@RequestMapping("/commonQuery")
public class CommonQueryController {

    @Autowired
    private CommonQueryService commonQueryService;

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


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

        List<Map> pp = commonQueryService.getTableListByPage(page, record);

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

    @RequestMapping(value = "/one", method = RequestMethod.GET)
    @ResponseBody
    public Result one(CommonQuery record) {

        Map pp = commonQueryService.selectByPrimaryKey(record);

        return new Result(true, pp);
    }

    /**
     * <p>保存。</p>
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestParam Map<String, Object> record) {

        JSONObject formData = JSONObject.fromObject(record.get("formData"));

        record.put("formData", formData);

        boolean num = commonQueryService.create(record);
        if (num) {
            return new Result(true, record.get("id"));
        }
        return new Result(false, "保存异常");
    }

    /**
     * <p>更新信息。</p>
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestParam Map<String, Object> record) {


        JSONObject formData = JSONObject.fromObject(record.get("formData"));

        record.put("formData", formData);

        boolean num = commonQueryService.update(record);
        if (num) {
            return new Result(true, "保存成功");
        }
        return new Result(false, "保存异常");
    }


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

        boolean num = commonQueryService.delete(record);
        if (num) {
            return new Result(true, "删除成功");
        }
        return new Result(false, "删除异常");
    }


    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    @ResponseBody
    public void exportExcel(HttpServletResponse response, CommonQuery record) throws Exception {
        List<Map> pp = commonQueryService.getTableListAll(record);

        String sheetName = record.getTableName();
        String titleName = record.getTableName();
        String fileName = record.getTableName();

        if (pp.size() > 0) {
            int columnNumber = pp.get(0).size();
            int[] columnWidth = new int[pp.get(0).size()];
            String[] columnName = new String[pp.get(0).size()];
            for (int i = 0; i < pp.get(0).size(); i++) {
                columnWidth[i] = 20;

            }
            int columnIndex = 0;
            for (Object key : pp.get(0).keySet()) {
                columnName[columnIndex] = String.valueOf(key);
                columnIndex++;
            }

            String[][] dataList = new String[pp.size()][columnNumber];

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


                for (int j = 0; j < columnName.length; j++) {
                    for (Object key : pp.get(i).keySet()) {
                        if (columnName[j].equals(key)) {
                            dataList[i][j] = String.valueOf(String.valueOf(pp.get(i).get(key)));
                        }
                    }
                }


            }

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


    }


}

