package com.pwt.web.service.impl;

import com.pwt.common.exception.ServiceException;
import com.pwt.common.utils.StringUtils;
import com.pwt.web.domain.PwtBattery;
import com.pwt.web.mapper.PwtBatteryMapper;
import com.pwt.web.service.IPwtBatteryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 电池管理Service业务层处理
 * 
 * @author linyun
 * @date 2022-11-17
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class PwtBatteryServiceImpl implements IPwtBatteryService 
{
    @Autowired
    private PwtBatteryMapper pwtBatteryMapper;

    /**
     * 查询电池管理
     * 
     * @param batteryId 电池管理ID
     * @return 电池管理
     */
    @Override
    public PwtBattery selectPwtBatteryById(Long batteryId)
    {
        return pwtBatteryMapper.selectPwtBatteryById(batteryId);
    }

    /**
     * 查询电池管理列表
     * 
     * @param pwtBattery 电池管理
     * @return 电池管理
     */
    @Override
    public List<PwtBattery> selectPwtBatteryList(PwtBattery pwtBattery)
    {
        return pwtBatteryMapper.selectPwtBatteryList(pwtBattery);
    }

    /**
     * 新增电池管理
     * 
     * @param pwtBattery 电池管理
     * @return 结果
     */
    @Override
    public int insertPwtBattery(PwtBattery pwtBattery)
    {
        checkVinUnique(pwtBattery.getVin());
        return pwtBatteryMapper.insertPwtBattery(pwtBattery);
    }

    private void checkVinUnique(String vin)
    {
        int row = pwtBatteryMapper.checkVinUnique(vin);
        if (row > 0)
        {
            throw new ServiceException("vin编码已存在,添加失败");
        }
    }

    /**
     * 修改电池管理
     * 
     * @param pwtBattery 电池管理
     * @return 结果
     */
    @Override
    public int updatePwtBattery(PwtBattery pwtBattery)
    {
        PwtBattery battery = pwtBatteryMapper.selectPwtBatteryById(pwtBattery.getBatteryId());
        if (!StringUtils.isAnyBlank(pwtBattery.getVin(), battery.getVin()) && !pwtBattery.getVin().equals(battery.getVin()))
        {
            checkVinUnique(pwtBattery.getVin());
        }
        return pwtBatteryMapper.updatePwtBattery(pwtBattery);
    }

    /**
     * 批量删除电池管理
     * 
     * @param batteryIds 需要删除的电池管理ID
     * @return 结果
     */
    @Override
    public int deletePwtBatteryByIds(Long[] batteryIds)
    {
        return pwtBatteryMapper.deletePwtBatteryByIds(batteryIds);
    }

    /**
     * 删除电池管理信息
     * 
     * @param batteryId 电池管理ID
     * @return 结果
     */
    @Override
    public int deletePwtBatteryById(Long batteryId)
    {
        return pwtBatteryMapper.deletePwtBatteryById(batteryId);
    }



    /**
     * @Description: 导入电池数据
     * @Author: linyun
     * @Date: 2022/11/19 14:21
     * @Param:[list 导入的电池数据对象]
     * @Return:java.lang.String
     */
    @Override
    public String importData(List<PwtBattery> list)
    {
        if (CollectionUtils.isEmpty(list))
        {
            throw new ServiceException("导入的电池数据不能为空!");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (PwtBattery battery : list)
        {
            try
            {
                PwtBattery pwtBattery = pwtBatteryMapper.getByVinCode(battery.getVin());
                if (StringUtils.isNull(pwtBattery))
                {
                    pwtBatteryMapper.insertPwtBattery(battery);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、电池 " + battery.getVin() + " 导入成功");
                } else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、电池 " + battery.getVin() + " 已存在");
                }
            } catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、电池 " + battery.getVin() + " 导入失败：";
                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();
    }
}
