/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.quality.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.quality.entity.AssayReportW;
import com.tofly.quality.entity.dto.AssayReportWQuery;
import com.tofly.quality.service.AssayReportWService;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 化验报告管理
 *
 * @author admin
 * @date Thu Nov 03 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/assayreportw")
@Api(tags = "化验报告管理接口")
public class AssayReportWController {

    private final AssayReportWService assayReportWService;

    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param query 化验报告管理
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getAssayReportWPage(Page page, @ModelAttribute AssayReportWQuery query) {
        return ResultRespone.success(assayReportWService.getPageData(page, query));
    }


    /**
     * 通过id查询化验报告管理
     *
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
    })
    public ResultRespone getById(@PathVariable("id") Long id) {
        return ResultRespone.success(assayReportWService.getById(id));
    }

    /**
     * 新增化验报告管理
     *
     * @param assayReportW 化验报告管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增化验报告管理")
    @ApiOperation(value = "新增化验报告管理")
    @PostMapping
    public ResultRespone save(@Validated AssayReportW assayReportW, @RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        return ResultRespone.success(assayReportWService.saveAssayReport(fileList, assayReportW, true));

    }


    /**
     * 修改化验报告管理
     *
     * @param assayReportW 化验报告管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改化验报告管理")
    @ApiOperation(value = "修改化验报告管理")
    @PutMapping
    public ResultRespone updateById(@Valid AssayReportW assayReportW, @RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        if(assayReportW.getId()==null || assayReportW.getId()==0){
            return ResultRespone.failed("参数id不能为空");
        }

        AssayReportW as = assayReportWService.getById(assayReportW.getId());
        if (as != null && as.getCreateUser().equals(SecurityUtils.getUserId())) {
            return ResultRespone.success(assayReportWService.saveAssayReport(fileList, assayReportW, false));
        } else {
            return ResultRespone.failed("只有上传本人才能修改");
        }
    }

    /**
     * 通过id批量删除化验报告管理
     *
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过ID对报告进行进行报告归档操作")
    @ApiOperation(value = "通过ID对报告进行进行报告归档操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量归档，多个id以逗号隔开", required = true, dataType = "String")
    })
    @PostMapping("/files")
    public ResultRespone synFile(String ids) {
        return ResultRespone.success(assayReportWService.synFile(ids));
    }


    /**
     * 通过id删除化验报告管理
     *
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过id删除化验报告管理")
    @ApiOperation(value = "通过ID删除化验报告管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable Long id) {
        return ResultRespone.success(assayReportWService.deleteByIds(id + ""));

    }

    /**
     * 通过id批量删除化验报告管理
     *
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过id批量删除化验报告管理")
    @ApiOperation(value = "通过ID批量删除化验报告管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(assayReportWService.deleteByIds(ids));
    }
}
