package com.ruoyi.web.controller.oa;

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

import com.ruoyi.system.service.ISysUserService;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.oa.domain.CmcSafeReport;
import com.ruoyi.oa.service.ICmcSafeReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 安全汇报Controller
 * 
 * @author cmc
 * @date 2024-06-24
 */
@RestController
@RequestMapping("/oa/safeReport")
public class CmcSafeReportController extends BaseController
{
    @Autowired
    private ICmcSafeReportService cmcSafeReportService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询安全汇报列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcSafeReport cmcSafeReport)
    {
        startPage();
        List<CmcSafeReport> list = cmcSafeReportService.selectCmcSafeReportList(cmcSafeReport);
        for (CmcSafeReport safeReport : list) {
            userIdToName(safeReport);
        }
        return getDataTable(list);
    }

    /**
     * 导出安全汇报列表
     */
    @Log(title = "安全汇报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcSafeReport cmcSafeReport)
    {
        List<CmcSafeReport> list = cmcSafeReportService.selectCmcSafeReportList(cmcSafeReport);
        for (CmcSafeReport safeReport : list) {
            userIdToName(safeReport);
        }
        ExcelUtil<CmcSafeReport> util = new ExcelUtil<CmcSafeReport>(CmcSafeReport.class);
        util.exportExcel(response, list, "安全汇报数据");
    }

    /**
     * 获取安全汇报详细信息
     */
    @GetMapping(value = "/{safeReportId}")
    public AjaxResult getInfo(@PathVariable("safeReportId") Integer safeReportId)
    {
        return success(cmcSafeReportService.selectCmcSafeReportBySafeReportId(safeReportId));
    }

    /**
     * 新增安全汇报
     */
    @Log(title = "安全汇报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcSafeReport cmcSafeReport)
    {
        return toAjax(cmcSafeReportService.insertCmcSafeReport(cmcSafeReport));
    }

    /**
     * 修改安全汇报
     */
    @Log(title = "安全汇报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcSafeReport cmcSafeReport)
    {
        return toAjax(cmcSafeReportService.updateCmcSafeReport(cmcSafeReport));
    }

    /**
     * 删除安全汇报
     */
    @Log(title = "安全汇报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{safeReportIds}")
    public AjaxResult remove(@PathVariable Integer[] safeReportIds)
    {
        return success(cmcSafeReportService.deleteCmcSafeReportBySafeReportIds(safeReportIds));
    }

    public void userIdToName(CmcSafeReport cmcSafeReport) {
        String userId = cmcSafeReport.getMember();
        String userName = cmcSafeReport.getUserName();
        StringBuilder userNames = new StringBuilder();
        if (userId != null && !userId.equals("") && (userName == null || userName.equals(""))) {
            for (String user : userId.split(","))
                userNames.append(userService.selectUserById(Long.parseLong(user)).getNickName()).append(",");
            cmcSafeReport.setUserName(userNames.substring(0, userNames.length() - 1));
        }
    }
}
