package com.fjhx.service.supplier.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.entity.common.CommonFile;
import com.fjhx.entity.supplier.SupplierKeep;
import com.fjhx.mapper.supplier.SupplierKeepMapper;
import com.fjhx.params.supplier.SupplierKeepVo;
import com.fjhx.service.common.CommonFileService;
import com.fjhx.service.supplier.SupplierKeepService;
import com.fjhx.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 供应商维护记录 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-08-08
 */
@Service
public class SupplierKeepServiceImpl extends ServiceImpl<SupplierKeepMapper, SupplierKeep> implements SupplierKeepService {

    @Autowired
    private CommonFileService commonFileService;

    @Override
    public Page<SupplierKeep> getPage(Map<String, String> condition) {

        QueryWrapper<SupplierKeep> wrapper = Wrappers.query();

        WrapperUtil.init(condition, wrapper)
                .createTimeDesc();

        Page<SupplierKeep> page = page(condition, wrapper);
        return page;
    }

    @Override
    public void add(SupplierKeepVo supplierKeepVo) {
        save(supplierKeepVo);
    }

    @Override
    public void edit(SupplierKeepVo supplierKeepVo) {
        updateById(supplierKeepVo);
    }

    @Override
    public void delete(SupplierKeepVo supplierKeepVo) {
        removeById(supplierKeepVo.getId());
    }

    @Override
    public List<SupplierKeepVo> getDetails(Long id) {

        List<SupplierKeep> list = list(SupplierKeep::getSupplierId, id);

        return list.stream().map(item -> {
            SupplierKeepVo supplierKeepVo = BeanUtil.toBean(item, SupplierKeepVo.class);
            List<CommonFile> commonFileList = commonFileService.fileList(supplierKeepVo.getFileLinkedId());
            supplierKeepVo.setFileList(commonFileList);
            return supplierKeepVo;
        }).collect(Collectors.toList());

    }

}
