package com.wms_main.service.controller.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
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;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物料控制类 服务实现
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class GoodsControllerServiceImpl implements IGoodsControllerService {
    private final ITAppGoodsService goodsService;
    /**
     * 实现--查询物料信息（分页）
     * @param goodsQuery 查询参数
     * @return 查询结果
     */
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    public WmsApiResponse<PageVo<GoodsVo>> getGoodsInfoByPage(GoodsQuery goodsQuery) {
        try {
            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.getGoodsDescription()), TAppGoods::getGoodsDescription, goodsQuery.getGoodsDescription()));

            PageVo<GoodsVo> pageVo = PageVo.of(goodsPage, goods -> BeanUtil.copyProperties(goods, GoodsVo.class));
            return WmsApiResponse.success("查询成功", pageVo);
        } catch (Exception e) {
            throw new RuntimeException("查询错误。");
        }
    }
}
