package com.ruoyi.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.common.utils.poi.ExcelUtil;
import com.ruoyi.cs.domain.CsAchievementInfo;
import com.ruoyi.cs.domain.CsAchievementInfoDetails;
import com.ruoyi.cs.service.ICsAchievementInfoDetailsService;
import com.ruoyi.cs.service.ICsAchievementInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 科研成果信息Controller
 *
 * @author ruoyi
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/cs/csAchievementInfo")
public class CsAchievementInfoController extends BaseController
{
    @Resource
    private ICsAchievementInfoService csAchievementInfoService;

    @Resource
    private ICsAchievementInfoDetailsService csAchievementInfoDetailsService;

    /**
     * 查询科研成果信息列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsAchievementInfo csAchievementInfo)
    {
        startPage();
        csAchievementInfo.setDelFlag("0");
        List<CsAchievementInfo> list = csAchievementInfoService.selectCsAchievementInfoList(csAchievementInfo);
        return getDataTable(list);
    }

    /**
     * 导出科研成果信息列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfo:export')")
    @Log(title = "科研成果信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsAchievementInfo csAchievementInfo)
    {
        csAchievementInfo.setDelFlag("0");
        List<CsAchievementInfo> list = csAchievementInfoService.selectCsAchievementInfoList(csAchievementInfo);
        ExcelUtil<CsAchievementInfo> util = new ExcelUtil<CsAchievementInfo>(CsAchievementInfo.class);
        util.exportExcel(response, list, "科研成果信息数据");
    }

    /**
     * 获取科研成果信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfo:query')")
    @GetMapping(value = "/{achievementInfoId}")
    public AjaxResult getInfo(@PathVariable("achievementInfoId") String achievementInfoId)
    {
        return AjaxResult.success(csAchievementInfoService.selectCsAchievementInfoByAchievementInfoId(achievementInfoId));
    }

    /**
     * 新增科研成果信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfo:add')")
    @Log(title = "科研成果信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsAchievementInfo csAchievementInfo)
    {
        return toAjax(csAchievementInfoService.insertCsAchievementInfo(csAchievementInfo));
    }

    /**
     * 修改科研成果信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfo:edit')")
    @Log(title = "科研成果信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsAchievementInfo csAchievementInfo)
    {
        return toAjax(csAchievementInfoService.updateCsAchievementInfo(csAchievementInfo));
    }

    /**
     * 删除科研成果信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfo:remove')")
    @Log(title = "科研成果信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{achievementInfoIds}")
    public AjaxResult remove(@PathVariable String[] achievementInfoIds)
    {
        return toAjax(csAchievementInfoService.deleteCsAchievementInfoByAchievementInfoIds(achievementInfoIds));
    }

    /**
     * 修改科研成果信息
     *
     * @param csAchievementInfo
     * @return
     */
    @Log(title = "科研成果信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAchievementInfo")
    public AjaxResult updateAchievementInfo(@RequestBody CsAchievementInfo csAchievementInfo){
        if(CollectionUtils.isNotEmpty(csAchievementInfo.getPatentAchievementList())){
            CsAchievementInfoDetails csAchievementInfoDetails = csAchievementInfo.getPatentAchievementList().get(0);
            csAchievementInfoDetailsService.updateCsAchievementInfoDetails(csAchievementInfoDetails);
        }
        return toAjax(csAchievementInfoService.updateCsAchievementInfo(csAchievementInfo));
    }

}
