package com.glxp.mipsdl.service.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glxp.mipsdl.constant.Constants;
import com.glxp.mipsdl.dao.inout.IoOrderDao;
import com.glxp.mipsdl.dao.inout.IoOrderUploadLogDao;
import com.glxp.mipsdl.entity.inout.IoOrderEntity;
import com.glxp.mipsdl.entity.inout.IoOrderUploadLogEntity;
import org.springframework.stereotype.Service;

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

@Service
public class OrderUploadLogService extends ServiceImpl<IoOrderUploadLogDao, IoOrderUploadLogEntity> {

    @Resource
    IoOrderUploadLogDao orderUploadLogDao;
    @Resource
    IoOrderDao orderDao;

    public void insertOrUpdate(IoOrderUploadLogEntity orderUploadLogEntity) {
        boolean b = orderUploadLogDao.exists(new QueryWrapper<IoOrderUploadLogEntity>().eq("billNo", orderUploadLogEntity.getBillNo()));
        if (b) {
            orderUploadLogDao.update(orderUploadLogEntity, new QueryWrapper<IoOrderUploadLogEntity>().eq("billNo", orderUploadLogEntity.getBillNo()));
        } else {
            orderUploadLogDao.insert(orderUploadLogEntity);
        }
    }


    /**
     * 本地提交错误
     *
     * @param orderEntity
     * @param errMsg
     */
    public void updateLocalErr(IoOrderEntity orderEntity, String errMsg) {
        IoOrderUploadLogEntity uploadLog = new IoOrderUploadLogEntity();
        uploadLog.setBillDate(orderEntity.getCreateTime());
        uploadLog.setBillType(orderEntity.getAction());
        uploadLog.setSubmitTime(new Date());
        uploadLog.setBillNo(orderEntity.getBillNo());
        uploadLog.setFromType(Constants.THIRD_ORDER_UPLOAD);
        orderDao.updateById(orderEntity);
        uploadLog.setStatus(4);
        uploadLog.setResult("单据数据异常：" + errMsg);
        insertOrUpdate(uploadLog);
    }
}
