package com.yihu.wlyy.service.app.sign;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import com.yihu.wlyy.entity.patient.SignFamily;
import com.yihu.wlyy.repository.patient.SignFamilyDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.yihu.wlyy.entity.patient.SignWeb;
import com.yihu.wlyy.repository.patient.SignWebDao;
import com.yihu.wlyy.service.BaseService;

/**
 * 网络签约业务处理类
 *
 * @author George
 */
@Component
@Transactional
public class SignWebService extends BaseService {

    @Autowired
    private SignWebDao signWebDao;
    @Autowired
    private SignFamilyDao signFamilyDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 根据医生代码及签约状态编码 获取该医生签约患者的信息列表
     *
     * @param doctor
     * @param status
     * @param doctorType
     * @return
     */
    public List<Map<String,Object>> getSignWebByDoctor(String doctor, int status,int doctorType) {
        // 查询语句
        String sql = "select " +
                     "    a.code " +
                     "    ,a.doctor " +
                     "    ,b.code  as  patient" +
                     "    ,b.name " +
                     "    ,b.province_name  as provinceName" +
                     "    ,b.city_name  as cityName" +
                     "    ,b.town_name  as townName" +
                     "    ,b.address " +
                     "    ,b.photo " +
                     "    ,a.status " +
                     "    ,a.id " +
                     "    ,a.apply_date as applyDate" +
                     "    ,a.reason " +
                     "    ,b.street_name as streetName" +
                     "    ,b.sex" +
                     " from " +
                     " ( select * from wlyy_sign_family where " + (doctorType == 2 ?"doctor" : "doctor_health") + " = ? and status = ? and type = 2 order by begin desc ) a " +
                     " ,wlyy_patient b " +
                     " where a.patient = b.code  order by a.begin desc ";
        // 已解约查询语句
        String surrSql = "select " +
                "    a.code " +
                "    ,a.doctor " +
                "    ,b.code  as  patient" +
                "    ,b.name " +
                "    ,b.province_name  as provinceName" +
                "    ,b.city_name  as cityName" +
                "    ,b.town_name  as townName" +
                "    ,b.address " +
                "    ,b.photo " +
                "    ,a.status " +
                "    ,a.id " +
                "    ,a.apply_date as applyDate" +
                "    ,a.reason " +
                "    ,b.street_name as streetName" +
                "    ,b.sex" +
                " from " +
                " ( select * from wlyy_sign_family where " + (doctorType == 2 ?"doctor" : "doctor_health") + " = ? and ( status = ? or status = ? ) and type = 2 order by begin desc ) a " +
                " ,wlyy_patient b " +
                " where a.patient = b.code  order by a.begin desc ";

        // 分页信息
        //PageRequest pageRequest = new PageRequest(page - 1, pageSize);
        //1:待签约 2, 待解约 3 已签约,4已经解约
        Page<Object> list = null;
        List<Map<String,Object>> patients = null;
        switch (status) {
            case 1:// 待签约
                //list = signFamilyDao.findToBeSignSignWebByDoctor_0(doctor,0, pageRequest);
                patients = jdbcTemplate.queryForList(sql,new Object[]{doctor,0});
                break;
            case 2:// 待解约
                //list = signFamilyDao.findToBeSignSignWebByDoctor(doctor,doctorType,2, pageRequest);
                patients = jdbcTemplate.queryForList(sql,new Object[]{doctor,2});
                break;
            case 3:// 已签约
                //list = signFamilyDao.findToBeSignSignWebByDoctor(doctor,doctorType,1, pageRequest);
                patients = jdbcTemplate.queryForList(sql,new Object[]{doctor,1});
                break;
            case 4:// 4已经解约
                //list = signFamilyDao.findToBeSignSignWebByDoctor_34(doctor,doctorType, pageRequest);
                patients = jdbcTemplate.queryForList(surrSql,new Object[]{doctor,-3,-4});
                break;
        }

        return patients;
    }

    /**
     * 根据代码查找签约信息明细
     *
     * @param code
     * @return
     */
    public SignWeb getSignWebByCode(String code) {
        return signWebDao.findByCode(code);
    }

    /**
     * 更新签约信息
     *
     * @return
     */
    public boolean saveSignWeb(SignWeb signWeb) {
        if (StringUtils.isEmpty(signWeb.getCode())) {
            signWeb.setCode(getCode());
            // 计算签约 月数
            if (signWeb.getBeginDate() != null && signWeb.getEndDate() != null) {
                Calendar startDate = Calendar.getInstance();
                Calendar endDate = Calendar.getInstance();
                startDate.setTime(signWeb.getBeginDate());
                endDate.setTime(signWeb.getEndDate());

                int startYear = startDate.get(Calendar.YEAR);
                int startMonth = startDate.get(Calendar.MONTH);

                int endYear = endDate.get(Calendar.YEAR);
                int endMonth = endDate.get(Calendar.MONTH);

                int result = Math.abs(endMonth - startMonth);
                if (startYear != endYear) {
                    result = 12 * (endYear - startYear) + result;
                }
                signWeb.setMonths(result);
            }
        }
        signWebDao.save(signWeb);
        return true;
    }

    /**
     * 根据患者Code 获取患者当前签约的医生信息
     *
     * @param patient
     * @return
     */
    public SignWeb getSignWebByPatient(String patient) {
        return signWebDao.findSignWebByPatient(patient);
    }

    /**
     * 根据患者Code 查询条件 以及签约状态获取签约信息
     *
     * @param patient 患者Code
     * @param status  签约状态
     * @return
     */
    public Page<Object> getSignInfoByPatient(String patient, int status, long id, int pageSize) {
        if (pageSize <= 0) {
            pageSize = 10;
        }
        // 分页信息
        PageRequest pageRequest = new PageRequest(0, pageSize);

        Page<Object> listInfo = null;
        if (id > 0) {
            switch (status) {
                case 0:// 待签约
                    listInfo = signWebDao.findToBedSignInfoByPatient(patient, id, pageRequest);
                    break;
                case 1:// 已解约
                    listInfo = signWebDao.findCancelledSignInfoByPatient(patient, id, pageRequest);
                    break;
                case 2:// 已签约
                    listInfo = signWebDao.findSignedSignInfoByPatient(patient, id, pageRequest);
                    break;
                case 3:// 待解约
                    listInfo = signWebDao.findToCancelSignInfoByPatient(patient, id, pageRequest);
                    break;
                default:
                    listInfo = signWebDao.findSignInfoByPatient(patient, id, pageRequest);
            }
        } else {
            switch (status) {
                case 0:// 待签约
                    listInfo = signWebDao.findToBedSignInfoByPatient(patient, pageRequest);
                    break;
                case 1:// 已解约
                    listInfo = signWebDao.findCancelledSignInfoByPatient(patient, pageRequest);
                    break;
                case 2:// 已签约
                    listInfo = signWebDao.findSignedSignInfoByPatient(patient, pageRequest);
                    break;
                case 3:// 待解约
                    listInfo = signWebDao.findToCancelSignInfoByPatient(patient, pageRequest);
                    break;
                default:
                    listInfo = signWebDao.findSignInfoByPatient(patient, pageRequest);
            }
        }
        return listInfo;
    }

    /**
     * 获取已签约信息
     *
     * @param patient
     * @return
     */
    public List<Object> getSignInfoByStatus(String patient) {
        return signWebDao.findSignedSignInfoBystatus(patient);
    }

    /**
     * 通过身份证获取签约信息
     */
    public List<SignFamily> getSignInfoByIdcard(String idcard) {
        return signFamilyDao.findAllByIdcard(idcard);
    }
}
