package com.sunxung.factoring.service.salescontractmanagement.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.exception.BusinessException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.component.util.excel.ExcelLogs;
import com.sunxung.factoring.component.util.excel.ExcelUtil;
import com.sunxung.factoring.entity.salescontractmanagement.CSalesContractGoodsInfo;
import com.sunxung.factoring.entity.salescontractmanagement.SalesContractSupplementGoodsInfo;
import com.sunxung.factoring.mapper.salescontractmanagement.SalesContractGoodsInfoMapper;
import com.sunxung.factoring.service.salescontractmanagement.ISalesContractSupplementGoodsInfoService;
import com.sunxung.factoring.service.salescontractmanagement.SalesContractGoodsInfoService;
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;

/**
 * @Description :
 * @Author : L.Jia
 * @Date: 2021-09-14 13:55
 */
@Service
public class SalesContractGoodsInfoServiceImpl extends ServiceImpl<SalesContractGoodsInfoMapper, CSalesContractGoodsInfo>
        implements SalesContractGoodsInfoService {

    @Autowired
    private ISalesContractSupplementGoodsInfoService salesContractSupplementGoodsInfoService;

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

        return baseMapper.selectList(queryWrapper);
    }

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

    @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, "导入失败，请确认文件模板格式是否符合要求");
        }
    }
}
