package com.bonus.boot.manager.basic.service.impl;


import com.bonus.boot.manager.basic.dao.AppLoginAuditDao;
import com.bonus.boot.manager.basic.dao.HospitalDao;
import com.bonus.boot.manager.basic.entity.AppLoginAudit;
import com.bonus.boot.manager.basic.entity.HospitalBean;
import com.bonus.boot.manager.basic.service.AppLoginAuditService;
import com.bonus.boot.manager.basic.service.HospitalService;
import com.bonus.boot.manager.manager.dao.UtilDao;
import com.bonus.boot.manager.manager.utils.StringHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

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

/**
 * APP登录记录表(AppLoginAudit)表服务实现类
 *
 * @author makejava
 * @since 2022-12-12 13:57:46
 */
@Service("hospitalService")
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private HospitalDao dao;
    @Resource
    private UtilDao utilDao;
    @Override
    public List<HospitalBean> getList(HospitalBean o) {
        return dao.getList(o);
    }

    @Override
    public int addInfo(HospitalBean bean) {
        if (StringHelper.isEmpty(bean.getOnLine())){
            bean.setOnLine("");
        }
        int num = utilDao.determineIfExists("pm_base_hospital","hospital",bean.getName(),"");
        if(num > 0){
            num = 3;
        }else {
            num = dao.addInfo(bean);
        }
        return num;
    }

    @Override
    public int updateInfo(HospitalBean bean) {
        if (StringHelper.isEmpty(bean.getOnLine())){
            bean.setOnLine("");
        }
        int num = utilDao.determineIfExists("pm_base_hospital","hospital",bean.getName(),bean.getId()+"");
        if(num > 0){
            num = 3;
        }else {
            num = dao.updateInfo(bean);
        }
        return num;
    }

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

    @Override
    @Transactional
    public int delById(HospitalBean bean) {
        int num = 0;
        num = dao.delById(bean);
        if (num == 0){
            throw new RuntimeException("删除医院失败");
        }
        if (num > 0){
            int code = dao.selectPBCById(bean);
            if (code > 0){
                num =  dao.delPBCache(bean);
                if (num == 0){
                    throw new RuntimeException("删除医院-预约体检失败");
                }
            }
        }
        return num;
    }
}
