package com.fs.survey;

import java.util.Date;
import java.util.List;

import com.fs.common.config.LoginContextManager;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.SecurityUtils;
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.survey.domain.DrugResearch;
import com.fs.survey.service.IDrugResearchService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 用药调研Controller
 * 
 * @author fs
 * @date 2026-02-06
 */
@RestController
@RequestMapping("/survey/drugResearch")
public class DrugResearchController extends BaseController
{
    @Autowired
    private IDrugResearchService drugResearchService;

    /**
     * 查询用药调研列表
     */
    @PreAuthorize("@ss.hasPermi('survey:drugResearch:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrugResearch drugResearch)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        drugResearch.setCompanyId(currentCompanyId);
        startPage();
        List<DrugResearch> list = drugResearchService.selectDrugResearchList(drugResearch);
        return getDataTable(list);
    }

    /**
     * 导出用药调研列表
     */
    @PreAuthorize("@ss.hasPermi('survey:drugResearch:export')")
    @Log(title = "用药调研", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DrugResearch drugResearch)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        drugResearch.setCompanyId(currentCompanyId);
        List<DrugResearch> list = drugResearchService.selectDrugResearchList(drugResearch);
        ExcelUtil<DrugResearch> util = new ExcelUtil<DrugResearch>(DrugResearch.class);
        return util.exportExcel(list, "用药调研数据");
    }

    /**
     * 获取用药调研详细信息
     */
    @PreAuthorize("@ss.hasPermi('survey:drugResearch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(drugResearchService.selectDrugResearchById(id));
    }

    /**
     * 新增用药调研
     */
    @PreAuthorize("@ss.hasPermi('survey:drugResearch:add')")
    @Log(title = "用药调研", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DrugResearch drugResearch)
    {
        Long userId = LoginContextManager.getCurrentCompanyUserId();
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        drugResearch.setCreateBy(userId);
        drugResearch.setUpdateBy(userId);
        drugResearch.setCreateTime(new Date());
        drugResearch.setUpdateTime(new Date());
        drugResearch.setDeleted(0l);
        drugResearch.setCompanyId(currentCompanyId);
        return toAjax(drugResearchService.companyInsertDrugResearch(drugResearch));
    }

    /**
     * 修改用药调研
     */
    @PreAuthorize("@ss.hasPermi('survey:drugResearch:edit')")
    @Log(title = "用药调研", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DrugResearch drugResearch)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        drugResearch.setCompanyId(currentCompanyId);
        return toAjax(drugResearchService.updateDrugResearch(drugResearch));
    }

    /**
     * 删除用药调研
     */
    @PreAuthorize("@ss.hasPermi('survey:drugResearch:remove')")
    @Log(title = "用药调研", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(drugResearchService.deleteDrugResearchByIds(ids));
    }
}
