package com.its.dsrc.service;

import com.its.app.AppUtils;
import com.its.dsrc.dao.mapper.RseSectMapper;
import com.its.dsrc.dao.mapper.RseObuClctMapper;
import com.its.dsrc.entity.TbRseSectPassHs;
import com.its.dsrc.entity.TbRseSectTraf;
import com.its.dsrc.entity.TbRseObuClct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = {Exception.class})
public class RseObuClctService {

    private RseObuClctMapper rseObuClctMapper;
    private RseSectMapper rseSectMapper;

    @PostConstruct
    private void init() {
        log.info("OBUGatherService.init: Start.");
        this.rseObuClctMapper = (RseObuClctMapper) AppUtils.getBean(RseObuClctMapper.class);
        this.rseSectMapper = (RseSectMapper) AppUtils.getBean(RseSectMapper.class);
        log.info("OBUGatherService.init: ..End.");
    }

    public int insertOBUGatherInfoHs(List<TbRseObuClct> list) {
        // TODO: Transaction 으로 처리하자
        TbRseObuClct pnst = null;
        for (TbRseObuClct vo : list) {
            this.rseObuClctMapper.insertRseObuClctHs(vo);
            pnst = vo;
        }
        if (pnst != null) {
            this.rseObuClctMapper.updateRseObuClctPnst(pnst);
        }
        int jobCnt = list.size();
        list.clear();
        return jobCnt;
    }

    public int insertDsrcSectTraf(List<TbRseSectTraf> list) {
        // TODO: Transaction 으로 처리하자
        for (TbRseSectTraf vo : list) {
            this.rseSectMapper.insertRseSectTrafHs(vo);
        }
        int jobCnt = list.size();
        list.clear();
        return jobCnt;
    }

    public int insertDsrcSectPassHs(List<TbRseSectPassHs> list) {
        // TODO: Transaction 으로 처리하자
        for (TbRseSectPassHs vo : list) {
            this.rseSectMapper.insertRseSectPassHs(vo);
        }
        int jobCnt = list.size();
        list.clear();
        return jobCnt;
    }
    /*
    List<AccountDto> accouts = new ArrayList<>();
    accounts.add(new AccountDto("wedul", 123);
    accounts.add(new AccountDto("cjung", 456);

    accountDao.insertAccounts(accounts);

<insert id="insert" parameterType="java.util.List">
    INSERT INTO account(
            id,
            age
            ) VALUES
<foreach item="item" index="index" collection="list">
            (
            #{item.id}
 ,#{item.age}
)
</foreach>
</insert>
    */
}
