package com.bonus.boot.manager.health.service.Impl;

import com.bonus.boot.manager.basic.entity.HospitalBean;
import com.bonus.boot.manager.health.dao.AppontdisposDao;
import com.bonus.boot.manager.health.entity.PmBaseHospLimit;
import com.bonus.boot.manager.health.service.AppontdisposService;
import com.bonus.boot.manager.manager.model.SysUser;
import com.bonus.boot.manager.manager.utils.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("AppontdisposService")
public class AppontdisposServiceImpl implements AppontdisposService {
    @Resource
    private AppontdisposDao dao;

    @Override
    public List<PmBaseHospLimit> getMsgList(PmBaseHospLimit o) {
//        o.setHospitalId(UserUtil.getLoginUser().getHospitalId());
        o.setPhone(UserUtil.getLoginUser().getPhone());
        String hospitalId = dao.getHospitalId(o);
        o.setHospitalId(hospitalId);
        return dao.getMsgList(o);
    }

    @Override
    public int addInfo(PmBaseHospLimit bean) throws Exception {
        String hospiral =UserUtil.getLoginUser().getHospitalId();
        String id =UserUtil.getLoginUser().getId()+"";
        bean.setHospital(hospiral);
        bean.setOptionId(id);
        int team = 0;
        //判断具体的开始和结束之间的时间
        List<String> getDate = findEveryDay(bean.getBusinessStart(),bean.getBusinessEnd());
        for(int i=0;i<getDate.size();i++){
            //判断是否存在，如果存在进行修改，不存在进行插入操作
            bean.setStartDate(getDate.get(i));
            int count  =dao.findByhospid(bean);
            //插入数据库中
             if(count>0){
                 team= dao.updateLimit(bean);
             }else{
                 team = dao.saveInfo(bean);
             }

        }
        return team;
    }

    @Override
    public int updateInfo(PmBaseHospLimit bean) throws Exception {
        int num =0;
        num = dao.updateInfo(bean);
        return num;
    }


    @Override
    @Transactional
    public int delById(PmBaseHospLimit bean) {
        int num = 0;
        num = dao.delById(bean);
        if (num == 0){
            throw new RuntimeException("删除医院失败");
        }
        return num;
    }

    @Override
    public PmBaseHospLimit getListById(String id) {
        return dao.getListById(id);
    }


    /**
     * 传入两个时间范围，返回这两个时间范围内的所有日期，并保存在一个集合中
     *
     * @param beginTime
     * @param endTime
     * @return
     * @throws Exception
     */
    public static List<String> findEveryDay(String beginTime, String endTime)
            throws Exception {
        //创建一个放所有日期的集合
        List<String> dates = new ArrayList();
        //创建时间解析对象规定解析格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //将传入的时间解析成Date类型,相当于格式化
        Date dBegin = sdf.parse(beginTime);
        Date dEnd = sdf.parse(endTime);
        //将格式化后的第一天添加进集合
        dates.add(sdf.format(dBegin));
        //使用本地的时区和区域获取日历
        Calendar calBegin = Calendar.getInstance();
        //传入起始时间将此日历设置为起始日历
        calBegin.setTime(dBegin);
        //判断结束日期前一天是否在起始日历的日期之后
        while (dEnd.after(calBegin.getTime())) {
            //根据日历的规则:月份中的每一天，为起始日历加一天
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            //得到的每一天就添加进集合
            dates.add(sdf.format(calBegin.getTime()));
            //如果当前的起始日历超过结束日期后,就结束循环
        }
        return dates;
    }
}
