package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.HttpUtil;
import cn.hnthyy.thmz.Utils.JsonUtil;
import cn.hnthyy.thmz.Utils.MacAddressUtil;
import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.entity.thmz.Windows;
import cn.hnthyy.thmz.service.thmz.WindowsService;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class WindowsController {
    @Autowired
    private WindowsService windowsService;

    /**
     * 保存窗口号,院区，打印机
     * @param windows
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveWindows", method = {RequestMethod.POST})
    public Map<String, Object> saveWindows(@RequestBody Windows windows,HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        if(windows==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存收费员设置失败，参数为空");
            return resultMap;
        }
        if(windows.getWindowsNo()==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存收费员设置失败，窗口号为空");
            return resultMap;
        }
        if(windows.getDeptNo()==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存收费员设置失败，院区号为空");
            return resultMap;
        }
//        if(windows.getPrintIndex()==null){
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存打印机失败，打印机下标为空");
//            return resultMap;
//        }
        String token = TokenUtil.getToken(httpServletRequest);
        if (StringUtils.isBlank(token)) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存收费员设置失败,用户Token不存在");
            return resultMap;
        }
        DecodedJWT decodedJWT=TokenUtil.parseJWT(token);
        User tokenUser=(User)JsonUtil.jsontoObject(decodedJWT.getSubject(),User.class);
        try {
//            String ipAddress=MacAddressUtil.getIpAddr(httpServletRequest);
//            windows.setIpAddress(ipAddress);
            windows.setUserIdCode(tokenUser.getUserIdCode());
            windows.setCreateDate(new Date());
            Windows windowsDb=windowsService.queryLastWindowsByUserIdCode(tokenUser.getUserIdCode(),null);
            if(windowsDb!=null){
                windows.setId(windowsDb.getId());
            }
            int num =windowsService.saveWindows(windows);
            if(num==1){
                resultMap.put("code", 0);
                resultMap.put("message", "保存收费员设置成功");
                return resultMap;
            }
            resultMap.put("code", -1);
            resultMap.put("message", "保存收费员设置失败");
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "保存收费员设置失败,错误信息：【"+e.getMessage()+"】");
            log.error("保存收费员设置失败,错误信息：[]",e.getMessage());
            return resultMap;
        }
    }



    /**
     * 保存打印机
     * @param windows
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/savePrint", method = {RequestMethod.POST})
    public Map<String, Object> savePrint(@RequestBody Windows windows,HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        if(windows==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存打印机失败，参数为空");
            return resultMap;
        }
//        if(windows.getPrintIndex()==null){
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存打印机失败，打印机下标为空");
//            return resultMap;
//        }
        String token = TokenUtil.getToken(httpServletRequest);
        if (StringUtils.isBlank(token)) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存打印机失败,用户Token不存在");
            return resultMap;
        }
        DecodedJWT decodedJWT=TokenUtil.parseJWT(token);
        User tokenUser=(User)JsonUtil.jsontoObject(decodedJWT.getSubject(),User.class);
        try {
            String ipAddress=MacAddressUtil.getIpAddr(httpServletRequest);
            windows.setIpAddress(ipAddress);
            windows.setUserIdCode(tokenUser.getUserIdCode());
            windows.setCreateDate(new Date());
            Windows windowsDb=windowsService.queryLastWindowsByUserIdCode(tokenUser.getUserIdCode(),ipAddress);
            if(windowsDb!=null){
                windows.setId(windowsDb.getId());
            }
            int num =windowsService.saveWindows(windows);
            if(num==1){
                resultMap.put("code", 0);
                resultMap.put("message", "保存打印机成功");
                return resultMap;
            }
            resultMap.put("code", -1);
            resultMap.put("message", "保存打印机失败");
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "保存打印机失败,错误信息：【"+e.getMessage()+"】");
            log.error("保存打印机失败,错误信息：[]",e.getMessage());
            return resultMap;
        }
    }


//    /**
//     * 保存院区号
//     * @param windows
//     * @return
//     */
//    @UserLoginToken
//    @RequestMapping(value = "/saveDeptNo", method = {RequestMethod.POST})
//    public Map<String, Object> saveDeptNo(@RequestBody Windows windows,HttpServletRequest httpServletRequest) {
//        Map<String, Object> resultMap = new HashMap<>();
//        if(windows==null){
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存院区号失败，参数为空");
//            return resultMap;
//        }
//        if(windows.getDeptNo()==null){
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存院区号失败，院区号为空");
//            return resultMap;
//        }
//        String token = TokenUtil.getToken(httpServletRequest);
//        if (StringUtils.isBlank(token)) {
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存院区号失败,用户Token不存在");
//            return resultMap;
//        }
//        DecodedJWT decodedJWT=TokenUtil.parseJWT(token);
//        User tokenUser=(User)JsonUtil.jsontoObject(decodedJWT.getSubject(),User.class);
//        try {
//            windows.setUserIdCode(tokenUser.getUserIdCode());
//            windows.setCreateDate(new Date());
//            Windows windowsDb=windowsService.queryLastWindowsByUserIdCode(tokenUser.getUserIdCode());
//            if(windowsDb!=null){
//                windows.setId(windowsDb.getId());
//            }
//            int num =windowsService.saveUser(windows);
//            if(num==1){
//                resultMap.put("code", 0);
//                resultMap.put("message", "保存院区号成功");
//                return resultMap;
//            }
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存院区号失败");
//            return resultMap;
//        }catch (Exception e){
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存院区失败,错误信息：【"+e.getMessage()+"】");
//            log.error("保存院区失败,错误信息：[]",e.getMessage());
//            return resultMap;
//        }
//    }
//
//
//
//    /**
//     * 保存打印机下标
//     * @param windows
//     * @return
//     */
//    @UserLoginToken
//    @RequestMapping(value = "/savePrintIndex", method = {RequestMethod.POST})
//    public Map<String, Object> savePrintIndex(@RequestBody Windows windows,HttpServletRequest httpServletRequest) {
//        Map<String, Object> resultMap = new HashMap<>();
//        if(windows==null){
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存打印机失败，参数为空");
//            return resultMap;
//        }
//        if(windows.getPrintIndex()==null){
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存打印机失败，打印机下标为空");
//            return resultMap;
//        }
//        String token = TokenUtil.getToken(httpServletRequest);
//        if (StringUtils.isBlank(token)) {
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存打印机失败,用户Token不存在");
//            return resultMap;
//        }
//        DecodedJWT decodedJWT=TokenUtil.parseJWT(token);
//        User tokenUser=(User)JsonUtil.jsontoObject(decodedJWT.getSubject(),User.class);
//        try {
//            windows.setUserIdCode(tokenUser.getUserIdCode());
//            windows.setCreateDate(new Date());
//            Windows windowsDb=windowsService.queryLastWindowsByUserIdCode(tokenUser.getUserIdCode());
//            if(windowsDb!=null){
//                windows.setId(windowsDb.getId());
//            }
//            int num =windowsService.saveUser(windows);
//            if(num==1){
//                resultMap.put("code", 0);
//                resultMap.put("message", "保存打印机成功");
//                return resultMap;
//            }
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存打印机失败");
//            return resultMap;
//        }catch (Exception e){
//            resultMap.put("code", -1);
//            resultMap.put("message", "保存打印机失败,错误信息：【"+e.getMessage()+"】");
//            log.error("保存打印机失败,错误信息：[]",e.getMessage());
//            return resultMap;
//        }
//    }

    /**
     * 查询用户的最后一个在使用中的收费员设置
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getLastWindowsByCurrentUser", method = {RequestMethod.GET})
    public Map<String, Object> getLastWindowsByCurrentUser(HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        String token = TokenUtil.getToken(httpServletRequest);
        if (StringUtils.isBlank(token)) {
            resultMap.put("code", -1);
            resultMap.put("message", "查询收费员设置失败,用户Token不存在");
            return resultMap;
        }
        try {
            String ipAddress=MacAddressUtil.getIpAddr(httpServletRequest);
            DecodedJWT decodedJWT=TokenUtil.parseJWT(token);
            User tokenUser=(User)JsonUtil.jsontoObject(decodedJWT.getSubject(),User.class);
            Windows windows=windowsService.queryLastWindowsByUserIdCode(tokenUser.getUserIdCode(),ipAddress);
            if(windows!=null){
                resultMap.put("code", 0);
                resultMap.put("message", "查询收费员设置成功");
                resultMap.put("data",windows);
                return resultMap;
            }
            resultMap.put("code", -2);
            resultMap.put("message", "查询收费员设置失败");
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询收费员设置失败,错误信息：【"+e.getMessage()+"】");
            log.error("查询收费员设置失败,错误信息：[]",e.getMessage());
            return resultMap;
        }
    }

}
