/*
 * 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.quality.entity.AssayResultW;
import com.tofly.quality.entity.dto.AssayResultWQuery;
import com.tofly.quality.service.AssayResultWService;
import lombok.AllArgsConstructor;
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("/assayresultw")
@Api(tags = "化验结果接口")
public class AssayResultWController {

    private final AssayResultWService assayResultWService;

    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param query 化验结果
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getAssayResultWPage(Page page, @ModelAttribute AssayResultWQuery query) {
        return ResultRespone.success(assayResultWService.getPageData(page, query));
    }

    /**
     * 获取没有化验报告的化验结果
     * @param assayReportId 化验报告Id
     * @return
     */
    @GetMapping("/getAssayResultList")
    @ApiOperation(value = "获取没有化验报告的化验结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assayReportId", value = "化验报告Id", dataType = "Long")
    })
    public ResultRespone getAssayResultList(Long assayReportId) {
        return ResultRespone.success(assayResultWService.getResultList(assayReportId));
    }


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

    /**
     * 新增化验结果
     *
     * @param assayResultW 化验结果
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增化验结果")
    @ApiOperation(value = "新增化验结果")
    @PostMapping
    public ResultRespone save(@Validated AssayResultW assayResultW, @RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        return ResultRespone.success(assayResultWService.saveAssayResult(fileList, assayResultW, true));

    }

    /**
     * 修改化验结果
     *
     * @param assayResultW 化验结果
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改化验结果")
    @ApiOperation(value = "修改化验结果")
    @PutMapping
    public ResultRespone updateById(@Valid AssayResultW assayResultW, @RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        if(assayResultW.getId()==0){
            return ResultRespone.failed("参数id不能为空");
        }
        return ResultRespone.success(assayResultWService.saveAssayResult(fileList, assayResultW, false));
    }

    /**
     * 通过id删除化验结果
     *
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过id删除化验结果")
    @ApiOperation(value = "通过ID删除化验结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable Long id) {
        return ResultRespone.success(assayResultWService.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(assayResultWService.deleteByIds(ids));
    }
}
