package com.xydl.cac.service.impl;

import com.xydl.cac.entity.Jg;
import com.xydl.cac.entity.Lx;
import com.xydl.cac.entity.Modev;
import com.xydl.cac.entity.Zsb;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.repository.LxRepository;
import com.xydl.cac.repository.ModevRepository;
import com.xydl.cac.repository.ZsbRepository;
import com.xydl.cac.service.JgService;
import com.xydl.cac.service.ZsbService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ZsbServiceImpl implements ZsbService {

    @Resource
    ZsbRepository repository;
    @Resource
    JgService jgService;
    @Resource
    LxRepository lxRepository;
    @Resource
    ModevRepository modevRepository;

    @Override
    public List<Zsb> listAll(Integer jgid) throws Exception {
        if (jgid == null) {
            if (StaticVariable.zsb_Cache == null) {
                StaticVariable.zsb_Cache = repository.findAll();
                this.fillJgBdzName(StaticVariable.zsb_Cache);
                this.fillLxName(StaticVariable.zsb_Cache);
            }
            return StaticVariable.zsb_Cache;
        } else {
            List<Zsb> result = repository.findByJgid(jgid);
            Jg jg = jgService.detail(jgid);
            if (!CollectionUtils.isEmpty(result)) {
                for (Zsb zsb : result) {
                    zsb.setJgName(jg.getMc());
                    zsb.setBdzName(jg.getBdzName());
                }
                this.fillLxName(result);
            }
            return result;
        }
    }

    private void fillJgBdzName(List<Zsb> list) {
        List<Jg> jgList = jgService.listAll(null);
        if (!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(jgList)) {
            for (Zsb zsb : list) {
                for (Jg jg : jgList) {
                    if (jg.getId().equals(zsb.getJgid())) {
                        zsb.setBdzName(jg.getBdzName());
                        zsb.setJgName(jg.getMc());
                        break;
                    }
                }
            }
        }
    }

    private void fillLxName(List<Zsb> list) {
        List<Lx> lxList = lxRepository.findAll();
        if (!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(lxList)) {
            for (Zsb zsb : list) {
                for (Lx lx : lxList) {
                    if (lx.getId().equals(zsb.getLxid())) {
                        zsb.setLxName(lx.getMc());
                        break;
                    }
                }
            }
        }
    }

    @Override
    public Zsb add(Zsb item) throws Exception {
        item.setId(null);
        Jg jg = jgService.detail(item.getJgid());
        item.setBdzid(jg.getBdzid());
        Optional<Lx> optionalLx = lxRepository.findById(item.getLxid());
        if (!optionalLx.isPresent()) {
            throw new BusinessException("未找到该设备类型id=" + item.getLxid());
        }
        List<Zsb> list = repository.findByJgidAndMc(item.getJgid(), item.getMc());
        if (!CollectionUtils.isEmpty(list)) {
            throw new BusinessException("该名称已被使用");
        }
        StaticVariable.zsb_Cache = null;
        return repository.save(item);
    }

    @Override
    public void update(Zsb item) throws Exception {
        Jg jg = jgService.detail(item.getJgid());
        item.setBdzid(jg.getBdzid());
        Optional<Lx> optionalLx = lxRepository.findById(item.getLxid());
        if (!optionalLx.isPresent()) {
            throw new BusinessException("未找到该设备类型id=" + item.getLxid());
        }
        List<Zsb> list = repository.findByJgidAndMcAndIdIsNot(item.getJgid(), item.getMc(), item.getId());
        if (!CollectionUtils.isEmpty(list)) {
            throw new BusinessException("该名称已被使用");
        }
        repository.save(item);
        StaticVariable.zsb_Cache = null;
    }

    @Override
    public void delete(Integer id) throws Exception {
        List<Modev> modevList = modevRepository.findByZsbid(id);
        if (!CollectionUtils.isEmpty(modevList)) {
            throw new BusinessException("已被监测装置使用不能删除");
        }
        repository.deleteById(id);
        StaticVariable.zsb_Cache = null;
    }

    @Override
    public Zsb detail(Integer id) throws Exception {
        Optional<Zsb> optional = repository.findById(id);
        if (!optional.isPresent()) {
            throw new BusinessException("未找到该主设备id=" + id);
        }
        Zsb zsb = optional.get();
        Jg jg = jgService.detail(zsb.getJgid());
        zsb.setBdzName(jg.getBdzName());
        zsb.setJgName(jg.getMc());
        Optional<Lx> optionalLx = lxRepository.findById(zsb.getLxid());
        if (optionalLx.isPresent()) {
            zsb.setLxName(optionalLx.get().getMc());
        }
        return zsb;
    }
}
