package com.izouma.awesomeadmin.web;

import java.io.File;
import java.util.*;

import com.izouma.awesomeadmin.model.GenCode;
import com.izouma.codegenerator.*;
import org.apache.commons.text.CaseUtils;
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.service.GenCodeService;
import org.springframework.web.context.ContextLoader;

/**
 * gen_code_model controller类
 * Fri May 04 15:57:06 CST 2018  Suo Chen Cheng
 */
@Controller
@RequestMapping("/genCode")
public class GenCodeController extends BaseController {

    @Autowired
    private GenCodeService genCodeService;

    /**
     * <p>获取全部记录。</p>
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public Result all(GenCode record) {

        List<GenCode> pp = genCodeService.getGenCodeList(record);
        return new Result(true, pp);
    }

    /**
     * <p>根据Id。</p>
     */
    @RequestMapping(value = "/getGenCode", method = RequestMethod.GET)
    @ResponseBody
    public Result getGenCode(@RequestParam(required = false, value = "id") String id) {

        GenCode data = genCodeService.getGenCodeById(id);
        return new Result(true, data);
    }

    /**
     * <p>根据条件获取。</p>
     */
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public Result getOne(GenCode record) {

        GenCode data = genCodeService.getGenCode(record);
        return new Result(true, data);
    }

    /**
     * <p>分页查询。</p>
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Result page(Page page, GenCode record) {

        Map<String, Object> result = new HashMap<>();

        List<GenCode> pp = genCodeService.getGenCodeByPage(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(@RequestBody GenCode record) {
        try {
            record.setClassName(CaseUtils.toCamelCase(record.getTableName(), true, '_'));
            GeneratorTool.getImports(record);

            ControllerGenerator.GenController(record);
            if (record.getGenTable()) {
                TableGenerator.GenTable(record);
            }
            if (record.getGenClass()) {
                ModelGenerator.GenModel(record);
                ControllerGenerator.GenController(record);
                ServiceGenerator.GenService(record);
                ServiceImplGenerator.GemServicelImpl(record);
                MapperGenerator.GenMapper(record);
                MapperXmlGenerator.GenXml(record);
            }
            if (record.getGenList()) {
                ListViewGenerator.GenListView(record);
            }
            if (record.getGenForm()) {
                EditViewGenerator.GenEditView(record);
            }
            return new Result(true, record.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "保存异常");
    }

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

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

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

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

    @RequestMapping(value = "/getSrcPath", method = RequestMethod.GET)
    @ResponseBody
    public Result getSrcPath() {
        String path = this.getClass().getPackage().getName();
        path = path.substring(0, path.lastIndexOf(".")).replace(".", File.separator);
        String[] arr = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/").split("target");
        //String root = StringUtils.join(Arrays.copyOfRange(arr, 0, arr.length - 2), File.separator);
        String root = arr[0];
        Map<String, Object> map = new HashMap<>();
        map.put("root", root);
        map.put("javaPath", "/src/main/java/" + path);
        map.put("viewPath", "/src/main/vue/src/pages");
        return new Result(true, map);
    }

}

