package com.fms.system.service.impl;

import java.util.List;
import com.fms.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.fms.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.fms.system.domain.AircraftType;
import com.fms.system.mapper.BrandMapper;
import com.fms.system.domain.Brand;
import com.fms.system.service.IBrandService;

/**
 * 机型系列Service业务层处理
 * 
 * @author Allen
 * @date 2024-05-14
 */
@Service
public class BrandServiceImpl implements IBrandService 
{
    @Autowired
    private BrandMapper brandMapper;

    /**
     * 查询机型系列
     * 
     * @param brandId 机型系列主键
     * @return 机型系列
     */
    @Override
    public Brand selectBrandByBrandId(Long brandId)
    {
        return brandMapper.selectBrandByBrandId(brandId);
    }

    /**
     * 查询机型系列列表
     * 
     * @param brand 机型系列
     * @return 机型系列
     */
    @Override
    public List<Brand> selectBrandList(Brand brand)
    {
        return brandMapper.selectBrandList(brand);
    }

    /**
     * 新增机型系列
     * 
     * @param brand 机型系列
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBrand(Brand brand)
    {
        brand.setCreateTime(DateUtils.getNowDate());
        int rows = brandMapper.insertBrand(brand);
        insertAircraftType(brand);
        return rows;
    }

    /**
     * 修改机型系列
     * 
     * @param brand 机型系列
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBrand(Brand brand)
    {
        brand.setUpdateTime(DateUtils.getNowDate());
        brandMapper.deleteAircraftTypeByBrandId(brand.getBrandId());
        insertAircraftType(brand);
        return brandMapper.updateBrand(brand);
    }

    /**
     * 批量删除机型系列
     * 
     * @param brandIds 需要删除的机型系列主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBrandByBrandIds(Long[] brandIds)
    {
        brandMapper.deleteAircraftTypeByBrandIds(brandIds);
        return brandMapper.deleteBrandByBrandIds(brandIds);
    }

    /**
     * 删除机型系列信息
     * 
     * @param brandId 机型系列主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBrandByBrandId(Long brandId)
    {
        brandMapper.deleteAircraftTypeByBrandId(brandId);
        return brandMapper.deleteBrandByBrandId(brandId);
    }

    /**
     * 新增机型信息
     * 
     * @param brand 机型系列对象
     */
    public void insertAircraftType(Brand brand)
    {
        List<AircraftType> aircraftTypeList = brand.getAircraftTypeList();
        Long brandId = brand.getBrandId();
        if (StringUtils.isNotNull(aircraftTypeList))
        {
            List<AircraftType> list = new ArrayList<AircraftType>();
            for (AircraftType aircraftType : aircraftTypeList)
            {
                aircraftType.setBrandId(brandId);
                list.add(aircraftType);
            }
            if (list.size() > 0)
            {
                brandMapper.batchAircraftType(list);
            }
        }
    }
}
