package cn.feast.coding.controller.generator;

import cn.feast.coding.controller.BaseController;
import cn.feast.coding.model.generator.*;
import cn.feast.coding.service.generator.*;
import cn.feast.coding.tools.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/moduleProject")
public class ModuleProjectController extends BaseController {
    @Autowired
    private ModuleProjectService moduleProjectService;
    @Autowired
    private ModuleService moduleService;
    @Autowired
    private TableService tableService;
    @Autowired
    private ColunmService colunmService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(ModuleProject moduleProject, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request)
    {
        PageInfo<ModuleProject> pageInfo = moduleProjectService.queryPage(moduleProject, pageNum(curPage), pageSize(request, pageSize));
        model.put("pages", pageInfo);
        model.put("moduleProject", moduleProject);
        return "moduleProject/moduleProject_list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModuleProject moduleProject, ModelMap model)
    {
        List<Module> moduleList = moduleService.queryAll();
        List<Table> tableList = tableService.queryByProject(moduleProject.getProjectId());
        model.put("moduleList", moduleList);
        model.put("tableList", tableList);
        model.put("moduleProject", moduleProject);
        return "moduleProject/moduleProject_add";
    }

    @RequestMapping(value = "/getColunm", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getColunm(@RequestParam("tableId") Integer tableId)
    {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(JsonResult.SUCCESS);
        List<Colunm> colunmList = colunmService.queryByTable(tableId);
        jsonResult.setData(colunmList);
        return jsonResult;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(ModuleProject moduleProject, @RequestParam("param1") Integer param1, @RequestParam("param2") Integer param2, @RequestParam("param3") Integer param3, HttpServletRequest request, ModelMap model)
    {
        Colunm colunm1 = colunmService.queryById(param1);
        if(null != colunm1){
            moduleProject.setModuleParam1(colunm1.getColunmName());
            moduleProject.setModuleParam1Type(colunm1.getColunmType());
            moduleProject.setModuleParam1Field(colunm1.getFiledName());
        }
        Colunm colunm2 = colunmService.queryById(param2);
        if(null != colunm2){
            moduleProject.setModuleParam2(colunm2.getColunmName());
            moduleProject.setModuleParam2Type(colunm2.getColunmType());
            moduleProject.setModuleParam2Field(colunm2.getFiledName());
        }
        Colunm colunm3 = colunmService.queryById(param3);
        if(null != colunm3){
            moduleProject.setModuleParam3(colunm3.getColunmName());
            moduleProject.setModuleParam3Type(colunm3.getColunmType());
            moduleProject.setModuleParam3Field(colunm3.getFiledName());
        }
        moduleProjectService.save(moduleProject);
        model.put("flag", 1);
        return "moduleProject/moduleProject_add";
    }


    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(ModuleProject moduleProject, ModelMap model)
    {
        moduleProject = moduleProjectService.queryById(moduleProject.getId());
        model.put("moduleProject", moduleProject);
        return "moduleProject/moduleProject_edit";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(ModuleProject moduleProject, HttpServletRequest request, ModelMap model)
    {
        moduleProjectService.update(moduleProject);
        model.put("flag", 1);
        return "moduleProject/moduleProject_edit";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public void delete(ModuleProject moduleProject)
    {
        moduleProjectService.deleteById(moduleProject.getId());
    }
}
