package com.aucma.dms.service.impl;



import com.aucma.common.core.domain.model.LoginUser;
import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.dms.domain.DmsBaseInspectRoute;
import com.aucma.dms.mapper.DmsBaseInspectRouteMapper;
import com.aucma.dms.service.IDmsBaseInspectRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 巡检线路信息Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-16
 */
@Service
public class DmsBaseInspectRouteServiceImpl implements IDmsBaseInspectRouteService
{
    @Autowired
    private DmsBaseInspectRouteMapper dmsBaseInspectRouteMapper;

    /**
     * 查询巡检线路信息
     *
     * @param inspectRouteId 巡检线路信息主键
     * @return 巡检线路信息
     */
    @Override
    public DmsBaseInspectRoute selectDmsBaseInspectRouteByInspectRouteId(Long inspectRouteId)
    {
        return dmsBaseInspectRouteMapper.selectDmsBaseInspectRouteByInspectRouteId(inspectRouteId);
    }

    /**
     * 查询巡检线路信息列表
     *
     * @param dmsBaseInspectRoute 巡检线路信息
     * @return 巡检线路信息
     */
    @Override
    public List<DmsBaseInspectRoute> selectDmsBaseInspectRouteList(DmsBaseInspectRoute dmsBaseInspectRoute)
    {
        return dmsBaseInspectRouteMapper.selectDmsBaseInspectRouteList(dmsBaseInspectRoute);
    }

    /**
     * 新增巡检线路信息
     *
     * @param dmsBaseInspectRoute 巡检线路信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseInspectRoute(DmsBaseInspectRoute dmsBaseInspectRoute)
    {
        if (dmsBaseInspectRoute.getInspectType()==null){
            dmsBaseInspectRoute.setInspectType("1");
        }
        DmsBaseInspectRoute InspectRoute = new DmsBaseInspectRoute();
        InspectRoute.setRouteCode(dmsBaseInspectRoute.getRouteCode());
        List<DmsBaseInspectRoute> InspectRoutelist = dmsBaseInspectRouteMapper.
                selectDmsBaseInspectRouteList(InspectRoute);
        if (InspectRoutelist.size()>0)throw new ServiceException("该路线已存在");
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBaseInspectRoute.setCreateBy(user.getUserId());
        dmsBaseInspectRoute.setIsFlag("1");
        dmsBaseInspectRoute.setCreateTime(DateUtils.getNowDate());
        return dmsBaseInspectRouteMapper.insertDmsBaseInspectRoute(dmsBaseInspectRoute);
    }

    /**
     * 修改巡检线路信息
     *
     * @param dmsBaseInspectRoute 巡检线路信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseInspectRoute(DmsBaseInspectRoute dmsBaseInspectRoute)
    {
        DmsBaseInspectRoute InspectRoute = new DmsBaseInspectRoute();
        InspectRoute.setRouteCode(dmsBaseInspectRoute.getRouteCode());
        List<DmsBaseInspectRoute> InspectRoutelist = dmsBaseInspectRouteMapper.
                selectDmsBaseInspectRouteList(InspectRoute);
        if (InspectRoutelist.size()>0 && !dmsBaseInspectRoute.getInspectRouteId()
                .equals(InspectRoutelist.get(0).getInspectRouteId())){
            throw new ServiceException("该路线已存在");
        }
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBaseInspectRoute.setUpdateBy(user.getUserId());
        dmsBaseInspectRoute.setUpdateTime(DateUtils.getNowDate());
        return dmsBaseInspectRouteMapper.updateDmsBaseInspectRoute(dmsBaseInspectRoute);
    }

    /**
     * 批量删除巡检线路信息
     *
     * @param inspectRouteIds 需要删除的巡检线路信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseInspectRouteByInspectRouteIds(Long[] inspectRouteIds)
    {
        return dmsBaseInspectRouteMapper.deleteDmsBaseInspectRouteByInspectRouteIds(inspectRouteIds);
    }

    /**
     * 删除巡检线路信息信息
     *
     * @param inspectRouteId 巡检线路信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseInspectRouteByInspectRouteId(Long inspectRouteId)
    {
        return dmsBaseInspectRouteMapper.deleteDmsBaseInspectRouteByInspectRouteId(inspectRouteId);
    }
}
