package com.jkcredit.invoice.hub.service.carFreeCarrierBillStart;

import com.jkcredit.invoice.hub.mapper.CarFreeCarrierBillStartMapper;
import com.jkcredit.invoice.hub.model.dto.carFreeCarrierBillStart.CarFreeCarrierBillStartDto;
import com.jkcredit.invoice.hub.model.po.carFreeCarrierBillStart.CarFreeCarrierBillStartPo;
import com.jkcredit.invoice.hub.service.base.BaseService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/16 23:24
 * @version: V1.0
 **/
@Slf4j
@Service
public class CarFreeCarrierBillStartServiceImpl extends BaseService implements CarFreeCarrierBillStartService {
    @Resource
    CarFreeCarrierBillStartMapper mapper;
    @Override
    public void saveBillStart(CarFreeCarrierBillStartDto dto) {
        try {
            validate(dto);
            CarFreeCarrierBillStartPo po = new CarFreeCarrierBillStartPo();
            BeanUtil.copyProperties(po, dto);
            po.setCreateTime(new Date());
            mapper.insert(po);
        } catch (Exception e) {
            log.error("新增运单开始指令数据失败，失败原因：{}", e.getMessage());
            throw new ServiceException("新增运单开始指令数据失败");
        }
    }

    @Override
    public void updateBillStartStatus(String num, Integer status) {
        CarFreeCarrierBillStartPo po;
        try {
            po = mapper.getBillStartByNum(num);
            if (po == null) {
                log.error("运单开始指令不存在,运单编号为：{}", num);
                throw new ServiceException(num + "运单开始指令不存在");
            } else {
                po.setStatus(status);
                po.setUpdateTime(new Date());
                mapper.updateById(po);
            }
        } catch (Exception e) {
            log.error("运单开始指令更新状态失败,运单编号为：{},状态为：{}", num, status);
            throw new ServiceException(num + "运单开始指令更新状态失败");
        }
    }

    @Override
    public List<CarFreeCarrierBillStartPo> getRealTimeBills() {
        try {
            List<CarFreeCarrierBillStartPo> billStartPoList = mapper.getRealTimeBills();
            return billStartPoList;
        } catch (Exception e) {
            log.error("实时运单查询失败,失败原因：{}", e.getMessage());
            throw new ServiceException("实时运单查询失败");
        }
    }

    @Override
    public List<CarFreeCarrierBillStartPo> getHistoryBills() {
        try {
            List<CarFreeCarrierBillStartPo> billStartPoList = mapper.getHistoryBills();
            return billStartPoList;
        } catch (Exception e) {
            log.error("历史运单查询失败,失败原因：{}", e.getMessage());
            throw new ServiceException("历史运单查询失败");
        }
    }
}
