package com.inspect.patrol.service.impl;

import com.inspect.base.core.utils.DateUtils;
import com.inspect.patrol.domain.PatrolLinkageStrategy;
import com.inspect.patrol.domain.PatrolLinkageStrategyRef;
import com.inspect.patrol.domain.PatrolLinkageStrategyReverse;
import com.inspect.patrol.mapper.PatrolLinkageStrategyMapper;
import com.inspect.patrol.mapper.PatrolLinkageStrategyRefMapper;
import com.inspect.patrol.mapper.PatrolLinkageStrategyReverseMapper;
import com.inspect.patrol.service.IPatrolLinkageStrategyService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PatrolLinkageStrategyServiceImpl implements IPatrolLinkageStrategyService {
    private final PatrolLinkageStrategyMapper patrolLinkageStrategyMapper;
    private final PatrolLinkageStrategyRefMapper patrolLinkageStrategyRefMapper;
    private final PatrolLinkageStrategyReverseMapper patrolLinkageStrategyReverseMapper;

    public PatrolLinkageStrategyServiceImpl(PatrolLinkageStrategyMapper patrolLinkageStrategyMapper, PatrolLinkageStrategyRefMapper patrolLinkageStrategyRefMapper, PatrolLinkageStrategyReverseMapper patrolLinkageStrategyReverseMapper) {
        this.patrolLinkageStrategyMapper = patrolLinkageStrategyMapper;
        this.patrolLinkageStrategyRefMapper = patrolLinkageStrategyRefMapper;
        this.patrolLinkageStrategyReverseMapper = patrolLinkageStrategyReverseMapper;
    }

    public PatrolLinkageStrategy selectPatrolLinkageStrategyByLinkageStrategyId(Long linkageStrategyId) {
        PatrolLinkageStrategy patrolLinkageStrategy = this.patrolLinkageStrategyMapper.selectPatrolLinkageStrategyByLinkageStrategyId(linkageStrategyId);
        PatrolLinkageStrategyRef patrolLinkageStrategyRef = new PatrolLinkageStrategyRef();
        patrolLinkageStrategyRef.setLinkageStrategyId(linkageStrategyId);
        List<PatrolLinkageStrategyRef> patrolLinkageStrategyRefs = this.patrolLinkageStrategyRefMapper.selectPatrolLinkageStrategyRefList(patrolLinkageStrategyRef);
        PatrolLinkageStrategyReverse patrolLinkageStrategyReverse = new PatrolLinkageStrategyReverse();
        patrolLinkageStrategyReverse.setLinkageStrategyId(linkageStrategyId);
        List<PatrolLinkageStrategyReverse> patrolLinkageStrategyReverses = this.patrolLinkageStrategyReverseMapper.selectPatrolLinkageStrategyReverseList(patrolLinkageStrategyReverse);
        patrolLinkageStrategy.setPatrolLinkageStrategyRefs(patrolLinkageStrategyRefs);
        patrolLinkageStrategy.setPatrolLinkageStrategyReverses(patrolLinkageStrategyReverses);
        return patrolLinkageStrategy;
    }

    public List<PatrolLinkageStrategy> selectPatrolLinkageStrategyList(PatrolLinkageStrategy patrolLinkageStrategy) {
        return this.patrolLinkageStrategyMapper.selectPatrolLinkageStrategyList(patrolLinkageStrategy);
    }

    @Transactional
    public int insertPatrolLinkageStrategy(PatrolLinkageStrategy patrolLinkageStrategy) {
        patrolLinkageStrategy.setCreateTime(DateUtils.getNowDate());
        int i = this.patrolLinkageStrategyMapper.insertPatrolLinkageStrategy(patrolLinkageStrategy);
        this.addRefs(patrolLinkageStrategy);
        return i;
    }

    private void addRefs(PatrolLinkageStrategy patrolLinkageStrategy) {
        this.patrolLinkageStrategyRefMapper.deletePatrolLinkageStrategyRefByLinkageStrategyRefId(patrolLinkageStrategy.getLinkageStrategyId());
        this.patrolLinkageStrategyReverseMapper.deletePatrolLinkageStrategyReverseByLinkageStrategyReverseId(patrolLinkageStrategy.getLinkageStrategyId());
        List<PatrolLinkageStrategyRef> patrolLinkageStrategyRefs = patrolLinkageStrategy.getPatrolLinkageStrategyRefs();
        if (patrolLinkageStrategyRefs != null && !patrolLinkageStrategyRefs.isEmpty()) {
            patrolLinkageStrategyRefs.forEach((item) -> {
                item.setLinkageStrategyId(patrolLinkageStrategy.getLinkageStrategyId());
            });
            this.patrolLinkageStrategyRefMapper.insertPatrolLinkageStrategyRefs(patrolLinkageStrategyRefs);
        }

        if ("3".equals(patrolLinkageStrategy.getTriggerSignalType())) {
            List<PatrolLinkageStrategyReverse> patrolLinkageStrategyReverses = patrolLinkageStrategy.getPatrolLinkageStrategyReverses();
            if (patrolLinkageStrategyReverses != null && !patrolLinkageStrategyReverses.isEmpty()) {
                patrolLinkageStrategyReverses.forEach((item) -> {
                    item.setLinkageStrategyId(patrolLinkageStrategy.getLinkageStrategyId());
                });
                this.patrolLinkageStrategyReverseMapper.insertPatrolLinkageStrategyReverses(patrolLinkageStrategyReverses);
            }
        }

    }

    public int updatePatrolLinkageStrategy(PatrolLinkageStrategy patrolLinkageStrategy) {
        this.addRefs(patrolLinkageStrategy);
        return this.patrolLinkageStrategyMapper.updatePatrolLinkageStrategy(patrolLinkageStrategy);
    }

    public int deletePatrolLinkageStrategyByLinkageStrategyIds(Long[] linkageStrategyIds) {

        for (Long linkageStrategyId : linkageStrategyIds) {
            this.patrolLinkageStrategyReverseMapper.deletePatrolLinkageStrategyReverseByLinkageStrategyReverseId(linkageStrategyId);
            this.patrolLinkageStrategyRefMapper.deletePatrolLinkageStrategyRefByLinkageStrategyRefId(linkageStrategyId);
        }

        return this.patrolLinkageStrategyMapper.deletePatrolLinkageStrategyByLinkageStrategyIds(linkageStrategyIds);
    }

    public int deletePatrolLinkageStrategyByLinkageStrategyId(Long linkageStrategyId) {
        return this.patrolLinkageStrategyMapper.deletePatrolLinkageStrategyByLinkageStrategyId(linkageStrategyId);
    }
}
