package com.inkasso.factoring.salesManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.inkasso.adapter.exception.BusinessException;
import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.adapter.util.StringUtil;
import com.inkasso.factoring.common.util.excel.ExcelLogs;
import com.inkasso.factoring.common.util.excel.ExcelUtil;
import com.inkasso.factoring.salesManagement.entity.CSalesContractGoodsInfo;
import com.inkasso.factoring.salesManagement.dao.CSalesContractGoodsInfoMapper;
import com.inkasso.factoring.salesManagement.service.ICSalesContractGoodsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 销售合同-货物信息表 服务实现类
 * </p>
 *
 * @author yafan
 * @since 2023-11-28
 */
@Service
public class CSalesContractGoodsInfoServiceImpl extends ServiceImpl<CSalesContractGoodsInfoMapper, CSalesContractGoodsInfo> implements ICSalesContractGoodsInfoService {
//    @Autowired
//    private ISalesContractSupplementGoodsInfoService salesContractSupplementGoodsInfoService;

    @Override
    public List<CSalesContractGoodsInfo> findByBasicInfoId(Long basicInfoId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sales_contract_basic_info_id", basicInfoId);

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public CSalesContractGoodsInfo findByGoodsId(Long goodsId, Boolean supplement) {
//        SalesContractGoodsInfo salesContractGoodsInfo;
//        if (supplement) {
//            salesContractGoodsInfo = new SalesContractGoodsInfo();
//            SalesContractSupplementGoodsInfo salesContractSupplementGoodsInfo = salesContractSupplementGoodsInfoService.getById(goodsId);
//            BeanUtils.copyProperties(salesContractSupplementGoodsInfo, salesContractGoodsInfo);
//        } else {
//            salesContractGoodsInfo = baseMapper.selectById(goodsId);
//        }
//        return salesContractGoodsInfo;
        return null;
    }

    @Override
    public List<CSalesContractGoodsInfo> importGoodsInfo(MultipartFile file) {
        if (file.isEmpty()) {
            throw new BusinessException(CodeUtil.FAIL, "导入Excel文件不能为空");
        }
        try {
            InputStream inputStream = file.getInputStream();
            Collection<CSalesContractGoodsInfo> salesContractGoodsInfos =
                    ExcelUtil.importExcel(CSalesContractGoodsInfo.class, inputStream, null, new ExcelLogs(), 0);
            return (List<CSalesContractGoodsInfo>) salesContractGoodsInfos;
        } catch (Exception e) {
            throw new BusinessException(CodeUtil.FAIL, "导入失败，请确认文件模板格式是否符合要求");
        }
    }
}
