package com.djhk.uav.uav.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.djhk.common.utils.DateUtils;
import com.djhk.uav.uav.mapper.UavBatteryMapper;
import com.djhk.uav.uav.model.UavBattery;
import com.djhk.uav.uav.service.IUavBatteryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 运力Service业务层处理
 *
 * @author jiachengshuai
 * @date 2023-02-21
 */
@Service
public class UavBatteryServiceImpl implements IUavBatteryService
{
    @Autowired
    private UavBatteryMapper uavBatteryMapper;

    /**
     * 查询运力
     *
     * @param id 运力主键
     * @return 运力
     */
    @Override
    public UavBattery selectUavBatteryById(Long id)
    {
        return uavBatteryMapper.selectUavBatteryById(id);
    }

    /**
     * 查询运力列表
     *
     * @param uavBattery 运力
     * @return 运力
     */
    @Override
    public List<UavBattery> selectUavBatteryList(UavBattery uavBattery)
    {
        return uavBatteryMapper.selectUavBatteryList(uavBattery);
    }

    /**
     * 新增运力
     *
     * @param uavBattery 运力
     * @return 结果
     */
    @Override
    public int insertUavBattery(UavBattery uavBattery)
    {
        uavBattery.setCreateTime(DateUtils.getNowDate());
        return uavBatteryMapper.insertUavBattery(uavBattery);
    }

    /**
     * 修改运力
     *
     * @param uavBattery 运力
     * @return 结果
     */
    @Override
    public int updateUavBattery(UavBattery uavBattery)
    {
        uavBattery.setUpdateTime(DateUtils.getNowDate());
        return uavBatteryMapper.updateUavBattery(uavBattery);
    }

    /**
     * 批量删除运力
     *
     * @param ids 需要删除的运力主键
     * @return 结果
     */
    @Override
    public int deleteUavBatteryByIds(Long[] ids)
    {
        AtomicInteger result = new AtomicInteger();
        List<Long> longList = Arrays.asList(ids);
        longList.forEach(id -> {
            result.addAndGet(uavBatteryMapper.update(UavBattery.builder()
                    .delFlag("2")
                    .deleteTime(DateUtils.getNowDate())
                    .build()
                    ,new LambdaQueryWrapper<UavBattery>().eq(UavBattery::getId,id)));
        });
        return result.get();
    }

    /**
     * 删除运力信息
     *
     * @param id 运力主键
     * @return 结果
     */
    @Override
    public int deleteUavBatteryById(Long id)
    {
        return uavBatteryMapper.deleteUavBatteryById(id);
    }
}
