package com.glxp.mipsdl.service.scpwxyy;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.glxp.mipsdl.entity.scpwxyy.A0006detail;
import com.glxp.mipsdl.entity.scpwxyy.A006head;
import com.glxp.mipsdl.req.base.UdiwmsOrderQueryRequest;
import com.glxp.mipsdl.res.scwpxyy.A006headRespone;
import org.springframework.stereotype.Service;
import com.glxp.mipsdl.thirddao.scpwxyy.A006headMapper;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class A006headService extends ServiceImpl<A006headMapper, A006head> {

    @Resource
    private A0006detailService a0006detailService;

    public List<A006headRespone> filterList(UdiwmsOrderQueryRequest udiwmsOrderQueryRequest) {
        if (null == udiwmsOrderQueryRequest) {
            return Collections.emptyList();
        }
        if (null != udiwmsOrderQueryRequest.getPage() && null != udiwmsOrderQueryRequest.getLimit()) {
            PageHelper.offsetPage((udiwmsOrderQueryRequest.getPage() - 1) * udiwmsOrderQueryRequest.getLimit(), udiwmsOrderQueryRequest.getLimit());
        }
        List<A006headRespone> list = this.baseMapper.filterList(udiwmsOrderQueryRequest);
        if (CollUtil.isNotEmpty(list)) {
            list.forEach( item -> {
                String billno = item.getBillno();
                if (StrUtil.isNotEmpty(billno)) {
                    List<A0006detail> details = a0006detailService.list(new LambdaQueryWrapper<A0006detail>().eq(A0006detail::getDetailid, billno));
                    item.setDetailList(details);
                }
            });
        }
        return list;

    }
}
