package com.fjhx.wms.service.arrival.impl;

import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.wms.entity.arrival.po.ArrivalStockRecords;
import com.fjhx.wms.entity.arrival.po.ArrivalStockRecordsDetails;
import com.fjhx.wms.entity.arrival.vo.ArrivalStockRecordsDetailsVo;
import com.fjhx.wms.mapper.arrival.ArrivalStockRecordsMapper;
import com.fjhx.wms.service.arrival.ArrivalStockRecordsDetailsService;
import com.fjhx.wms.service.arrival.ArrivalStockRecordsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.wms.entity.arrival.vo.ArrivalStockRecordsVo;
import com.fjhx.wms.entity.arrival.dto.ArrivalStockRecordsSelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.wms.entity.arrival.dto.ArrivalStockRecordsDto;
import cn.hutool.core.bean.BeanUtil;

import java.util.List;


/**
 * <p>
 * 采购到货入库记录 维多利亚 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-26
 */
@Service
public class ArrivalStockRecordsServiceImpl extends ServiceImpl<ArrivalStockRecordsMapper, ArrivalStockRecords> implements ArrivalStockRecordsService {

    @Autowired
    ArrivalStockRecordsDetailsService arrivalStockRecordsDetailsService;
    @Autowired
    ProductInfoService productInfoService;

    @Override
    public Page<ArrivalStockRecordsVo> getPage(ArrivalStockRecordsSelectDto dto) {
        IWrapper<ArrivalStockRecords> wrapper = getWrapper();
        wrapper.orderByDesc("asr", ArrivalStockRecords::getId);
        Page<ArrivalStockRecordsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public ArrivalStockRecordsVo detail(Long id) {
        ArrivalStockRecords ArrivalStockRecords = this.getById(id);
        ArrivalStockRecordsVo result = BeanUtil.toBean(ArrivalStockRecords, ArrivalStockRecordsVo.class);
        List<ArrivalStockRecordsDetails> qualityDetailsList = arrivalStockRecordsDetailsService.list(q -> q.eq(ArrivalStockRecordsDetails::getArrivalStockRecordsId, result.getId()));
        List<ArrivalStockRecordsDetailsVo> arrivalStockRecordsDetailsVos = BeanUtil.copyToList(qualityDetailsList, ArrivalStockRecordsDetailsVo.class);
        //赋值产品信息
        productInfoService.attributeAssign(arrivalStockRecordsDetailsVos, ArrivalStockRecordsDetailsVo::getProductInfoId, (item, productInfo) -> {
            item.setProductName(productInfo.getName());
            item.setProductType(productInfo.getType());
            item.setProductCode(productInfo.getCode());
            item.setProductUnit(productInfo.getUnit());
            item.setProductSpec(productInfo.getSpec());
            item.setProductDefinition(productInfo.getDefinition());
        });
        result.setArrivalStockRecordsDetailsList(arrivalStockRecordsDetailsVos);
        return result;
    }

    @Override
    public void add(ArrivalStockRecordsDto arrivalStockRecordsDto) {
        this.save(arrivalStockRecordsDto);
    }

    @Override
    public void edit(ArrivalStockRecordsDto arrivalStockRecordsDto) {
        this.updateById(arrivalStockRecordsDto);
    }

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

}
