package com.ruoyi.web.controller.doubleDefense;

import com.ruoyi.common.annotation.Anonymous;
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.SecurityUtils;
import com.ruoyi.doubledefense.domain.BasicSafetyDocument;
import com.ruoyi.doubledefense.dto.BasicSafetyDocumentDto;
import com.ruoyi.doubledefense.service.IBasicSafetyDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;










/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-08-03
 */
@RestController
@RequestMapping("/system/document")
public class BasicSafetyDocumentController extends BaseController
{
    @Autowired
    private IBasicSafetyDocumentService basicSafetyDocumentService;

    @Value("${ruoyi.profile}")
    public String url;

    /**
     * 查询【请填写功能名称】列表
     */
//    //TODO 20230920PreAuthorize("@("@ss.hasPermi('system:document:list')")
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(BasicSafetyDocument basicSafetyDocument)
    {
        startPage();
        List<BasicSafetyDocumentDto> list = basicSafetyDocumentService.selectBasicSafetyDocumentList(basicSafetyDocument);
        for (BasicSafetyDocumentDto basicSafetyDocumentDto : list) {
            basicSafetyDocumentDto.setFullUrl((url+basicSafetyDocumentDto.getUploadUrl()).replaceAll("/", Matcher.quoteReplacement(File.separator)));
        }
        return getDataTable(list);
    }


    /**
     * 获取【请填写功能名称】详细信息
     */
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('system:document:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(basicSafetyDocumentService.getById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('system:document:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasicSafetyDocument basicSafetyDocument)
    {
        basicSafetyDocument.setCreateDefault();
        basicSafetyDocument.setCompanyCode(SecurityUtils.getUserCompanyCode());
        return toAjax(basicSafetyDocumentService.saveOrUpdate(basicSafetyDocument));
    }

    /**
     * 修改【请填写功能名称】
     */
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('system:document:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasicSafetyDocument basicSafetyDocument)
    {
        basicSafetyDocument.setUpdateDefault();
        return toAjax(basicSafetyDocumentService.updateById(basicSafetyDocument));
    }

    /**
     * 删除【请填写功能名称】
     */
//    //TODO 20230920PreAuthorize("@("@ss.hasPermi('system:document:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(basicSafetyDocumentService.removeBatchByIds(Arrays.asList(ids)));
    }
}
