package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.common.model.order.StoreOrderReturn;
import com.zbkj.service.dao.StoreOrderReturnDao;
import com.zbkj.service.service.StoreOrderReturnInfoService;
import com.zbkj.service.service.StoreOrderReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StoreOrderReturnServiceImpl extends ServiceImpl<StoreOrderReturnDao, StoreOrderReturn> implements StoreOrderReturnService {

    @Autowired private StoreOrderReturnDao dao;
    @Autowired private StoreOrderReturnInfoService storeOrderReturnInfoService;

    @Override
    public void createReturn(StoreOrderReturn oReturn) {
        dao.insert(oReturn);
        oReturn.getReturnInfos().forEach(info -> info.setReturnId(oReturn.getId()));
        storeOrderReturnInfoService.saveBatch(oReturn.getReturnInfos());
    }

    @Override
    public void refundErr(Integer orderId, String verifyFailReason) {
        LambdaQueryWrapper<StoreOrderReturn> lw = new LambdaQueryWrapper<>();
        lw.eq(StoreOrderReturn::getOrderId, orderId);
        StoreOrderReturn one = getOne(lw);
        StoreOrderReturn upd = new StoreOrderReturn();
        upd.setId(one.getId());
        upd.setReturnVerifyDate(new Date());
        upd.setRefundVerifyFailReason(verifyFailReason);
        upd.setReturnState(3);
        dao.updateById(upd);
    }

    @Override
    public void refundOk(Integer orderId) {
        LambdaQueryWrapper<StoreOrderReturn> lw = new LambdaQueryWrapper<>();
        lw.eq(StoreOrderReturn::getOrderId, orderId);
        StoreOrderReturn one = getOne(lw);
        StoreOrderReturn upd = new StoreOrderReturn();
        upd.setId(one.getId());
        upd.setReturnVerifyDate(new Date());
        upd.setReturnState(2);
        dao.updateById(upd);
    }
}
