package com.ruoyi.controller;

import cn.hutool.core.bean.BeanUtil;
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.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cs.domain.CsAchievementInfoDetails;
import com.ruoyi.cs.domain.dto.CsOrderApplyEditDTO;
import com.ruoyi.cs.domain.vo.*;
import com.ruoyi.cs.service.ICsAchievementInfoDetailsService;
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/csAchievementInfoDetails")
public class CsAchievementInfoDetailsController extends BaseController
{
    @Resource
    private ICsAchievementInfoDetailsService csAchievementInfoDetailsService;

    /**
     * 查询科研成果-成果详情信息列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfoDetails:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsAchievementInfoDetails csAchievementInfoDetails)
    {
        startPage();
        // csAchievementInfoDetails.setDelFlag("0");
        List<CsAchievementInfoDetails> list = csAchievementInfoDetailsService.selectCsAchievementInfoDetailsList(csAchievementInfoDetails);
        return getDataTable(list);
    }

    /**
     * 导出科研成果-成果详情信息列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfoDetails:export')")
    @Log(title = "科研成果-成果详情信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsAchievementInfoDetails csAchievementInfoDetails)
    {
        List<CsAchievementInfoDetails> list = csAchievementInfoDetailsService.selectCsAchievementInfoDetailsList(csAchievementInfoDetails);
        if (CollectionUtils.isNotEmpty(list)){
            list.forEach(l -> {
                l.setUploadTime(l.getAllTime());
            });
        }
        if (StringUtils.equals(csAchievementInfoDetails.getAchievementType(), "0")){
            ExcelUtil<CsAchievementInfoDetailsLWVo> util = new ExcelUtil<CsAchievementInfoDetailsLWVo>(CsAchievementInfoDetailsLWVo.class);
            List<CsAchievementInfoDetailsLWVo> lwVos = BeanUtil.copyToList(list, CsAchievementInfoDetailsLWVo.class);
            util.exportExcel(response, lwVos, "论文成果");
        }else if (StringUtils.equals(csAchievementInfoDetails.getAchievementType(), "1")){
            ExcelUtil<CsAchievementInfoDetailsZLVo> util =
                    new ExcelUtil<CsAchievementInfoDetailsZLVo>(CsAchievementInfoDetailsZLVo.class);
            List<CsAchievementInfoDetailsZLVo> lwVos = BeanUtil.copyToList(list, CsAchievementInfoDetailsZLVo.class);
            util.exportExcel(response, lwVos, "专利成果");
        }else if (StringUtils.equals(csAchievementInfoDetails.getAchievementType(), "2")){
            ExcelUtil<CsAchievementInfoDetailsJXVo> util =
                    new ExcelUtil<CsAchievementInfoDetailsJXVo>(CsAchievementInfoDetailsJXVo.class);
            List<CsAchievementInfoDetailsJXVo> lwVos = BeanUtil.copyToList(list, CsAchievementInfoDetailsJXVo.class);
            util.exportExcel(response, lwVos, "奖项成果");
        }else if (StringUtils.equals(csAchievementInfoDetails.getAchievementType(), "3")){
            ExcelUtil<CsAchievementInfoDetailsQTVo> util =
                    new ExcelUtil<CsAchievementInfoDetailsQTVo>(CsAchievementInfoDetailsQTVo.class);
            List<CsAchievementInfoDetailsQTVo> lwVos = BeanUtil.copyToList(list, CsAchievementInfoDetailsQTVo.class);
            util.exportExcel(response, lwVos, "其他成果");
        }else if (StringUtils.equals(csAchievementInfoDetails.getAchievementType(), "4")){
            ExcelUtil<CsAchievementInfoDetailsYYVo> util =
                    new ExcelUtil<CsAchievementInfoDetailsYYVo>(CsAchievementInfoDetailsYYVo.class);
            List<CsAchievementInfoDetailsYYVo> lwVos = BeanUtil.copyToList(list, CsAchievementInfoDetailsYYVo.class);
            util.exportExcel(response, lwVos, "预约成果");
        }else {
            ExcelUtil<CsAchievementInfoDetails> util = new ExcelUtil<CsAchievementInfoDetails>(CsAchievementInfoDetails.class);
            util.exportExcel(response, list, "科研成果");
        }
    }

    /**
     * 获取科研成果-成果详情信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfoDetails:query')")
    @GetMapping(value = "/{infoDetailsId}")
    public AjaxResult getInfo(@PathVariable("infoDetailsId") String infoDetailsId)
    {
        // return AjaxResult.success(csAchievementInfoDetailsService.selectCsAchievementInfoDetailsByInfoDetailsId(infoDetailsId));
        return AjaxResult.success(csAchievementInfoDetailsService.selectManageCsAchievementInfoDetailsByInfoDetailsId(infoDetailsId));
    }

    /**
     * 新增科研成果-成果详情信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfoDetails:add')")
    @Log(title = "科研成果-成果详情信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsAchievementInfoDetails csAchievementInfoDetails)
    {
        return toAjax(csAchievementInfoDetailsService.insertCsAchievementInfoDetails(csAchievementInfoDetails));
    }

    /**
     * 修改科研成果-成果详情信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfoDetails:edit')")
    @Log(title = "科研成果-成果详情信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsOrderApplyEditDTO orderDTO)
    {
        // return toAjax(csAchievementInfoDetailsService.updateCsAchievementInfoDetails(csAchievementInfoDetails));
        return toAjax(csAchievementInfoDetailsService.updateManageCsAchievementInfoDetails(orderDTO));
    }

    /**
     * 删除科研成果-成果详情信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfoDetails:remove')")
    @Log(title = "科研成果-成果详情信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{infoDetailsIds}")
    public AjaxResult remove(@PathVariable String[] infoDetailsIds)
    {
        return toAjax(csAchievementInfoDetailsService.deleteCsAchievementInfoDetailsByInfoDetailsIds(infoDetailsIds));
    }

    /**
     * 科研成果-成果审核
     */
    @PreAuthorize("@ss.hasPermi('cs:csAchievementInfoDetails:edit')")
    @Log(title = "科研成果-成果审核", businessType = BusinessType.UPDATE)
    @PostMapping("/editAuditing")
    public AjaxResult editAuditing(@RequestBody CsAchievementInfoDetails csAchievementInfoDetails)
    {
        return toAjax(csAchievementInfoDetailsService.updateCsAchievementInfoDetails(csAchievementInfoDetails));
    }

    /**
     * 获取用户成果信息数量
     */
    @GetMapping( "/getUserAchievementNum")
    public AjaxResult getUserAchievementNum(String memberUserId)
    {
        return AjaxResult.success(csAchievementInfoDetailsService.getUserAchievementNum(memberUserId));
    }
}
