package com.fs.app.controller;


import com.fs.app.utils.JwtUtils;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.his.domain.FsPrescribeDataScrm;
import com.fs.his.dto.FsPrescribeDataDoctorQueryDto;
import com.fs.his.dto.FsPrescribeDataDrugDoctorQueryDto;
import com.fs.his.service.IFsPrescribeDataScrmService;
import com.fs.qw.vo.FsPrescribeDataScrmVO;
import com.github.pagehelper.PageHelper;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 处方表(SCRM)Controller
 *
 * @author ruoyi
 * @date 2026-06-11
 */
@RestController
@RequestMapping("/his/prescribeDataScrm")
public class FsPrescribeDataScrmController extends BaseController
{
    @Autowired
    private IFsPrescribeDataScrmService fsPrescribeScrmService;

    @Autowired
    JwtUtils jwtUtils;

    /**
     * 查询处方表(SCRM)列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FsPrescribeDataScrm fsPrescribeDataScrm)
    {
        startPage();
        List<FsPrescribeDataScrm> list = fsPrescribeScrmService.selectFsPrescribeDataScrmList(fsPrescribeDataScrm);
        return getDataTable(list);
    }

    /**
     * 导出处方表(SCRM)列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsPrescribeDataScrm fsPrescribeDataScrm) throws IOException {
        List<FsPrescribeDataScrm> list = fsPrescribeScrmService.selectFsPrescribeDataScrmList(fsPrescribeDataScrm);
        ExcelUtil<FsPrescribeDataScrm> util = new ExcelUtil<FsPrescribeDataScrm>(FsPrescribeDataScrm.class);
        util.exportExcel(response, list, "处方表(SCRM)数据");
    }

    /**
     * 获取处方表(SCRM)详细信息
     */
    @GetMapping(value = "/{prescribeId}")
    public AjaxResult getInfo(@PathVariable("prescribeId") Long prescribeId)
    {
        return AjaxResult.success(fsPrescribeScrmService.selectFsPrescribeDataScrmByPrescribeId(prescribeId));
    }

    /**
     * 新增处方表(SCRM)
     */
    @PostMapping
    public AjaxResult add(@RequestBody FsPrescribeDataScrm fsPrescribeDataScrm)
    {
        return toAjax(fsPrescribeScrmService.insertFsPrescribeDataScrm(fsPrescribeDataScrm));
    }

    /**
     * 修改处方表(SCRM)
     */
    @PutMapping
    public AjaxResult edit(@RequestBody FsPrescribeDataScrm fsPrescribeDataScrm)
    {
        return toAjax(fsPrescribeScrmService.updateFsPrescribeDataScrm(fsPrescribeDataScrm));
    }

    /**
     * 删除处方表(SCRM)
     */
    @DeleteMapping("/{prescribeIds}")
    public AjaxResult remove(@PathVariable Long[] prescribeIds)
    {
        return toAjax(fsPrescribeScrmService.deleteFsPrescribeDataScrmByPrescribeIds(prescribeIds));
    }
    /**
     * 医生---查询待开方商城处方列表
     * */
    @GetMapping("/waitOpenPrescribeList")
    public TableDataInfo waitOpenPrescribeList(FsPrescribeDataDoctorQueryDto queryDto){
        String doctorId = getDoctorId();
        if (StringUtils.isBlank(doctorId)){
            throw new CustomException("登录已过期，请重新登录");
        }
        queryDto.setDoctorId(Long.valueOf(doctorId));
        PageHelper.startPage(queryDto.getPageNum(),queryDto.getPageSize());
        List<FsPrescribeDataScrmVO> list=fsPrescribeScrmService.waitOpenPrescribeList(queryDto);
        return getDataTable(list);
    }

    /**
     * 药师---查询待审核商城处方列表
     * */
    @GetMapping("/pendingStorePrescribeList")
    public TableDataInfo pendingStorePrescribeList(FsPrescribeDataDrugDoctorQueryDto queryDto){
        String doctorId = getDoctorId();
        if (StringUtils.isBlank(doctorId)){
            throw new CustomException("登录已过期，请重新登录");
        }
        queryDto.setDrugDoctorId(Long.valueOf(doctorId));
        PageHelper.startPage(queryDto.getPageNum(),queryDto.getPageSize());
        List<FsPrescribeDataScrmVO> list=fsPrescribeScrmService.pendingStorePrescribeList(queryDto);
        return getDataTable(list);
    }
    /**
     * 获取当前登录医生id
     * */
    private String getDoctorId()
    {
        String headValue =  ServletUtils.getRequest().getHeader("APPToken");
        Claims claims=jwtUtils.getClaimByToken(headValue);
        String doctorId = claims.getSubject().toString();
        return doctorId;
    }
}