package cn.feast.coding.controller.generator;


import cn.feast.coding.controller.BaseController;
import cn.feast.coding.model.generator.DataSource;
import cn.feast.coding.model.generator.Project;
import cn.feast.coding.service.generator.DataSourceService;
import cn.feast.coding.service.generator.ProjectService;
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("/dataSource")
public class DataSourceController extends BaseController {
    @Autowired
    private DataSourceService dataSourceService;
    @Autowired
    private ProjectService projectService;

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

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(DataSource dataSource, HttpServletRequest request, ModelMap model)
    {
        dataSourceService.save(dataSource);
        model.put("flag", 1);
        return "dataSource/dataSource_add";
    }


    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(DataSource dataSource, ModelMap model)
    {
        dataSource = dataSourceService.queryById(dataSource.getId());
        model.put("dataSource", dataSource);
        return "dataSource/dataSource_edit";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(DataSource dataSource, HttpServletRequest request, ModelMap model)
    {
        dataSourceService.update(dataSource);
        model.put("flag", 1);
        return "dataSource/dataSource_edit";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult delete(DataSource dataSource)
    {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(JsonResult.SUCCESS);
        List<Project> projects = projectService.queryByDataSource(dataSource.getId());
        if(projects.size() > 0){
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("存在关联项目，不能删除！");
            return jsonResult;
        }
        dataSourceService.deleteById(dataSource.getId());
        return jsonResult;
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view(DataSource dataSource, ModelMap model)
    {
        dataSource = dataSourceService.queryById(dataSource.getId());
        model.put("dataSource", dataSource);
        return "dataSource/dataSource_view";
    }
}
