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.his.domain.FsFollow;
import com.fs.his.param.FsFollowParam;
import com.fs.his.service.IFsFollowService;
import com.fs.his.vo.FsFollowExcelListVO;
import com.fs.his.vo.FsFollowListVO;
import com.fs.his.vo.FsFollowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 随访管理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:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(FsFollowParam fsFollow)
    {
        startPage();
        LoginUser loginUser = com.fs.framework.security.SecurityUtils.getLoginUser();
        fsFollow.setCompanyUserId(loginUser.getUser().getUserId());
        List<FsFollowListVO> list = fsFollowService.selectFsFollowListVO(fsFollow);

        for (FsFollowListVO vo : list) {
            if (vo.getPhone()!=null&&vo.getPatientPhone().length()>0){
                vo.setPhone(vo.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
            if (vo.getPatientPhone()!=null&&vo.getPatientPhone().length()>0){
                vo.setPatientPhone(vo.getPatientPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }

        }
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('his:follow:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsFollowParam fsFollow)
    {
        startPage();
        LoginUser loginUser = com.fs.framework.security.SecurityUtils.getLoginUser();
        fsFollow.setCompanyId(loginUser.getCompany().getCompanyId());
        List<FsFollowListVO> list = fsFollowService.selectFsFollowListVO(fsFollow);
        for (FsFollowListVO vo : list) {
            if (vo.getPhone()!=null&&vo.getPatientPhone().length()>0){
                vo.setPhone(vo.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
            if (vo.getPatientPhone()!=null&&vo.getPatientPhone().length()>0){
                vo.setPatientPhone(vo.getPatientPhone().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)
    {
        LoginUser loginUser = com.fs.framework.security.SecurityUtils.getLoginUser();
        fsFollow.setCompanyId(loginUser.getCompany().getCompanyId());
        logger.info("导出随访管理:"+ loginUser.getUser().getUserId());
        if (fsFollowService.isEntityNull(fsFollow)){
            return AjaxResult.error("请筛选数据导出");
        }
        List<FsFollowExcelListVO> list = fsFollowService.selectFsFollowListExcelVO(fsFollow);
        if (list.size()>30000){
            return AjaxResult.error("导出数据不可超过3w条");
        }
        for (FsFollowExcelListVO vo : list) {

            if (vo.getPatientPhone()!=null&&vo.getPatientPhone().length()>0){
                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:myExport')")
    @Log(title = "随访管理", businessType = BusinessType.EXPORT)
    @GetMapping("/myExport")
    public AjaxResult myExport(FsFollowParam fsFollow)
    {
        LoginUser loginUser = com.fs.framework.security.SecurityUtils.getLoginUser();
        fsFollow.setCompanyUserId(loginUser.getUser().getUserId());
        logger.info("导出随访管理:"+ loginUser.getUser().getUserId());

        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, "健康随访信息清单");
    }

    /**
     * 获取随访管理详细信息
     */
    @GetMapping(value = "/{followId}")
    public AjaxResult getInfo(@PathVariable("followId") Long followId)
    {
        FsFollowVO vo = fsFollowService.selectFsFollowVOByFollowId(followId);
       if (vo.getPhone()!=null&&vo.getPatientPhone().length()>0){
            vo.setPhone(vo.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        if (vo.getPatientPhone()!=null&&vo.getPatientPhone().length()>0){
            vo.setPatientPhone(vo.getPatientPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        return AjaxResult.success(vo);
    }

    /**
     * 新增随访管理
     */
    @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)
    {
        LoginUser loginUser = com.fs.framework.security.SecurityUtils.getLoginUser();
        FsFollow follow = new FsFollow();
        follow.setFollowId(follow.getFollowId());
        follow.setFormJson(fsFollow.getFormJson());
        fsFollow.setWriteStatus(1);
        fsFollow.setWriteUserName(loginUser.getCompany().getCompanyName()+" "+loginUser.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));
    }
}
