package com.saye.hrs.service.impl;

import com.saye.hrs.commons.date.DateDUtil;
import com.saye.hrs.mapper.PatientMapper;
import com.saye.hrs.service.PatientService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;

/**
 * (Patient)表服务实现类
 *
 * @author makejava
 * @since 2021-09-27 10:24:47
 */
@Service
@Transactional
public class PatientServiceImpl implements PatientService {
    @Resource
    private PatientMapper patientMapper;

    @Override
    public HashMap<Object, Object> findPatientById(HashMap<Object,Object> map) {
        return this.patientMapper.findPatientById(map);
    }

    @Override
    public  List<HashMap<Object, Object>> findPatientList(HashMap<Object,Object> map) {
        return this.patientMapper.findPatientList(map);
    }

    @Override
    public void insertPatient(HashMap<Object,Object> map) {
        this.patientMapper.insertPatient(map);
    }

    @Override
    public void updatePatient(HashMap<Object,Object> map) {
        this.patientMapper.updatePatient(map);
    }

    @Override
    public void deletePatientById(HashMap<Object,Object> map) {
        this.patientMapper.deletePatientById(map);
    }

    @Override
    public List<HashMap<Object, Object>> findPatientAccountPageList(HashMap<Object, Object> map) {
        return this.patientMapper.findPatientAccountPageList(map);
    }

    @Override
    public List<HashMap<Object, Object>> findPatientByOpenid(HashMap<Object, Object> map) {

        return this.patientMapper.findPatientByOpenid(map);
    }

    @Override
    public void patientBindWXUser(HashMap<Object, Object> map) {
        //先新增微信用户 已有的不添加
        this.patientMapper.insertWXUser(map);
        //添加或更新患者信息
        this.patientMapper.insertOrUpdatePatientByHis(map);
        map.put("create_time", DateDUtil.getTheCurrentTime());
        map.put("create_date", DateDUtil.getCurrentDate("yyyy-MM-dd"));
        //添加绑定日志
        map.put("status", "0");
        this.patientMapper.addBindCardLog(map);
        //微信用户绑定患者
        this.patientMapper.patientBindWXUser(map);
        //如果是补卡  重新绑定卡号时删除原来绑定的卡号
        this.patientMapper.delCardNotBind(map); 
    }

    @Override
    public void zyPatientBindWXUser(HashMap<Object, Object> map) {
        //先新增微信用户 已有的不添加
        this.patientMapper.insertWXUser(map);
        //添加或更新患者信息
        this.patientMapper.insertOrUpdateZyPatientByHis(map);
        map.put("create_time", DateDUtil.getTheCurrentTime());
        map.put("create_date", DateDUtil.getCurrentDate("yyyy-MM-dd"));
        //添加绑定日志
        map.put("status", "0");

        this.patientMapper.addZyBindCardLog(map);
        //微信用户绑定患者
        this.patientMapper.zyPatientBindWXUser(map);
        //如果是补卡  重新绑定卡号时删除原来绑定的卡号 此处进行了删除 绑定表里删除没有
        this.patientMapper.delZyCardNotBind(map);
    }

    @Override
    public void setPatientTypeByOpenid(HashMap<Object, Object> map) {
        //先将其它设为非默认 再将指定患者设为默认
        this.patientMapper.setOtherPatientNotDefault(map);
        this.patientMapper.setPatientDefault(map);
    }

    @Override
    public void patientUnbindWXUser(HashMap<Object, Object> map) {
        //解除绑定
        this.patientMapper.deleteWXUserPatnent(map);
        //添加解除绑定日志
        map.put("create_time", DateDUtil.getTheCurrentTime());
        map.put("create_date", DateDUtil.getCurrentDate("yyyy-MM-dd"));
        map.put("status", "1");
        map.put("cardno", map.get("medical_card"));
        this.patientMapper.addBindCardLog(map);
        //判断解除绑定后是不是没有默认选择 没有就默认一个
        List<HashMap<Object,Object>> list = this.patientMapper.findPatientByOpenid(map);
        if(!list.isEmpty()){
            boolean b = true;
            for(HashMap<Object,Object> m:list){
                String type = (String) m.get("TYPE");
                if("0".equals(type)){
                    b = false;
                }
            }
            if(b){
                map.put("medical_card",list.get(0).get("MEDICAL_CARD"));
                this.patientMapper.setPatientDefault(map);
            }
        }
    }

    @Override
    public void updatePatientByOpenid(HashMap<Object, Object> map) {
        this.patientMapper.updatePatientByOpenid(map);
        //
        this.patientMapper.setOtherPatientNotDefault(map);
        this.patientMapper.setPatientDefault(map);
    }

	@Override
	public void syncAccountRecord(List<HashMap<Object,Object>> list) {
		for(HashMap map:list) {
			this.patientMapper.addAccount(map);
		}
	}

	@Override
	public List<HashMap<Object, Object>> findRefundList(HashMap<Object, Object> map) {
		
		return this.patientMapper.findRefundList(map);
	}

	@Override
	public List<HashMap<Object, Object>> findPayRequestList(HashMap<Object, Object> map) {
		// TODO Auto-generated method stub
		return this.patientMapper.findPayRequestList(map);
	}

	@Override
	public List<HashMap<Object, Object>> findBindCardLogList(HashMap<Object, Object> map) {
		
		return this.patientMapper.findBindCardLogList(map);
	}

    @Override
    public List<HashMap<Object, Object>> getZyPatientByOpenid(HashMap<Object, Object> map) {
        return this.patientMapper.getZyPatientByOpenid(map);
    }

    @Override
    public List<HashMap<Object, Object>> findZyPatientAccountPageList(HashMap<Object, Object> map) {
        return this.patientMapper.findZyPatientAccountPageList(map);
    }
}
