package com.mx.cneeds.server.logupload.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mx.cneeds.common.dto.DeviceLogDto;
import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.common.pager.Query;
import com.mx.cneeds.server.dao.DeviceLogDao;
import com.mx.cneeds.server.entity.DeviceLogEntity;
import com.mx.cneeds.server.logupload.service.DeviceLogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @ClassName DeviceLogServiceImpl
 * @Author zzrdark
 * @Date 2020-03-31 14:14
 * @Description TODO
 **/
@Service
public class DeviceLogServiceImpl extends ServiceImpl<DeviceLogDao, DeviceLogEntity> implements DeviceLogService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DeviceLogEntity> page = this.page(
                new Query<DeviceLogEntity>().getPage(params),
                new QueryWrapper<DeviceLogEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] deviceLogIds) {
        //删除
        this.removeByIds(Arrays.asList(deviceLogIds));
    }

    /**
     * 设置状态
     * @param logId
     */
    @Override
    public void changeStatus(Long logId){
        DeviceLogEntity deviceLogEntity = new DeviceLogEntity();
        deviceLogEntity.setStatus(1);
        getBaseMapper().update(deviceLogEntity, new UpdateWrapper<DeviceLogEntity>().eq("log_id", logId));
    }

    @Override
    public Long wechatUploadLog(DeviceLogEntity deviceLogEntity){
        DeviceLogEntity logEntity = queryOneBylogAcceptId(deviceLogEntity.getLogAcceptId());
        if(logEntity != null){
            /*//未处理状态
            deviceLogEntity.setStatus(0);
            deviceLogEntity.setLogfileStatus(1);
            getBaseMapper().update(deviceLogEntity,
                    new UpdateWrapper<DeviceLogEntity>()
                            .eq("log_accept_id", deviceLogEntity.getLogAcceptId()));*/
        }else {
            deviceLogEntity.setCreateTime(new Date());
            deviceLogEntity.setStatus(-1);
//            deviceLogEntity.setLogfileStatus();
            getBaseMapper().insert(deviceLogEntity);
            logEntity = queryOneBylogAcceptId(deviceLogEntity.getLogAcceptId());
        }

        return logEntity.getLogId();

    }


    @Override
    public void deviceUploadLogFile(DeviceLogEntity deviceLogEntity){
        DeviceLogEntity logEntity = queryOneBylogAcceptId(deviceLogEntity.getLogAcceptId());
        if(logEntity != null){
            //未处理状态
            deviceLogEntity.setStatus(0);
            deviceLogEntity.setLogfileStatus(1);
            getBaseMapper().update(deviceLogEntity,
                    new UpdateWrapper<DeviceLogEntity>()
                            .eq("log_accept_id", deviceLogEntity.getLogAcceptId()));
        }
        /*else {
            deviceLogEntity.setCreateTime(new Date());
            deviceLogEntity.setStatus(-1);
            getBaseMapper().insert(deviceLogEntity);
        }*/
    }

    @Override
    public DeviceLogEntity queryOneBylogAcceptId(String logAcceptId){
        return getBaseMapper().selectOne(new QueryWrapper<DeviceLogEntity>()
                .eq("log_accept_id",logAcceptId));
    }

}
