package com.jpsoft.shinestar.modules.base.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jpsoft.shinestar.modules.base.dao.AlarmConfigDAO;
import com.jpsoft.shinestar.modules.base.dao.CompanyInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.AlarmConfig;
import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.service.AlarmConfigService;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

@Transactional
@Component(value="alarmConfigService")
public class AlarmConfigServiceImpl implements AlarmConfigService {
	@Resource(name="alarmConfigDAO")
	private AlarmConfigDAO alarmConfigDAO;

	@Resource(name="companyInfoDAO")
	private CompanyInfoDAO companyInfoDAO;

	@Override
	public AlarmConfig get(String id) {
		// TODO Auto-generated method stub
		return alarmConfigDAO.get(id);
	}

	@Override
	public int insert(AlarmConfig model) {
		// TODO Auto-generated method stub
		//model.set${PkFieldName}(UUID.randomUUID().toString());

		return alarmConfigDAO.insert(model);
	}

	@Override
	public int update(AlarmConfig model) {
		// TODO Auto-generated method stub
		return alarmConfigDAO.update(model);
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return alarmConfigDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = alarmConfigDAO.exist(id);

		return count > 0 ? true : false;
	}

	@Override
	public List<AlarmConfig> list() {
		// TODO Auto-generated method stub
		return alarmConfigDAO.list();
	}

	@Override
	public Page<AlarmConfig> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,boolean count,List<Sort> sortList) {
        Page<AlarmConfig> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            alarmConfigDAO.search(searchParams,sortList);
        });

        return page;
	}

	@Override
	public List<AlarmConfig> findByCompanyId(String companyId) {
		return alarmConfigDAO.findByCompanyId(companyId);
	}

	@Override
	public Set<Integer> getWeekdaySetByCompanyId(String companyId) {
		CompanyInfo companyInfo = companyInfoDAO.get(companyId);
		String[] arr = companyInfo.getCode().split(",");

		Set<Integer> weekdaySet = new TreeSet<>();

		List<AlarmConfig> configList = null;

		for (int i=arr.length-1;i>=0;i--) {
			configList = findByCompanyId(arr[i]);

			if (configList!=null && configList.size()>0){
				break;
			}
		}

		for (AlarmConfig alarmConfig : configList) {
			String[] weekdays = alarmConfig.getWeekdays().split(",");

			for (String weekday : weekdays) {
				weekdaySet.add(Integer.valueOf(weekday));
			}
		}

		return weekdaySet;
	}

	@Override
	public List<AlarmConfig> findByCompanyIdAndWeekDay(String companyId, String weekDay, Integer attendanceType) {
		return alarmConfigDAO.findByCompanyIdAndWeekDay(companyId,weekDay,attendanceType);
	}

	@Override
	public List<AlarmConfig> findByTime(String time) {
		return alarmConfigDAO.findByTime(time);
	}

	@Override
	public List<String> findSubCompanyList(String companyCode, String companyId) {
		return alarmConfigDAO.findSubCompanyList(companyCode, companyId);
	}

	@Override
	public List<AlarmConfig> findByCompanyIdAndType(String companyId, Integer attendanceType) {
		return alarmConfigDAO.findByCompanyIdAndType(companyId, attendanceType);
	}
}