package com.bonus.boot.manager.basic.controller;

import cn.hutool.json.JSONUtil;
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.annotation.LogAnnotation;
import com.bonus.boot.manager.manager.entity.R;
import com.bonus.boot.manager.manager.utils.AjaxRes;
import com.bonus.boot.manager.manager.utils.GlobalConst;
import com.bonus.boot.manager.manager.utils.UserUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

/**
 * 医院管理表控制层
 * @author tqzhang
 * @Data 2023/5/31 9:30
 */
@RestController
@RequestMapping("hospital")
public class HospitalController {
    /**
     * 服务对象
     */
    @Resource
    private HospitalService service;

    @Resource
    private HospitalDao dao;

    @LogAnnotation
    @PostMapping("getMsgContent")
    @ApiOperation(value = "医院管理-列表")
    public R getList(HospitalBean o){
        List<HospitalBean> list = service.getList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @RequestMapping(value = "addInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "医院管理-新增")
    public AjaxRes addInfo(@RequestBody HospitalBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.addInfo(bean); 
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("医院名称已存在");
            }
        }
        return  ar;
    }

    @RequestMapping(value = "updateInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "医院管理-修改")
    public AjaxRes updateInfo(@RequestBody HospitalBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.updateInfo(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            if (cb == 3){
                ar.setFailMsg("医院名称已存在");
            }
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
        }
        return  ar;
    }
    @RequestMapping(value = "getListById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "医院管理-查询单个医院")
    public AjaxRes getListById(String id){
        AjaxRes ar = new AjaxRes();
        HospitalBean hospitalBean = service.getListById(id);
        Map<String, Object> map = new HashMap<>();
        if(hospitalBean !=null){
            map.put("HospitalBean",hospitalBean);
        }
        ar.setSucceed(map,GlobalConst.DATA_SUCCEED);
        return ar;
    }


    @LogAnnotation
    @PostMapping("delById")
    @ApiOperation(value = "医院管理-删除")
    public AjaxRes delById(HospitalBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.delById(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

}

