package com.securitycontrol.background.service.impl;

import com.securitycontrol.background.mapper.IDeviceOfBdMapper;
import com.securitycontrol.background.service.IDeviceOfBdService;
import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.common.core.utils.uuid.IdUtils;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.common.security.utils.ValidatorsUtils;
import com.securitycontrol.entity.background.vo.DeviceBdDetailVo;
import com.securitycontrol.entity.background.dto.DeviceBdDto;
import com.securitycontrol.entity.background.vo.DeviceBdChildVo;
import com.securitycontrol.entity.background.vo.DeviceBdVo;
import com.securitycontrol.entity.background.vo.ProBdDevVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author：cwchen
 * @date：2024-03-20-13:11
 * @version：1.0
 * @description：边带设备-业务逻辑层
 */
@Service(value = "IDeviceOfBdService")
@Slf4j
public class DeviceOfBdServiceImpl implements IDeviceOfBdService {

    @Resource(name = "IDeviceOfBdMapper")
    private IDeviceOfBdMapper mapper;

    @Resource(name = "ValidatorsUtils")
    private ValidatorsUtils validatorsUtils;

    @Override
    public List<DeviceBdVo> getDeviceBdList(DeviceBdDto dto) {
        List<DeviceBdVo> list = new ArrayList<>();
        try {
            list = mapper.getDeviceBdList(dto);
        } catch (Exception e) {
            log.error("获取边带列表", e);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addOrUpdateDeviceBd(DeviceBdVo vo) {
        try {
            String validResult = validatorsUtils.valid(vo, DeviceBdVo.Query.class);
            if (StringUtils.isNotBlank(validResult)) {
                return AjaxResult.error(validResult);
            }
            int result = mapper.isBdCodeExist(vo);
            int isRepeatBanding = 0;
            if (result > 0) {
                return AjaxResult.error("边带编码不能重复");
            }
            if (StringUtils.isEmpty(vo.getId())) {
                String bdId = IdUtils.getUUId();
                vo.setId(bdId);
                vo.setType(1);
                vo.setCreateTime(DateTimeHelper.getNowTime());
                vo.setUpdateTime(DateTimeHelper.getNowTime());
                vo.setBindTime(StringUtils.isNotEmpty(vo.getBidCode()) ? DateTimeHelper.getNowTime() : null);
            } else {
                vo.setType(2);
                if (StringUtils.isNotEmpty(vo.getBidCode())) {
                    isRepeatBanding = mapper.isRepeatBanding(vo);
                    if (isRepeatBanding == 0) {
                        vo.setBindTime(DateTimeHelper.getNowTime());
                    }
                }
            }
            mapper.addOrUpdateDeviceBd(vo);
            // 边带 绑定工程则新增边带绑定记录
            if (!(StringUtils.isEmpty(vo.getBidCode()) || isRepeatBanding > 0)) {
                ProBdDevVo proBdDevVo = new ProBdDevVo();
                proBdDevVo.setId(IdUtils.getUUId());
                proBdDevVo.setDevId(vo.getId());
                proBdDevVo.setBidTime(vo.getBindTime());
                proBdDevVo.setBidCode(vo.getBidCode());
                mapper.addProBdDevData(proBdDevVo);
            }
        } catch (Exception e) {
            log.error("新增/修改边带", e);
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getDeviceBdById(DeviceBdDto dto) {
        DeviceBdVo vo = new DeviceBdVo();
        try {
            vo = mapper.getDeviceBdById(dto);
        } catch (Exception e) {
            log.error("边带设备详情", e);
        }
        return AjaxResult.success(vo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delDeviceBdById(DeviceBdDto dto) {
        try {
            if (StringUtils.isEmpty(dto.getId())) {
                return AjaxResult.error("参数不完整");
            }
            int num = mapper.isHasChildDeviceBd(dto);
            if (num > 0) {
                return AjaxResult.error("边带包含子边带设备");
            }
            mapper.delDeviceBdById(dto);
        } catch (Exception e) {
            log.error("删除边带", e);
            //手动回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public List<DeviceBdChildVo> getDeviceBdChildList(DeviceBdDto dto) {
        List<DeviceBdChildVo> list = new ArrayList<>();
        try {
            list = mapper.getDeviceBdChildList(dto);
        } catch (Exception e) {
            log.error("获取边带子设备列表", e);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addOrUpdateDeviceBdChild(DeviceBdChildVo vo) {
        try {
            String validResult = validatorsUtils.valid(vo, DeviceBdChildVo.Query.class);
            if (StringUtils.isNotBlank(validResult)) {
                return AjaxResult.error(validResult);
            }
            int result = mapper.isDeviceCodeExist(vo);
            if (result > 0) {
                return AjaxResult.error("设备编码不能重复");
            }
            if (StringUtils.isEmpty(vo.getDeviceId())) {
                String deviceId = IdUtils.getUUId();
                vo.setDeviceId(deviceId);
                vo.setType(1);
                vo.setCreateTime(DateTimeHelper.getNowTime());
                vo.setUpdateTime(DateTimeHelper.getNowTime());
            } else {
                vo.setType(2);
                vo.setUpdateTime(DateTimeHelper.getNowTime());
            }
            mapper.addOrUpdateDeviceBdChild(vo);
        } catch (Exception e) {
            log.error("新增/修改边带设备", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getDeviceBdChildById(DeviceBdDto dto) {
        DeviceBdChildVo vo = new DeviceBdChildVo();
        try {
            vo = mapper.getDeviceBdChildById(dto);
        } catch (Exception e) {
            log.error("边带子设备详情", e);
        }
        return AjaxResult.success(vo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delDeviceBdChildById(DeviceBdDto dto) {
        try {
            if (StringUtils.isEmpty(dto.getId())) {
                return AjaxResult.error("参数不完整");
            }
            mapper.delDeviceBdChildById(dto);
        } catch (Exception e) {
            log.error("删除边带设备", e);
            //手动回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public List<DeviceBdDetailVo> getDeviceBdChildDetailList(DeviceBdDto dto) {
        List<DeviceBdDetailVo> list = new ArrayList<>();
        try {
            list = mapper.getDeviceBdChildDetailList(dto);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("获取边带子设备详情列表", e);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addOrUpdateDeviceBdChildDetail(DeviceBdDetailVo vo) {
        try {
            String validResult = validatorsUtils.valid(vo, DeviceBdDetailVo.Query.class);
            if (StringUtils.isNotBlank(validResult)) {
                return AjaxResult.error(validResult);
            }
            int result = mapper.isBdDeviceDetailExist(vo);
            if (result > 0) {
                return AjaxResult.error("检测不能重复");
            }
            if (StringUtils.isEmpty(vo.getId())) {
                String id = IdUtils.getUUId();
                vo.setId(id);
                vo.setType(1);
            } else {
                vo.setType(2);
            }
            mapper.addOrUpdateDeviceBdChildDetail(vo);
            mapper.addOrUpdateDeviceValue(vo);
        } catch (Exception e) {
            log.error("新增/修改边带子设备详情数据", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getDeviceBdChildDetailById(DeviceBdDto dto) {
        DeviceBdDetailVo vo = new DeviceBdDetailVo();
        try {
            vo = mapper.getDeviceBdChildDetailById(dto);
        } catch (Exception e) {
            log.error("边带子设备详情数据", e);
        }
        return AjaxResult.success(vo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delDeviceBdChildDetail(DeviceBdDto dto) {
        try {
            if (StringUtils.isEmpty(dto.getId())) {
                return AjaxResult.error("参数不完整");
            }
            mapper.delDeviceBdChildDetail(dto);
            mapper.delDeviceValue(dto);
        } catch (Exception e) {
            log.error("删除边带子设备详情数据", e);
            //手动回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }
}
