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.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 java.util.List;

/**
 * 药事服务管理
 * creator wsp
 */
@Controller
@RequestMapping("/dtp/pmService")
public class PharmaceuticalServiceController extends BaseController {

    // 档案馆
    private String prefix_archives = "dtp/archives";

    // 随访任务
    private String prefix_followUp = "dtp/followUp";

    // 随访人员分配
    private String prefix_followUp_assign = "dtp/followUpAssign";

    //患者咨询
    private String prefix_patient_counseling = "dtp/patientCounseling";

    //患者咨询
    private String prefix_followUp_evaluation = "dtp/followUpEvaluation";

    @Autowired
    private PharmaceuticalService pharmaceuticalService;

    /**
     * 档案管理页
     *
     * @return
     */
    @RequiresPermissions("dtp:pmService:view")
    @GetMapping("/archives")
    public String archives() {
        return prefix_archives + "/archivesList";
    }

    /**
     * 档案管理数据查询
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:archivesList")
    @PostMapping("/archivesList")
    @ResponseBody
    public TableDataInfo archivesList() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = pharmaceuticalService.findArchivesList(pd);
        return getDataTable(pageData);
    }

    /**
     * 档案管理数据删除 根据id
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:remove")
    @Log(title = "档案管理删除", businessType = BusinessType.DELETE)
    @PostMapping("/archivesRemove")
    @ResponseBody
    public AjaxResult archivesRemove() throws Exception {
        PageData pd = this.getPageData();
        Integer integer = pharmaceuticalService.archivesRemove(pd);
        return toAjax(integer);
    }

    /**
     * 档案管理数据修改
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:edit")
    @GetMapping("/archivesEdit/{archivesId}")
    public String archivesView(@PathVariable("archivesId") Long archivesId, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("archivesId", archivesId);
        PageData pageData = pharmaceuticalService.findArchivesList(pd).get(0);
        mmap.putAll(pageData);
        return prefix_archives + "/archivesEdit";
    }

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

    /**
     * 随访任务页
     * @return
     */
    @RequiresPermissions("dtp:pmService:view")
    @GetMapping("/followUp")
    public String followUp() {
        return prefix_followUp + "/followUpList";
    }

    /**
     * 随访任务数据查询
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:archivesList")
    @PostMapping("/followUpList")
    @ResponseBody
    public TableDataInfo followUpList() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = pharmaceuticalService.findFollowUpList(pd);
        return getDataTable(pageData);
    }

    /**
     * 随访任务数据删除 根据id
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:remove")
    @Log(title = "档案管理删除", businessType = BusinessType.DELETE)
    @PostMapping("/followUpRemove")
    @ResponseBody
    public AjaxResult followUpRemove() throws Exception {
        PageData pd = this.getPageData();
        try {
            Integer deleteResult = pharmaceuticalService.followUpRemove(pd);
            if (deleteResult == 1) {
                // 删除成功
                return AjaxResult.success("删除成功");
            } else {
                // 删除失败
                logger.error("Failed to update archives with ID: {}", pd.get("id"));
                return AjaxResult.error("删除失败");
            }
        } catch (Exception e) {
            // 异常处理
            logger.error("Error occurred while updating archives with ID: {}, Exception: {}", pd.get("id"), e.getMessage(), e);
            return AjaxResult.error("系统异常：" + e.getMessage());
        }
    }

    /**
     * 随访任务数据修改
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:edit")
    @GetMapping("/followUpEdit/{followUpId}")
    public String followUpView(@PathVariable("followUpId") Long followUpId, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("id", followUpId);
        PageData pageData = pharmaceuticalService.findFollowUpList(pd).get(0);
        mmap.putAll(pageData);
        return prefix_followUp + "/followUpEdit";
    }

    /**
     * 保存随访任务数据修改
     */
    @RequiresPermissions("dtp:pmService:edit")
    @Log(title = "档案管理修改", businessType = BusinessType.UPDATE)
    @PostMapping("/followUpEdit")
    @ResponseBody
    public AjaxResult followUpEditSave() {
        PageData pd = this.getPageData();
        pd.put("up", "up");
        try {
            Integer updateResult = pharmaceuticalService.updateFollowUp(pd);
            if (updateResult == 1) {
                // 成功更新
                return AjaxResult.success("修改成功");
            } else {
                // 更新失败
                logger.error("Failed to update archives with ID: {}", pd.get("id"));
                return AjaxResult.error("修改失败");
            }
        } catch (Exception e) {
            // 异常处理
            logger.error("Error occurred while updating archives with ID: {}, Exception: {}", pd.get("id"), e.getMessage(), e);
            return AjaxResult.error("系统异常：" + e.getMessage());
        }
    }

    /**
     * 随访任务页
     * @return
     */
    @RequiresPermissions("dtp:pmService:view")
    @GetMapping("/followUpAssign")
    public String followUpAssign() {
        return prefix_followUp_assign + "/followUpAssignList";
    }

    /**
     * 随访任务数据查询
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:followUpAssignList")
    @PostMapping("/followUpAssignList")
    @ResponseBody
    public TableDataInfo followUpAssignList() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = pharmaceuticalService.findFollowUpAssignList(pd);
        return getDataTable(pageData);
    }

    /**
     * 随访任务数据删除 根据id
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:remove")
    @Log(title = "档案管理删除", businessType = BusinessType.DELETE)
    @PostMapping("/followUpAssignRemove")
    @ResponseBody
    public AjaxResult followUpAssignRemove() throws Exception {
        PageData pd = this.getPageData();
        try {
            Integer deleteResult = pharmaceuticalService.followUpAssignRemove(pd);
            if (deleteResult == 1) {
                // 删除成功
                return AjaxResult.success("删除成功");
            } else {
                // 删除失败
                logger.error("Failed to update archives with ID: {}", pd.get("id"));
                return AjaxResult.error("删除失败");
            }
        } catch (Exception e) {
            // 异常处理
            logger.error("Error occurred while updating archives with ID: {}, Exception: {}", pd.get("id"), e.getMessage(), e);
            return AjaxResult.error("系统异常：" + e.getMessage());
        }
    }

    /**
     * 随访任务数据修改
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:edit")
    @GetMapping("/followUpAssignEdit/{followUpAssignId}")
    public String followUpAssignView(@PathVariable("followUpAssignId") Long followUpAssignId, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("id", followUpAssignId);
        PageData pageData = pharmaceuticalService.findFollowUpAssignList(pd).get(0);
        mmap.putAll(pageData);
        return prefix_followUp_assign + "/followUpAssignEdit";
    }

    /**
     * 保存随访任务数据修改
     */
    @RequiresPermissions("dtp:pmService:edit")
    @Log(title = "档案管理修改", businessType = BusinessType.UPDATE)
    @PostMapping("/followUpAssignEdit")
    @ResponseBody
    public AjaxResult followUpAssignEditSave() {
        PageData pd = this.getPageData();
        pd.put("up", "up");
        try {
            Integer updateResult = pharmaceuticalService.updateFollowUpAssign(pd);
            if (updateResult == 1) {
                // 成功更新
                return AjaxResult.success("修改成功");
            } else {
                // 更新失败
                logger.error("Failed to update archives with ID: {}", pd.get("id"));
                return AjaxResult.error("修改失败");
            }
        } catch (Exception e) {
            // 异常处理
            logger.error("Error occurred while updating archives with ID: {}, Exception: {}", pd.get("id"), e.getMessage(), e);
            return AjaxResult.error("系统异常：" + e.getMessage());
        }
    }

    /**
     * 患者咨询 页面
     * @return
     */
    @RequiresPermissions("dtp:pmService:view")
    @GetMapping("/patientCounseling")
    public String patientCounseling() {
        return prefix_patient_counseling + "/patientCounselingList";
    }

    /**
     * 患者咨询数据查询
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:patientCounselingList")
    @PostMapping("/patientCounselingList")
    @ResponseBody
    public TableDataInfo patientCounselingList() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = pharmaceuticalService.findPatientCounselingList(pd);
        return getDataTable(pageData);
    }

    /**
     * 患者咨询数据删除 根据id
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:remove")
    @Log(title = "患者咨询删除", businessType = BusinessType.DELETE)
    @PostMapping("/patientCounselingRemove")
    @ResponseBody
    public AjaxResult patientCounselingRemove() throws Exception {
        PageData pd = this.getPageData();
        try {
            Integer deleteResult = pharmaceuticalService.patientCounselingRemove(pd);
            if (deleteResult == 1) {
                // 删除成功
                return AjaxResult.success("删除成功");
            } else {
                // 删除失败
                logger.error("Failed to update archives with ID: {}", pd.get("id"));
                return AjaxResult.error("删除失败");
            }
        } catch (Exception e) {
            // 异常处理
            logger.error("Error occurred while updating archives with ID: {}, Exception: {}", pd.get("id"), e.getMessage(), e);
            return AjaxResult.error("系统异常：" + e.getMessage());
        }
    }

    /**
     * 患者咨询数据修改
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:edit")
    @GetMapping("/patientCounselingEdit/{patientCounselingId}")
    public String patientCounselingView(@PathVariable("patientCounselingId") Long patientCounselingId, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("id", patientCounselingId);
        PageData pageData = pharmaceuticalService.findPatientCounselingList(pd).get(0);
        mmap.putAll(pageData);
        return prefix_patient_counseling + "/patientCounselingEdit";
    }

    /**
     * 保存患者咨询数据修改
     */
    @RequiresPermissions("dtp:pmService:edit")
    @Log(title = "档案管理修改", businessType = BusinessType.UPDATE)
    @PostMapping("/patientCounselingEdit")
    @ResponseBody
    public AjaxResult patientCounselingEditSave() {
        PageData pd = this.getPageData();
        pd.put("up", "up");
        try {
            Integer updateResult = pharmaceuticalService.updatePatientCounseling(pd);
            if (updateResult == 1) {
                // 成功更新
                return AjaxResult.success("修改成功");
            } else {
                // 更新失败
                logger.error("Failed to update archives with ID: {}", pd.get("id"));
                return AjaxResult.error("修改失败");
            }
        } catch (Exception e) {
            // 异常处理
            logger.error("Error occurred while updating archives with ID: {}, Exception: {}", pd.get("id"), e.getMessage(), e);
            return AjaxResult.error("系统异常：" + e.getMessage());
        }
    }


    /**
     * 随访满意度评价 页面
     * @return
     */
    @RequiresPermissions("dtp:pmService:view")
    @GetMapping("/followUpEvaluation")
    public String followUpEvaluation() {
        return prefix_followUp_evaluation + "/followUpEvaluationList";
    }

    /**
     * 随访满意度评价 数据查询
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:patientCounselingList")
    @PostMapping("/followUpEvaluationList")
    @ResponseBody
    public TableDataInfo followUpEvaluationList() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = pharmaceuticalService.findFollowUpEvaluationList(pd);
        return getDataTable(pageData);
    }

    /**
     * 随访满意度评价 数据删除 根据id
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:remove")
    @Log(title = "随访满意度评价删除", businessType = BusinessType.DELETE)
    @PostMapping("/followUpEvaluationRemove")
    @ResponseBody
    public AjaxResult followUpEvaluationRemove() throws Exception {
        PageData pd = this.getPageData();
        try {
            Integer deleteResult = pharmaceuticalService.followUpEvaluationRemove(pd);
            if (deleteResult == 1) {
                // 删除成功
                return AjaxResult.success("删除成功");
            } else {
                // 删除失败
                logger.error("Failed to update archives with ID: {}", pd.get("id"));
                return AjaxResult.error("删除失败");
            }
        } catch (Exception e) {
            // 异常处理
            logger.error("Error occurred while updating archives with ID: {}, Exception: {}", pd.get("id"), e.getMessage(), e);
            return AjaxResult.error("系统异常：" + e.getMessage());
        }
    }

    /**
     * 随访满意度评价数据修改
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("dtp:pmService:edit")
    @GetMapping("/followUpEvaluationEdit/{followUpEvaluationId}")
    public String followUpEvaluationView(@PathVariable("followUpEvaluationId") Long followUpEvaluationId, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("id", followUpEvaluationId);
        PageData pageData = pharmaceuticalService.findFollowUpEvaluationList(pd).get(0);
        mmap.putAll(pageData);
        return prefix_followUp_evaluation + "/followUpEvaluationEdit";
    }

    /**
     * 保存随访满意度评价数据修改
     */
    @RequiresPermissions("dtp:pmService:edit")
    @Log(title = "随访满意度评价修改", businessType = BusinessType.UPDATE)
    @PostMapping("/followUpEvaluationEdit")
    @ResponseBody
    public AjaxResult followUpEvaluationEditSave() {
        PageData pd = this.getPageData();
        pd.put("up", "up");
        try {
            Integer updateResult = pharmaceuticalService.updateFollowUpEvaluation(pd);
            if (updateResult == 1) {
                // 成功更新
                return AjaxResult.success("修改成功");
            } else {
                // 更新失败
                logger.error("Failed to update archives with ID: {}", pd.get("id"));
                return AjaxResult.error("修改失败");
            }
        } catch (Exception e) {
            // 异常处理
            logger.error("Error occurred while updating archives with ID: {}, Exception: {}", pd.get("id"), e.getMessage(), e);
            return AjaxResult.error("系统异常：" + e.getMessage());
        }
    }
}
