package com.zd.laboratory.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.log.annotation.Log;
import com.zd.common.security.service.TokenService;
import com.zd.laboratory.domain.LabHardware;
import com.zd.laboratory.domain.vo.LabHardwareVO;
import com.zd.laboratory.mapper.ApkFileUpLoadMapper;
import com.zd.laboratory.mapper.ApkLogMapper;
import com.zd.laboratory.mapper.LabHardwareMapper;
import com.zd.laboratory.mqtt.enums.BigViewDataType;
import com.zd.laboratory.mqtt.service.impl.SubMessageSendManager;
import com.zd.laboratory.onemachine.domain.ApkLog;
import com.zd.laboratory.onemachine.domain.ApkLogVo;
import com.zd.laboratory.onemachine.domain.ApkUpload;
import com.zd.laboratory.onemachine.domain.ApkUploadVo;
import com.zd.laboratory.service.ApkFileUpLoadService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Controller
 *
 * @author cyl
 * @date 2021/12/7
 */
@Service
public class ApkFileUpLoadServiceImpl implements ApkFileUpLoadService {

    @Autowired
    private ApkFileUpLoadMapper apkFileUpLoadMapper;

    @Autowired
    private LabHardwareMapper labHardwareMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ApkLogMapper apkLogMapper;

    @Autowired
    private SubMessageSendManager messageSendService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertApkFileUpLoad(ApkUploadVo apkUploadVo) {
        apkUploadVo.setCreateBy(tokenService.getLoginUser().getSysUser().getNickName());
        apkUploadVo.setCreateTime(new Date());
        int apkId = apkFileUpLoadMapper.insertApkFileUpLoad(apkUploadVo);
        LabHardwareVO labHardware = new LabHardwareVO();
        labHardware.setDeptId(apkUploadVo.getDeptId());
        labHardware.setSubjectIds(apkUploadVo.getSubId());
        if(labHardware.getDeptId()!=null || labHardware.getSubjectIds()!=null){
            List <LabHardware> hardwareList =  labHardwareMapper.getHardwareListBySubId(labHardware);
            Optional.ofNullable(hardwareList).orElseGet(Collections::emptyList).stream().forEach(a->{
                ApkLog apkLog = new ApkLog();
                apkLog.setOnepcHardwareNum(a.getHardwareNum());
                apkLog.setApkUploadId(apkUploadVo.getId()+"");
                apkLog.setLogStatus(3);
                apkLog.setCreateBy(tokenService.getLoginUser().getSysUser().getNickName());
                apkLog.setCreateTime(new Date());
                apkLogMapper.insertApkLog(apkLog);
            });
        }

        return apkId;
    }

    @Override
    public int insertApkUpLoad(ApkUpload apkUpload) {
        ApkUploadVo apkUploadVo = new ApkUploadVo();
        BeanUtils.copyProperties(apkUpload, apkUploadVo);
        apkUploadVo.setCreateBy(tokenService.getLoginUser().getSysUser().getNickName());
        apkUploadVo.setCreateTime(new Date());
        int apkId = apkFileUpLoadMapper.insertApkFileUpLoad(apkUploadVo);
        return apkId;
    }

    @Override
    public List <ApkLogVo> selectApkLogList(ApkLogVo apkLogVo) {
        return apkLogMapper.selectApkLogList(apkLogVo);
    }

    @Override
    public List <ApkUploadVo> selectApkFileUpLoadList(ApkUploadVo apkUploadVo) {
        return apkFileUpLoadMapper.selectApkFileUpLoadList(apkUploadVo);
    }

    @Override
    public int addApkLog(ApkLogVo apkLogVo) {
        LabHardwareVO labHardware = new LabHardwareVO();
        labHardware.setDeptId(apkLogVo.getDeptId());
        labHardware.setSubjectIds(apkLogVo.getSubId());
        if(labHardware.getDeptId()!=null || labHardware.getSubjectIds()!=null){
            if(apkLogVo.getOnepcType()==1){
                labHardware.setPcType(0);
            }else if(apkLogVo.getOnepcType()==2){
                labHardware.setPcType(1);
            }else{
                throw new ServiceException("无效的设备类型");
            }
            List <LabHardware> hardwareList =  labHardwareMapper.getHardwareListBySubId(labHardware);
            Optional.ofNullable(hardwareList).orElseGet(Collections::emptyList).stream().forEach(a->{
                ApkLog apkLog = new ApkLog();
                apkLog.setOnepcHardwareNum(a.getHardwareNum());
                apkLog.setApkUploadId(apkLogVo.getApkId()+"");
                apkLog.setLogStatus(3);
                apkLog.setCreateBy(tokenService.getLoginUser().getSysUser().getNickName());
                apkLog.setCreateTime(new Date());
                apkLogMapper.insertApkLog(apkLog);
                //调用一体机推送方法
                sendOnePcInfo(apkLog.getId());
            });
        }
        return 1;
    }

    @Override
    public int updateApkLog(ApkLogVo apkLogVo) {
        ApkLog apkLog = new ApkLog();
        apkLog.setId(apkLogVo.getId());
        apkLog.setLogStatus(3);
        apkLog.setApkUploadId(apkLogVo.getApkId()+"");
        apkLog.setUpdateTime(new Date());
        apkLog.setUpdateBy(tokenService.getLoginUser().getSysUser().getNickName());
        int flag = apkLogMapper.updateApkLog(apkLog);
        //调用一体机推送方法
        sendOnePcInfo(apkLogVo.getId());
        return flag;
    }


    public void sendOnePcInfo(Long id){
        ApkLogVo newApkLogVo = new ApkLogVo();
        newApkLogVo.setId(id);
        List<ApkLogVo> apkLogList = apkLogMapper.selectApkLogList(newApkLogVo);
        if(apkLogList.size()>0){
            //向一体机发送消息，通知升级apk
            JSONObject object = new JSONObject();
            object.put("onepcHardwareNum",apkLogList.get(0).getOnepcHardwareNum());
            object.put("apkUploadFile", apkLogList.get(0).getApkFileUpload());
            object.put("version", apkLogList.get(0).getVersion());
            messageSendService.sendOnepcApkup(object);
        }
    }


    @Override
    public ApkLogVo selectApkLogDetail(ApkLogVo apkLogVo) {
        return apkLogMapper.selectApkLogDetail(apkLogVo);
    }


    @Override
    public int updateApkLogByStatus(ApkLog apkLog) {
        ApkLogVo apkLogVo = new ApkLogVo();
        apkLogVo.setOnepcHardwareNum(apkLog.getOnepcHardwareNum());
        List <ApkLogVo> apkLogList = apkLogMapper.selectApkLogList(apkLogVo);
        int flag = 0;
        if(apkLogList.size()>0){
            apkLog.setId(apkLogList.get(0).getId());
            flag = apkLogMapper.updateApkLogByStatus(apkLog);
        }
        return flag;
    }

    @Override
    public int deleteApkUploadByIds(Long apkId) {
        return apkFileUpLoadMapper.deleteApkUploadByIds(apkId);
    }
}
