package com.hw.dms.service.impl;

import java.util.List;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.dms.domain.DmsBaseDeviceBom;
import com.hw.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsBaseDeviceTypeMapper;
import com.hw.dms.domain.DmsBaseDeviceType;
import com.hw.dms.service.IDmsBaseDeviceTypeService;

/**
 * 设备类型信息Service业务层处理
 *
 * @author xins
 * @date 2023-12-27
 */
@Service
public class DmsBaseDeviceTypeServiceImpl implements IDmsBaseDeviceTypeService
{
    @Autowired
    private DmsBaseDeviceTypeMapper dmsBaseDeviceTypeMapper;

    /**
     * 查询设备类型信息
     *
     * @param deviceTypeId 设备类型信息主键
     * @return 设备类型信息
     */
    @Override
    public DmsBaseDeviceType selectDmsBaseDeviceTypeByDeviceTypeId(Long deviceTypeId)
    {
        return dmsBaseDeviceTypeMapper.selectDmsBaseDeviceTypeByDeviceTypeId(deviceTypeId);
    }

    /**
     * 查询设备类型信息列表
     *
     * @param dmsBaseDeviceType 设备类型信息
     * @return 设备类型信息
     */
    @Override
    public List<DmsBaseDeviceType> selectDmsBaseDeviceTypeList(DmsBaseDeviceType dmsBaseDeviceType)
    {
        return dmsBaseDeviceTypeMapper.selectDmsBaseDeviceTypeList(dmsBaseDeviceType);
    }

    /**
     * 新增设备类型信息
     *
     * @param dmsBaseDeviceType 设备类型信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseDeviceType(DmsBaseDeviceType dmsBaseDeviceType)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBaseDeviceType.setCreateBy(user.getUsername());
        dmsBaseDeviceType.setIsFlag(1l);
        dmsBaseDeviceType.setCreateTime(DateUtils.getNowDate());
        //如果有父级 则查询父级祖籍列表 +父级编号
        if (dmsBaseDeviceType.getParentId()==0){
            dmsBaseDeviceType.setAncestors("0");
        }
        else{
            DmsBaseDeviceType dmsBaseDeviceType1 = new DmsBaseDeviceType();
            dmsBaseDeviceType1.setDeviceTypeId(dmsBaseDeviceType.getParentId());
            String ancestors = dmsBaseDeviceTypeMapper.selectDmsBaseDeviceTypeList(dmsBaseDeviceType1).get(0).getAncestors();
            dmsBaseDeviceType.setAncestors(ancestors+","+dmsBaseDeviceType.getParentId());
        }
        return dmsBaseDeviceTypeMapper.insertDmsBaseDeviceType(dmsBaseDeviceType);
    }

    /**
     * 修改设备类型信息
     *
     * @param dmsBaseDeviceType 设备类型信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseDeviceType(DmsBaseDeviceType dmsBaseDeviceType)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBaseDeviceType.setUpdateBy(user.getUsername());
        dmsBaseDeviceType.setUpdateTime(DateUtils.getNowDate());
        //如果有父级 则查询父级祖籍列表 +父级编号
        if (dmsBaseDeviceType.getParentId()==0){
            dmsBaseDeviceType.setAncestors("0");
        }
        else{
            DmsBaseDeviceType dmsBaseDeviceType1 = new DmsBaseDeviceType();
            dmsBaseDeviceType1.setParentId(dmsBaseDeviceType.getParentId());
            String ancestors = dmsBaseDeviceTypeMapper.selectDmsBaseDeviceTypeList(dmsBaseDeviceType1).get(0).getAncestors();
            dmsBaseDeviceType.setAncestors(ancestors+","+dmsBaseDeviceType.getParentId());
        }
        return dmsBaseDeviceTypeMapper.updateDmsBaseDeviceType(dmsBaseDeviceType);
    }

    /**
     * 批量删除设备类型信息
     *
     * @param deviceTypeIds 需要删除的设备类型信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseDeviceTypeByDeviceTypeIds(Long[] deviceTypeIds)
    {
        return dmsBaseDeviceTypeMapper.deleteDmsBaseDeviceTypeByDeviceTypeIds(deviceTypeIds);
    }

    /**
     * 删除设备类型信息信息
     *
     * @param deviceTypeId 设备类型信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseDeviceTypeByDeviceTypeId(Long deviceTypeId)
    {
        return dmsBaseDeviceTypeMapper.deleteDmsBaseDeviceTypeByDeviceTypeId(deviceTypeId);
    }

    @Override
    public List<DmsBaseDeviceType> selectDevicetypeByDeviceId(Long deviceId) {
        return dmsBaseDeviceTypeMapper.selectDevicetypeByDeviceId(deviceId);
    }
}
