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

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.hub.mapper.CarFreeCarrierBillStartMapper;
import com.jkcredit.invoice.hub.model.dto.carFreeCarrierBillStart.BillRecordDto;
import com.jkcredit.invoice.hub.model.dto.carFreeCarrierBillStart.CarFreeCarrierBillStartDto;
import com.jkcredit.invoice.hub.model.po.carFreeCarrierBillStart.BillRecordPo;
import com.jkcredit.invoice.hub.model.po.carFreeCarrierBillStart.CarFreeCarrierBillStartPo;
import com.jkcredit.invoice.hub.model.vo.record.BillRecordVo;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @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("历史运单查询失败");
        }
    }

    @Override
    public CarFreeCarrierBillStartPo getBillStartByNum(String num) {
        try {
            CarFreeCarrierBillStartPo po = mapper.getBillStartByNum(num);
            return po;
        } catch (Exception e) {
            log.error("运单查询失败,失败原因：{}", e.getMessage());
            throw new ServiceException("运单查询失败");
        }
    }

    @Override
    public void saveBillStart(CarFreeCarrierBillStartPo po) {
        try {
            po.setCreateTime(new Date());
            mapper.insert(po);
        } catch (Exception e) {
            log.error("新增运单开始指令数据失败，失败原因：{}", e.getMessage());
            throw new ServiceException("新增运单开始指令数据失败");
        }
    }

    @Override
    public void updateBillStartIsSearch(String num) {
        try {
            CarFreeCarrierBillStartPo po = mapper.getBillStartByNum(num);
            po.setIsSearch(2);
            po.setUpdateTime(new Date());
            mapper.updateById(po);
        } catch (Exception e) {
            log.error("更新运单查询标记失败,失败原因：{}", e.getMessage());
            throw new ServiceException("更新运单查询标记失败");
        }
    }

    @Override
    public List<String> getNoSearchNums(String userId) {
        List<String> nums;
        try {
            nums = mapper.getNoSearchNums(userId);
        } catch (Exception e) {
            log.error("获取未查询过运单编号失败,失败原因：{}", e.getMessage());
            throw new ServiceException("获取未查询过运单编号失败");
        }
        return nums;
    }

    @Override
    public IPage<BillRecordVo> getBillRecord(Page page, BillRecordDto dto) {
        try {
            IPage<BillRecordPo> billRecords = mapper.getBillRecord(page, dto);
            return billRecords.convert(this::convert);
        } catch (Exception e) {
            log.error("获取运单详情记录失败,失败原因：{}", e.getMessage());
            throw new ServiceException("获取运单详情记录失败");
        }
    }

    private BillRecordVo convert(BillRecordPo po) {
        BillRecordVo vo = new BillRecordVo();
        BeanUtil.copyProperties(vo, po);
        // 将数据库存储的decimal转double
        return vo;
    }

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