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.PharmaceuticalService;
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 com.bzd.system.service.dtp.SDtpYypzFollowUpSopService;

import java.util.List;

/**
*
* 随访SOP表 前端控制器Controller
* @author LiXiangFei
* @since 2024-10-15
*/
@Controller
@RequestMapping(value = "dtp/sdtpyypzfollowupsop")
public class SDtpYypzFollowUpSopController extends BaseController {

    // 页面跳转前缀
    private String prefix = "dtp";

    @Autowired
    private SDtpYypzFollowUpSopService sDtpYypzFollowUpSopService;

    /**
    * 随访SOP表 新增页面跳转
    *
    * @return
    */
    @RequiresPermissions("dtp:dtp:view")
    @GetMapping("/sDtpYypzFollowUpSopAdd")
    public String add() {
        return prefix + "/sfrw/SDtpYypzFollowUpSopAdd";
    }

    /**
    * 随访SOP表 新增
    *
    * @return
    */
    @Log(title = "随访SOP表新增", businessType = BusinessType.INSERT)
    @PostMapping("/sDtpYypzFollowUpSopAdd")
    @ResponseBody
    public AjaxResult addSave() throws Exception {
        PageData pd = this.getPageData();
        try {
            Integer result = sDtpYypzFollowUpSopService.addSDtpYypzFollowUpSop(pd);
            if (result == 1) {
                return AjaxResult.success("新增成功");
            } else {
                logger.error("Failed to update 表skccrkckreceiptinfo with ID: {}", pd.get("id"));
                return AjaxResult.error("新增失败");
            }
        } catch (Exception e) {
            logger.error("Error occurred while updating 表skccrkckreceiptinfo with ID: {}, Exception: {}", pd.get("id"), e.getMessage(), e);
            return AjaxResult.error("系统异常：" + e.getMessage());
        }
    }

    /**
    * 随访SOP表 查询页面跳转
    *
    * @return
    */
    @RequiresPermissions("dtp:dtp:view")
    @GetMapping("/sDtpYypzFollowUpSopView")
    public String sdtpyypzfollowupsopListView() {
        return prefix + "/sfrw/SDtpYypzFollowUpSopList";
    }

    /**
    * 随访SOP表 数据查询
    *
    * @return
    * @throws Exception
    */
    @RequiresPermissions("dtp:dtp:list")
    @PostMapping("/sDtpYypzFollowUpSopList")
    @ResponseBody
    public TableDataInfo sdtpyypzfollowupsopList() throws Exception {
    PageData pd = this.getPageData();
    startPage();
    List<PageData> pageData = sDtpYypzFollowUpSopService.findSDtpYypzFollowUpSopList(pd);
        return getDataTable(pageData);
    }

    /**
    * 随访SOP表 数据删除 根据id
    *
    * @return
    * @throws Exception
    */
    @RequiresPermissions("dtp:dtp:remove")
    @Log(title = "随访SOP表删除", businessType = BusinessType.DELETE)
    @PostMapping("/sDtpYypzFollowUpSopRemove")
    @ResponseBody
    public AjaxResult sdtpyypzfollowupsopRemove() throws Exception {
        PageData pd = this.getPageData();
        Integer integer = sDtpYypzFollowUpSopService.SDtpYypzFollowUpSopRemove(pd);
        return toAjax(integer);
    }

    /**
    * 随访SOP表 数据修改
    *
    * @return
    * @throws Exception
    */
    @RequiresPermissions("dtp:dtp:edit")
    @GetMapping("/sDtpYypzFollowUpSopEdit/{id}")
    public String sDtpYypzFollowUpSopEditPage(@PathVariable("id") Long id, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("id", id);
        PageData Template = sDtpYypzFollowUpSopService.selectTemplateById(pd);
        if(StringUtils.isNotNull(Template)){
            pd.put("templateId", Template.get("templateId"));
            List<PageData> drugsData = sDtpYypzFollowUpSopService.selectDrugsByTemplateId(pd);
            List<PageData> NodeList = sDtpYypzFollowUpSopService.selectNodeConfigByTemplateId(pd);
            mmap.put("drugs",drugsData);
            mmap.put("NodeList",NodeList);
        }
        mmap.put("Template",Template);

        if(null!=pd.getString("editTage")){
            return prefix+ "/sfrw/SDtpYypzFollowUpSopPageEdit";
        }
        return prefix+ "/sfrw/SDtpYypzFollowUpSopEdit";
    }

    /**
    * 随访SOP表保存修改的数据
    */
    @RequiresPermissions("dtp:dtp:edit")
    @Log(title = "随访SOP表修改", businessType = BusinessType.UPDATE)
    @PostMapping("/sDtpYypzFollowUpSopEdit")
    @ResponseBody
    public AjaxResult sdtpyypzfollowupsopEditSave() throws Exception {
        PageData pd = this.getPageData();
        pd.put("up", "up");
        try {
            Integer updateResult = sDtpYypzFollowUpSopService.updateSDtpYypzFollowUpSop(pd);
            if (updateResult == 1) {
                // 成功更新
                return AjaxResult.success("修改成功");
            } else {
                // 更新失败
                logger.error("Failed to update 表sdtpyypzfollowupsop with ID: {}", pd.get("id"));
                return AjaxResult.error("修改失败");
            }
        } catch (Exception e) {
            // 异常处理
            logger.error("Error occurred while updating 表sdtpyypzfollowupsop with ID: {}, Exception: {}", pd.get("id"), e.getMessage(), e);
            return AjaxResult.error("系统异常：" + e.getMessage());
        }
    }

    /**
     * 随访SOP状态 状态: 0已创建 1启用，2禁用 默认为已创建0
     */
    @RequiresPermissions("dtp:dtp:edit")
    @Log(title = "随访SOP表修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSopStatusByTemplateId")
    @ResponseBody
    public AjaxResult updateSopStatusByTemplateId() throws Exception {
        PageData pd = this.getPageData();
        try {
            Integer updateResult = sDtpYypzFollowUpSopService.updateSopStatusByTemplateId(pd);
            if (updateResult>0) {
                // 成功更新
                return AjaxResult.success("成功");
            } else {
                // 更新失败
                logger.error("Failed to update 表sdtpyypzfollowupsop with templateId: {}", pd.get("templateId"));
                return AjaxResult.error("失败");
            }
        } catch (Exception e) {
            // 异常处理
            logger.error("Error occurred while updating 表sdtpyypzfollowupsop with templateId: {}, Exception: {}", pd.get("templateId"), e.getMessage(), e);
            return AjaxResult.error("系统异常：" + e.getMessage());
        }
    }


}
