package com.sw.mes.fb.checkout.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sw.mes.base.system.entity.SysLog;
import com.sw.mes.base.system.service.SysLogService;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.IpUtils;
import com.sw.mes.fb.checkout.dao.SquareRodNumberDao;
import com.sw.mes.fb.checkout.dto.SquareRodNumberDto;
import com.sw.mes.fb.checkout.entity.SquareRodNumber;
import com.sw.mes.fb.checkout.notification.SquareRodNumberMessage;
import com.sw.mes.fb.checkout.service.SquareRodNumberService;
import com.sw.mes.fb.utils.StringUtils;
import com.sw.mes.mqtt.service.NotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
@Slf4j
public class SquareRodNumberServiceImpl extends BaseServiceImpl<SquareRodNumber> implements SquareRodNumberService {

    @Autowired
    private NotificationService notificationService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private SysLogService sysLogService;

    SquareRodNumberDao dao;

    @Autowired
    public void setDao(SquareRodNumberDao dao) {
        super.setBaseDao(dao);
        this.dao = dao;
    }

    @Override
    public String getFbSectionNo(String point) {
        String byPoint = dao.findByPoint(point);
        if(!StringUtils.isBlank(byPoint)){
            return byPoint;
        }
        return null;
    }

    @Override
    public SquareRodNumber getSquareRodNumber(String point) {
        SquareRodNumber squareRodNumber = dao.findByPoint1(point);
        if(null != squareRodNumber){
            return squareRodNumber;
        }
        return null;
    }

    @Override
    public List<SquareRodNumber> getSquareRodNumbers(String point) {
        List<SquareRodNumber> squareRodNumber = dao.findByPoint2(point);
        if(null != squareRodNumber){
            return squareRodNumber;
        }
        return null;
    }

    @Override
    public List<SquareRodNumber> getSquareRodNumbersBySectionNo(String sectionNo) {
        List<SquareRodNumber> squareRodNumber = dao.findBySectionNo(sectionNo);
        if(null != squareRodNumber){
            return squareRodNumber;
        }
        return null;
    }

    @Override
    @Async
    public void sendToMcsData(String crystalNo) {
        SquareRodNumberDto squareRodNumberDto = dao.findByCrystalNo(crystalNo);
        String returnStr;
        ResponseEntity<String> exchange;
        try {
            HttpHeaders headers = new HttpHeaders();
            MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(mediaType);
            headers.add("Accept", MediaType.APPLICATION_JSON.toString());
            HttpEntity<SquareRodNumberDto> httpEntity = new HttpEntity<>(squareRodNumberDto, headers);
            exchange = restTemplate.exchange("", HttpMethod.POST, httpEntity, String.class);

            returnStr = exchange.getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
            log.error("请求MCS发送晶棒编号及点位数据失败", e);
            returnStr = e.getMessage();
        }

        SysLog syslog = new SysLog();
        syslog.setCreateTime(new Date());
        syslog.setType("info");
        syslog.setCreateUserName("SYSTEM");
        syslog.setContent(JSON.toJSONString(squareRodNumberDto));
        syslog.setResult(returnStr);
        syslog.setMethodName("sendToMcsData()");
        syslog.setStatus(1);
        syslog.setServerAddr("");
        syslog.setReqType("HTTP");
        syslog.setReqIp(IpUtils.getLocalIP());
        sysLogService.save(syslog);
    }

    @Override
    public void saveData(SquareRodNumber squareRodNumber) {
        squareRodNumber.setCreateDate(new Date());
        try {
            dao.save(squareRodNumber);
//            Long id = squareRodNumber.getId();
//            String sectionNoFb = squareRodNumber.getCrystalNo();
//            String point = squareRodNumber.getPoint();
//            //发送MQTT通知
//            String message = sendSquareRodNumberMqtt(id, sectionNoFb, point);
//            SysLog syslog = new SysLog();
//            syslog.setCreateTime(new Date());
//            syslog.setMethodName("记录并发送晶棒编号及点位信息");
//            syslog.setCreateUserName("SYSTEM");
//            syslog.setContent(JSON.toJSONString(message));
//            syslog.setReqType("MQTT");
//            sysLogService.save(syslog);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("记录异常：" + e.getMessage());
        }


    }

    private String sendSquareRodNumberMqtt(long id,String sectionNoFb,String point){
        SquareRodNumberMessage message = new SquareRodNumberMessage();
        message.setPoint(point);
        message.setCrystalNo(sectionNoFb);
        message.setId(id);
        notificationService.sendNotification(message);
        return JSONObject.toJSONString(message);
    }
}
