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.StringUtils;
import com.bzd.system.service.gxhpz.DrugConfigService;
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;

/**
 * 药品配置管理
 * creator wsp
 */
@Controller
@RequestMapping("/yppz/drugConfig")
public class DrugConfigController extends BaseController {
    private String prefix = "gxhpz";
    @Autowired
    private DrugConfigService drugConfigService;
    @RequiresPermissions("gxhpz:yppz:view")
    @GetMapping("/index")
    public String recipe()
    {
        return prefix + "/drugconfigList";
    }
    /**
     *
     * 查询
     */
    @RequiresPermissions("gxhpz:yppz:list")
    @PostMapping("/drugconfigList")
    @ResponseBody
    public TableDataInfo list() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = drugConfigService.findForList(pd);
        return  getDataTable(pageData);
    }
    /**
     * 新增页面
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("posts", 1);
        return prefix + "/drugconfigAdd";
    }
    /**
     * 详情
     */
    @RequiresPermissions("dtp:yppz:view")
    @GetMapping("/drugConfigDetail/{id}")
    public String drugConfigDetail(@PathVariable("id") Long id, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("id",id);
        PageData pageData = drugConfigService.findForList(pd).get(0);
        mmap.putAll(pageData);
        return prefix + "/drugconfigDetail";
    }
    /**
     * 查询药品库s_gxhpz_product_info
     */
    @RequiresPermissions("dtp:yppz:view")
    @PostMapping("/searchDrugs")
    @ResponseBody
    public AjaxResult searchDrugs() throws Exception {
        PageData pd = this.getPageData();
       String query = (String) pd.get("query");
        if(StringUtils.isNull(query)){
            return AjaxResult.success();
        }
        List<PageData> pds =drugConfigService.selectproductByCodeAndName(pd);
            return  AjaxResult.success(pds);
    }
    @RequiresPermissions("dtp:yppz:view")
    @PostMapping("/searchDrugsObject")
    @ResponseBody
    public AjaxResult searchDrugsObject() throws Exception {
        PageData pd = this.getPageData();
        String query = (String) pd.get("query");
        if(StringUtils.isNull(query)){
            return AjaxResult.success();
        }
        PageData pdo =drugConfigService.selectOneproductByCodeAndName(pd);
        return  AjaxResult.success(pdo);
    }

    /**
     * 药品新增保存
     */
    @RequiresPermissions("gxhpz:yppz:add")
    @Log(title = "药品新增", businessType = BusinessType.INSERT)
    @PostMapping("/savedrugConfig")
    @ResponseBody
    public AjaxResult savedrugConfig() throws Exception {
        PageData pd = this.getPageData();
        Object pds = drugConfigService.findOne(pd);
        if(pds !=null){
            return AjaxResult.error("药品编码已存在，药品重复");
        }
        int result = drugConfigService.save(pd);
        if(result>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("失败");
        }

    }
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @RequiresPermissions("gxhpz:yppz:remove")
    @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 = drugConfigService.del(ids);
            return toAjax(integer);

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

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

    /**
     * 保存修改信息
     */
    @RequiresPermissions("gxhpz:yppz:edit")
    @Log(title = "药品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editDrugConfig")
    @ResponseBody
    public AjaxResult editSave() throws Exception
    {
        PageData pd = this.getPageData();
        Integer update = drugConfigService.update(pd);
        if(update!=1){
            return  AjaxResult.error("失败");
        }else {
            return AjaxResult.success("成功");
        }
    }
    /**
     * 药品配置-启用停用
     */
    @RequiresPermissions("gxhpz:yppz: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 = "启用";
       }
        Integer update = drugConfigService.changeStatus(pd);
        if(update!=1){
            return AjaxResult.error(str+"失败");
        }else {
            return AjaxResult.success(str+"成功");
        }

    }

    /**
     * 药品配置-启用停用
     */
    @RequiresPermissions("gxhpz:yppz:edit")
    @Log(title = "药品配置-启用停用", businessType = BusinessType.UPDATE)
    @PostMapping("/changedvalueStatus")
    @ResponseBody
    public AjaxResult changedvalueStatus() throws Exception
    {
        String str="";
        PageData pd = this.getPageData();
        String status = (String) pd.get("status");
        if(status.equals("0")){
            str = "停用";

        }else {
            str = "启用";
        }
        Integer update = drugConfigService.changeStatus(pd);
        if(update!=1){
            return AjaxResult.error(str+"失败");
        }else {
            return AjaxResult.success(str+"成功");
        }

    }


    /**
     * 跳转到关联D值界面
     * @param id
     * @param mmap
     * @return
     * @throws Exception
     */
    @RequiresPermissions("gxhpz:yppz:edit")
    @GetMapping("/toCorrelation/{id}")
    public String toCorrelation(@PathVariable("id") String id, ModelMap mmap)throws Exception
    {
        PageData pd = this.getPageData();
        pd.put("id",id);
        PageData pageData = drugConfigService.findForList(pd).get(0);
        mmap.put("drugData",pageData);
        mmap.putAll(pageData);
        System.out.println("drugData"+pageData);
        return prefix + "/addRepurchasedGoods";
    }
    @RequiresPermissions("gxhpz:yppz:edit")
    @GetMapping("/toCorrelationAdd")
    public String toCorrelationAdd(ModelMap mmap)throws Exception
    {
        mmap.put("add",1);
        return prefix + "/addRepurchasedGoods";
    }

    /**
     * 保存D值关联信息
     */
    @RequiresPermissions("gxhpz:yppz:edit")
    @Log(title = "D值关联信息", businessType = BusinessType.UPDATE)
    @PostMapping("/toCorrelationSave")
    @ResponseBody
    public AjaxResult toCorrelationSave() throws Exception
    {
        PageData pd = this.getPageData();
        Integer update = drugConfigService.update(pd);
        if(update!=1){
            return error("修改失败");
        }
        return toAjax(update);
    }
    /**
     * 药品信息页面
     */
    @RequiresPermissions("dtp:yppz:view")
    @GetMapping("/allDrugsInfo")
    public String allDrugsInfo(ModelMap mmap)
    {
        mmap.put("posts", 1);
        return prefix + "/allDrugsInfo";
    }
    /**
     * 药品信息页面
     */
    @RequiresPermissions("dtp:yppz:view")
    @GetMapping("/getAllProduct")
    public String getAllProduct(ModelMap mmap)
    {
        mmap.put("posts", 1);
        return prefix + "/allProduct";
    }
    /**配置药**/
    @RequiresPermissions("gxhpz:yppz:list")
    @PostMapping("/getAllDrugs")
    @ResponseBody
    public TableDataInfo getAllDrugs() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = drugConfigService.getAllDrugs(pd);
        return  getDataTable(pageData);
    }
    /**配置药**/
    @RequiresPermissions("gxhpz:yppz:list")
    @PostMapping("/getAllProductInfo")
    @ResponseBody
    public TableDataInfo getAllProductInfo() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = drugConfigService.getAllProductInfo(pd);
        return  getDataTable(pageData);
    }

    /**
     * D值关联-启用停用
     */
    @RequiresPermissions("gxhpz:dzpz:edit")
    @Log(title = "D值关联-启用停用", businessType = BusinessType.UPDATE)
    @PostMapping("/changesdvalueStatus")
    @ResponseBody
    public AjaxResult changesdvalueStatus() throws Exception{
        String str="";
        PageData pd = this.getPageData();
        String dvalueStatus = (String) pd.get("dvalueStatus");
        if(dvalueStatus.equals("0")){
            str = "停止";

        }else {
            str = "启用";
        }
        Integer update = drugConfigService.changedvalueStatus(pd);
        if(update!=1){
            return AjaxResult.error(str+"失败");
        }else {
            return AjaxResult.success(str+"成功");
        }

    }
}
