package com.mosty.spxl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.utils.DateUtils;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.base.model.dto.spxl.TbSpxlSpxlfaInsertDto;
import com.mosty.base.model.dto.spxl.TbSpxlSpxlfaSxtDto;
import com.mosty.base.model.dto.spxl.TbSpxlSpxlfaSxtzDto;
import com.mosty.base.model.entity.spxl.TbSpxlSpxlfa;
import com.mosty.base.model.entity.spxl.TbSpxlSpxlfaSxt;
import com.mosty.base.model.entity.spxl.TbSpxlSpxlfaSxtPz;
import com.mosty.base.model.entity.spxl.TbSpxlSpxlfaSxtz;
import com.mosty.spxl.mapper.TbSpxlSpxlfaMapper;
import com.mosty.spxl.mapper.TbSpxlSpxlfaSxtMapper;
import com.mosty.spxl.mapper.TbSpxlSpxlfaSxtPzMapper;
import com.mosty.spxl.mapper.TbSpxlSpxlfaSxtzMapper;
import com.mosty.spxl.service.TbSpxlSpxlfaSxtService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author dw
 * @since 2022/7/11
 * 视频巡逻方案-摄像头-服务-实现
 **/
@Service
public class TbSpxlSpxlfaSxtServiceImpl extends ServiceImpl<TbSpxlSpxlfaSxtMapper, TbSpxlSpxlfaSxt>
        implements TbSpxlSpxlfaSxtService {

    @Resource
    private TbSpxlSpxlfaMapper tbSpxlSpxlfaMapper;

    @Resource
    private TbSpxlSpxlfaSxtzMapper tbSpxlSpxlfaSxtzMapper;

    @Resource
    private TbSpxlSpxlfaSxtPzMapper tbSpxlSpxlfaSxtPzMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEntity(TbSpxlSpxlfaInsertDto dto) {
        TbSpxlSpxlfa fa = this.tbSpxlSpxlfaMapper.selectById(dto.getFaid());
        if (fa == null) {
            throw new BusinessException("方案不存在，或已经删除");
        }
        List<TbSpxlSpxlfaSxtzDto> sxtzList = dto.getSxtzList();
        if (CollectionUtils.isEmpty(sxtzList)) {
            throw new BusinessException("请至少添加一个摄像头组");
        }
        AtomicInteger a = new AtomicInteger(1);
        sxtzList.forEach(newSxtz -> {
            // 添加摄像头组
            TbSpxlSpxlfaSxtz sxtz = new TbSpxlSpxlfaSxtz();
            BeanUtils.copyProperties(newSxtz, sxtz);
            if (StringUtils.isBlank(sxtz.getSxtzmc())) {
                sxtz.setSxtzmc("摄像头组" + a.getAndIncrement());
            }
            sxtz.setFaid(fa.getId());
            sxtz.setFamc(fa.getFamc());
            sxtz.setXtSjly("1");
            this.tbSpxlSpxlfaSxtzMapper.insert(sxtz);
            // 添加摄像头
            List<TbSpxlSpxlfaSxtDto> sxtList = newSxtz.getSxtList();
            if (CollectionUtils.isEmpty(sxtList)) {
                throw new BusinessException("每个组至少添加一个摄像头");
            }
            sxtList.forEach(newSxt -> {
                // 查询是否存在该摄像头已经被其他单位配置，并且在同一时段内
                TbSpxlSpxlfaSxt temp = this.baseMapper.selectOne(
                        new LambdaQueryWrapper<TbSpxlSpxlfaSxt>()
                                .eq(TbSpxlSpxlfaSxt::getSxtid, newSxt.getSxtid())
                                .eq(TbSpxlSpxlfaSxt::getXtScbz, "0")
                                .eq(TbSpxlSpxlfaSxt::getXtSjzt, "1")
                                .last(" limit 1")
                );
                if (temp != null) {
                    TbSpxlSpxlfa faTemp = this.tbSpxlSpxlfaMapper.selectOne(
                            new LambdaQueryWrapper<TbSpxlSpxlfa>()
                                    .eq(TbSpxlSpxlfa::getId, temp.getFaid())
                                    .eq(TbSpxlSpxlfa::getXtScbz, "0")
                                    .eq(TbSpxlSpxlfa::getXtSjzt, "1")
                    );
                    if (faTemp != null) {
                        Date faStartTime = DateUtils.strToDate(fa.getKssj(), "09");
                        Date faEndTime = DateUtils.strToDate(fa.getJssj(), "09");
                        Date faTempStartTime = DateUtils.strToDate(faTemp.getJssj(), "09");
                        Date faTempEndTime = DateUtils.strToDate(faTemp.getJssj(), "09");
                        if (faTempStartTime.getTime() > faStartTime.getTime() || faTempEndTime.getTime() < faEndTime.getTime()) {
                            throw new BusinessException("该摄像头在该时段有其他方案正在运行中，请选择其他摄像头，或修改之前配置。");
                        }
                    }
                }
                TbSpxlSpxlfaSxt sxt = new TbSpxlSpxlfaSxt();
                BeanUtils.copyProperties(newSxt, sxt);
                sxt.setFaid(fa.getId());
                sxt.setFamc(fa.getFamc());
                sxt.setSxtzid(sxtz.getId());
                sxt.setXtSjly("1");
                this.baseMapper.insert(sxt);
                // 获取摄像头配置信息
                TbSpxlSpxlfaSxtPz pz = newSxt.getSxtPz();
                if (pz == null && "2".equals(fa.getFalx())) {
                    throw new BusinessException("请配置每一个摄像头");
                }
                if (pz != null) {
                    pz.setFaid(fa.getId());
                    pz.setFamc(fa.getFamc());
                    pz.setSxtzid(sxtz.getId());
                    pz.setSxtid(sxt.getId());
                    pz.setXtSjly("1");
                    this.tbSpxlSpxlfaSxtPzMapper.insert(pz);
                }
            });
        });
        return 1;
    }

    @Override
    @Transactional
    public int updateSxtPz(TbSpxlSpxlfaInsertDto dto) {
        // 删除该方案配置的所有的信息
        this.deleteFaPz(dto.getFaid());
        // 重新添加方案配置信息
        return this.insertEntity(dto);
    }

    @Override
    @Transactional
    public void deleteFaPz(String faid) {
        // 删除摄像头组
        this.tbSpxlSpxlfaSxtzMapper.delete(
                new LambdaQueryWrapper<TbSpxlSpxlfaSxtz>()
                        .eq(TbSpxlSpxlfaSxtz::getFaid, faid)
        );
        // 删除摄像头
        this.baseMapper.delete(
                new LambdaQueryWrapper<TbSpxlSpxlfaSxt>()
                        .eq(TbSpxlSpxlfaSxt::getFaid, faid)
        );
        // 删除摄像头配置
        this.tbSpxlSpxlfaSxtPzMapper.delete(
                new LambdaQueryWrapper<TbSpxlSpxlfaSxtPz>()
                        .eq(TbSpxlSpxlfaSxtPz::getFaid, faid)
        );
    }

    @Override
    public TbSpxlSpxlfa pzInfo(String faid) {
        TbSpxlSpxlfa fa = this.tbSpxlSpxlfaMapper.selectById(faid);
        if (fa == null) {
            throw new BusinessException("方案不存在，或已经被删除");
        }
        // 查询摄像头组
        List<TbSpxlSpxlfaSxtz> sxtzList = this.tbSpxlSpxlfaSxtzMapper.selectList(
                new LambdaQueryWrapper<TbSpxlSpxlfaSxtz>()
                        .eq(TbSpxlSpxlfaSxtz::getFaid, faid)
        );
        sxtzList.forEach(sxtz -> {
            // 查询摄像头
            List<TbSpxlSpxlfaSxt> sxtList = this.baseMapper.selectList(
                    new LambdaQueryWrapper<TbSpxlSpxlfaSxt>()
                            .eq(TbSpxlSpxlfaSxt::getFaid, faid)
                            .eq(TbSpxlSpxlfaSxt::getSxtzid, sxtz.getId())
            );
            sxtList.forEach(sxt -> {
                // 查询摄像头配置
                TbSpxlSpxlfaSxtPz pz = this.tbSpxlSpxlfaSxtPzMapper.selectOne(
                        new LambdaQueryWrapper<TbSpxlSpxlfaSxtPz>()
                                .eq(TbSpxlSpxlfaSxtPz::getFaid, faid)
                                .eq(TbSpxlSpxlfaSxtPz::getSxtid, sxt.getId())
                );
                sxt.setPz(pz);
            });
            sxtz.setSxtList(sxtList);
        });
        fa.setSxtzList(sxtzList);
        return fa;
    }
}
