package com.tofly.qualitycontrol.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.qualitycontrol.entity.CollectionNumber;
import com.tofly.qualitycontrol.service.ICollectionNumberService;
import com.tofly.qualitycontrol.util.QualityControlUtil;
import com.tofly.qualitycontrol.vo.CollectionNumberVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static com.tofly.qualitycontrol.common.QualityControlConstant.FAILED;
import static com.tofly.qualitycontrol.common.QualityControlConstant.SUCCESS;

/**
 * 归集管理控制器
 *
 * @author B
 */
@RestController
@RequestMapping("/collectionNumber")
@Api(value = "归集管理接口", tags = "归集管理接口")
@RequiredArgsConstructor
public class CollectionNumberController {

    private final ICollectionNumberService collectionNumberService;

    @ApiOperation(value = "添加归集", notes = "参数:归集名称,归集类型", httpMethod = "POST")
    @PostMapping("/save")
    public ResultRespone saveCollectionNumber(@Validated(CollectionNumber.ValidateGroup.class) @RequestBody CollectionNumber collectionNumber, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultRespone.failed(QualityControlUtil.getErrorStrings(bindingResult), FAILED);
        }
        Boolean flag = collectionNumberService.saveCollectionNumber(collectionNumber);
        return flag ? ResultRespone.success(flag, SUCCESS) : ResultRespone.failed(flag, FAILED);
    }

    @ApiOperation(value = "修改归集", notes = "可修改:归集名称,归集类型", httpMethod = "POST")
    @PostMapping("/update")
    public ResultRespone updateCollectionNumber(@Validated(CollectionNumber.ValidateGroup.class) @RequestBody CollectionNumber collectionNumber, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultRespone.failed(QualityControlUtil.getErrorStrings(bindingResult), FAILED);
        }
        Boolean flag = collectionNumberService.updateCollectionNumber(collectionNumber);
        return flag ? ResultRespone.success(flag, SUCCESS) : ResultRespone.failed(flag, FAILED);
    }

    @ApiOperation(value = "启用,禁用归集", notes = "参数带id,状态值", httpMethod = "POST")
    @PostMapping("/changeStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "ids", name = "归集号ID，多个逗号隔开", required = true, dataType = "string", dataTypeClass = String.class),
            @ApiImplicitParam(value = "collectionStatus", name = "归集状态 1：启用；2：禁用", required = true, dataType = "string", dataTypeClass = String.class)
    })
    public ResultRespone changeStatus(String ids , String collectionStatus) {
        if (StringUtil.isEmpty(ids)){
            throw new ToflyDeniedException("请选择归集号");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        Boolean flag = collectionNumberService.update(Wrappers.<CollectionNumber>lambdaUpdate()
                .in(CollectionNumber::getId , idList).set(CollectionNumber::getCollectionStatus , collectionStatus));
        return flag ? ResultRespone.success(flag, SUCCESS) : ResultRespone.failed(flag, FAILED);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "ids", name = "ids,以','分割", required = true, paramType = "query", dataType = "string", dataTypeClass = String.class)
    })
    @ApiOperation(value = "删除归集,可批量", notes = "", httpMethod = "GET")
    @GetMapping("/delete")
    public ResultRespone deleteCollectionNumber(@RequestParam String ids) {
        Boolean flag = collectionNumberService.deleteCollectionNumber(Arrays.asList(ids.split(",")));
        return flag ? ResultRespone.success(flag, SUCCESS) : ResultRespone.failed(flag, FAILED);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "归集类型", name = "collectionType", required = false, paramType = "query", dataType = "string", dataTypeClass = String.class),
            @ApiImplicitParam(value = "归集编号或归集名称", name = "input", required = false, paramType = "query", dataType = "string", dataTypeClass = String.class),
            @ApiImplicitParam(value = "归集状态,默认就是'全部'状态,不传参", name = "collectionStatus", required = false, paramType = "query", dataType = "string", dataTypeClass = String.class),
    })
    @ApiOperation(value = "归集管理分页,查询", notes = "", httpMethod = "POST")
    @PostMapping("/list")
    public ResultRespone<IPage<CollectionNumberVO>> list(@RequestParam(required = false) String collectionType, @RequestParam(required = false) String input, @RequestParam(required = false) String collectionStatus, @RequestBody Page<CollectionNumber> page) {
        return ResultRespone.success(collectionNumberService.getList(collectionType, input, collectionStatus, page), SUCCESS);
    }

}
