package com.fs.his.controller;

import java.util.List;

import com.fs.common.utils.SecurityUtils;
import com.fs.his.param.FsFollowParam;
import com.fs.his.vo.FsFollowExcelListVO;
import com.fs.his.vo.FsFollowExportListVO;
import com.fs.his.vo.FsFollowListVO;
import com.fs.his.vo.FsFollowVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.his.domain.FsFollow;
import com.fs.his.service.IFsFollowService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 随访管理Controller
 *
 * @author fs
 * @date 2023-07-14
 */
@RestController
@RequestMapping("/his/follow")
public class FsFollowController extends BaseController
{
    @Autowired
    private IFsFollowService fsFollowService;

    /**
     * 查询随访管理列表
     */
    @PreAuthorize("@ss.hasPermi('his:follow:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsFollowParam fsFollow)
    {
        startPage();
        List<FsFollowListVO> list = fsFollowService.selectFsFollowListVO(fsFollow);
//        for (FsFollowListVO vo : list) {
//            vo.setPhone(vo.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
//        }
        return getDataTable(list);
    }

    /**
     * 导出随访管理列表
     */
    @PreAuthorize("@ss.hasPermi('his:follow:export')")
    @Log(title = "随访管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsFollowParam fsFollow)
    {
        logger.info("hz>\n【随访管理】：{}", SecurityUtils.getUserId());
        if (fsFollowService.isEntityNull(fsFollow)){
            return AjaxResult.error("请筛选数据导出");
        }
        Long count = fsFollowService.selectFsFollowListExcelVOCount(fsFollow);
        if (count>30000){
            return AjaxResult.error("导出数据不可超过3w条");
        }
        List<FsFollowExcelListVO> list = fsFollowService.selectFsFollowListExcelVO(fsFollow);

        for (FsFollowExcelListVO vo : list) {
            vo.setPatientPhone(vo.getPatientPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        ExcelUtil<FsFollowExcelListVO> util = new ExcelUtil<FsFollowExcelListVO>(FsFollowExcelListVO.class);
        return util.exportExcel(list, "健康随访信息清单");
    }

    /**
     * 获取随访管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:follow:query')")
    @GetMapping(value = "/{followId}")
    public AjaxResult getInfo(@PathVariable("followId") Long followId)
    {
        FsFollowVO fsFollowVO = fsFollowService.selectFsFollowVOByFollowId(followId);
        if (fsFollowVO.getPhone()!=null){
            fsFollowVO.setPhone(fsFollowVO.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        return AjaxResult.success(fsFollowVO);
    }

    /**
     * 新增随访管理
     */
    @PreAuthorize("@ss.hasPermi('his:follow:add')")
    @Log(title = "随访管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsFollow fsFollow)
    {
        return toAjax(fsFollowService.insertFsFollow(fsFollow));
    }

    /**
     * 填写随访管理
     */
    @PreAuthorize("@ss.hasPermi('his:follow:edit')")
    @Log(title = "填写随访管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsFollow fsFollow)
    {
        FsFollow follow = new FsFollow();
        follow.setFollowId(follow.getFollowId());
        follow.setFormJson(fsFollow.getFormJson());
        fsFollow.setWriteStatus(1);
        fsFollow.setWriteUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        return toAjax(fsFollowService.updateFsFollow(fsFollow));
    }

    /**
     * 删除随访管理
     */
    @PreAuthorize("@ss.hasPermi('his:follow:remove')")
    @Log(title = "随访管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{followIds}")
    public AjaxResult remove(@PathVariable Long[] followIds)
    {
        return toAjax(fsFollowService.deleteFsFollowByFollowIds(followIds));
    }

    @PreAuthorize("@ss.hasPermi('his:follow:statistics')")
    @Log(title = "处方统计表", businessType = BusinessType.EXPORT)
    @GetMapping("/statistics")
    public AjaxResult statistics(FsFollowParam fsFollow)
    {


        List<FsFollowExportListVO> list = fsFollowService.selectFsFollowExportListVO(fsFollow);
        ExcelUtil<FsFollowExportListVO> util = new ExcelUtil<FsFollowExportListVO>(FsFollowExportListVO.class);
        return util.exportExcel(list, "处方统计表");
    }
}
