/**   
* @Title: HospitalBaseServiceImpl.java 
* @Package com.platform.service.prereg.impl 
* @Description: TODO(用一句话描述该文件做什么) 
* @author kiosk
* @date 2017年12月5日 下午5:11:26 
* @version V1.0   
*/
package com.platform.service.impl;

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

import javax.annotation.Resource;

import com.platform.pojo.*;
import com.platform.util.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.platform.dao.DoctorSchedulesPlanMapper;
import com.platform.dao.OrderRegMapper;
import com.platform.dao.SectiontypeDictionaryMapper;
import com.platform.dao.VendorInfoMapper;
import com.platform.service.IPreregService;

/**
 * @ClassName: HospitalBaseServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author kiosk
 * @date 2017年12月5日 下午5:11:26
 * 
 */
@Service("preregService")
@Transactional(rollbackFor = Exception.class)
public class PreregServiceImpl implements IPreregService {
	@Resource
	private VendorInfoMapper vendorInfoMapper;// 引入厂商信息mapper
	@Resource
	private DoctorSchedulesPlanMapper doctorSchedulesPlanMapper;// 引入医生排班计划mapper
	@Resource
	private OrderRegMapper orderRegMapper;// 引入预约记录表Mapper
	@Resource
	private SectiontypeDictionaryMapper sectiontypeDictionaryMapper;//引入科室大类mapper

	/**
	 * 查询厂商列表接口实现
	 */
	public List<VendorInfo> selectVendorList(VendorInfoExample example) {
		return vendorInfoMapper.selectByExample(example);
	}

	/**
	 * 查询排班科室接口实现
	 */
	public List<Section> selectSecions(String startDate, String endDate, String timeInterval,String sectionType) {
		return doctorSchedulesPlanMapper.selectSections(startDate, endDate, timeInterval,sectionType);
	}

	/**
	 * 查询某科室下排班医生接口实现
	 */
	public List<Doctor> selectDoctors(String startDate, String endDate, String timeInterval, String sectionNo) {
		return doctorSchedulesPlanMapper.selectDoctors(startDate, endDate, timeInterval, sectionNo);
	}

	/**
	 * 查询全部可用号表明细
	 */
	public List<Ticket> selectTicketsAll(String startDate, String endDate, String timeInterval, String sectionNo,
			String doctorNo) {
		return doctorSchedulesPlanMapper.selectTicketsAll(startDate, endDate, timeInterval, sectionNo, doctorNo);
	}

	/**
	 * 查询除保留号外全部可用号源明细
	 */
	public List<Ticket> selectTicketsNoRetain(String startDate, String endDate, String timeInterval, String sectionNo,
			String doctorNo) {
		return doctorSchedulesPlanMapper.selectTicketsNoRetain(startDate, endDate, timeInterval, sectionNo, doctorNo);
	}

	
	
	
	/**
	 * 
	* @Title: insertAndUpdate 
	* @Description: TODO(插入预约记录，并更新原号表记录为已预约) 
	* @param @param orderReg
	* @param @return    设定文件 
	* @return int    返回类型 
	* @throws
	 */
	public int insertAndUpdate(OrderReg orderReg) {
		int num = 0;
		DoctorSchedulesPlanExample example = new DoctorSchedulesPlanExample();
		example.or().andIdEqualTo(orderReg.getPlanId());
		DoctorSchedulesPlan record = new DoctorSchedulesPlan();
		record.setSourceStatus("3");
		try {
			int x = orderRegMapper.insertSelective(orderReg);
			int y = doctorSchedulesPlanMapper.updateByExampleSelective(record, example);
			num = x + y;
			if (num != 2) {
				throw new Exception("插入更新同步执行出错");
			}
		} catch (Exception e) {
			throw new RuntimeException();
		}
		return num;
	}
	
	/**
	 * 根据挂号时间查询可预约的号表记录
	 */
	public List<Ticket> selectTicketsByRegTime(String regTime,String timeInterval, String sectionNo,
			String doctorNo) {
		return doctorSchedulesPlanMapper.selectTicketsByRegTime(regTime, timeInterval, sectionNo, doctorNo);
	}

	/**
	 *
	 * @return
	 */
	public List<SectionType> selectAllSectionTypes(){
		return sectiontypeDictionaryMapper.selectAllSectionTypes();
	}

	/**
	 *查询我的预约信息
	 * @param cardNo
	 * @param visitDate
	 * @return
	 */
	public List<OrderTicket> selectPreregedTickets(String cardNo, String visitDate){
		return doctorSchedulesPlanMapper.selectPreregedTickets(cardNo,visitDate);
	}

	/**
	 *
	 * @param planId
	 * @return
	 */
	public int updateStatusFlag(String planId){
		return doctorSchedulesPlanMapper.updateStatusFlag(planId);
	}

	/**
	 * 查询某医生下的预约病人信息
	 * @param doctorNo
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public List<PreregedPatient> selectPreregedPatients(String doctorNo,String startDate,String endDate){
		return  doctorSchedulesPlanMapper.selectPreregedPatient(doctorNo,startDate,endDate);
	}


	/**
	 * 根据主键更新号表计划表状态为锁号状态
	 * @param
	 * @return
	 */
	public int updateSchedulesPlanByPrimaryKey(String id, String statusId){
		DoctorSchedulesPlan record=new DoctorSchedulesPlan();
		Date ss=new Date();
		Date lockTime= DateUtil.parseDate(DateUtil.format(ss,"yyyy-MM-dd HH:mm:ss"));
		record.setId(id);
		record.setSourceStatus(statusId);
		record.setLockTime(lockTime);
		return doctorSchedulesPlanMapper.updateByPrimaryKeySelective(record);
	}

}
