package com.wms.controller;

import com.wms.annotation.MyLog;
import com.wms.constants.enums.ResponseCode;
import com.wms.entity.app.ResponseEntity;
import com.wms.entity.app.request.UserQuery;
import com.wms.system_service.ISystemService;
import com.wms.utils.MailUtils;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import static com.wms.utils.StringUtils.convertJsonString;

@Controller
@CrossOrigin
@RequestMapping(value = "/wms/system")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SystemController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 系统服务
     */
    private final ISystemService systemService;

    /**
     * 重启系统
     *
     * @param user 用户
     * @return 结果
     */
    @PostMapping("/restartSystem")
    @ResponseBody
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    @MyLog(logTitle = "重启系统", logMethod = "restartSystem")
    public String restartSystem(@RequestBody UserQuery user) {
        logger.info("接收到重启系统请求：{}", convertJsonString(user));
        ResponseEntity response = new ResponseEntity();
        if (user == null || user.getRoleId() != 1) {// 不是管理员不允许重启
            logger.error("非管理员不允许重启");
            response.setCode(ResponseCode.ERROR.getCode());
            response.setMessage("非管理员不允许重启");
            return convertJsonString(response);
        }
        if (systemService.restartSystem()) {
            logger.info("重启成功");
            response.setCode(ResponseCode.OK.getCode());
            response.setMessage("重启成功");
        } else {
            logger.error("重启失败");
            response.setCode(ResponseCode.ERROR.getCode());
            response.setMessage("重启失败");
        }
        return convertJsonString(response);
    }

    /**
     * 重新载入配置
     *
     * @param user 用户
     * @return 结果
     */
    @PostMapping("/reloadConfig")
    @ResponseBody
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    @MyLog(logTitle = "重新载入配置", logMethod = "reloadConfig")
    public String reloadConfig(@RequestBody UserQuery user) {
        logger.info("接收到重载配置请求：{}", convertJsonString(user));
        ResponseEntity response = new ResponseEntity();
        if (user == null || user.getRoleId() != 1) {// 不是管理员不允许重启
            logger.error("非管理员不允许重载配置");
            response.setCode(ResponseCode.ERROR.getCode());
            response.setMessage("非管理员不允许重载配置");
            return convertJsonString(response);
        }
        if (systemService.reloadConfig()) {
            logger.info("重载配置成功");
            response.setCode(ResponseCode.OK.getCode());
            response.setMessage("重载配置成功");
        } else {
            logger.error("重载配置失败");
            response.setCode(ResponseCode.ERROR.getCode());
            response.setMessage("重载配置失败");
        }
        return convertJsonString(response);
    }

    /**
     * 发送邮件
     *
     * @param user 用户
     * @return 结果
     */
    @PostMapping("/sendMail")
    @ResponseBody
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
//    @MyLog(logTitle = "发送邮件", logMethod = "sendMail")
    public String sendMail(@RequestBody UserQuery user) {
        try {
            MailUtils.sendMailSmtp("594755172@qq.com", "测试发送", "测试发送");
        } catch (Exception e) {
            logger.error("发送邮件失败");
        }
        return convertJsonString(new ResponseEntity());
    }
}
