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.HospitalListService;
import com.bzd.system.service.gxhpz.PharmacistsService;
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/pharmacists")
public class PharmacistsController extends BaseController {
    private String prefix = "gxhpz";
    @Autowired
    private PharmacistsService pharmacistsService;
    @RequiresPermissions("gxhpz:pharmacists:view")
    @GetMapping("/index")
    public String recipe()
    {
        return prefix + "/pharmacistsList";
    }
    /**
     *
     * D值查询
     */
    @RequiresPermissions("gxhpz:pharmacists:list")
    @PostMapping("/pharmacistsList")
    @ResponseBody
    public TableDataInfo list() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = pharmacistsService.findForList(pd);
        return  getDataTable(pageData);
    }
    /**
     * 新增页面
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("posts", 1);
        return prefix + "/pharmacistsAdd";
    }
    /**
     * 审核药师新增
     */
    //@RequiresPermissions("server:serv:add")
    @Log(title = "审核药师新增", businessType = BusinessType.INSERT)
    @PostMapping("/savePharmacists")
    @ResponseBody
    public AjaxResult saveDpharmacists() throws Exception {
        PageData pd = this.getPageData();
        int result = pharmacistsService.save(pd);
        if(result>0){
            return AjaxResult.success("成功");
        }if(result == -1){
            return AjaxResult.warn("D值编码已存在");
        }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 = pharmacistsService.del(ids);
           return toAjax(integer);

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

    /**
     * 跳转到修改界面
     * @param id
     * @param mmap
     * @return
     * @throws Exception
     */
    @RequiresPermissions("gxhpz:pharmacists: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 = pharmacistsService.findForList(pd).get(0);
        mmap.putAll(pageData);
        return prefix + "/pharmacistsEdit";
    }

    /**
     * 审核药师修改
     */
    @RequiresPermissions("gxhpz:pharmacists:edit")
    @Log(title = "审核药师修改", businessType = BusinessType.UPDATE)
    @PostMapping("/pharmacistsEdit")
    @ResponseBody
    public AjaxResult editSave() throws Exception
    {
        PageData pd = this.getPageData();
        Integer update = pharmacistsService.update(pd);
        if(update!=1){
            return error("修改失败");
        }
        return toAjax(update);
    }

}
