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 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("/dzpz/dvalueConfig")
public class DvalueConfigController extends BaseController {
    private String prefix = "gxhpz";
    @Autowired
    private DvalueConfigService dvalueConfigService;
    @RequiresPermissions("gxhpz:dzpz:view")
    @GetMapping("/index")
    public String recipe()
    {
        return prefix + "/dvalueconfigList";
    }
    /**
     *
     * D值查询
     */
    @RequiresPermissions("gxhpz:dzpz:list")
    @PostMapping("/dvalueconfigList")
    @ResponseBody
    public TableDataInfo list() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = dvalueConfigService.findForList(pd);
        return  getDataTable(pageData);
    }
    /**
     * 新增页面
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("posts", 1);
        return prefix + "/dvalueconfigAdd";
    }
    /**
     * D值详情
     */
    @RequiresPermissions("gxhpz:dzpz:view")
    @GetMapping("/dvalueconfigDetail/{id}")
    public String drugInfo(@PathVariable("id") Long id, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("id",id);
        PageData pageData = dvalueConfigService.findForList(pd).get(0);
        mmap.putAll(pageData);
        return prefix + "/dvalueconfigDetail";
    }
    /**
     * D值新增保存
     */
    //@RequiresPermissions("server:serv:add")
    @Log(title = "D值新增", businessType = BusinessType.INSERT)
    @PostMapping("/savedvalueconfig")
    @ResponseBody
    public AjaxResult savedvalueconfig() throws Exception {
        PageData pd = this.getPageData();
        int result = dvalueConfigService.save(pd);
        if(result>0){
            return AjaxResult.success("成功");
        }if(result == -1){
            return AjaxResult.warn("D值编码已存在");
        }else {
            return AjaxResult.error("失败");
        }

    }
    @Log(title = "D值删除", 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 = dvalueConfigService.del(ids);
           return toAjax(integer);

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

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

    /**
     * 保存D值修改信息
     */
    @RequiresPermissions("gxhpz:dzpz:edit")
    @Log(title = "D值管理", businessType = BusinessType.UPDATE)
    @PostMapping("/dvalueconfigEdit")
    @ResponseBody
    public AjaxResult editSave() throws Exception
    {
        PageData pd = this.getPageData();
        Integer update = dvalueConfigService.update(pd);
        if(update!=1){
            return error("修改失败");
        }
        return toAjax(update);
    }
    /**
     * 药品D值配置-启用停用
     */
    @RequiresPermissions("gxhpz:dzpz:edit")
    @Log(title = "药品配置-启用停用", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus() throws Exception
    {
        String str="";
        PageData pd = this.getPageData();
        String status = (String) pd.get("status");
        if(status.equals("0")){
            str = "停用";

        }else {
            str = "启用";
        }
        String update = dvalueConfigService.changeStatus(pd);
        if(update.equals("error")){
            return AjaxResult.error(str+"失败");
        } else if(update.equals("有绑定药品不能删除!")){
           return AjaxResult.error(str+"失败"+update);
         }else {
            return AjaxResult.success(update);
        }

    }



    @PostMapping("/searchDvalue")
    @ResponseBody
    public AjaxResult searchDvalue() throws Exception
    {
        PageData pd = this.getPageData();
        String query = (String) pd.get("query");
        System.out.println(query);
        // 根据业务逻辑决定先尝试匹配 d_value_code 还是 dValueName
        pd.put("query", query);
        pd.put("d_value_code", query);
        pd.put("dValueName", query);
        try {
            Object result = dvalueConfigService.findOne(pd);
            if (result != null) {
                return AjaxResult.success(result);
            } else {
                return AjaxResult.warn("无数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询出错");
        }
    }
    @PostMapping("/createDVauleId")
    @ResponseBody
    public AjaxResult createDVauleId( ModelMap mmap) throws Exception
    {
        PageData pd = this.getPageData();
        String nameValue = (String) pd.get("nameValue");
        System.out.println("nameValue="+nameValue);
        int s5id= IdUtils.get5randomNumber();
        pd.put("createdBy",getLoginName());
        pd.put("createdTime", DateUtils.getTime());
        pd.put("d_value_code",s5id);
        pd.put("dValueName",nameValue);
        try {
            Object result = dvalueConfigService.save(pd);
            if (result != null) {
                mmap.put("dvauleid",s5id);
                mmap.put("dValueName",nameValue);
                return AjaxResult.success(mmap);
            } else {
                return AjaxResult.warn("无数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询出错");
        }
    }

}
