package com.jeeplus.modules.wexinpackage.access.web;

import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.Role;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.service.RoleService;
import com.jeeplus.modules.sys.service.SystemService;
import com.jeeplus.modules.sys.service.UserService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;
import com.jeeplus.modules.workstaff.service.WorkStaffBasicInfoService;
import org.bouncycastle.math.raw.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(value = "${adminPath}/wxMessage/messageController")
public class MessageController extends BaseController {

    @Autowired
    private SystemService systemService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private OfficeService officeService;

    @Autowired
    private WorkStaffBasicInfoService workStaffBasicInfoService;
    //主页展示
    @RequestMapping(value = "messageList")
    public String messageList(HttpServletRequest request, HttpServletResponse response) {
        return "modules/weixin/home";
    }
    //详细信息
    @RequestMapping(value = "contactForm")
    public String contactForm(WorkStaffBasicInfo workStaffBasicInfo, Model model, boolean reFlag) {
        workStaffBasicInfo = workStaffBasicInfoService.get(workStaffBasicInfo.getId());
        Role role = roleService.get(roleService.getRoleIdByUserId(workStaffBasicInfo.getUserId()));
        workStaffBasicInfo.setRoleId(role.getId());
        workStaffBasicInfo.setRoleName(role.getName());
        model.addAttribute("workStaffBasicInfo", workStaffBasicInfo);
        return  "modules/weixin/user/contactForm";
    }

    //联系人列表展示
    @RequestMapping(value = "contactList")
    public String contactList(WorkStaffBasicInfo workStaffBasicInfo,HttpServletRequest request, HttpServletResponse response, Model model) {
        List<WorkStaffBasicInfo> workStaffBasicInfos = workStaffBasicInfoService.wxFindAddressBook(workStaffBasicInfo);
        model.addAttribute("users",workStaffBasicInfos);
        return  "modules/weixin/user/contactList";
    }
    //部门列表
    @RequestMapping(value = "departmentList")
    public String departmentList(Model model){
        Office office = new Office();
        List<Office> list = officeService.findList(office);
        model.addAttribute("departments",list);
        return "modules/weixin/departmentList";
    }

//    public String




//    // 微信加密签名
//    ${adminPath}/wxMessage/** = anon
//    String msg_signature = request.getParameter("msg_signature");
//    // 时间戳
//    String timestamp = request.getParameter("timestamp");
//    // 随机数
//    String nonce = request.getParameter("nonce");
//    // 随机字符串
//    String echoStr = request.getParameter("echostr");
//    //回调key值
//    String sEchoStr = null;
//
//    String contacts_token = "MYTVsHqnNnAwvNJXn6";
//    String contacts_encodingaeskey = "fnM1vuVzzTdpk8b6EOVto4JMvb7F9ybOmC60R7Lrqy7";
//    String corpId = "wwb8c36b4b35675fe9";
//    WXBizMsgCrypt wxcpt;
//        try {
//        PrintWriter writer = response.getWriter();
//        wxcpt = new WXBizMsgCrypt(contacts_token,contacts_encodingaeskey,corpId);
//        sEchoStr = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echoStr);
//        if (StringUtils.isBlank(sEchoStr)){
//            logger.error("url验证失败");
//        }
//        writer.write(sEchoStr+"111111111111111");
//        System.out.println(msg_signature+"111111111111111");
//        System.out.println(timestamp+"111111111111111");
//        System.out.println(nonce+"111111111111111");
//        System.out.println(echoStr+"111111111111111");
//        System.out.println("连接成功-------------");
//        writer.close();
//    }catch (Exception e){
//        logger.error("企业微信回调验证错误",e);
//    }
}
