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.UavpsMultiTargetFormation;
import com.uavps.system.mapper.UavpsMultiTargetFormationMapper;
import com.uavps.system.service.IUavpsMultiTargetFormationService;
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.List;

/**
 * 多目标集群信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-07
 */
@Service
public class UavpsMultiTargetFormationServiceImpl implements IUavpsMultiTargetFormationService {
    private static final Logger log = LoggerFactory.getLogger(UavpsMultiTargetFormationServiceImpl.class);

    @Autowired
    private UavpsMultiTargetFormationMapper uavpsMultiTargetFormationMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询多目标集群信息
     *
     * @param id 多目标集群信息主键
     * @return 多目标集群信息
     */
    @Override
    public UavpsMultiTargetFormation selectUavpsMultiTargetFormationById(Long id) {
        return uavpsMultiTargetFormationMapper.selectUavpsMultiTargetFormationById(id);
    }

    /**
     * 查询多目标集群信息列表
     *
     * @param uavpsMultiTargetFormation 多目标集群信息
     * @return 多目标集群信息
     */
    @Override
    public List<UavpsMultiTargetFormation> selectUavpsMultiTargetFormationList(UavpsMultiTargetFormation uavpsMultiTargetFormation) {
        return uavpsMultiTargetFormationMapper.selectUavpsMultiTargetFormationList(uavpsMultiTargetFormation);
    }

    /**
     * 新增多目标集群信息
     *
     * @param uavpsMultiTargetFormation 多目标集群信息
     * @return 结果
     */
    @Override
    public int insertUavpsMultiTargetFormation(UavpsMultiTargetFormation uavpsMultiTargetFormation) {
        uavpsMultiTargetFormation.setCreateTime(DateUtils.getNowDate());
        return uavpsMultiTargetFormationMapper.insertUavpsMultiTargetFormation(uavpsMultiTargetFormation);
    }

    /**
     * 修改多目标集群信息
     *
     * @param uavpsMultiTargetFormation 多目标集群信息
     * @return 结果
     */
    @Override
    public int updateUavpsMultiTargetFormation(UavpsMultiTargetFormation uavpsMultiTargetFormation) {
        uavpsMultiTargetFormation.setUpdateTime(DateUtils.getNowDate());
        return uavpsMultiTargetFormationMapper.updateUavpsMultiTargetFormation(uavpsMultiTargetFormation);
    }

    /**
     * 批量删除多目标集群信息
     *
     * @param ids 需要删除的多目标集群信息主键
     * @return 结果
     */
    @Override
    public int deleteUavpsMultiTargetFormationByIds(Long[] ids) {
        return uavpsMultiTargetFormationMapper.deleteUavpsMultiTargetFormationByIds(ids);
    }

    /**
     * 删除多目标集群信息
     *
     * @param id 多目标集群信息主键
     * @return 结果
     */
    @Override
    public int deleteUavpsMultiTargetFormationById(Long id) {
        return uavpsMultiTargetFormationMapper.deleteUavpsMultiTargetFormationById(id);
    }

    /**
     * 导入多目标集群信息
     *
     * @param uavpsMultiTargetFormationList        多目标集群信息数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importUavpsMultiTargetFormation(List<UavpsMultiTargetFormation> uavpsMultiTargetFormationList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(uavpsMultiTargetFormationList) || uavpsMultiTargetFormationList.size() == 0) {
            throw new ServiceException("导入多目标集群信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (UavpsMultiTargetFormation formation : uavpsMultiTargetFormationList) {
            try {
                // 验证是否存在这个多目标集群信息
                UavpsMultiTargetFormation u = uavpsMultiTargetFormationMapper.selectByTargetId(formation.getTargetId());
                if (StringUtils.isNull(u)) {
                    BeanValidators.validateWithException(validator, formation);
                    formation.setCreateBy(operName);
                    uavpsMultiTargetFormationMapper.insertUavpsMultiTargetFormation(formation);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、目标ID " + formation.getTargetId() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, formation);
                    formation.setId(u.getId());
                    formation.setUpdateBy(operName);
                    uavpsMultiTargetFormationMapper.updateUavpsMultiTargetFormation(formation);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、目标ID " + formation.getTargetId() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、目标ID " + formation.getTargetId() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、目标ID " + formation.getTargetId() + " 导入失败：";
                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<UavpsMultiTargetFormation> selectUavpsMultiTargetFormationAll() {
        return uavpsMultiTargetFormationMapper.selectUavpsMultiTargetFormationAll();
    }
}
