package cn.pluss.platform.controller.agreement;

import cn.pluss.platform.agreement.AgreementService;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.Agreement;
import cn.pluss.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("agreement")
public class AgreementController  extends BaseNoModelController {

	  private Integer PAGE_SIZE=12;

    @Autowired
	  private AgreementService agreementService;

    /**
    *引导页
    * @return
    */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
   
        return "agreement/agreementList";
    }

    /**
    *分页查询
    */
    @RequestMapping(value = "/queryAgreementByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> queryAgreementByPage(@RequestParam(defaultValue="1") Integer currPage,Agreement agreement){
   
    	Map<String,Object> map=new HashMap<String, Object>(16);
    	Map<String,Object> result=new HashMap<String,Object>(16);
    	map.put("pageSize",PAGE_SIZE);
    	map.put("offset", (currPage-1)*PAGE_SIZE);
    	if(StringUtil.isNotEmpty(agreement.getName())) {
    		map.put("name", agreement.getName());
    	}
    	List<Agreement> agreementList=agreementService.queryAgreementPage(map);
    	Integer count=agreementService.queryAgreementPageCount(map);
        result.put("agreementList", agreementList);
        result.put("count", count);
        result.put("pageCount",StringUtil.getPageCount(count,PAGE_SIZE));
        result.put("currPage", currPage);
    	  return result;
    }
    /**
    *查询详情
    */
     @RequestMapping(value = "/queryAgreementDetails", method = RequestMethod.GET)
    public String queryAgreementDetails(HttpServletRequest request,Integer id,Integer pageIndex){
   
   	  if(id!=null&&id!=0) {
   		 Agreement agreement=new Agreement();
   		 agreement.setId(id);
           agreement=agreementService.queryAgreement(agreement);
          request.setAttribute("agreement", agreement);
   		request.setAttribute("pageIndex", pageIndex);
       	}
        return "agreement/agreementDetails";
    }
    /**
    *查询详情
    */
     @RequestMapping(value = "/queryAgreementBaseInfo", method = RequestMethod.GET)
    public String queryAgreementBaseInfo(HttpServletRequest request,Integer id,Integer pageIndex){
   
   	  if(id!=null&&id!=0) {
   		Agreement agreement=new Agreement();
   		agreement.setId(id);
        agreement=agreementService.queryAgreement(agreement);
          request.setAttribute("agreement", agreement);
   		request.setAttribute("pageIndex", pageIndex);
       	}
        return "agreement/agreementBaseInfo";
    }
    /**
    *查询详情
    */
     @RequestMapping(value = "/queryAgreementExtendsInfo", method = RequestMethod.GET)
    public String queryAgreementExtendsInfo(){
   
        return "agreement/agreementExtendsInfo";
    }
    /**
    *查询详情
    */
     @RequestMapping(value = "/queryAgreementFunctionsInfo", method = RequestMethod.GET)
    public String queryAgreementFunctionsInfo(){
   
        return "agreement/agreementFunctionsInfo";
    }
    /**
    *保存对象
    *@throws IOException
    */
    @RequestMapping(value = "/saveAgreement", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> saveAgreement(Agreement agreement,String oldDiscern){
    	Map<String,Object> result=new HashMap<String,Object>(16);
    	 Agreement a=new Agreement();
    	 String discern = agreement.getDiscern();
    	 a.setDiscern(discern);
       if(agreement.getId()!=null&&agreement.getId()!=0){
    	   if(!agreement.getDiscern().equals(oldDiscern)) {
    		   List<Agreement> queryAgreementList =  agreementService.queryAgreementList(a);
    		   if(queryAgreementList!=null&&queryAgreementList.size()>0) {
      			 result.put("code", "0");
      		     return result;
      		   }
    	   }
    	   agreementService.updateAgreement(agreement);
    	 }else{ 
    		 List<Agreement> queryAgreementList = agreementService.queryAgreementList(a);
    		 if(queryAgreementList!=null&&queryAgreementList.size()>0) {
    			 result.put("code", "0");
    		     return result;
    		 }
    		 ShiroUser shiroUser = queryCurrentShiroUser();
    		agreement.setCreateor(shiroUser.getLoginName());
    		agreementService.saveAgreement(agreement);
    	 }  
    	return result;
    }
    /**
    *修改对象
    *@throws IOException
    */
    @RequestMapping(value = "/updateAgreement", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateAgreement(Integer id){
   
   	Agreement agreement=new Agreement();
   	agreement.setId(id);
      agreementService.updateAgreement(agreement);
   	Map<String,Object> result=new HashMap<String,Object>();
    	result.put("resultCode", "1");
    	result.put("resultMessage", "保存成功!");
    	return result;
    }
    /**
    *删除对象
    *@throws IOException
    */
    @RequestMapping(value = "/deleteAgreement", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> deleteAgreement(Integer id){
   	Agreement agreement=new Agreement();
   	agreement.setId(id);
		agreementService.deleteAgreement(agreement);
		Map<String,Object> result=new HashMap<String,Object>();
    	result.put("resultCode", "1");
    	result.put("resultMessage", "保存成功!");
    	return result;
    }

}


