package com.ssj.framework.core.common.controller;
import com.ssj.bean.sys.role.domain.Admin;
import com.ssj.bean.weixin.user.domain.UserAccount;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.service.weixin.user.service.UserAccountService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * 类名称：BaseController<br>
 * 类描述： Controller基类<br>
 * @version v1.0
 *
 */
public abstract class BaseController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;
    
    @Autowired
    protected UserAccountService userAccountService;
    
    @Autowired
    protected TokenManager tokenManager;
	

    /**
     * 绑定字符过滤编辑器
     * @param binder
     */
   /* @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringEscapeEditor(true));//html转义
    }*/

    /**
     * 重定向到指定url
     * @param url
     * @return
     */
    protected String redirectTo(String url) {
        StringBuffer rto = new StringBuffer("redirect:");
        rto.append(url);
        return rto.toString();
    }
    protected String orgCodeStr() {
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        return admin !=null ?admin.getOrgCodeStr() :null;
    }
    protected List<String> orgcodeList(){
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        return admin !=null ?admin.getOrgcodeList() :null;
    }
    protected String jxOrgCodeStr() {
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        return admin !=null ?admin.getJxOrgCodeStr() :null;
    }
    protected List<String> jxOrgcodeList(){
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        return admin !=null ?admin.getJxOrgcodeList() :null;
    }
    /* protected String libId() {
         Admin admin = (Admin)request.getSession().getAttribute("admin");
         String libId = admin !=null ? (admin.getLibJoinId()!=null ? admin.getLibJoinId()  :"libId") :"libId";
         return libId;
     }*/
    protected String loginName() {
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        String loginName = admin !=null ? admin.getLoginName() :"adminName";
        return loginName;
    }
    protected String adminId() {
        String adminId = (String)request.getSession().getAttribute("adminId");
        return adminId !=null ? adminId :"adminId";
    }

    /**
     * 初始化分页查询参数Page
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    protected Pageable initPage(Integer pageNo, Integer pageSize) {
        // @RequestParam在处理不存在的参数时，赋值为null，基本类型无法为null，改为包装类型
        if (pageNo == null) {
            pageNo = 0;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        pageNo = pageNo == 0 ? 0 : pageNo - 1;
        pageSize = pageSize == 0 ? 10 : pageSize;
        Pageable page = new PageRequest(pageNo, pageSize);
        return page;
    }

    /**
     * 初始化分页查询参数Page
     *
     * @param pageNo
     * @param pageSize
     * @param direction
     * @param properties
     * @return
     */
    protected Pageable initPage(Integer pageNo, Integer pageSize, Direction direction, String... properties) {
        if (pageNo == null) {
            pageNo = 0;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        pageNo = pageNo == 0 ? 0 : pageNo - 1;
        pageSize = pageSize == 0 ? 10 : pageSize;
        Pageable page = new PageRequest(pageNo, pageSize, direction, properties);
        return page;
    }

    protected Map<String, Object> getPageResultMap(Page page) {
        Map<String, Object> resultMap = new HashMap<>();
        if (page != null) {
            resultMap.put("pages", page.getTotalPages());
            resultMap.put("pageNo", page.getNumber() + 1);
            resultMap.put("pageSize", page.getSize());
            resultMap.put("list", page.getContent());
        }
        return resultMap;
    }
    
    protected String getAccessToken(String userId,String accessToken){
        UserAccount userAccount = userAccountService.findByUserIdAndType(userId,11);
        if(userAccount == null) {
            accessToken = tokenManager.createToken(accessToken, userId, "APP_TEACHER");
        }else {
            accessToken = tokenManager.createToken(accessToken, userId, userAccount.getOpenid(), 4, userAccount.getUnionid(),"APP_TEACHER");
        }
        return accessToken;
    }
    
}