package com.hw.dms.service.impl;

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

/**
 * 设备BOM信息Service业务层处理
 *
 * @author xins
 * @date 2023-12-28
 */
@Service
public class DmsBaseDeviceBomServiceImpl implements IDmsBaseDeviceBomService
{
    @Autowired
    private DmsBaseDeviceBomMapper dmsBaseDeviceBomMapper;

    /**
     * 查询设备BOM信息
     *
     * @param deviceBomId 设备BOM信息主键
     * @return 设备BOM信息
     */
    @Override
    public DmsBaseDeviceBom selectDmsBaseDeviceBomByDeviceBomId(Long deviceBomId)
    {
        return dmsBaseDeviceBomMapper.selectDmsBaseDeviceBomByDeviceBomId(deviceBomId);
    }

    /**
     * 查询设备BOM信息列表
     *
     * @param dmsBaseDeviceBom 设备BOM信息
     * @return 设备BOM信息
     */
    @Override
    public List<DmsBaseDeviceBom> selectDmsBaseDeviceBomList(DmsBaseDeviceBom dmsBaseDeviceBom)
    {
        return dmsBaseDeviceBomMapper.selectDmsBaseDeviceBomList(dmsBaseDeviceBom);
    }

    /**
     * 新增设备BOM信息
     *
     * @param dmsBaseDeviceBom 设备BOM信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseDeviceBom(DmsBaseDeviceBom dmsBaseDeviceBom)
    {
        LoginUser loginUser = new LoginUser();
        //添加标识
        dmsBaseDeviceBom.setIsFlag(1l);
        dmsBaseDeviceBom.setCreateBy(loginUser.getUsername());
        dmsBaseDeviceBom.setCreateTime(DateUtils.getNowDate());
        //如果有父级 则查询父级祖籍列表 +父级编号
        if (dmsBaseDeviceBom.getParentId()==0){
            dmsBaseDeviceBom.setAncestors("0");
        }
        else{
            DmsBaseDeviceBom dmsBaseDeviceBom1 = new DmsBaseDeviceBom();
            dmsBaseDeviceBom1.setDeviceBomId(dmsBaseDeviceBom.getParentId());
            String ancestors = dmsBaseDeviceBomMapper.selectDmsBaseDeviceBomList(dmsBaseDeviceBom1).get(0).getAncestors();
            dmsBaseDeviceBom.setAncestors(ancestors+","+dmsBaseDeviceBom.getParentId());
        }
        return dmsBaseDeviceBomMapper.insertDmsBaseDeviceBom(dmsBaseDeviceBom);
    }

    /**
     * 修改设备BOM信息
     *
     * @param dmsBaseDeviceBom 设备BOM信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseDeviceBom(DmsBaseDeviceBom dmsBaseDeviceBom)
    {
        //如果有父级 则查询父级祖籍列表 +父级编号
        if (dmsBaseDeviceBom.getParentId()==0){
            dmsBaseDeviceBom.setAncestors("0");
        }
        else{
            DmsBaseDeviceBom dmsBaseDeviceBom1 = new DmsBaseDeviceBom();
            dmsBaseDeviceBom1.setDeviceBomId(dmsBaseDeviceBom.getParentId());
            String ancestors = dmsBaseDeviceBomMapper.selectDmsBaseDeviceBomList(dmsBaseDeviceBom1).get(0).getAncestors();
            dmsBaseDeviceBom.setAncestors(ancestors+","+dmsBaseDeviceBom.getParentId());
        }
        dmsBaseDeviceBom.setUpdateTime(DateUtils.getNowDate());
        return dmsBaseDeviceBomMapper.updateDmsBaseDeviceBom(dmsBaseDeviceBom);
    }

    /**
     * 批量删除设备BOM信息
     *
     * @param deviceBomIds 需要删除的设备BOM信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseDeviceBomByDeviceBomIds(Long[] deviceBomIds)
    {
        return dmsBaseDeviceBomMapper.deleteDmsBaseDeviceBomByDeviceBomIds(deviceBomIds);
    }

    /**
     * 删除设备BOM信息信息
     *
     * @param deviceBomId 设备BOM信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseDeviceBomByDeviceBomId(Long deviceBomId)
    {
        return dmsBaseDeviceBomMapper.deleteDmsBaseDeviceBomByDeviceBomId(deviceBomId);
    }

    @Override
    public List<DmsBaseDeviceBom> selectBomByDeviceType(String deviceType) {
        return dmsBaseDeviceBomMapper.selectBomByDeviceType(deviceType);
    }


}
