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.poi.ExcelUtil;
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.List;

/**
 * 处方登记管理
 * creator wsp
 */
@Controller
@RequestMapping("/dtp/recipe")
public class RecipeRegisterController extends BaseController {

    private String prefix = "dtp/recipe";
    private String prefix_archives = "dtp/archives";

    @Autowired
    private DTPService dtpService;


    @RequiresPermissions("dtp:recipe:view")
    @GetMapping()
    public String recipe()
    {
        return prefix + "/recipe";
    }

/**
 *
 * 处方登记查询
 */
    @RequiresPermissions("dtp:recipe:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list() throws Exception {
        PageData pd = this.getPageData();

        startPage();

        List<PageData> pageData = dtpService.findForList(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();
        if (!dtpService.checkPatientNameAndPhoneUnique(pd))
        {
            return error("处方登记新增： '" + pd.getString("patientName") +"+"+ pd.getString("patientPhone")+"'失败，患者姓名或电话已存在");
        }
        Integer integer = dtpService.save(pd);
        return toAjax(integer);
    }

    @Log(title = "处方登记删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove() throws Exception
    {
        PageData pd = new PageData();
        pd = this.getPageData();
        Integer integer = dtpService.del(pd);
        return toAjax(integer);
    }

    @RequiresPermissions("dtp:recipe:edit")
    @GetMapping("/edit/{id}")
    public String view(@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:recipe: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));
    }
    /**
     *
     * 校验患者姓名和手机是否存在
     */
    @RequiresPermissions("dtp:recipe:checkPatientNameUnique")
    @PostMapping("/checkPatientNameAndPhoneUnique")
    @ResponseBody
    public boolean checkPatientNameAndPhoneUnique() throws Exception {
        PageData pd = this.getPageData();
        return  dtpService.checkPatientNameAndPhoneUnique(pd);
    }


    @Log(title = "处方信息", businessType = BusinessType.EXPORT)
    @RequiresPermissions("dtp:recipe:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export()throws Exception
    {
        PageData pd = this.getPageData();
        List<PageData> pageData= dtpService.findForList(pd);
        ExcelUtil<PageData> util = new ExcelUtil<>(PageData.class);
        return util.exportExcel(pageData, "处方数据");
    }

}
