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.BaseAccountManagement;
import com.ozs.service.mapper.BaseAccountManagementMapper;
import com.ozs.service.service.BaseAccountManagementService;
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 BaseAccountManagementServiceImpl extends ServiceImpl<BaseAccountManagementMapper, BaseAccountManagement> implements BaseAccountManagementService {
    @Autowired
    protected Validator validator;
    @Resource
    BaseAccountManagementMapper baseAccountManagementMapper;

    /**
     * 台账管理导入
     *
     * @param accountManageList 台账相机列表
     * @param updateSupport     是否更新支持，如果已存在，则进行更新数据
     * @return
     */
    @Override
    public String importBaseAccountManagement(List<BaseAccountManagement> 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 (BaseAccountManagement accountManagement : accountManageList) {
            try {
                // 判断相机编码是否存在
                LambdaQueryWrapper<BaseAccountManagement> lw = new LambdaQueryWrapper<BaseAccountManagement>();
                if (!ObjectUtils.isEmpty(accountManagement.getCameraCode())) {
                    lw.eq(BaseAccountManagement::getAccountManagementId, accountManagement.getCameraCode());
                }
                BaseAccountManagement baseAccountManagement = baseAccountManagementMapper.selectOne(lw);
                if (!ObjectUtils.isEmpty(baseAccountManagement)) {
                    BeanValidators.validateWithException(validator, accountManagement);
                    baseAccountManagementMapper.insert(accountManagement);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、相机编码 " + accountManagement.getCameraCode() + " 导入成功");
                } else if (updateSupport) {
                    BeanValidators.validateWithException(validator, accountManagement);
                    baseAccountManagementMapper.update(accountManagement, lw);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、相机编码 " + accountManagement.getCameraCode() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、相机编码 " + accountManagement.getCameraCode() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、相机编码 " + accountManagement.getCameraCode() + " 导入失败：";
                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();
    }
}
