package cn.feast.coding.controller.generator;

import cn.feast.coding.controller.BaseController;
import cn.feast.coding.model.generator.Module;
import cn.feast.coding.model.generator.ModuleProject;
import cn.feast.coding.service.generator.ModuleProjectService;
import cn.feast.coding.service.generator.ModuleService;
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.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/module")
public class ModuleController extends BaseController {
	@Autowired
	private ModuleService moduleService;
	@Autowired
	private ModuleProjectService moduleProjectService;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Module module,  Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request)
	{
		PageInfo<Module> pageInfo = moduleService.queryPage(module, pageNum(curPage), pageSize(request, pageSize));
		model.put("pages", pageInfo);
		model.put("module", module);
		return "module/module_list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model)
	{
    	return "module/module_add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Module module,HttpServletRequest request, ModelMap model)
	{
		module.setModuleDefault(1);
		moduleService.save(module);
		model.put("flag", 1);
		return "module/module_add";
	}
	
	
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update(Module module,ModelMap model)
	{
		module = moduleService.queryById(module.getId());
		model.put("module", module);
		return "module/module_edit";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Module module,HttpServletRequest request, ModelMap model)
	{
		moduleService.update(module);
		model.put("flag", 1);
		return "module/module_edit";
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult delete(Module module)
	{
		JsonResult jsonResult = new JsonResult();
		jsonResult.setState(JsonResult.SUCCESS);
		module = moduleService.queryById(module.getId());
		if(module.getModuleDefault().equals(0)){
			jsonResult.setState(JsonResult.ERROR);
			jsonResult.setMessage("默认功能模块，不能删除");
			return jsonResult;
		}
		List<ModuleProject> moduleProjects = moduleProjectService.queryByModule(module.getId());
		if(moduleProjects.size() > 0){
			jsonResult.setState(JsonResult.ERROR);
			jsonResult.setMessage("存在关联项目，不能删除");
			return jsonResult;
		}
		moduleService.deleteById(module.getId());
		return jsonResult;
	}
}
