package com.huimv.video.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.huimv.video.domain.SjHkAq;
import com.huimv.video.domain.SjHkWd;
import com.huimv.video.repo.SjHkWdRepository;
import com.huimv.video.result.Result;
import com.huimv.video.result.ResultStatus;
import com.huimv.video.service.SjHkWdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SjHkWdServiceImpl implements SjHkWdService {

	@Autowired
	private SjHkWdRepository rep;

	@Override
	@DS("slave")
	public Result add(SjHkWd entity){
		if (entity == null){
			return new Result(10002, ResultStatus.addNull);
		}
		try {
			rep.save(entity);
			return new Result(10000, ResultStatus.addSuccess);
		}catch (Exception e){
			return new Result(10001, ResultStatus.addFailed);
		}
	}

	@Override
	@DS("slave")
	public Result remove(Integer[] ids) {
		if (ids == null || ids.length==0){
			return new Result(10002, ResultStatus.deleteNull);
		}
		try {
			for (Integer id : ids) {
				rep.deleteById(id);
			}
			return new Result(10000, ResultStatus.deleteSuccess);
		}catch (Exception e){
			return new Result(10001, ResultStatus.deleteFailed);
		}
	}

	@Override
	@DS("slave")
	public Result update(SjHkWd entity) {
		if (entity == null){
			return new Result(10002, ResultStatus.updateNull);
		}
		try {
			rep.save(entity);
			return new Result(10000, ResultStatus.updateSuccess);
		}catch (Exception e){
			return new Result(10001, ResultStatus.updateFailed);
		}
	}

	@Override
	@DS("slave")
	public Result findAll(String name , Integer pageNum , Integer pageSize) {
		try {
			Map map = new HashMap();
			int size = rep.findAll().size() ;
			Integer startPage = (pageNum-1) * pageSize;
			List<SjHkWd> all = rep.findAll(name,startPage,pageSize);
			map.put("total",size);
			map.put("totalPageNum",(size  +  pageSize  - 1) / pageSize);
			map.put("data",all);
			
			return new Result(10000, ResultStatus.findSuccess,map);
		}catch (Exception e){
			return new Result(10001, ResultStatus.findFailed,null);
		}
	}

	@Override
	@DS("slave")
	public Result findAllById(Integer id) {
		try {
			SjHkWd entity = rep.findById(id).get();
			return new Result(10000, ResultStatus.findSuccess,entity);
		}catch (Exception e){
			return new Result(10001, ResultStatus.findFailed,null);
		}
	}

	@Override
	@DS("slave")
	public Result findAllByLQID(Integer lqid) {
		Date date = DateUtil.date();
		DateTime dateTime = DateUtil.beginOfDay(date);
		List<SjHkWd> allByScsjBetweenAndLqid = rep.findAllByScsjBetweenAndLqid(dateTime, date, lqid);

		List list = new ArrayList();
		for (SjHkWd sjHkWd : allByScsjBetweenAndLqid) {
			Map map = new HashMap();
			map.put("data",sjHkWd.getDqwd());
			map.put("time",(sjHkWd.getScsj()+"").substring(11,16));
			list.add(map);
		}

		List endList = new ArrayList();
		for (int i =1; i<list.size(); i++){
			if ( i % 5 ==0 ){
				endList.add(list.get(i));
			}
		}

		return new Result(10000,"成功",endList);
	}
}
