package com.ssj.sys.controller;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.cardinfo.domain.TbCardInfo;
import com.ssj.bean.weixin.libmy.domain.TbLibJoin;
import com.ssj.bean.weixin.user.domain.User;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.service.sys.cardinfo.service.ICardInfoService;
import com.ssj.service.weixin.library.service.ILibJoinService;
import com.ssj.service.weixin.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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 java.util.*;

@Controller
@RequestMapping("/sys/userCard")
public class UserCardtController extends BaseController {

	
	@Autowired
	private ICardInfoService cardInfoService;
	
	@Autowired
	private UserService usersService;
	
	@Autowired
	private ILibJoinService libJoinService;
	
	@Autowired
	private  UserService  userService;
	
	/**
	 * 老师审核列表列表
	 * @param model
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/cardInfoList")
	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("LIKE_realName", request.getParameter("LIKE_realName")==null?"":request.getParameter("LIKE_realName"));
//		params.put("GTE_type", "1");
		SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI().toString());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Pageable pageable = initPage(pageNo, pageSize);
	    Page<Map<String, Object>>  pageOne=cardInfoService.findCardInfoPage(params,pageable);
//	    Page<ApplyTeacher> page= applyService.findByPage(params, initPage(pageNo,pageSize,Direction.DESC, new String[] { "createTime" }));
//	    Page<Map<String, Object>> page = cardInfoService.findCardInfoPage(params);
		sp.setRowCnt((int) pageOne.getTotalElements());
		model.addAttribute("list", pageOne.getContent());
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);

		return "sys/audit/cardInfoList";
	}
	
	/**
	 * 审核
	 * @param model
	 * @param id
	 * @return
	 * @throws Exception
	 */
	 @RequestMapping("/cardEdit")
	 public String cardEdit(Model model,@RequestParam( defaultValue = "0")String id) throws Exception {

		 TbCardInfo cardInfo = cardInfoService.getById(id);
		 if(cardInfo == null){
			 cardInfo = new TbCardInfo();
			 cardInfo.setState(1);
			 model.addAttribute("wxName","");
			 model.addAttribute("libName","");
		 }else{
			 User user = usersService.getById(cardInfo.getUserId());
			 model.addAttribute("wxName", user.getName());
			 if(cardInfo.getLibId()!=null && !"".equals(cardInfo.getLibId())){
				 model.addAttribute("libName",libJoinService.getById(cardInfo.getLibId()).getLeagueName());
			 }else{
				 model.addAttribute("libName","");
			 }
		 }
		 model.addAttribute("ws", cardInfo);
		 return "sys/audit/cardEdit";
	 }
	
	 /**
	  * 保存
	  * @param lib
	  * @return
	 * @throws Exception 
	  */
	 @RequestMapping(value = "/cardSave", method = RequestMethod.POST)
	 @ResponseBody
	 public Response cardSave(TbCardInfo cardInfo) throws Exception{
    	Response response = new  Response();
    	cardInfoService.save(cardInfo);
    	response.success();
    	return response;
	 }
	 
	 /**
		 * 图书馆列表
		 * @param model
		 * @param pageSize
		 * @param pageNo
		 * @return
		 * @throws Exception
		 */
	@RequestMapping("/libJoinList")
	public String libJoinList(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("LIKE_leagueName", request.getParameter("LIKE_leagueName")==null?"":request.getParameter("LIKE_leagueName"));
		params.put("LIKE_leagueCode", request.getParameter("LIKE_leagueCode")==null?"":request.getParameter("LIKE_leagueCode"));
		params.put("IN_orgCode", orgcodeList());
		params.put("GTE_type", "1");
		SplitPage sp = new SplitPage();
	        sp.setAction(request.getRequestURI().toString());
	        sp.setPageNo(pageNo);
	        sp.setPageSize(pageSize);
	        sp.setParams(params);
	    Page<TbLibJoin> page= libJoinService.findByPage(params, initPage(pageNo,pageSize,Direction.DESC, new String[] { "type" }));
		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/liblist";
	}
	public static void main(String[] args) {
		System.out.println(Arrays.asList(new String[] {"8","13"}));
		List<String> s =new ArrayList<>();
		s.add("8");s.add("13");
		System.out.println(s);
	}

	 @RequestMapping("/userList")
	 public String userList(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("name", request.getParameter("name")==null?"":request.getParameter("name"));
		 params.put("subscribe", "");
		 params.put("beginDate","");
		 params.put("endDate","");
		SplitPage sp = new SplitPage();
	        sp.setAction(request.getRequestURI().toString());
	        sp.setPageNo(pageNo);
	        sp.setPageSize(pageSize);
	        sp.setParams(params);
	    Page<Map<String, Object>> page= userService.getWXUser(params, initPage(pageNo,pageSize));
	    List<Map<String, Object>> list = page.getContent();
		sp.setRowCnt((int)page.getTotalElements());
		model.addAttribute("list", list);
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);
		 
	     return "sys/audit/userList";
	 }
}
