package com.uavps.system.service.impl;

import com.uavps.common.exception.ServiceException;
import com.uavps.common.utils.DateUtils;
import com.uavps.common.utils.StringUtils;
import com.uavps.common.utils.bean.BeanValidators;
import com.uavps.system.domain.UavpsUnmannedAerialVehicle;
import com.uavps.system.mapper.UavpsUnmannedAerialVehicleMapper;
import com.uavps.system.service.IUavpsUnmannedAerialVehicleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.Collections;
import java.util.List;

/**
 * 无人机信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-07
 */
@Service
public class UavpsUnmannedAerialVehicleServiceImpl implements IUavpsUnmannedAerialVehicleService {
    private static final Logger log = LoggerFactory.getLogger(UavpsUnmannedAerialVehicleServiceImpl.class);

    @Autowired
    private UavpsUnmannedAerialVehicleMapper uavpsUnmannedAerialVehicleMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询无人机信息
     *
     * @param id 无人机信息主键
     * @return 无人机信息
     */
    @Override
    public UavpsUnmannedAerialVehicle selectUavpsUnmannedAerialVehicleById(Long id) {
        return uavpsUnmannedAerialVehicleMapper.selectUavpsUnmannedAerialVehicleById(id);
    }

    /**
     * 查询无人机信息列表
     *
     * @param uavpsUnmannedAerialVehicle 无人机信息
     * @return 无人机信息
     */
    @Override
    public List<UavpsUnmannedAerialVehicle> selectUavpsUnmannedAerialVehicleList(UavpsUnmannedAerialVehicle uavpsUnmannedAerialVehicle) {
        return uavpsUnmannedAerialVehicleMapper.selectUavpsUnmannedAerialVehicleList(uavpsUnmannedAerialVehicle);
    }

    /**
     * 新增无人机信息
     *
     * @param uavpsUnmannedAerialVehicle 无人机信息
     * @return 结果
     */
    @Override
    public int insertUavpsUnmannedAerialVehicle(UavpsUnmannedAerialVehicle uavpsUnmannedAerialVehicle) {
        uavpsUnmannedAerialVehicle.setCreateTime(DateUtils.getNowDate());
        return uavpsUnmannedAerialVehicleMapper.insertUavpsUnmannedAerialVehicle(uavpsUnmannedAerialVehicle);
    }

    /**
     * 修改无人机信息
     *
     * @param uavpsUnmannedAerialVehicle 无人机信息
     * @return 结果
     */
    @Override
    public int updateUavpsUnmannedAerialVehicle(UavpsUnmannedAerialVehicle uavpsUnmannedAerialVehicle) {
        uavpsUnmannedAerialVehicle.setUpdateTime(DateUtils.getNowDate());
        return uavpsUnmannedAerialVehicleMapper.updateUavpsUnmannedAerialVehicle(uavpsUnmannedAerialVehicle);
    }

    /**
     * 批量删除无人机信息
     *
     * @param ids 需要删除的无人机信息主键
     * @return 结果
     */
    @Override
    public int deleteUavpsUnmannedAerialVehicleByIds(Long[] ids) {
        return uavpsUnmannedAerialVehicleMapper.deleteUavpsUnmannedAerialVehicleByIds(ids);
    }

    /**
     * 删除无人机信息信息
     *
     * @param id 无人机信息主键
     * @return 结果
     */
    @Override
    public int deleteUavpsUnmannedAerialVehicleById(Long id) {
        return uavpsUnmannedAerialVehicleMapper.deleteUavpsUnmannedAerialVehicleById(id);
    }

    /**
     * 导入无人机信息
     *
     * @param uavpsUnmannedAerialVehicleList        无人机信息数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importUavpsUav(List<UavpsUnmannedAerialVehicle> uavpsUnmannedAerialVehicleList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(uavpsUnmannedAerialVehicleList) || uavpsUnmannedAerialVehicleList.size() == 0) {
            throw new ServiceException("导入无人机信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (UavpsUnmannedAerialVehicle uav : uavpsUnmannedAerialVehicleList) {
            try {
                // 验证是否存在这个无人机信息
                UavpsUnmannedAerialVehicle u = uavpsUnmannedAerialVehicleMapper.selectByName(uav.getName());
                if (StringUtils.isNull(u)) {
                    BeanValidators.validateWithException(validator, uav);
                    uav.setCreateBy(operName);
                    uavpsUnmannedAerialVehicleMapper.insertUavpsUnmannedAerialVehicle(uav);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、名称 " + uav.getName() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, uav);
                    uav.setId(u.getId());
                    uav.setUpdateBy(operName);
                    uavpsUnmannedAerialVehicleMapper.updateUavpsUnmannedAerialVehicle(uav);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、名称 " + uav.getName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、名称 " + uav.getName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、名称 " + uav.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<UavpsUnmannedAerialVehicle> selectUavpsUavAll() {
        return uavpsUnmannedAerialVehicleMapper.selectUavpsUavAll();
    }
}
