package com.inspect.maintain.service.impl;

import com.inspect.base.core.domain.maintain.MaintainRegion;
import com.inspect.maintain.mapper.MaintainRegionMapper;
import com.inspect.maintain.service.IMaintainRegionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MaintainRegionServiceImpl implements IMaintainRegionService {
    private final MaintainRegionMapper maintainRegionMapper;

    public MaintainRegionServiceImpl(MaintainRegionMapper maintainRegionMapper) {
        this.maintainRegionMapper = maintainRegionMapper;
    }

    public MaintainRegion selectMaintainRegionByLineId(Long lineId) {
        return maintainRegionMapper.selectMaintainRegionByLineId(lineId);
    }

    public List<MaintainRegion> selectMaintainRegionList(MaintainRegion maintainRegion) {
        return this.maintainRegionMapper.selectMaintainRegionList(maintainRegion);
    }

    public int insertMaintainRegion(MaintainRegion maintainRegion) {
        maintainRegion.setCreateTime(new Date());
        return this.maintainRegionMapper.insertMaintainRegion(maintainRegion);
    }

    public int updateMaintainRegion(MaintainRegion maintainRegion) {
        return this.maintainRegionMapper.updateMaintainRegion(maintainRegion);
    }

    public int deleteMaintainRegionByLineIds(Long[] lineIds) {
        return this.maintainRegionMapper.deleteMaintainRegionByLineIds(lineIds);
    }

    public int deleteMaintainRegionByLineId(Long lineId) {
        return this.maintainRegionMapper.deleteMaintainRegionByLineId(lineId);
    }

    @Override
    public boolean checkConfigCodeUnique(String configCode) {
        MaintainRegion maintainRegion = this.maintainRegionMapper.checkConfigCodeUnique(configCode);
        if (maintainRegion == null) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertMaintainRegionWithConfigCode(MaintainRegion maintainRegion) {
        maintainRegion.setCreateTime(new Date());
        int rows = this.maintainRegionMapper.insertMaintainRegion(maintainRegion);

        if (rows > 0) {
            Long lineId = maintainRegion.getLineId();
            maintainRegion.setConfigCode(String.valueOf(lineId));
            maintainRegionMapper.updateMaintainRegion(maintainRegion);
        }

        return rows;
    }
}
