package com.zjty.autotest.service.impl;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.zjty.autotest.dao.TestReportDao;
import com.zjty.autotest.pojo.sjq.AutoResultSet;
import com.zjty.autotest.pojo.sjq.EvaReport;
import com.zjty.autotest.pojo.sjq.TestReport;
import com.zjty.autotest.pojo.sjq.common.AppHttpCodeEnum;
import com.zjty.autotest.pojo.sjq.common.ResponseResult;
import com.zjty.autotest.service.TestReportService;
import com.zjty.autotest.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class TestReportServiceImpl implements TestReportService {

	@Autowired
	private TestReportDao testReportDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<TestReport> findAll() {
		return testReportDao.findAll();
	}

	


	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public TestReport findById(String id) {
		return testReportDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param testReport
	 */
	@Transient
	public void add(TestReport testReport) {
		testReport.setId( idWorker.nextId()+"" );
		testReportDao.save(testReport);
	}
	@Transient
	/**
	 * 修改
	 * @param testReport
	 */
	public void update(TestReport testReport) {
		testReportDao.updateOut(testReport.getOutReport(),testReport.getResultId());
	}

	/**
	 * 删除
	 * @param resultId
	 */
	@Transient
	public ResponseResult deleteByResultId(String resultId) {
		int i = testReportDao.deleteByResultId(resultId);
		return ResponseResult.okResult(i);
	}
	@Transient
	@Override
	public ResponseResult save(TestReport testReport) {
			testReportDao.save(testReport);
			return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
	}
	@Transient
	@Override
	public ResponseResult addInReport(String id, String inReport) {
		TestReport testReport=new TestReport();
		testReport.setResultId(id);
		testReport.setId(idWorker.nextId()+"");
		testReport.setInputReport(inReport);
		testReport.setOutReport("");
		testReportDao.save(testReport);
		return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
	}

	@Override
	public ResponseResult findByIdInData(String resultId) {
		String in = testReportDao.findByInResultId(resultId);
		if(StringUtils.isEmpty(in)){
			return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
		}
		return ResponseResult.okResult(in);
	}

	@Override
	public TestReport findByResultId(String resultId) {
		TestReport testReport = testReportDao.findByResultId(resultId);
		return testReport;
	}

	@Override
	public ResponseResult findByIdOutData(String resultId) {
		String out = testReportDao.findByOutResultId(resultId);
		if(StringUtils.isEmpty(out)){
			return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
		}
		return ResponseResult.okResult(out);
	}

	@Override
	public ResponseResult findStatus(Map searchMap) {
		Specification<TestReport> specification = createSpecification(searchMap);
		List<TestReport> testReports = testReportDao.findAll(specification);
		return ResponseResult.okResult(testReports);
	}

	@Override
	public ResponseResult findAllStatus() {
		List<Map<String, Object>> allStatus = testReportDao.findAllStatus();
		for (Map<String, Object> status : allStatus) {

		}

		return ResponseResult.okResult(allStatus);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<TestReport> createSpecification(Map searchMap) {

		return new Specification<TestReport>() {

			@Override
			public Predicate toPredicate(Root<TestReport> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				if (searchMap.get("resultId") != null && !"".equals(searchMap.get("resultId"))) {
					predicateList.add(cb.like(root.get("resultId").as(String.class), "%" + (String) searchMap.get("resultId") + "%"));
				}
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
