package com.pwt.web.service.impl;

import com.pwt.common.exception.ServiceException;
import com.pwt.common.utils.DateUtils;
import com.pwt.common.utils.StringUtils;
import com.pwt.web.domain.PwtBattery;
import com.pwt.web.domain.PwtVehicle;
import com.pwt.web.mapper.PwtVehicleMapper;
import com.pwt.web.service.IPwtVehicleService;
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 PwtVehicleServiceImpl implements IPwtVehicleService 
{
    @Autowired
    private PwtVehicleMapper pwtVehicleMapper;

    /**
     * 查询车辆管理
     * 
     * @param vehicleId 车辆管理ID
     * @return 车辆管理
     */
    @Override
    public PwtVehicle selectPwtVehicleById(Long vehicleId)
    {
        return pwtVehicleMapper.selectPwtVehicleById(vehicleId);
    }

    /**
     * 查询车辆管理列表
     * 
     * @param pwtVehicle 车辆管理
     * @return 车辆管理
     */
    @Override
    public List<PwtVehicle> selectPwtVehicleList(PwtVehicle pwtVehicle)
    {
        return pwtVehicleMapper.selectPwtVehicleList(pwtVehicle);
    }

    /**
     * 新增车辆管理
     * 
     * @param pwtVehicle 车辆管理
     * @return 结果
     */
    @Override
    public int insertPwtVehicle(PwtVehicle pwtVehicle)
    {
        checkVehicleNoUnique(pwtVehicle.getVehicleNo());
        checkVehicleVinUnique(pwtVehicle.getVin());
        checkVehicleRfidUnique(pwtVehicle.getRfid());
        pwtVehicle.setCreateTime(DateUtils.getNowDate());
        return pwtVehicleMapper.insertPwtVehicle(pwtVehicle);
    }

    /**
     * 修改车辆管理
     * 
     * @param pwtVehicle 车辆管理
     * @return 结果
     */
    @Override
    public int updatePwtVehicle(PwtVehicle pwtVehicle)
    {
        PwtVehicle vehicle = pwtVehicleMapper.selectPwtVehicleById(pwtVehicle.getVehicleId());
        if (!StringUtils.isAnyBlank(pwtVehicle.getVehicleNo(), vehicle.getVehicleNo()) && !pwtVehicle.getVehicleNo().equals(vehicle.getVehicleNo()))
        {
            checkVehicleNoUnique(pwtVehicle.getVehicleNo());
        }
        if (!StringUtils.isAnyBlank(pwtVehicle.getVin(), vehicle.getVin()) && !pwtVehicle.getVin().equals(vehicle.getVin()))
        {
            checkVehicleVinUnique(pwtVehicle.getVin());
        }
        if (!StringUtils.isAnyBlank(pwtVehicle.getRfid(), vehicle.getRfid()) && !pwtVehicle.getRfid().equals(vehicle.getRfid()))
        {
            checkVehicleRfidUnique(pwtVehicle.getRfid());
        }
        return pwtVehicleMapper.updatePwtVehicle(pwtVehicle);
    }

    private void checkVehicleRfidUnique(String rfid)
    {
        int row = pwtVehicleMapper.checkVehicleRfidUnique(rfid);
        if (row > 0)
        {
            throw new ServiceException("rfid已存在,不允许操作");
        }
    }

    private void checkVehicleVinUnique(String vin)
    {
        int row = pwtVehicleMapper.checkVehicleVinUnique(vin);
        if (row > 0)
        {
            throw new ServiceException("车牌vin编码已存在,不允许操作");
        }
    }

    private void checkVehicleNoUnique(String vehicleNo)
    {
        int row = pwtVehicleMapper.checkVehicleNoUnique(vehicleNo);
        if (row > 0)
        {
            throw new ServiceException("车牌号已存在,不允许操作");
        }
    }

    /**
     * 批量删除车辆管理
     * 
     * @param vehicleIds 需要删除的车辆管理ID
     * @return 结果
     */
    @Override
    public int deletePwtVehicleByIds(Long[] vehicleIds)
    {
        return pwtVehicleMapper.deletePwtVehicleByIds(vehicleIds);
    }

    /**
     * 删除车辆管理信息
     * 
     * @param vehicleId 车辆管理ID
     * @return 结果
     */
    @Override
    public int deletePwtVehicleById(Long vehicleId)
    {
        return pwtVehicleMapper.deletePwtVehicleById(vehicleId);
    }

    /**
     * @Description: 导入车辆数据
     * @Author: linyun
     * @Date: 2022/11/19 15:05
     * @Param:[list 导入的数据]
     * @Return:java.lang.String
     */
    @Override
    public String importData(List<PwtVehicle> list)
    {
        if (CollectionUtils.isEmpty(list))
        {
            throw new ServiceException("导入的车辆数据不能为空!");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (PwtVehicle pwtVehicle : list)
        {
            try
            {
                PwtVehicle vehicle1 = pwtVehicleMapper.getByVehicleNo(pwtVehicle.getVehicleNo());
                PwtVehicle vehicle2 = pwtVehicleMapper.getByVinCode(pwtVehicle.getVin());
                PwtVehicle vehicle3 = pwtVehicleMapper.getByRfid(pwtVehicle.getRfid());
                if (StringUtils.isNull(vehicle1) && StringUtils.isNull(vehicle2) && StringUtils.isNull(vehicle3))
                {
                    pwtVehicleMapper.insertPwtVehicle(pwtVehicle);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、车辆 " + pwtVehicle.getVehicleNo() + " 导入成功");
                } else
                {
                    failureNum++;
                    String msg = "";
                    if (StringUtils.isNull(vehicle1))
                    {
                        msg += "车牌号 ";
                    } else if (StringUtils.isNull(vehicle2))
                    {
                        msg += "vin ";
                    } else if (StringUtils.isNull(vehicle3))
                    {
                        msg += "RFID ";
                    }
                    failureMsg.append("<br/>" + failureNum + "、车辆 " + msg + " 已存在");
                }
            } catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、车辆 " + pwtVehicle.getVehicleNo() + " 导入失败：";
                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();
    }

    /**
     * 根据rfid查询VIN码
     *
     * @param rfid
     * @return VIN码
     */
    @Override
    public PwtVehicle getVinByRfid(String rfid)
    {
        PwtVehicle vehicle = pwtVehicleMapper.getByRfid(rfid);
        if (vehicle != null && StringUtils.isNotBlank(vehicle.getVin()))
        {
            return vehicle;
        }
        return null;
    }

    /**
     * 验证vin是否存在
     *
     * @param vin vin码
     * @return true:存在 false:不存在
     */
    @Override
    public boolean checkVinExist(String vin)
    {
        int row = pwtVehicleMapper.checkVehicleVinUnique(vin);
        return row > 0;
    }

    @Override
    public int getCount() {
        return pwtVehicleMapper.getCount();
    }
}
