package com.goldenwater.yjjt.service.impl;

import com.goldenwater.common.utils.DateUtils;
import com.goldenwater.yjjt.domain.YjjtSzData;
import com.goldenwater.yjjt.mapper.YjjtSzDataMapper;
import com.goldenwater.yjjt.service.IYjjtSzDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service("yjjtSzDataService")
public class YjjtSzDataServiceImpl implements IYjjtSzDataService {

    @Resource
    private YjjtSzDataMapper yjjtSzDataMapper;

    @Override
    public List<YjjtSzData> selectYjjtSzDataList(YjjtSzData yjjtSzData) {
        return yjjtSzDataMapper.selectYjjtSzDataList(yjjtSzData);
    }

    @Override
    public YjjtSzData selectYjjtSzDataById(Long id) {
        return yjjtSzDataMapper.selectYjjtSzDataById(id);
    }

    @Override
    public int insertYjjtSzData(YjjtSzData yjjtSzData) {
        yjjtSzData.setCreateTime(DateUtils.getNowDate());
        return yjjtSzDataMapper.insertYjjtSzData(yjjtSzData);
    }

    @Override
    public int updateYjjtSzData(YjjtSzData yjjtSzData) {
        yjjtSzData.setUpdateTime(DateUtils.getNowDate());
        return yjjtSzDataMapper.updateYjjtSzData(yjjtSzData);
    }

    @Override
    public int deleteYjjtSzDataById(Long id) {
        return yjjtSzDataMapper.deleteYjjtSzDataById(id);
    }

    @Override
    public int deleteYjjtSzDataByIds(Long[] ids) {
        return yjjtSzDataMapper.deleteYjjtSzDataByIds(ids);
    }

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