package com.neusoft.smart.pos.advertisement.service;

import com.neusoft.smart.pos.advertisement.constants.AdvertisementErrorCode;
import com.neusoft.smart.pos.advertisement.persistent.domain.MediaResource;
import com.neusoft.smart.pos.advertisement.persistent.ext.domain.MediaResourceQuery;
import com.neusoft.smart.pos.advertisement.persistent.repository.MediaResourceRepository;
import com.neusoft.smart.pos.advertisement.persistent.repository.MediaResourceStrategyRepository;
import com.neusoft.smart.pos.dto.advertisement.request.MediaResourceQueryRequest;
import com.neusoft.smart.pos.dto.advertisement.request.MediaResourceRequest;
import com.neusoft.smart.pos.dto.advertisement.response.MediaResourceResponse;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.framework.utils.BeanUtil;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class MediaResourceService {
	
	@Autowired
	private MediaResourceRepository mediaResourceRepository;
	
	@Autowired
	private MediaResourceStrategyRepository mediaResourceStrategyRepository;
	
	@Transactional
	public MediaResourceResponse create(MediaResourceRequest mediaResourceRequest) {
		if (mediaResourceRepository.countByName(mediaResourceRequest.getMediaName()) > 0) {
			throw new BusinessCommonException(AdvertisementErrorCode.MEDIA_NAME_DUPLICATE);
		}
		MediaResource mediaResource = new MediaResource();
		BeanUtils.copyProperties(mediaResourceRequest, mediaResource);
		mediaResource.setCreateBy(RequestHeaderUtil.getUserId());
		mediaResource.setOrganizationId(RequestHeaderUtil.getOrgId());
		mediaResourceRepository.create(mediaResource);
		MediaResourceResponse response = new MediaResourceResponse();
		BeanUtils.copyProperties(mediaResource, response);
		return response;
	}
	
	/**
	 * @param mediaResourceQuery
	 * @return
	 */
	public List<MediaResourceResponse> getList(MediaResourceQueryRequest mediaResourceQuery) {
		List<MediaResourceResponse> result = new ArrayList<>();
		List<MediaResource> poList = mediaResourceRepository.getList(BeanUtil.parse(mediaResourceQuery, MediaResourceQuery.class));
		poList.forEach(po -> {
			MediaResourceResponse response = new MediaResourceResponse();
			BeanUtils.copyProperties(po, response);
			result.add(response);
		});
		return result;
	}
	
	/**
	 * @param mediaResourceQuery
	 * @return
	 */
	public Integer getCount(MediaResourceQueryRequest mediaResourceQuery) {
		return mediaResourceRepository.getCount(BeanUtil.parse(mediaResourceQuery, MediaResourceQuery.class));
	}
	
	/**
	 * @param resourceId
	 * @param status
	 */
	@Transactional
	public void updateStatus(Integer resourceId, Integer status) {
		Integer count = mediaResourceStrategyRepository.queryStrategyCount(resourceId);
		if (count > 0) {
			throw new BusinessCommonException(AdvertisementErrorCode.STRATEGY_EXIST);
		}
		MediaResource mediaResource = new MediaResource();
		mediaResource.setId(resourceId);
		mediaResource.setStatus(status);
		mediaResourceRepository.updateStatus(mediaResource);
	}
	
}
