package com.sw.mes.fb.maketray.service.impl;


import com.alibaba.fastjson.JSON;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.fb.maketray.dao.MakeTrayItemDao;
import com.sw.mes.fb.maketray.dto.MakeTrayItemDto;
import com.sw.mes.fb.maketray.entity.MakeTrayItem;
import com.sw.mes.fb.maketray.service.MakeTrayItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class MakeTrayItemServiceImpl extends BaseServiceImpl<MakeTrayItem> implements MakeTrayItemService {
	MakeTrayItemDao dao;
	@Autowired
	public void setDao(MakeTrayItemDao dao) {
		super.setBaseDao(dao);
		this.dao=dao;
	}

	@Override
	public List<MakeTrayItem> getItemsByTrayId(Integer trayId){
		return dao.getItemsByTrayId(trayId);
	}

	@Override
	/**
	 * @Description // 获取方棒硅棒物料卡
	 * @Author  Rj li
	 * @Date   2020/12/21 15:20
	 * @Param  [trayId]
	 * @Return com.sw.mes.fb.common.response.Result<java.util.List<com.sw.mes.fb.maketray.dto.MakeTrayItemDto>>
	 */
	public Result<List<MakeTrayItemDto>> getTrayItemDtoTrayId(Integer trayId) {
		// 创建返回结果集对象
		Result<List<MakeTrayItemDto>> result = new Result<>();

		try {
			// 调动dao ,获取对应的结果集对象
			List<Map<String, Object>> trayItemDtoTrayId = this.dao.getTrayItemDtoTrayId(trayId);

			if (trayItemDtoTrayId.size() > 0 && !trayItemDtoTrayId.isEmpty()){

				// 将其遍历转换
				List<MakeTrayItemDto> collect = trayItemDtoTrayId.stream().map(entity -> JSON.parseObject(JSON.toJSONString(entity), MakeTrayItemDto.class)).collect(Collectors.toList());

				result.setData(collect);
			}
			return result;
		} catch (Exception e) {
			return ResultGenerator.fail(e.getMessage());
		}

	}

	@Override
	public List<Map<String, Object>> userCount(String name, String date1, String date2,Integer rows,Integer page) {	
		if((rows==null || rows==' ')&&(page==null || page==' ')) {
			page=0;
			rows=10;
		}
        if(page==1) {
        	page=0;
        }
		if(page>1) {
			page=(page-1)*rows;
		}
        
		if(StringUtils.isBlank(date1) && StringUtils.isBlank(date2) && StringUtils.isBlank(name)) {
			return dao.getMakeTray(page,rows);
		}
		if (StringUtils.isNotBlank(name) && StringUtils.isBlank(date1) && StringUtils.isBlank(date2)) {
			return dao.getItmsBySum(name,page,rows);
		}
		if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(date1) && StringUtils.isNotBlank(date2)) {
			return dao.getCondition(name,date1,date2,page,rows);
		}
		return dao.getItmsByTime(date1, date2,page,rows);
	}

	@Override
	public List<Map<String, Object>> byCount(String name, String date1, String date2) {
		if(StringUtils.isBlank(date1) && StringUtils.isBlank(date2) && StringUtils.isBlank(name)) {
			return dao.getByCount();
		}
		if (StringUtils.isNotBlank(name) && StringUtils.isBlank(date1) && StringUtils.isBlank(date2)) {
			return dao.getByName(name);
		}
		if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(date1) && StringUtils.isNotBlank(date2)) {
			return dao.getByAll(name,date1,date2);
		}
		return dao.getByDay(date1,date2);
	}

	//	@Override
	//	public Long findByCount(String name, String date1, String date2) {
	//		if (StringUtils.isNotBlank(name)) {
	//			return dao.getByCount(name, date1, date2);
	//		}
	//		return dao.getTimeCount(date1, date2);
	//	}

	@Override
	public List<MakeTrayItem> getByTrayNo(String trayNo) {
		return dao.getByTrayNo(trayNo);
	}

	@Override
	public void saveList(List<MakeTrayItem> list) {
		dao.saveAll(list);
	}

	@Override
	public String findBySectionFb(String sectionFb) {
		List<MakeTrayItem> makeTrayItem = dao.findBySectionNoFb(sectionFb);
		if(makeTrayItem.size()>0){
			return sectionFb+"单晶已存在一次检验类型";
		}
		return null;
	}

	@Override
	public MakeTrayItem getBySectionFb(String sectionNoFb) {
		return dao.getBySectionFb(sectionNoFb);
	}
}
