/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.audit.information.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jeeplus.modules.sg.audit.information.entity.Information;
import com.jeeplus.modules.sg.audit.information.entity.append;
import com.jeeplus.modules.sg.audit.information.entity.constant;
import com.jeeplus.modules.sg.audit.information.mapper.InformationMapper;
import com.jeeplus.modules.sg.audit.material.entity.Material;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;

/**
 * 项目管理Service
 * @author
 * @version
 */
@Service
@Transactional(readOnly = true)
public class InformationService extends CrudService<InformationMapper, Information> {

	@Autowired
	private InformationMapper informationMapper;

	public Information get(String id) {
		Information information = super.get(id);
		return information;
	}

	@Override
	public List<Information> findList(Information information) {
		List<Information> list = super.findList(information);
		String find = informationMapper.findOverTime("overTime");
		int overTime = Integer.parseInt(find);
		Calendar cal = Calendar.getInstance();
		long between_days = 0;
		for (Information in : list) {
			//判断审定日期是否为空
//			if (in.getApprovalDate() != null) {
			//格式化审定日期
			if ("否".equals(in.getFirstFinish()) || "否".equals(in.getSecondFinish())) {
				in.setComplete("否");
				if (in.getApprovalDate() != null) {
					cal.setTime(in.getApprovalDate());
					long appDay = cal.getTimeInMillis();
					cal.setTime(new Date());
					long nowDate = cal.getTimeInMillis();
					between_days = (nowDate - appDay) / (1000 * 3600 * 24);
					in.setNoFinishDate(String.valueOf(between_days) + "天");
				}
			} else {
				in.setComplete("是");
				if (in.getApprovalDate() != null) {
					cal.setTime(in.getApprovalDate());
					long appDay = cal.getTimeInMillis();
					if (in.getFirstFinishDate() != null || in.getSecondFinishDate() != null) {
						if (in.getSecondFinishDate() != null) {
							cal.setTime(in.getSecondFinishDate());
							long secondDate = cal.getTimeInMillis();
							between_days = (secondDate - appDay) / (1000 * 3600 * 24);
							in.setFinishDate(String.valueOf(between_days) + "天");
							if (between_days > overTime) {
								in.setOverdue("是");
							} else {
								in.setOverdue("否");
							}
						} else {
							cal.setTime(in.getFirstFinishDate());
							long firstDate = cal.getTimeInMillis();
							between_days = (firstDate - appDay) / (1000 * 3600 * 24);
							in.setFinishDate(String.valueOf(between_days) + "天");
							if (between_days > overTime) {
								in.setOverdue("是");
							} else {
								in.setOverdue("否");
							}
						}
					}
				}
			}
//			}
		}
		return list;

	}


	public List<Information> findAllList(Information entity) {
		return super.findAllList(entity);
	}

	public List<Information> findNoFinishList(List<String> strings) {
		return informationMapper.findNoFinishList(strings);
	}

	public Page<Information> findPage(Page<Information> page, Information entity) {
		Page<Information> page1 = super.findPage(page, entity);
		List<Information> list = page1.getList();
/*		String find = informationMapper.findOverTime("overTime");
		int overTime = Integer.parseInt(find);*/
		Calendar cal = Calendar.getInstance();

		long between_days = 0;
		for (Information in : list) {
			//判断审定日期是否为空
//			if (in.getApprovalDate() != null) {
				//格式化审定日期
				if ("否".equals(in.getFirstFinish()) || "否".equals(in.getSecondFinish())) {
					in.setComplete("否");
					if (in.getApprovalDate() != null) {
						cal.setTime(in.getApprovalDate());
						long appDay = cal.getTimeInMillis();
						cal.setTime(new Date());
						long nowDate = cal.getTimeInMillis();
						between_days = (nowDate - appDay) / (1000 * 3600 * 24);
						in.setNoFinishDate(String.valueOf(between_days) + "天");
					}
				} else {
					in.setComplete("是");
					if (in.getApprovalDate() != null) {
						cal.setTime(in.getApprovalDate());
						long appDay = cal.getTimeInMillis();
						if (in.getFirstFinishDate() != null || in.getSecondFinishDate() != null) {
							if (in.getSecondFinishDate() != null) {
								cal.setTime(in.getSecondFinishDate());
								long secondDate = cal.getTimeInMillis();
								between_days = (secondDate - appDay) / (1000 * 3600 * 24);
								in.setFinishDate(String.valueOf(between_days) + "天");
								if (between_days > 14) {
									in.setOverdue("是");
								} else {
									in.setOverdue("否");
								}
							} else {
								cal.setTime(in.getFirstFinishDate());
								long firstDate = cal.getTimeInMillis();
								between_days = (firstDate - appDay) / (1000 * 3600 * 24);
								in.setFinishDate(String.valueOf(between_days) + "天");
								if (between_days > 14) {
									in.setOverdue("是");
								} else {
									in.setOverdue("否");
								}
							}
						}
					}
				}
//			}
		}
		return page1;
	}

/*	public Page<Information> findSearchPage(Page<Information> page, searchDto entity) {
		//从数据库中查询数据
		Page<Information> page1 = super.findPage(page, entity);

		//获取本页数据对象列表
		List<Information> list = page1.getList();


		Calendar cal = Calendar.getInstance();
		long between_days = 0;
		for (Information in : list) {
			//判断审定日期是否为空
//			if (in.getApprovalDate() != null) {
			//格式化审定日期
			if ("否".equals(in.getFirstFinish()) || "否".equals(in.getSecondFinish())) {
				in.setComplete("否");
				if (in.getApprovalDate() != null) {
					cal.setTime(in.getApprovalDate());
					long appDay = cal.getTimeInMillis();
					cal.setTime(new Date());
					long nowDate = cal.getTimeInMillis();
					between_days = (nowDate - appDay) / (1000 * 3600 * 24);
					in.setNoFinishDate(String.valueOf(between_days) + "天");
				}
			} else {
				in.setComplete("是");
				if (in.getApprovalDate() != null) {
					cal.setTime(in.getApprovalDate());
					long appDay = cal.getTimeInMillis();
					if (in.getFirstFinishDate() != null || in.getSecondFinishDate() != null) {
						if (in.getSecondFinishDate() != null) {
							cal.setTime(in.getSecondFinishDate());
							long secondDate = cal.getTimeInMillis();
							between_days = (secondDate - appDay) / (1000 * 3600 * 24);
							in.setFinishDate(String.valueOf(between_days) + "天");
							if (between_days > 14) {
								in.setOverdue("是");
							} else {
								in.setOverdue("否");
							}
						} else {
							cal.setTime(in.getFirstFinishDate());
							long firstDate = cal.getTimeInMillis();
							between_days = (firstDate - appDay) / (1000 * 3600 * 24);
							in.setFinishDate(String.valueOf(between_days) + "天");
							if (between_days > 14) {
								in.setOverdue("是");
							} else {
								in.setOverdue("否");
							}
						}
					}
				}
			}
//			}
		}

		return page1;
	}*/


	@Override
	public List<Information> findListBy(List<Information> entity) {
		List<Information> information = super.findListBy(entity);
		String find = informationMapper.findOverTime("overTime");
		int overTime = Integer.parseInt(find);
		Calendar cal = Calendar.getInstance();
		long between_days = 0;
		for (Information in : information) {
		if ("否".equals(in.getFirstFinish()) || "否".equals(in.getSecondFinish())) {
			in.setComplete("否");
			if (in.getApprovalDate() != null) {
				cal.setTime(in.getApprovalDate());
				long appDay = cal.getTimeInMillis();
				cal.setTime(new Date());
				long nowDate = cal.getTimeInMillis();
				between_days = (nowDate - appDay) / (1000 * 3600 * 24);
				in.setNoFinishDate(String.valueOf(between_days) + "天");
			}
		} else {
			in.setComplete("是");
			if (in.getApprovalDate() != null) {
				cal.setTime(in.getApprovalDate());
				long appDay = cal.getTimeInMillis();
				if (in.getFirstFinishDate() != null || in.getSecondFinishDate() != null) {
					if (in.getSecondFinishDate() != null) {
						cal.setTime(in.getSecondFinishDate());
						long secondDate = cal.getTimeInMillis();
						between_days = (secondDate - appDay) / (1000 * 3600 * 24);
						in.setFinishDate(String.valueOf(between_days) + "天");
						if (between_days > overTime) {
							in.setOverdue("是");
						} else {
							in.setOverdue("否");
						}
					} else {
						cal.setTime(in.getFirstFinishDate());
						long firstDate = cal.getTimeInMillis();
						between_days = (firstDate - appDay) / (1000 * 3600 * 24);
						in.setFinishDate(String.valueOf(between_days) + "天");
						if (between_days > overTime) {
							in.setOverdue("是");
						} else {
							in.setOverdue("否");
						}
					}
				}
			}}
		}
		return information;
	}

	@Transactional(readOnly = false)
	public int updateDate(List<Material> material){
		return informationMapper.updateDate(material);
	}



	public Information findListById(String projectId){
		return informationMapper.findListById(projectId);
	}

	@Transactional(readOnly = false)
	public void save(Information entity) {
		entity.preInsert();
		informationMapper.insert(entity);
	}

	@Transactional(readOnly = false)
	public int saveInfo(List<Information> entity) {
		return informationMapper.saveInfo(entity);
	}



	public  List<Information> findAllInformationList() {
		return informationMapper.findAllInformationList();
	}

	@Transactional(readOnly = false)
	public int updateInformationDate(Information information) {
		return informationMapper.updateInformationDate(information);
	}

	public Information findInformationByID(Information information) {
		return informationMapper.findInformationByID(information);
	}

	@Transactional(readOnly = false)
	public int updateAppend(List<append> list) {
		return informationMapper.updateAppend(list);
	}


	public List<Material> allNoBack(List<Information> page) {
		List<Material> materialList = informationMapper.allNoBack(page);

		return materialList;
	}

	public List<Material> allNo(List<Information> page) {
		List<Material> materialList = informationMapper.allNo(page);
		return materialList;
	}

	public List<Information> exportInfor(List<Information> list) {
		List<Information> informationList = informationMapper.exportInfor(list);
		String find = informationMapper.findOverTime("overTime");
		int overTime = Integer.parseInt(find);
		Calendar cal = Calendar.getInstance();
		long between_days = 0;
		for (Information in : informationList) {
			if ("否".equals(in.getFirstFinish()) || "否".equals(in.getSecondFinish())) {
				in.setComplete("否");
				if (in.getApprovalDate() != null) {
					cal.setTime(in.getApprovalDate());
					long appDay = cal.getTimeInMillis();
					cal.setTime(new Date());
					long nowDate = cal.getTimeInMillis();
					between_days = (nowDate - appDay) / (1000 * 3600 * 24);
					in.setNoFinishDate(String.valueOf(between_days) + "天");
				}
			} else {
				in.setComplete("是");
				if (in.getApprovalDate() != null) {
					cal.setTime(in.getApprovalDate());
					long appDay = cal.getTimeInMillis();
					if (in.getFirstFinishDate() != null || in.getSecondFinishDate() != null) {
						if (in.getSecondFinishDate() != null) {
							cal.setTime(in.getSecondFinishDate());
							long secondDate = cal.getTimeInMillis();
							between_days = (secondDate - appDay) / (1000 * 3600 * 24);
							in.setFinishDate(String.valueOf(between_days) + "天");
							if (between_days > overTime) {
								in.setOverdue("是");
							} else {
								in.setOverdue("否");
							}
						} else {
							cal.setTime(in.getFirstFinishDate());
							long firstDate = cal.getTimeInMillis();
							between_days = (firstDate - appDay) / (1000 * 3600 * 24);
							in.setFinishDate(String.valueOf(between_days) + "天");
							if (between_days > overTime) {
								in.setOverdue("是");
							} else {
								in.setOverdue("否");
							}
						}
					}
				}
			}
		}
		return informationList;
	}

	public List<Information> export(List<Information> list) {
		List<Information> export = informationMapper.export(list);
		String find = informationMapper.findOverTime("overTime");
		int overTime = Integer.parseInt(find);
		Calendar cal = Calendar.getInstance();
		long between_days = 0;
		for (Information in : export) {
			if ("否".equals(in.getFirstFinish()) || "否".equals(in.getSecondFinish())) {
				in.setComplete("否");
				if (in.getApprovalDate() != null) {
					cal.setTime(in.getApprovalDate());
					long appDay = cal.getTimeInMillis();
					cal.setTime(new Date());
					long nowDate = cal.getTimeInMillis();
					between_days = (nowDate - appDay) / (1000 * 3600 * 24);
					in.setNoFinishDate(String.valueOf(between_days) + "天");
				}
			} else {
				in.setComplete("是");
				if (in.getApprovalDate() != null) {
					cal.setTime(in.getApprovalDate());
					long appDay = cal.getTimeInMillis();
					if (in.getFirstFinishDate() != null || in.getSecondFinishDate() != null) {
						if (in.getSecondFinishDate() != null) {
							cal.setTime(in.getSecondFinishDate());
							long secondDate = cal.getTimeInMillis();
							between_days = (secondDate - appDay) / (1000 * 3600 * 24);
							in.setFinishDate(String.valueOf(between_days) + "天");
							if (between_days > overTime) {
								in.setOverdue("是");
							} else {
								in.setOverdue("否");
							}
						} else {
							cal.setTime(in.getFirstFinishDate());
							long firstDate = cal.getTimeInMillis();
							between_days = (firstDate - appDay) / (1000 * 3600 * 24);
							in.setFinishDate(String.valueOf(between_days) + "天");
							if (between_days > overTime) {
								in.setOverdue("是");
							} else {
								in.setOverdue("否");
							}
						}
					}
				}
			}
		}

		return export;
	}

	public List<Information> findIdByList(List<Information> list) {
		return informationMapper.findIdByList(list);
	}

	public String findOverTime(String overType){
		return informationMapper.findOverTime(overType);
	}

	@Transactional(readOnly = false)
	public int updateConstant(constant constant) {
		return informationMapper.updateConstant(constant);
	}

	public List<Information> findListWord(Information entity) {
		return informationMapper.findListWord(entity);
	}
	/**
	 * 根据id列表 获取甲供材项目信息
	 * @param strings
	 * @return
	 */
	public List<Information> getFindListMain(List<String> ids){
		return informationMapper.findListMain(ids);
	}
}