package com.fjhx.sale.service.pack.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fjhx.sale.entity.pack.po.PackShipment;
import com.fjhx.sale.mapper.pack.PackShipmentMapper;
import com.fjhx.sale.service.pack.PackDetailGoodsService;
import com.fjhx.sale.service.pack.PackDetailProductService;
import com.fjhx.sale.service.pack.PackShipmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.obs.services.internal.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.sale.entity.pack.vo.PackShipmentVo;
import com.fjhx.sale.entity.pack.dto.PackShipmentSelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.sale.entity.pack.dto.PackShipmentDto;
import cn.hutool.core.bean.BeanUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 出货绑定主合同表 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-19
 */
@Service
public class PackShipmentServiceImpl extends ServiceImpl<PackShipmentMapper, PackShipment> implements PackShipmentService {
    @Autowired
    private PackDetailGoodsService packDetailGoodsService;

    @Autowired
    private PackDetailProductService packDetailProductService;

    @Override
    public Page<PackShipmentVo> getPage(PackShipmentSelectDto dto) {
        IWrapper<PackShipment> wrapper = getWrapper();
        wrapper.orderByDesc("ps", PackShipment::getId);
        Page<PackShipmentVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public PackShipmentVo detail(Long id) {
        PackShipment PackShipment = this.getById(id);
        PackShipmentVo result = BeanUtil.toBean(PackShipment, PackShipmentVo.class);
        return result;
    }

    @Override
    public void add(PackShipmentDto packShipmentDto) {
        this.save(packShipmentDto);
    }

    @Override
    public void edit(PackShipmentDto packShipmentDto) {
        this.updateById(packShipmentDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }


    /**
     * 查询出货绑定主合同列表详情
     * @param dto
     * @return
     */
    @Override
    public List<PackShipmentVo> getList(PackShipmentSelectDto dto) {
        //查询出货绑定主合同列表详情
        List<PackShipmentVo> packShipments = baseMapper.getList(Wrappers.query()
                .like(ObjectUtil.isNotEmpty(dto.getKeyword())," c.code",dto.getKeyword()));
        return packShipments;
    }

    /**
     * 查询出货-主合同表的产品信息详情列表
     * @param dto
     * @return
     */
    @Override
    public List<Map<String, Object>> productDetailList(PackShipmentSelectDto dto) {
        if (ObjectUtil.isEmpty(dto.getPackDetailIds())){
            throw  new ServiceException("参数缺失：包装明细单ID不能为null");
        }
        String packDetailIds = dto.getPackDetailIds();
        //获取到包装明细单ID列表
        List<String> ids = Arrays.asList(packDetailIds.split(","));
        return baseMapper.productDetailList(ids);
    }

}
