package com.jeeplus.modules.sg.project.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sg.project.entity.WbsProject;
import com.jeeplus.modules.sg.project.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import springfox.documentation.schema.Model;


@Controller
@RequestMapping("${adminPath}/project")
public class ProjectController extends BaseController {
    @Autowired
    private ProjectService projectService;

    @ModelAttribute
    public WbsProject get(@RequestParam(required=false) String id) {
        WbsProject entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = projectService.get(id);
        }
        if (entity == null){
            entity = new WbsProject();
        }
        return entity;
    }

    //项目保存
    @ResponseBody
    @RequestMapping("/save")
    public AjaxJson save(WbsProject wbsProject, Model model, RedirectAttributes redirectAttributes){
        AjaxJson j = new AjaxJson();
        //数据验证
        String errMsg = beanValidator(wbsProject);
        if (StringUtils.isNotBlank(errMsg)){
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        }
        //新增或编辑表单保存
        projectService.save(wbsProject);//保存
        j.setSuccess(true);
        j.setMsg("新建项目成功");
        return j;
    }

    //项目删除
    @ResponseBody
    @RequestMapping(value = "delete")
    public AjaxJson delete(WbsProject wbsProject) {
        AjaxJson j = new AjaxJson();
        projectService.delete(wbsProject);
        j.setMsg("删除项目成功");
        return j;
    }

}
