package com.xtd.project.assets.service.impl;

import com.xtd.common.exception.CustomException;
import com.xtd.common.utils.DateUtils;
import com.xtd.common.utils.SecurityUtils;
import com.xtd.common.utils.StringUtils;
import com.xtd.project.assets.domain.DbAssetsStock;
import com.xtd.project.assets.mapper.DbAssetsStockMapper;
import com.xtd.project.assets.service.DbBulkImportService;
import com.xtd.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DbBulkImportServiceImpl implements DbBulkImportService {

    @Autowired
    private DbAssetsStockMapper dbAssetsStockMapper;

    @Value("${assets.numberName}")
    private String numberName;

    @Override
    public void insertListData(List<List<Object>> listob) {
        String number = dbAssetsStockMapper.selectNumber();
        if (StringUtils.isNull(number)) {
            number = numberName;
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //创建人
        Long userId = user.getUserId();
        String userName = user.getUserName();

        String[] split = number.split("-");
        String head = split[0];
        Long tailNum = Long.valueOf(split[1]);
        List<DbAssetsStock> collect = new ArrayList<>();
        try {


            for (List<Object> x : listob) {
                DbAssetsStock dbAssetsStock = new DbAssetsStock();
                tailNum = tailNum + 1;
                String format = String.format("%08d", tailNum);
                String newNumber = head + "-" + format;
                dbAssetsStock.setaNumber(newNumber);
                String name = x.get(0).toString();
                dbAssetsStock.setaName(name);
                String materielid = x.get(1).toString();
                dbAssetsStock.setDbMaterielId(Long.valueOf(materielid));
                //设置为常规入库
                dbAssetsStock.setStorageType(0L);
                //设置为在库
                dbAssetsStock.setStorageStatus(0L);
                //设置制单人
                dbAssetsStock.setSysUserId(userId);
                dbAssetsStock.setSysUser(userName);
                //创建时间
                Date date = new Date();
                //设置入库时间
                dbAssetsStock.setWareTime(date);
                //设置供应商类型
                Long dbSupplierId = Long.valueOf(x.get(2).toString());

                dbAssetsStock.setDbSupplierId(dbSupplierId);
                //设置规格
                String specType = x.get(3).toString();
                dbAssetsStock.setSpecType(specType);
                //设置保质期开始/assets/input/list
                Object startTime = x.get(4);
                dbAssetsStock.setShelfLifeStart(DateUtils.parseDate(startTime));
                //设置保质期结束
                Object endTime = x.get(5);
                dbAssetsStock.setShelfLifeEnd(DateUtils.parseDate(endTime));
                //设置创建人
                dbAssetsStock.setCreateBy(userId.toString());
                //设置创建时间
                dbAssetsStock.setCreateTime(date);
                collect.add(dbAssetsStock);
            }
        } catch (Exception e) {
            throw new CustomException("文件数据上传错误!");
        }
        if (collect.size() > 0) {
            dbAssetsStockMapper.insertList(collect);
        }
    }
}
