package com.bzd.web.controller.gxhpz;

import com.bzd.common.annotation.Log;
import com.bzd.common.config.dao.PageData;
import com.bzd.common.core.controller.BaseController;
import com.bzd.common.core.domain.AjaxResult;
import com.bzd.common.core.page.TableDataInfo;
import com.bzd.common.enums.BusinessType;
import com.bzd.common.utils.DateUtils;
import com.bzd.common.utils.StringUtils;
import com.bzd.common.utils.uuid.IdUtils;
import com.bzd.system.service.gxhpz.DvalueConfigService;
import com.bzd.system.service.gxhpz.HospitalListService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * D值配置管理
 * creator wsp
 */
@Controller
@RequestMapping("/gxhpz/hospital")
public class HospitalController extends BaseController {
    private String prefix = "gxhpz";
    @Autowired
    private HospitalListService hospitalListService;
    @RequiresPermissions("gxhpz:hospital:view")
    @GetMapping("/index")
    public String recipe()
    {
        return prefix + "/hospitalList";
    }
    /**
     *
     * D值查询
     */
    @RequiresPermissions("gxhpz:hospital:list")
    @PostMapping("/hospitalList")
    @ResponseBody
    public TableDataInfo list() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = hospitalListService.findForList(pd);
        return  getDataTable(pageData);
    }
    /**
     * 新增页面
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("posts", 1);
        return prefix + "/hospitalAdd";
    }
    /**
     * D值详情
     */
    @RequiresPermissions("gxhpz:hospital:view")
    @GetMapping("/hospitalDetail/{id}")
    public String drugInfo(@PathVariable("id") Long id, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("id",id);
        PageData pageData = hospitalListService.findForList(pd).get(0);
        mmap.putAll(pageData);
        return prefix + "/hospitalDetail";
    }
    /**
     * D值新增保存
     */
    //@RequiresPermissions("server:serv:add")
    @Log(title = "新增医院", businessType = BusinessType.INSERT)
    @PostMapping("/saveHospital")
    @ResponseBody
    public AjaxResult saveHospital() throws Exception {
        PageData pd = this.getPageData();
        int result = hospitalListService.save(pd);
        if(result>0){
            return AjaxResult.success("成功");
        }if(result == -1){
            return AjaxResult.warn("医院已存在");
        }else {
            return AjaxResult.error("失败");
        }

    }
    @Log(title = "删除医院", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove() throws Exception
    {
        PageData pd = new PageData();
        pd = this.getPageData();
       Object id = pd.get("ids");
       if(StringUtils.isNotNull(id)){
           String[] split = id.toString().split(",");
           List<String> ids = Arrays.asList(split);
           Integer integer = hospitalListService.del(ids);
           return toAjax(integer);

       }
        return AjaxResult.success("请选择要删除的医院");
    }

    /**
     * 查询医院信息并跳转到修改界面
     * @param id
     * @param mmap
     * @return
     * @throws Exception
     */
    @RequiresPermissions("gxhpz:hospital:edit")
    @GetMapping("/edit/{id}")
    public String view(@PathVariable("id") String id, ModelMap mmap)throws Exception
    {
        PageData pd = this.getPageData();
        pd.put("id",id);
        PageData pageData = hospitalListService.findForList(pd).get(0);
        mmap.putAll(pageData);
        return prefix + "/hospitalEdit";
    }

    /**
     * 修改医院信息
     */
    @RequiresPermissions("gxhpz:hospital:edit")
    @Log(title = "医院修改", businessType = BusinessType.UPDATE)
    @PostMapping("/hospitalEdit")
    @ResponseBody
    public AjaxResult editSave() throws Exception
    {
        PageData pd = this.getPageData();
        Integer update = hospitalListService.update(pd);
        if(update!=1){
            return error("修改失败");
        }
        return toAjax(update);
    }

}
