/*
 * 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.CollectW;
import com.tofly.quality.entity.dto.CollectWQuery;
import com.tofly.quality.service.CollectWService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.time.DateFormatUtils;
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 javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;

/**
 * 采集记录管理
 *
 * @author admin
 * @date Wed Nov 02 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/collectw")
@Api(tags = "采集记录管理接口")
public class CollectWController {

    private final CollectWService collectWService;

    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param query 采集记录管理
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getCollectWPage(Page page, @ModelAttribute CollectWQuery query) {
        return ResultRespone.success(collectWService.getPageData(page, query));
    }

    /**
     * 获取没有化验结果的采集记录
    * @param resultId 采集记录管理
     * @return
     */
    @GetMapping("/getCollectList")
    @ApiOperation(value = "获取没有化验结果的采集记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "resultId", value = "化验结果ID", dataType = "Long")
    })
    public ResultRespone getCollectList(Long resultId) {
        return ResultRespone.success(collectWService.getCollectList(resultId));
    }

    /**
     * 通过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(collectWService.getById(id));
    }

    /**
     * 新增采集记录管理
     *
     * @param collectW 采集记录管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增采集记录管理")
    @ApiOperation(value = "新增采集记录管理")
    @PostMapping
    public ResultRespone save(@RequestBody @Valid CollectW collectW) {

        return ResultRespone.success(collectWService.save(collectW));
    }

    /**
     * 修改采集记录管理
     *
     * @param collectW 采集记录管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改采集记录管理")
    @ApiOperation(value = "修改采集记录管理")
    @PutMapping
    public ResultRespone updateById(@RequestBody @Valid CollectW collectW) {
        if(collectW.getId()==null || collectW.getId()==0){
            return ResultRespone.failed("参数id不能为空");
        }

        CollectW co = collectWService.getById(collectW.getId());
        if (co != null && co.getCreateUser().equals(SecurityUtils.getUserId())) {
            co.setCollectNo(null);//不允许修改编号
            return ResultRespone.success(collectWService.updateById(collectW));
        } else {
            return ResultRespone.failed("只有记录本人才能修改");
        }
    }

    /**
     * 通过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(collectWService.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(collectWService.deleteByIds(ids));
    }
}
