package com.zcloud.accident.gatewayimpl;

import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.accident.domain.gateway.AccidentGateway;
import com.zcloud.accident.domain.model.AccidentE;
import com.zcloud.accident.domain.model.AccidentCorpCountStat;
import com.zcloud.accident.domain.model.AccidentCountStat;
import com.zcloud.accident.persistence.dataobject.AccidentDO;
import com.zcloud.accident.persistence.repository.AccidentRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* web-infrastructure
* @Author fangjiakai
* @Date 2025-10-29 14:28:42
*/
@Service
@AllArgsConstructor
public class AccidentGatewayImpl implements AccidentGateway {
    private final AccidentRepository accidentRepository;

    @Override
    public Boolean add(AccidentE accidentE) {
        AccidentDO d = new AccidentDO();
        BeanUtils.copyProperties(accidentE, d);
        d.setCorpinfoId(AuthContext.getTenantId());
        d.setCorpinfoName(AuthContext.getCurrentUser().getTenantName());
        accidentRepository.save(d);
        return true;
    }

    @Override
    public Boolean update(AccidentE accidentE) {
        AccidentDO d = new AccidentDO();
        BeanUtils.copyProperties(accidentE, d);
        accidentRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedAccidentById(Long id) {
        return accidentRepository.removeById(id);
    }

    @Override
    public Boolean deletedAccidentByIds(Long[] ids) {
        return accidentRepository.removeByIds(Arrays.asList(ids));
    }

    @Override
    public List<AccidentCorpCountStat> countByCorpinfo(List<String> corpinfoIds) {
        return accidentRepository.countByCorpinfo(corpinfoIds);
    }

    @Override
    public List<AccidentCountStat> countByCorpinfoIdAndIncidentType(List<String> corpinfoIds, Integer eqAccidentType) {
        return accidentRepository.countByCorpinfoIdAndIncidentType(corpinfoIds,eqAccidentType);
    }
}

