package com.willalp.stat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.stat.domain.HsStatClockingIn;
import com.willalp.stat.mapper.HsStatClockingInMapper;
import com.willalp.stat.service.IHsStatClockingInService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 统计考勤Service业务层处理
 *
 * @author willalp
 * @date 2022-08-03
 */
@Service
public class HsStatClockingInServiceImpl extends ServiceImpl<HsStatClockingInMapper, HsStatClockingIn> implements IHsStatClockingInService {
    @Resource
    private HsStatClockingInMapper hsStatClockingInMapper;

    /**
     * 查询统计考勤列表
     *
     * @param hsStatClockingIn 统计考勤
     * @return 统计考勤
     */
    @Override
    public List<HsStatClockingIn> selectHsStatClockingInList(HsStatClockingIn hsStatClockingIn) {
        return hsStatClockingInMapper.selectHsStatClockingInList(hsStatClockingIn);
    }

    @Override
    public String importData(List<HsStatClockingIn> list, ExcelUtil<HsStatClockingIn> util, InputStream is) {
        JSONArray jsonArray = new JSONArray();
        try {
            //所有数据转换成完整JSON
            jsonArray = util.importExcelToJson(StringUtils.EMPTY, is, 1, 23,0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (0 != list.size() && jsonArray.size() == list.size()) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setZend(new JSONArray(Collections.singletonList(jsonArray.get(i))));
                list.get(i).setCreateTime(new Date());
            }
        }
        if (saveOrUpdateBatch(list)) {
            return "工资数据导入成功";
        }
        return "工资数据导入失败";
    }
}
