package org.dromara.erp.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.ErpOrderAudit;
import org.dromara.erp.domain.vo.ErpOrderAuditVo;
import org.dromara.erp.mapper.ErpOrderAuditMapper;
import org.dromara.erp.service.IErpOrderAuditService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单审核记录 Service 业务层处理
 * @Author: Antigravity
 */
@RequiredArgsConstructor
@Service
public class ErpOrderAuditServiceImpl implements IErpOrderAuditService {

    private final ErpOrderAuditMapper baseMapper;

    @Override
    public TableDataInfo<ErpOrderAuditVo> queryPageList(ErpOrderAudit query, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpOrderAudit> lqw = Wrappers.lambdaQuery();
        lqw.eq(query.getOrderId() != null, ErpOrderAudit::getOrderId, query.getOrderId());
        lqw.orderByDesc(ErpOrderAudit::getCreateTime);
        Page<ErpOrderAuditVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<ErpOrderAuditVo> queryByOrderId(String orderId) {
        LambdaQueryWrapper<ErpOrderAudit> lqw = Wrappers.lambdaQuery();
        lqw.eq(ErpOrderAudit::getOrderId, orderId);
        lqw.orderByDesc(ErpOrderAudit::getCreateTime);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public Boolean insert(ErpOrderAudit audit) {
        if (audit.getRowId() == null) {
            audit.setRowId(IdUtil.fastUUID());
        }
        if (audit.getAuditTime() == null) {
            audit.setAuditTime(new Date());
        }
        return baseMapper.insert(audit) > 0;
    }
}
