package org.dromara.erp.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.ErpSaleOrderMaster;
import org.dromara.erp.domain.query.ErpSaleOrderMasterQuery;
import org.dromara.erp.domain.vo.ErpSaleOrderMasterVo;
import org.dromara.erp.mapper.ErpSaleOrderMasterMapper;
import org.dromara.erp.service.IErpSaleOrderMasterService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ERP 销售订单主表 Service 业务层处理
 *
 * @author Antigravity
 */
@RequiredArgsConstructor
@Service
public class ErpSaleOrderMasterServiceImpl implements IErpSaleOrderMasterService {

    private final ErpSaleOrderMasterMapper baseMapper;

    @Override
    public TableDataInfo<ErpSaleOrderMasterVo> queryPageList(ErpSaleOrderMasterQuery query, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSaleOrderMaster> lqw = buildQueryWrapper(query);
        IPage<ErpSaleOrderMasterVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public List<ErpSaleOrderMasterVo> queryList(ErpSaleOrderMasterQuery query) {
        LambdaQueryWrapper<ErpSaleOrderMaster> lqw = buildQueryWrapper(query);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public ErpSaleOrderMasterVo queryById(String rowId) {
        return baseMapper.selectVoById(rowId);
    }

    private LambdaQueryWrapper<ErpSaleOrderMaster> buildQueryWrapper(ErpSaleOrderMasterQuery query) {
        LambdaQueryWrapper<ErpSaleOrderMaster> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotEmpty(query.getDocCode()), ErpSaleOrderMaster::getDocCode, query.getDocCode());
        lqw.eq(ObjectUtil.isNotEmpty(query.getDealerNum()), ErpSaleOrderMaster::getDealerNum, query.getDealerNum());
        lqw.like(ObjectUtil.isNotEmpty(query.getDealerName()), ErpSaleOrderMaster::getDealerName, query.getDealerName());
        lqw.eq(ObjectUtil.isNotEmpty(query.getClientNum()), ErpSaleOrderMaster::getClientNum, query.getClientNum());
        lqw.like(ObjectUtil.isNotEmpty(query.getClientName()), ErpSaleOrderMaster::getClientName, query.getClientName());
        lqw.eq(ObjectUtil.isNotEmpty(query.getDocStatus()), ErpSaleOrderMaster::getDocStatus, query.getDocStatus());
        lqw.eq(ObjectUtil.isNotEmpty(query.getOrderState()), ErpSaleOrderMaster::getOrderState, query.getOrderState());
        lqw.eq(ObjectUtil.isNotEmpty(query.getProdMode()), ErpSaleOrderMaster::getProdMode, query.getProdMode());
        lqw.orderByDesc(ErpSaleOrderMaster::getDocDate);
        return lqw;
    }

}
