package com.goldenwater.yjjt.service.impl;

import com.goldenwater.common.utils.DateUtils;
import com.goldenwater.common.utils.StringUtils;
import com.goldenwater.yjjt.domain.YjjtFile;
import com.goldenwater.yjjt.domain.YjjtSlData;
import com.goldenwater.yjjt.mapper.YjjtFileMapper;
import com.goldenwater.yjjt.mapper.YjjtSlDataMapper;
import com.goldenwater.yjjt.service.IYjjtSlDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

@Service("yjjtSlDataService")
public class YjjtSlDataServiceImpl implements IYjjtSlDataService {

    @Resource
    private YjjtSlDataMapper yjjtSlDataMapper;

    @Resource
    private YjjtFileMapper yjjtFileMapper;

    @Override
    public List<YjjtSlData> selectYjjtSlDataList(YjjtSlData yjjtSlData) {
        return yjjtSlDataMapper.selectYjjtSlDataList(yjjtSlData);
    }

    @Override
    public YjjtSlData selectYjjtSlDataById(Long id) {
        return yjjtSlDataMapper.selectYjjtSlDataById(id);
    }

    @Override
    public int insertYjjtSlData(YjjtSlData yjjtSlData) {
        yjjtSlData.setCreateTime(DateUtils.getNowDate());
        return yjjtSlDataMapper.insertYjjtSlData(yjjtSlData);
    }

    @Override
    public int updateYjjtSlData(YjjtSlData yjjtSlData) {
        yjjtSlData.setUpdateTime(DateUtils.getNowDate());
        return yjjtSlDataMapper.updateYjjtSlData(yjjtSlData);
    }

    @Override
    public int deleteYjjtSlDataById(Long id) {
        return yjjtSlDataMapper.deleteYjjtSlDataById(id);
    }

    @Override
    public int deleteYjjtSlDataByIds(Long[] ids) {
        return yjjtSlDataMapper.deleteYjjtSlDataByIds(ids);
    }

    @Override
    @Transactional
    public void importSlData(List<YjjtSlData> dataList, String bizType) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (YjjtSlData data : dataList) {
            String sptStr = sdf.format(data.getSpt());
            int count = yjjtSlDataMapper.countByStcdAndSpt(data.getStcd(), sptStr, data.getAddvcd());
            if (count > 0) {
                yjjtSlDataMapper.updateYjjtSlData(data);
            } else {
                yjjtSlDataMapper.insertYjjtSlData(data);
            }
        }
    }
}