package com.mortals.xhx.base.system.gentable.web;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.gentable.model.GentableColumnEntity;
import com.mortals.xhx.base.system.gentable.model.GentableColumnQuery;
import com.mortals.xhx.base.system.gentable.model.GentableEntity;
import com.mortals.xhx.base.system.gentable.service.GentableColumnService;
import com.mortals.xhx.base.system.gentable.service.GentableService;
import com.mortals.xhx.common.code.*;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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


/**
 * <p>Title: 代码生成业务表</p>
 * <p>Description: GentableController  </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 *
 * @author zxfei
 * @version 1.0.0
 */
@RestController
@RequestMapping("gentable")
public class GentableController extends BaseCRUDJsonMappingController<GentableService, GentableForm, GentableEntity, Long> {

    @Autowired
    private GentableColumnService gentableColumnService;


    public GentableController() {
        super.setFormClass(GentableForm.class);
        super.setModuleDesc("代码生成业务表");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, GentableForm form, Map<String, Object> model, Context context) {
        this.addDict(model, "dividedTableType", DividedTableTypeEnum.getEnumMap());
        this.addDict(model, "cacheServiceType", CacheServiceTypeEnum.getEnumMap());
        this.addDict(model, "genType", GenTypeEnum.getEnumMap());
        this.addDict(model, "htmlType", HtmlTypeEnum.getEnumMap());

        super.init(request, response, form, model, context);
    }

    @PostMapping("/db/list")
    public String dblist(GentableForm form) {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询数据库表";
        int code = VALUE_RESULT_SUCCESS;
        try {
            List<GentableEntity> list = this.service.selectDbTableList(form.getQuery());

            model.put("result", list);

            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }

        ret.put(KEY_RESULT_CODE, code);
        ret.put(KEY_RESULT_QUERY, form.getQuery());
        ret.put(KEY_RESULT_DATA, model);
        return ret.toJSONString();
    }


    @PostMapping("/importTable")
    public String importTable(@RequestParam(value = "tables") String tables) {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询数据库表";
        int code = VALUE_RESULT_SUCCESS;
        try {
            String[] tableNames = Convert.toStrArray(tables);
            //查询
            List<GentableEntity> list = this.service.selectDbTableListByNames(tableNames);
            //导入
            this.service.importGenTable(list, getContext());
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }

        ret.put(KEY_RESULT_CODE, code);
        ret.put(KEY_RESULT_DATA, model);
        return ret.toJSONString();
    }


    @PostMapping("/info")
    public String getInfo(@RequestParam(value = "tableId") Long tableId) {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询数据库表";
        int code = VALUE_RESULT_SUCCESS;
        try {
            //查询单表信息
            GentableEntity gentableEntity = this.service.get(tableId);
            //关联查询表与列信息
            List<GentableEntity> allList = this.service.selectGenTableAll();
            //查询列信息
            List<GentableColumnEntity> columnEntities = gentableColumnService.find(new GentableColumnQuery().tableId(tableId));
            model.put("info", gentableEntity);
            model.put("rows", columnEntities);
            model.put("tables", allList);

            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        this.init(this.request, this.response, null, model, getContext());
        ret.put(KEY_RESULT_CODE, code);
        ret.put(KEY_RESULT_DATA, model);
        return ret.toJSONString();
    }

    /**
     * 通过表名生成代码
     *
     * @param tableName
     * @return
     */
    @PostMapping("/genCode")
    public void genCode(@RequestParam(value = "tableName") String tableName) {
        Map<String, Object> model = new HashMap<>();
        String busiDesc = "生成单表代码";
        try {

            byte[] bytes = this.service.downloadCode(tableName);
            genCode(response, bytes);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            this.doException(request, busiDesc, model, e);
        }
    }


    /**
     * 通过表名生成代码
     *
     * @param tables
     * @return
     */
    @PostMapping("/batchGenCode")
    public void batchGenCode(@RequestParam(value = "tables") String tables) {
        Map<String, Object> model = new HashMap<>();
        String busiDesc = "批量生成单表代码";
        try {
            String[] tableNames = Convert.toStrArray(tables);
            byte[] bytes = this.service.downloadCode(tableNames);
            genCode(response, bytes);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            this.doException(request, busiDesc, model, e);
        }
    }


    /**
     * 预览代码
     *
     * @param id
     * @return
     */
    @PostMapping("/preview")
    public String preview(@RequestParam(value = "id") Long id) {

        JSONObject ret = new JSONObject();
        String busiDesc = "预览代码";
        int code = VALUE_RESULT_SUCCESS;
        try {

            Map<String, String> model = this.service.previewCode(id);
            ret.put(KEY_RESULT_DATA, model);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
        }
        ret.put(KEY_RESULT_CODE, code);
        return ret.toJSONString();
    }

    /**
     * 同步数据库
     *
     * @param tableName
     * @return
     */
    @PostMapping("/synchDb")
    public String synchDb(@RequestParam(value = "tableName") String tableName) {
        JSONObject ret = new JSONObject();
        String busiDesc = "同步数据库";
        int code = VALUE_RESULT_SUCCESS;
        try {
            this.service.synchDb(tableName);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
        }
        ret.put(KEY_RESULT_CODE, code);
        return ret.toJSONString();
    }


    /**
     * 生成zip文件
     */
    private void genCode(HttpServletResponse response, byte[] data) throws IOException {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"genCode.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }


}