package com.wms_main.service.controller.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms_main.dao.ITAppGoodsService;
import com.wms_main.model.po.TAppGoods;
import com.wms_main.model.dto.query.GoodsQuery;
import com.wms_main.model.vo.wms.GoodsVo;
import com.wms_main.model.vo.wms.PageVo;
import com.wms_main.model.dto.response.wms.WmsApiResponse;
import com.wms_main.repository.utils.StringUtils;
import com.wms_main.service.controller.IGoodsControllerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 物料控制类 服务实现
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class GoodsControllerServiceImpl implements IGoodsControllerService {
    private final ITAppGoodsService goodsService;

    /**
     * 实现--查询物料信息（分页）
     * @param goodsQuery 查询参数
     * @return 查询结果
     */
    @Override
    public WmsApiResponse<PageVo<GoodsVo>> getGoodsInfoByPage(GoodsQuery goodsQuery) {
        if (goodsQuery == null) {
            return WmsApiResponse.error("查询参数不能为NULL", null);
        }
        Page<TAppGoods> page = goodsQuery.toMpPage();
        Page<TAppGoods> goodsPage = goodsService.page(page, new LambdaQueryWrapper<TAppGoods>()
                .like(StringUtils.isNotEmpty(goodsQuery.getGoodsId()), TAppGoods::getGoodsId, goodsQuery.getGoodsId())
                .like(StringUtils.isNotEmpty(goodsQuery.getGoodsDesc()), TAppGoods::getGoodsDescription, goodsQuery.getGoodsDesc()));

        PageVo<GoodsVo> pageVo = PageVo.of(goodsPage, GoodsVo::of);
        return WmsApiResponse.success("查询成功", pageVo);
    }

    /**
     * 根据物料id查询物料信息
     * @param goodsId 物料id
     * @return 查询结果
     */
    @Override
    public WmsApiResponse<GoodsVo> getGoodsInfoByGoodsId(String goodsId) {
        if (StringUtils.isEmpty(goodsId)) {
            return WmsApiResponse.error("请求参数不能为空。", null);
        }
        TAppGoods targetGoods = goodsService.getById(goodsId);
        if (targetGoods == null) {
            return WmsApiResponse.error("查询结果为空。", null);
        }
        return WmsApiResponse.success("查询成功。", GoodsVo.of(targetGoods));
    }
}
