package cn.com.ctop.reimburse.mondule.service.impl;

import cn.com.ctop.common.module.service.ISysUserService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.ExportExcelUtils;
import cn.com.ctop.reimburse.mondule.entity.ReimburseExpress;
import cn.com.ctop.reimburse.mondule.mapper.ReimburseExpressMapper;
import cn.com.ctop.reimburse.mondule.service.IReimburseExpressService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 报销申请——快递记录表
 *
 * @author jeecg-boot
 * 2022-11-21
 * @version V1.0
 */
@Service
public class ReimburseExpressServiceImpl extends ServiceImpl<ReimburseExpressMapper, ReimburseExpress> implements IReimburseExpressService {
    @Autowired
    private ISysUserService sysUserService;

    @Override
    public Result<Object> importExcel(MultipartFile file) throws Exception {
        JSONObject returnJson = new JSONObject();
        Workbook workbook = null;
        String fileName = file.getOriginalFilename();
        String substring = fileName.substring(fileName.lastIndexOf(".") + 1).toUpperCase();
        if ("XLS".equals(substring)) {
            workbook = new HSSFWorkbook(file.getInputStream());
        } else if ("XLSX".equals(substring)) {
            workbook = new XSSFWorkbook(file.getInputStream());
        } else {
            throw new Exception("文件不是Excel文件");
        }
        Sheet sheet = workbook.getSheetAt(0);
        int rows = sheet.getLastRowNum();//行数，一共有多少行+
        if (rows == 0) {
            throw new Exception("请填写数据");
        }
        ExportExcelUtils eeu = new ExportExcelUtils();
        //标题行
        Row titleRow = sheet.getRow(0);
        //最后一列
        int lastCellNum = titleRow.getLastCellNum();
        List<ReimburseExpress> list = new ArrayList<>();
        for (int i = 1; i <= rows + 1; i++) {
            Row row = sheet.getRow(i);
            if (row != null) {
                ReimburseExpress express = new ReimburseExpress();
                express.setStatDate(eeu.getCellValue2(row.getCell(0)));
                express.setExpressNumber(eeu.getCellValue2(row.getCell(1)));
                express.setUserName(eeu.getCellValue2(row.getCell(2)));
                SysUser user = sysUserService.getUserByRealName(express.getUserName());
                if (!Check.isNull(user)) {
                    express.setUserId(user.getId());
                }
                express.setRemarks(eeu.getCellValue2(row.getCell(3)));
                list.add(express);
            }
        }
        baseMapper.replaceBatch(list);
        return Result.ok("数据导入成功 ");
    }
}
