package com.pqs9900.controller.filebackup;

import com.njcn.enums.LogTypeEnum;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.AppConfig;
import com.njcn.utils.PubUtils;
import com.pqs9900.service.filebackup.FileBackUpService;
import com.pqs9900.serviceImpl.HisData.DataFlickerServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.security.auth.login.AccountException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xuyang
 * @date 2020/9/11 16:49
 */
@Controller
@RequestMapping(value = "fileBackUp")
public class FileBackUpController {

    private static final Logger logger= LoggerFactory.getLogger(DataFlickerServiceImpl.class);

    @Autowired
    private UserLogDetailService userLogDetailService;

    @Autowired
    private AppConfig appConfig;

    @Resource
    private FileBackUpService fileBackUpService;

    @RequestMapping(value = "writeFile",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> fileCode(ServletRequest request){
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        Map<String,Object> map ;
        try {
              userLogDetailService.saveUserLog(TokenManager.getToken().getUserIndex(), TokenManager.getToken().getLoginName(),"文件备份","成功",appConfig.getSystemFlag(), PubUtils.getIpAddr(httpRequest), LogTypeEnum.SYSTEM.toString(),1);
//            if ((int) map.get("code") == 0){
//            } else {
//                userLogDetailService.saveUserLog(TokenManager.getToken().getUserIndex(), TokenManager.getToken().getLoginName(),"查询结果为0条,备份","成功",appConfig.getSystemFlag(), PubUtils.getIpAddr(httpRequest), LogTypeEnum.SYSTEM.toString(),0);
//            }
            map = fileBackUpService.fileCode();
        } catch (AccountException e) {
            try {
                userLogDetailService.saveUserLog(TokenManager.getToken().getUserIndex(), TokenManager.getToken().getLoginName(),"文件备份","失败",appConfig.getSystemFlag(), PubUtils.getIpAddr(httpRequest), LogTypeEnum.SYSTEM.toString(),1);
            } catch (Exception e1) {
                logger.error("用户异常登录日志入库出错，" + e1.toString());
            }
            logger.error("备份失败，异常为："+e.toString());
            map=new HashMap<>();
            map.put("code",0);
        }
        return map;
    }

    @RequestMapping(value = "deleteUserlog",method = RequestMethod.POST)
    @ResponseBody
    public Integer deleteUserlog(ServletRequest request){
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        Integer code = 0;
        int deleteLine = fileBackUpService.deleteUserLog();
        if (deleteLine > 0){
            code = 200;
        }
        return code;
    }

    @RequestMapping(value = "fileRecovery",method = RequestMethod.POST)
    @ResponseBody
    public  Map<String,Object> fileRecovery(ServletRequest request){
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        Map<String,Object> map = fileBackUpService.fileRecovery();
        try {
            if ((int)map.get("code") == 200){
                    userLogDetailService.saveUserLog(TokenManager.getToken().getUserIndex(), TokenManager.getToken().getLoginName(),"数据恢复","成功",appConfig.getSystemFlag(), PubUtils.getIpAddr(httpRequest), LogTypeEnum.SYSTEM.toString(),1);
                } else {
                    userLogDetailService.saveUserLog(TokenManager.getToken().getUserIndex(), TokenManager.getToken().getLoginName(),"数据恢复","失败",appConfig.getSystemFlag(), PubUtils.getIpAddr(httpRequest), LogTypeEnum.SYSTEM.toString(),1);
                }
            }
        catch (AccountException e) {
            logger.error("恢复失败，异常为："+e.toString());
        }
        return map;
    }
}
