package com.sw.mes.yb.ipc.service.impl;

import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.base.factory.utils.PageUtils;
import com.sw.mes.yb.ipc.dao.ParamSetDAO;
import com.sw.mes.yb.ipc.entity.ParamSet;
import com.sw.mes.yb.ipc.service.ParamSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ParamSetServiceImpl extends BaseServiceImpl<ParamSet> implements ParamSetService {

	ParamSetDAO paramSetDAO;

	@Autowired
	public void setDao(ParamSetDAO paramSetDAO) {
		this.paramSetDAO = paramSetDAO;
		super.setBaseDao(paramSetDAO);
	}

	@Override
	public List<ParamSet> findAll(RequestPage page) {
		org.springframework.data.domain.Page<ParamSet> springDataPage = paramSetDAO.findAll(PageUtils.createPageable(page));
		page.setTotal(springDataPage.getTotalElements());
		return springDataPage.getContent();
	}

	@Override
	public List<ParamSet> findAll(Specification<ParamSet> specification) {
		return paramSetDAO.findAll(specification);
	}

	@Override
	public List<ParamSet> findByExample(
            Specification<ParamSet> specification, RequestPage page) {
		org.springframework.data.domain.Page<ParamSet> springDataPage = paramSetDAO.findAll(specification, PageUtils.createPageable(page));
		page.setTotal(springDataPage.getTotalElements());
		return springDataPage.getContent();
	}

	@Override
	public List<ParamSet> findByParamName(String paramName) {
		return paramSetDAO.findByParamName(paramName);
	}

	@Override
	public List<ParamSet> findAll() {
		return paramSetDAO.findAll();
	}

	@Override
	public List<String> selectProcessName(String paramName) {
		return paramSetDAO.selectProcessName(paramName);
	}

	@Override
	public ParamSet getByProcessName(String name) {
		List<ParamSet> paramSets = paramSetDAO.findByProcessName(name);
		if (paramSets != null && paramSets.size() > 0) {
			return paramSets.get(paramSets.size() - 1);
		}
		return null;
	}

	@Override
	public List<ParamSet> findByDeviceName(String deviceTypeCode) {
		return paramSetDAO.findByDeviceName(deviceTypeCode);
	}
}
