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.his.domain.FsUserInformationCollectionSchedule;
import com.fs.his.enums.PrescriptionTaskStepEnum;
import com.fs.his.service.IFsUserInformationCollectionScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 用户信息采集进度Controller
 *
 * @author cgp
 * @date 2025-11-18
 */
@RestController
@RequestMapping("/store/collectionSchedule")
public class FsUserInformationCollectionScheduleController extends BaseController
{
    @Autowired
    private IFsUserInformationCollectionScheduleService fsUserInformationCollectionScheduleService;

    /**
     * 对于每条用户信息采集记录，只获取其最新的任务流程
     */
    @PreAuthorize("@ss.hasPermi('his:collectionSchedule:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserInformationCollectionSchedule fsUserInformationCollectionSchedule)
    {
        startPage();
        List<FsUserInformationCollectionSchedule> list = fsUserInformationCollectionScheduleService.getUserRecordLatestTaskProcess(fsUserInformationCollectionSchedule);
        return getDataTable(list);
    }

    /**
     * 导出用户信息采集进度列表
     */
    @PreAuthorize("@ss.hasPermi('his:collectionSchedule:export')")
    @Log(title = "用户信息采集进度", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserInformationCollectionSchedule fsUserInformationCollectionSchedule)
    {
        List<FsUserInformationCollectionSchedule> list = fsUserInformationCollectionScheduleService.selectFsUserInformationCollectionScheduleList(fsUserInformationCollectionSchedule);
        ExcelUtil<FsUserInformationCollectionSchedule> util = new ExcelUtil<FsUserInformationCollectionSchedule>(FsUserInformationCollectionSchedule.class);
        return util.exportExcel(list, "用户信息采集进度数据");
    }

    /**
     * 获取用户信息采集进度详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:collectionSchedule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsUserInformationCollectionScheduleService.selectFsUserInformationCollectionScheduleById(id));
    }

    /**
     * 新增用户信息采集进度
     */
    @PreAuthorize("@ss.hasPermi('his:collectionSchedule:add')")
    @Log(title = "用户信息采集进度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUserInformationCollectionSchedule fsUserInformationCollectionSchedule)
    {
        return toAjax(fsUserInformationCollectionScheduleService.insertFsUserInformationCollectionSchedule(fsUserInformationCollectionSchedule));
    }

    /**
     * 修改用户信息采集进度
     */
    @PreAuthorize("@ss.hasPermi('his:collectionSchedule:edit')")
    @Log(title = "用户信息采集进度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsUserInformationCollectionSchedule fsUserInformationCollectionSchedule)
    {
        return toAjax(fsUserInformationCollectionScheduleService.updateFsUserInformationCollectionSchedule(fsUserInformationCollectionSchedule));
    }

    /**
     * 删除用户信息采集进度
     */
    @PreAuthorize("@ss.hasPermi('his:collectionSchedule:remove')")
    @Log(title = "用户信息采集进度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsUserInformationCollectionScheduleService.deleteFsUserInformationCollectionScheduleByIds(ids));
    }

    /**
     * 获取用户信息采集任务流程步骤枚举列表
     */
    @PreAuthorize("@ss.hasPermi('qw:collectionSchedule:list')")
    @GetMapping("/steps")
    public AjaxResult getTaskSteps() {
        List<Map<String, Object>> steps = Arrays.stream(PrescriptionTaskStepEnum.values())
                .map(enumItem -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("dictValue", enumItem.getCode());
                    map.put("dictLabel", enumItem.getDesc());
                    return map;
                })
                .collect(Collectors.toList());
        return AjaxResult.success(steps);
    }
}
