package com.viettel.campaign.repository.impl;

import com.viettel.campaign.model.CampaignCompleteCode;
import com.viettel.campaign.repository.CampaignCompleteCodeRepositoryCustom;
import com.viettel.campaign.web.dto.ResultDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.List;

@Repository
public class CampaignCompleteCompleteCodeRepositoryIpml implements CampaignCompleteCodeRepositoryCustom {
    @Autowired
    EntityManager entityManager;

    private Logger log = LoggerFactory.getLogger(CampaignCompleteCompleteCodeRepositoryIpml.class);


    @Override
    @Transactional
    public ResultDTO updateStatusById(Long id) {
        ResultDTO dto = new ResultDTO();
        try {
            CampaignCompleteCode e = entityManager.find(CampaignCompleteCode.class, id);
            e.setStatus(new Short("0")); // set status = 0 --> khong hoat dong
            entityManager.merge(e);
            dto.setErrorCode("00");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            dto.setErrorCode("01");
            dto.setDescription(e.getMessage());
        }
        return dto;
    }
}
