package com.bzd.web.controller.dtp;

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.DTPService;
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("/dtp/configInfo")
public class ConfigInfoController extends BaseController {
    private String prefix = "dtp/configInfo";
    @Autowired
    private DTPService dtpService;
    @RequiresPermissions("dtp:configInfo:view")
    @GetMapping("/storeTag")
    public String storeTag()
    {
        return prefix + "/storeTag";
    }
    @RequiresPermissions("dtp:configInfo:view")
    @GetMapping("/dtpHospital")
    public String dtpHospital()
    {
        return prefix + "/dtpHospital";
    }
    @RequiresPermissions("dtp:configInfo:view")
    @GetMapping("/drugList")
    public String drugList()
    {
        return prefix + "/drugList";
    }
    @RequiresPermissions("dtp:configInfo:view")
    @GetMapping("/AssociatedPrescription/{id}")
    public String AssociatedPrescription(@PathVariable("id") Long id, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("id",id);
        List<PageData> pageDataList = dtpService.findForList(pd);
        if(pageDataList.size()>0){
            PageData pageData = dtpService.findForList(pd).get(0);
            mmap.putAll(pageData);
            return prefix + "/AssociatedPrescription";
        }else{
            return prefix + "/AssociatedPrescription";
        }
    }
 /**配置信息查询**/
    /**
     * 一店一目标签
     */
    @RequiresPermissions("dtp:configInfo:list")
    @PostMapping("/storeTagList")
    @ResponseBody
    public TableDataInfo storeTagList() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = dtpService.findstoreTagList(pd);
        return  getDataTable(pageData);
    }
    /**
     *DTP医院查询
     */
    @RequiresPermissions("dtp:configInfo:list")
    @PostMapping("/dtpHospitalList")
    @ResponseBody
    public AjaxResult dtpHospitalList() throws Exception {
        PageData pd = this.getPageData();
        AjaxResult ajax = new AjaxResult();
        startPage();
        List<PageData> pageData = dtpService.findHospitalList(pd);
        ajax.put("code", 200);
        ajax.put("value", pageData);
        return  ajax;
    }
    /**
     * 药品名单
     */
    @RequiresPermissions("dtp:configInfo:list")
    @PostMapping("/drugListList")
    @ResponseBody
    public TableDataInfo drugListList() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = dtpService.findstoreTagList(pd);
        return  getDataTable(pageData);
    }
    /**
     * 新增配置信息页面
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("posts", 1);
        return prefix + "/add";
    }
    /**
     * 配置信息新增保存
     */
    //@RequiresPermissions("server:serv:add")
    @Log(title = "配置信息新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave() throws Exception {
        PageData pd = new PageData();
        pd = this.getPageData();

        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 = dtpService.deleteProductList(ids);
            return toAjax(integer);

        }
        return AjaxResult.success("请选择要删除的数据");
    }
    @RequiresPermissions("dtp:configInfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)throws Exception
    {
        PageData pd = this.getPageData();
        pd.put("id",id);
        PageData pageData = dtpService.findForList(pd).get(0);
        mmap.putAll(pageData);
        return prefix + "/edit";
    }

    /**
     * 保存处方登记修改信息
     */
    @RequiresPermissions("dtp:configInfo:edit")
    @Log(title = "处方登记管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave() throws Exception
    {
        PageData pd = this.getPageData();
        Integer update = dtpService.update(pd);
        if(update!=1){
            return error("修改失败");
        }
        return toAjax(dtpService.update(pd));
    }


}
