package com.ruoyi.web.work.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.work.domain.FollowRecord;
import com.ruoyi.web.work.domain.FollowTemplate;
import com.ruoyi.web.work.domain.dto.VisitDto;
import com.ruoyi.web.work.domain.vo.VisitVoList;
import com.ruoyi.web.work.service.IFollowRecordService;
import com.ruoyi.web.work.service.IFollowTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 回访记录
 *
 * @author lsw
 * @date 2024-07-18
 */
@RestController
@RequestMapping("/work/record")
public class FollowRecordController extends BaseController {
    @Autowired
    private IFollowRecordService followRecordService;

    @Autowired
    private IFollowTemplateService followTemplateService;

    @PreAuthorize("@ss.hasAnyPermi('work:record:list,work:up:list')")
    @GetMapping("/list")
    public TableDataInfo list(FollowRecord followRecord) {
        startPage();
        List<FollowRecord> list = followRecordService.selectList(followRecord);
        return getDataTable(list);
    }

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        return AjaxResult.success(followRecordService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('work:record:list')")
    @GetMapping("/visit/list")
    public AjaxResult visitList(VisitDto dto) {
        dto.setDoctorCode(getLoginUser().getUsername());
        List<VisitVoList> list = followRecordService.selectVisitList(dto);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasAnyPermi('work:record:list,work:up:list')")
    @GetMapping("/template/list")
    public AjaxResult templateList(FollowTemplate followTemplate) {
        List<FollowTemplate> list = followTemplateService.selectList(followTemplate);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasAnyPermi('work:record:add,work:up:add')")
    @Log(title = "回访记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody FollowRecord followRecord) {
        return followRecordService.add(followRecord);
    }

    @PreAuthorize("@ss.hasAnyPermi('work:record:edit,work:up:edit')")
    @Log(title = "回访记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody FollowRecord followRecord) {
        return toAjax(followRecordService.updateById(followRecord));
    }

    @PreAuthorize("@ss.hasAnyPermi('work:record:remove,work:up:remove')")
    @Log(title = "回访记录", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(followRecordService.removeByIds(Arrays.asList(ids)));
    }
}
