package com.ozs.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ozs.common.exception.ServiceException;
import com.ozs.common.utils.bean.BeanValidators;
import com.ozs.service.entity.BaseLineManagement;
import com.ozs.service.mapper.BaseLineManagementMapper;
import com.ozs.service.service.BaseLineManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ozs
 * @since 2023-02-17
 */
@Service
public class BaseLineManagementServiceImpl extends ServiceImpl<BaseLineManagementMapper, BaseLineManagement> implements BaseLineManagementService {
   
    @Resource
    BaseLineManagementMapper baseLineManagementMapper;
    @Autowired
    protected Validator validator;

    /**
     * 导出线路台账
     * @param accountManageList 线路台账列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return
     */
    @Override
    public String importBaseLineManagement(List<BaseLineManagement> accountManageList, boolean updateSupport) {
        if (com.ozs.common.utils.StringUtils.isNull(accountManageList) || accountManageList.size() == 0) {
            throw new ServiceException("导入线路台账信息不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BaseLineManagement lineManagement : accountManageList) {
            try {
                // 判断相机编码是否存在
                LambdaQueryWrapper<BaseLineManagement> lw = new LambdaQueryWrapper<BaseLineManagement>();
                if (!ObjectUtils.isEmpty(lineManagement.getLineCode())) {
                    lw.eq(BaseLineManagement::getLineCode, lineManagement.getLineCode());
                }
                BaseLineManagement baseLineManagement = baseLineManagementMapper.selectOne(lw);
                if (!ObjectUtils.isEmpty(baseLineManagement)) {
                    BeanValidators.validateWithException(validator, lineManagement);
                    baseLineManagementMapper.insert(lineManagement);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、线路编码 " + lineManagement.getLineCode() + " 导入成功");
                } else if (updateSupport) {
                    BeanValidators.validateWithException(validator, lineManagement);
                    baseLineManagementMapper.update(lineManagement,lw);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、线路编码 " + lineManagement.getLineCode()  + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、线路编码 " + lineManagement.getLineCode() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、线路编码 " + lineManagement.getLineCode() + " 导入失败：";
                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();
    }
}
