package com.ruoyi.web.controller.workTecket;

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.worktecket.domain.WorkTicketSafetyMeasures;
import com.ruoyi.worktecket.dto.WorkTicketSafetyMeasuresDTO;
import com.ruoyi.worktecket.service.IWorkTicketSafetyMeasuresService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 作业票安全措施模板Controller
 *
 * @author ruoyi
 * @date 2023-08-04
 */
@Api(tags = "作业票安全措施模板接口")
@RestController
@RequestMapping("/worktecket/measures")
public class WorkTicketSafetyMeasuresController extends BaseController {
    @Autowired
    private IWorkTicketSafetyMeasuresService workTicketSafetyMeasuresService;

    /**
     * 查询作业票安全措施模板列表
     */
    @ApiOperation("查询作业票安全措施模板列表")
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('worktecket:measures:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkTicketSafetyMeasuresDTO workTicketSafetyMeasures) {
        startPage();
//        List<WorkTicketSafetyMeasuresDTO> list = workTicketSafetyMeasuresService.selectWorkTicketSafetyMeasuresList(workTicketSafetyMeasures);
        List<WorkTicketSafetyMeasures> dataList = workTicketSafetyMeasuresService.selectList(workTicketSafetyMeasures);

//        List<WorkTicketSafetyMeasuresDTO> resultList = new ArrayList<>();
//        for (WorkTicketSafetyMeasures workTicketSafetyMeasures1 : dataList) {
//            WorkTicketSafetyMeasuresDTO workTicketSafetyMeasuresDTO1 = new WorkTicketSafetyMeasuresDTO();
//            BeanUtils.copyProperties(workTicketSafetyMeasures1, workTicketSafetyMeasuresDTO1);
//            resultList.add(workTicketSafetyMeasuresDTO1);
//        }

        return getDataTable(dataList);
    }


    /**
     * 导出作业票安全措施模板列表
     */
    @ApiOperation("导出作业票安全措施模板列表")
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('worktecket:measures:export')")
    @Log(title = "作业票安全措施模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkTicketSafetyMeasuresDTO workTicketSafetyMeasures) {
        List<WorkTicketSafetyMeasuresDTO> list = workTicketSafetyMeasuresService.selectWorkTicketSafetyMeasuresList(workTicketSafetyMeasures);
        ExcelUtil<WorkTicketSafetyMeasuresDTO> util = new ExcelUtil<WorkTicketSafetyMeasuresDTO>(WorkTicketSafetyMeasuresDTO.class);
        util.exportExcel(response, list, "作业票安全措施模板数据");
    }

    /**
     * 获取作业票安全措施模板详细信息
     */
    @ApiOperation("获取作业票安全措施模板详细信息")
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('worktecket:measures:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(workTicketSafetyMeasuresService.selectWorkTicketSafetyMeasuresById(id));
    }

    /**
     * 新增作业票安全措施模板
     */
    @ApiOperation("新增作业票安全措施模板")
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('worktecket:measures:add')")
    @Log(title = "作业票安全措施模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkTicketSafetyMeasuresDTO workTicketSafetyMeasures) {
        return toAjax(workTicketSafetyMeasuresService.insertWorkTicketSafetyMeasures(workTicketSafetyMeasures));
    }

    /**
     * 修改作业票安全措施模板
     */
    @ApiOperation("修改作业票安全措施模板")
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('worktecket:measures:edit')")
    @Log(title = "作业票安全措施模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkTicketSafetyMeasuresDTO workTicketSafetyMeasures) {
        return toAjax(workTicketSafetyMeasuresService.updateWorkTicketSafetyMeasures(workTicketSafetyMeasures));
    }

    /**
     * 删除作业票安全措施模板
     */
    @ApiOperation("删除作业票安全措施模板")
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('worktecket:measures:remove')")
    @Log(title = "作业票安全措施模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(workTicketSafetyMeasuresService.deleteWorkTicketSafetyMeasuresByIds(ids));
    }
}
