package com.inspect.patrol.service.impl;

import com.inspect.base.core.utils.DateUtils;
import com.inspect.patrol.domain.PatrolPresetAction;
import com.inspect.patrol.domain.PatrolPresetParam;
import com.inspect.patrol.domain.PatrolPresetPos;
import com.inspect.patrol.domain.PatrolPresetPosSlim;
import com.inspect.patrol.mapper.PatrolPresetActionMapper;
import com.inspect.patrol.mapper.PatrolPresetParamMapper;
import com.inspect.patrol.mapper.PatrolPresetPosMapper;
import com.inspect.patrol.service.IPatrolPresetPosService;
import com.inspect.patrol.vo.CameraVo;
import io.seata.spring.annotation.GlobalTransactional;

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 PatrolPresetPosServiceImpl implements IPatrolPresetPosService {
    private final PatrolPresetPosMapper patrolPresetPosMapper;
    private final PatrolPresetParamMapper patrolPresetParamMapper;
    private final PatrolPresetActionMapper patrolPresetActionMapper;

    public PatrolPresetPosServiceImpl(PatrolPresetPosMapper patrolPresetPosMapper, PatrolPresetParamMapper patrolPresetParamMapper, PatrolPresetActionMapper patrolPresetActionMapper) {
        this.patrolPresetPosMapper = patrolPresetPosMapper;
        this.patrolPresetParamMapper = patrolPresetParamMapper;
        this.patrolPresetActionMapper = patrolPresetActionMapper;
    }

    public List<PatrolPresetPos> selectListByDeviceId(String deviceId) {
        return this.patrolPresetPosMapper.selectListByDeviceId(deviceId);
    }

    public List<PatrolPresetPos> selectListByDeviceIds(List<String> deviceIds) {
        return this.patrolPresetPosMapper.selectListByDeviceIds(deviceIds);
    }

    public PatrolPresetPos selectPatrolPresetPosByPresetPosId(String presetPosId) {
        return this.patrolPresetPosMapper.selectPatrolPresetPosByPresetPosId(presetPosId);
    }

    public void removeBasePresetByPresetPosId(Long presetPosId) {
        this.patrolPresetPosMapper.deleteBasePresetByPresetPosId(presetPosId);
    }

    public List<PatrolPresetPos> selectPatrolPresetPosByPatrolPointId(String patrolpointId) {
        return this.patrolPresetPosMapper.selectPatrolPresetPosByPatrolPointId(patrolpointId);
    }

    public List<PatrolPresetPos> selectPatrolPresetPosList(PatrolPresetPos patrolPresetPos) {
        return this.patrolPresetPosMapper.selectPatrolPresetPosList(patrolPresetPos);
    }

    public List<CameraVo> selectCameraByChannelType(String channelType) {
        return this.patrolPresetPosMapper.selectCameraByChannelType(channelType);
    }

    public List<PatrolPresetPos> selectPresetCodeByChannelCode(PatrolPresetPos patrolPresetPos) {
        return this.patrolPresetPosMapper.selectPresetCodeByChannelCode(patrolPresetPos);
    }

    public PatrolPresetPosSlim selectPresetCodeSlimByPatrolPointId(PatrolPresetPosSlim patrolPresetPosSlim) {
        return this.patrolPresetPosMapper.selectPresetCodeSlimByPatrolPointId(patrolPresetPosSlim);
    }

    @Transactional
    public int insertPatrolPresetPos(PatrolPresetPos patrolPresetPos) {
        patrolPresetPos.setCreateTime(DateUtils.getNowDate());
        int count = this.patrolPresetPosMapper.insertPatrolPresetPos(patrolPresetPos);
        this.insertOther(patrolPresetPos);
        return count;
    }

    private void insertOther(PatrolPresetPos patrolPresetPos) {
        List<PatrolPresetAction> presetActionList = patrolPresetPos.getPresetActionList();
        if (!presetActionList.isEmpty()) {
            presetActionList.forEach((item) -> {
                item.setCreateTime(DateUtils.getNowDate());
                item.setPresetPosId(patrolPresetPos.getPresetPosId());
            });
            this.patrolPresetActionMapper.insertPatrolPresetActionList(presetActionList);
        }

        List<PatrolPresetParam> presetParamList = patrolPresetPos.getPresetParamList();
        if (!presetParamList.isEmpty()) {
            presetParamList.forEach((item) -> {
                item.setCreateTime(DateUtils.getNowDate());
                item.setPresetPosId(patrolPresetPos.getPresetPosId());
            });
            this.patrolPresetParamMapper.insertPatrolPresetParamList(presetParamList);
        }

    }

    public int updatePatrolPresetPos(PatrolPresetPos patrolPresetPos) {
        this.patrolPresetActionMapper.deletePatrolPresetActionByPatrolPresetPosId(patrolPresetPos.getPresetPosId());
        this.patrolPresetParamMapper.deletePatrolPresetParamByPatrolPresetPosId(patrolPresetPos.getPresetPosId());
        this.insertOther(patrolPresetPos);
        return this.patrolPresetPosMapper.updatePatrolPresetPos(patrolPresetPos);
    }

    @GlobalTransactional
    public int deletePatrolPresetPosByPresetPosId(Long[] presetPosIds) {
        this.patrolPresetActionMapper.deletePatrolPresetActionByPatrolPresetPosIds(presetPosIds);
        this.patrolPresetParamMapper.deletePatrolPresetParamByPresetPosIds(presetPosIds);
        this.patrolPresetPosMapper.deleteBasedataPresetByPresetPosIds(presetPosIds);
        return this.patrolPresetPosMapper.deletePatrolPresetPosByPresetPosIds(presetPosIds);
    }

    public List<PatrolPresetPos> selectPatrolPresetPosListByIds(Long[] presetPosIds) {
        return this.patrolPresetPosMapper.selectPatrolPresetPosListByIds(presetPosIds);
    }
}
