package com.ssj.sys.controller;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.kmt.kmt.domain.KmtExampaperArea;
import com.ssj.bean.kmt.kmt.domain.KmtExampaperDetail;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.kmt.service.IKmtExampaperAreaService;
import com.ssj.service.kmt.service.KmtExampaperDetailService;
import com.ssj.service.sys.sort.service.SortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.*;

@Controller
@RequestMapping("/sys/kmtExampaper")
/**
 * 试卷管理
 * @author sh
 *
 */
public class KmtExampaperAreaController extends BaseController {

	@Autowired
	private IKmtExampaperAreaService exampaperService;
	
	@Autowired
	private SortService sortService;
	@Autowired
	private KmtExampaperDetailService kmtExampaperDetailService;
	
	/**
	 * 老师审核列表列表
	 * @param model
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/exampaperList")
	public String cardInfoList(Model model,
			@RequestParam(required = false, defaultValue = "10") int pageSize,
			@RequestParam(required = false, defaultValue = "1") int pageNo)
			throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("EQ_subject", request.getParameter("EQ_subject")==null?"":request.getParameter("EQ_subject"));
		params.put("EQ_grade",request.getParameter("EQ_grade")==null?"":request.getParameter("EQ_grade"));//年级
		params.put("EQ_semester",request.getParameter("EQ_semester")==null?"":request.getParameter("EQ_semester"));//学期
		
		SplitPage sp = new SplitPage();
	        sp.setAction(request.getRequestURI().toString());
	        sp.setPageNo(pageNo);
	        sp.setPageSize(pageSize);
	        sp.setParams(params);
	    Page<KmtExampaperArea> page= exampaperService.findByPage(params, initPage(pageNo,pageSize,Direction.DESC, new String[] { "createTime" }));
		sp.setRowCnt((int) page.getTotalElements());
		model.addAttribute("list", page.getContent());
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);
		model.addAttribute("zyb_course", sortService.getParamValByCode("zyb_course"));
		model.addAttribute("grade", sortService.getParamValByCode("grade"));//年级
		return "sys/audit/exampaperList";
	}
	
	/**
	 * 审核
	 * @param model
	 * @param id
	 * @return
	 * @throws Exception
	 */
	 @RequestMapping("/exampaperEdit")
	 public String exampaperEdit(Model model,@RequestParam( defaultValue = "0")String id) throws Exception {

		 
		 KmtExampaperArea exampaper = exampaperService.getById(id);
		 if(exampaper == null){
			 exampaper = new KmtExampaperArea();
		 }
		 model.addAttribute("ws", exampaper);
		 model.addAttribute("grade", sortService.getParamValByCode("grade"));//年级
		 return "sys/audit/exampaperEdit";
	 }
	
	 /**
	  * 保存
	  * @param lib
	  * @return
	 * @throws Exception 
	  */
	 @RequestMapping(value = "/exampaperSave", method = RequestMethod.POST)
	 @ResponseBody
	 public Response exampaperSave(KmtExampaperArea kmtExampaperArea) throws Exception{
    	Response response = new  Response();
    
    	KmtExampaperArea area = null;
    	if(StringUtil.isEmpty(kmtExampaperArea.getId())){
    		kmtExampaperArea.setCreateTime(new Date());
    		area = exampaperService.findBeanByAnyThings(kmtExampaperArea.getYear(),kmtExampaperArea.getCity(),kmtExampaperArea.getArea(),kmtExampaperArea.getSchool(),kmtExampaperArea.getSemester(),kmtExampaperArea.getType(),kmtExampaperArea.getSubject(),kmtExampaperArea.getGrade(),kmtExampaperArea.getAddStr());
    	}else{
    		area = exampaperService.findBeanByAnyThings(kmtExampaperArea.getYear(),kmtExampaperArea.getCity(),kmtExampaperArea.getArea(),kmtExampaperArea.getSchool(),kmtExampaperArea.getSemester(),kmtExampaperArea.getType(),kmtExampaperArea.getSubject(),kmtExampaperArea.getGrade(),kmtExampaperArea.getAddStr(),kmtExampaperArea.getId());
    	}
    	//先查询是否存在 数据
    	if(area == null){
    		exampaperService.save(kmtExampaperArea);
        	response.success();
    	}else{
    		response.failure("已经存在与保存数据相同的数据，请另行修改保存");
    	}
    	return response;
	 }
	 
	 /**
	     * 栏目主页面
	     * @return
	     */
	    @RequestMapping("/main")
	    public String main(Model model,@RequestParam( defaultValue = "0")String areaId) {
	    	model.addAttribute("areaId", areaId);
	        return "sys/audit/main";
	    }
	    /**
	     * 栏目树
	     * @param pid
	     * @return
	     * @throws Exception 
	     */
	    @RequestMapping("/tree")
	    @ResponseBody
	    public List<KmtExampaperDetail> tree(@RequestParam(required = false, value = "id") String pId,
											 @RequestParam(required = false, value = "areaId") String areaId
	    		) throws Exception {
	    	
	    	if(pId ==null) {
	    		KmtExampaperArea exampaper = exampaperService.getById(areaId);
	    		List<KmtExampaperDetail> list =new ArrayList<KmtExampaperDetail>();
	    		KmtExampaperDetail det =new  KmtExampaperDetail();
	    		det.setName(exampaper.getSubject()+exampaper.getYear()+ exampaper.getAddStr());
	    		det.setAreaId(areaId);
	    		det.setCode("-1");
	    		det.setSortNo(1);
	    		det.setSubCount(1);
	    		det.setOrgLevel(1);
	    		det.setStatus(1);
	    		det.setIsParent(true);
	    		det.setpId("-1");
	    		det.setId("-1");
	    		list.add(det);
	    		return list;
	    	}
	    	return kmtExampaperDetailService.getPidAndCodes(pId,areaId);
	    }
	    /**
	     * 查询列表
	     * @param request
	     * @return
	     * @throws Exception 
	     */
	    @RequestMapping("/list")
	    public String list( Model model,@RequestParam(required = false, defaultValue = "-1") String EQ_pId,
	    		@RequestParam(required = false, value = "areaId") String areaId,
				@RequestParam(required = false, defaultValue = "10") int pageSize,
				@RequestParam(required = false, defaultValue = "1") int pageNo) throws Exception {
	    	Map<String, Object> params = new HashMap<String, Object>();
	        params.put("EQ_pId", EQ_pId);
	        params.put("EQ_areaId", areaId);
	        params.put("LIKE_name",request.getParameter("LIKE_name")==null?"":request.getParameter("LIKE_name"));
			SplitPage sp = new SplitPage();
		        sp.setAction(request.getRequestURI().toString());
		        sp.setPageNo(pageNo);
		        sp.setPageSize(pageSize);
		        sp.setParams(params);
		    Page<KmtExampaperDetail> page= kmtExampaperDetailService.findByPage(params, initPage(pageNo,pageSize,Direction.DESC, new String[] { "sortNo" }));
		     params.put("areaId", areaId);
			sp.setRowCnt((int) page.getTotalElements());
			model.addAttribute("list", page.getContent());
			model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
			model.addAttribute("page", sp);
			model.addAttribute("search", params);
	        return "sys/audit/list";
	    }
	    
	    @RequestMapping(value = "/updateSortNo", method = RequestMethod.POST)
	    @ResponseBody
	    public Response  updateSortNo(String id,Integer sortNo){
	    	Response response = new  Response();
	    	kmtExampaperDetailService.updateSortNo(id, sortNo);
	    	response.success();
	        return response;
	    }
	    @RequestMapping(value = "/edit")
	    public String edit(HttpServletRequest request, Model model, String id,String pId,String areaId) throws Exception {
	    	KmtExampaperDetail org = kmtExampaperDetailService.getById(id);
	    	if(org ==null){
	    		org = new KmtExampaperDetail();
	    		org.setpId(pId);
	    		org.setStatus(1);
	    		org.setAreaId(areaId);
	    	}
	    	 model.addAttribute("org", org);
	    	 return "sys/audit/edit";
	    }
	    
	    @RequestMapping(value = "/save", method = RequestMethod.POST)
	    @ResponseBody
	    public Response  updateSortNo(KmtExampaperDetail org){
	    	Response response = new  Response();
	    	org.setCreateTime(new Date());
	    	if("-1".equals(org.getpId())){
	    		org.setOrgLevel(2);
	    	}else{
	    		org.setOrgLevel(kmtExampaperDetailService.getById(org.getpId()).getOrgLevel()+1);
	    	}
	    	org=kmtExampaperDetailService.save(org);
	    	if(!"-1".equals(org.getpId())){
	    		kmtExampaperDetailService.changeSubCount(org.getpId(), true);
	    	}
	    	response.success(org);
	        return response;
	    }
	    
	    @RequestMapping(value = "/delete", method = RequestMethod.POST)
	    @ResponseBody
	    public Response  delete(String id,String pId){
	    	Response response = new  Response();
	    	kmtExampaperDetailService.delete(id);
	    	if(!"-1".equals(pId)){
	    		kmtExampaperDetailService.changeSubCount(pId, false);
	    	}
	    	response.success();
	        return response;
	    }
}
