package com.fs.company.controller.handwrite;

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

import com.fs.framework.security.SecurityUtils;
import com.fs.handwrite.domain.HandwriteCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.utils.poi.ExcelUtil;
import com.fs.handwrite.domain.HandwritePatientForm;
import com.fs.handwrite.service.IHandwritePatientFormService;

/**
 * 手写初诊单Controller
 * 
 * @author fs
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/handwrite/patientForm")
public class HandwritePatientFormController extends BaseController
{
    @Autowired
    private IHandwritePatientFormService handwritePatientFormService;

    /**
     * 查询手写初诊单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HandwritePatientForm handwritePatientForm)
    {
        startPage();
        List<HandwritePatientForm> list = handwritePatientFormService.selectHandwritePatientFormList(handwritePatientForm);
        for (HandwritePatientForm item : list) {
            if (item.getPatientPhone() != null) {
                item.setPatientPhone(item.getPatientPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出手写初诊单列表
     */
    @PostMapping("/export")
    public AjaxResult export(HttpServletResponse response, HandwritePatientForm handwritePatientForm)
    {
        List<HandwritePatientForm> list = handwritePatientFormService.selectHandwritePatientFormList(handwritePatientForm);
        for (HandwritePatientForm item : list) {
            if (item.getPatientPhone() != null) {
                item.setPatientPhone(item.getPatientPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        ExcelUtil<HandwritePatientForm> util = new ExcelUtil<HandwritePatientForm>(HandwritePatientForm.class);
        return util.exportExcel(list, "手写信息初诊单数据");
    }

    /**
     * 获取手写初诊单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        HandwritePatientForm handwritePatientForm = handwritePatientFormService.selectHandwritePatientFormById(id);
        if (handwritePatientForm.getPatientPhone() != null) {
            handwritePatientForm.setPatientPhone(handwritePatientForm.getPatientPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        return AjaxResult.success(handwritePatientForm);
    }

    /**
     * 新增手写初诊单
     */
    @PostMapping
    public AjaxResult add(@RequestBody HandwritePatientForm handwritePatientForm)
    {
        //获取当前登录用户
        Long currentUserId = SecurityUtils.getLoginUser().getUser().getUserId();
        handwritePatientForm.setCompanyUserId(currentUserId);
        return toAjax(handwritePatientFormService.insertHandwritePatientForm(handwritePatientForm));
    }

    /**
     * 修改手写初诊单
     */
    @PutMapping
    public AjaxResult edit(@RequestBody HandwritePatientForm handwritePatientForm)
    {
        //获取当前登录用户
        Long currentUserId = SecurityUtils.getLoginUser().getUser().getUserId();
        handwritePatientForm.setUpdateBy(currentUserId);
        return toAjax(handwritePatientFormService.updateHandwritePatientForm(handwritePatientForm));
    }

    /**
     * 删除手写初诊单
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(handwritePatientFormService.deleteHandwritePatientFormByIds(ids));
    }
}