package com.fs.company.controller.store;

import com.fs.common.annotation.Log;
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.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.his.domain.FsExportTask;
import com.fs.his.domain.FsPrescribe;
import com.fs.his.param.FsPrescribeParam;
import com.fs.his.service.IFsExportTaskService;
import com.fs.his.service.IFsPrescribeService;
import com.fs.his.vo.FsMessageFeedbackExportListVO;
import com.fs.his.vo.FsPrescribeExcelListVO;
import com.fs.his.vo.FsPrescribeListVO;
import com.fs.his.vo.FsPrescribeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 处方Controller
 *
 * @author fs
 * @date 2023-06-13
 */
@RestController
@RequestMapping("/store/prescribe")
public class FsPrescribeController extends BaseController
{
    @Autowired
    private IFsPrescribeService fsPrescribeService;
    @Autowired
    private IFsExportTaskService exportTaskService;
    /**
     * 查询处方列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FsPrescribeParam fsPrescribe)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsPrescribe.setCompanyId(loginUser.getCompany().getCompanyId());
        List<FsPrescribeListVO> list = fsPrescribeService.selectFsPrescribeListVO(fsPrescribe);
        for (FsPrescribeListVO vo : list) {
            if (vo.getPatientTel()!=null){
                vo.setPatientTel(vo.getPatientTel().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        return getDataTable(list);
    }

    @GetMapping("/myList")
    public TableDataInfo myList(FsPrescribeParam fsPrescribe)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsPrescribe.setCompanyUserId(loginUser.getUser().getUserId());
        List<FsPrescribeListVO> list = fsPrescribeService.selectFsPrescribeListVO(fsPrescribe);
        for (FsPrescribeListVO vo : list) {
            if (vo.getPatientTel()!=null){
                vo.setPatientTel(vo.getPatientTel().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        return getDataTable(list);
    }


    /**
     * 导出处方列表
     */
    @PreAuthorize("@ss.hasPermi('his:prescribe:export')")
    @Log(title = "处方", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsPrescribeParam fsPrescribe)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsPrescribe.setCompanyId(loginUser.getCompany().getCompanyId());
        Long userId = loginUser.getUser().getUserId();
        logger.info("导出处方订单:"+ userId);
        Long count =fsPrescribeService.selectFsPrescribeExcelListVOCount(fsPrescribe);
        if (count>30000){
            return AjaxResult.error("导出数据不可超过3w条");
        }
        Integer exportType1 = exportTaskService.isExportType2(userId);
        if (exportType1>0){
            return AjaxResult.error("你已经有正在导出的任务");
        }
        FsExportTask task=new FsExportTask();
        task.setTaskType(7);
        task.setStatus(0);
        task.setStartTime(new Date());
        task.setRemark("处方订单导出");
        task.setSysType(2);
        task.setCompanyUserId(userId);
        exportTaskService.insertFsExportTask(task);
        fsPrescribe.setTaskId(task.getTaskId());
        fsPrescribeService.exportData(fsPrescribe);
        return new AjaxResult(200,"后台正在导出,请等待...任务ID:"+task.getTaskId(),task.getTaskId());

    }

    @PreAuthorize("@ss.hasPermi('his:prescribe:myExport')")
    @Log(title = "处方", businessType = BusinessType.EXPORT)
    @GetMapping("/myExport")
    public AjaxResult myExport(FsPrescribeParam fsPrescribe)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsPrescribe.setCompanyUserId(loginUser.getUser().getUserId());
        List<FsPrescribeExcelListVO> list = fsPrescribeService.selectFsPrescribeExcelListVO(fsPrescribe);
        for (FsPrescribeExcelListVO vo : list){
            if (vo.getPatientTel()!=null){
                vo.setPatientTel(vo.getPatientTel().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        ExcelUtil<FsPrescribeExcelListVO> util = new ExcelUtil<FsPrescribeExcelListVO>(FsPrescribeExcelListVO.class);

        return util.exportExcel(list, "处方数据");
    }

    /**
     * 导出医疗信息反馈单
     */
    @PreAuthorize("@ss.hasPermi('his:prescribe:messageFeedbackExport')")
    @Log(title = "医疗信息反馈单", businessType = BusinessType.EXPORT)
    @GetMapping("/messageFeedbackExport")
    public AjaxResult messageFeedbackExport(FsPrescribeParam fsPrescribe)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsPrescribe.setCompanyId(loginUser.getCompany().getCompanyId());
        List<FsMessageFeedbackExportListVO> list = fsPrescribeService.selectFsMessageFeedbackExportListVO(fsPrescribe);
        for (FsMessageFeedbackExportListVO vo : list){
            vo.setFeedbackContent("复诊");
            if (vo.getPatientTel()!=null){
                vo.setPatientTel(vo.getPatientTel().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
            vo.setFeedbackTime(vo.getAuditTime());
        }
        ExcelUtil<FsMessageFeedbackExportListVO> util = new ExcelUtil<FsMessageFeedbackExportListVO>(FsMessageFeedbackExportListVO.class);
        return util.exportExcel(list, "医疗信息反馈单数据");
    }

    /**
     * 获取处方详细信息
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/{prescribeId}")
    public AjaxResult getInfo(@PathVariable("prescribeId") Long prescribeId)
    {

        FsPrescribeVO fsPrescribe = fsPrescribeService.selectFsPrescribeByPrescribeIdVO(prescribeId);
        if (fsPrescribe.getPatientTel()!=null){
            fsPrescribe.setPatientTel(fsPrescribe.getPatientTel().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        return AjaxResult.success(fsPrescribe);
    }




    /**
     * 获取处方详细信息
     */

    @GetMapping("/Drug/{prescribeId}")
    public AjaxResult getDrugInfo(@PathVariable("prescribeId") Long prescribeId)
    {
        return AjaxResult.success(fsPrescribeService.selectFsPrescribeDrugByPrescribeIdVO(prescribeId));
    }

    /**
     * 新增处方
     */
    @PreAuthorize("@ss.hasPermi('his:prescribe:add')")
    @Log(title = "处方", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsPrescribe fsPrescribe)
    {
        return toAjax(fsPrescribeService.insertFsPrescribe(fsPrescribe));
    }

    /**
     * 修改处方
     */
    @PreAuthorize("@ss.hasPermi('his:prescribe:edit')")
    @Log(title = "处方", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsPrescribe fsPrescribe)
    {
        return toAjax(fsPrescribeService.updateFsPrescribe(fsPrescribe));
    }

    /**
     * 删除处方
     */
    @PreAuthorize("@ss.hasPermi('his:prescribe:remove')")
    @Log(title = "处方", businessType = BusinessType.DELETE)
	@DeleteMapping("/{prescribeIds}")
    public AjaxResult remove(@PathVariable Long[] prescribeIds)
    {
        return toAjax(fsPrescribeService.deleteFsPrescribeByPrescribeIds(prescribeIds));
    }




}
