package com.liangjian.dataplatform.member.controller;

import com.liangjian.dataplatform.member.entity.po.elasticsearch.ProductData;
import com.liangjian.dataplatform.member.entity.po.elasticsearch.ProductDataTag;
import com.liangjian.dataplatform.member.entity.response.ResultObject;
import com.liangjian.dataplatform.member.entity.vo.request.BirdGirdRequest;
import com.liangjian.dataplatform.member.entity.vo.response.BirdGirdTableResponse2;
import com.liangjian.dataplatform.member.service.IProductDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author 黄树文
 * @date 2020/3/10 9:32
 */
@RestController
@RequestMapping("/productData")
@Api(value = "商品数据搜索控制器", tags = "商品数据数据搜索控制器")
public class ProductDataController {

    @Autowired
    private IProductDataService iProductDataService;

    @PostMapping("/productDataCondition")
    @ApiOperation(value = "查询产品(BirdGird)")
    public BirdGirdTableResponse2<ProductData> query2(@ApiParam(value = "birdgird的条件请求对象") @RequestBody BirdGirdRequest girdRequest) {
        girdRequest.setPageIndex(girdRequest.getPageIndex() - 1);
        return iProductDataService.queryByCondition(girdRequest);
    }

    @GetMapping("/productDataTag")
    @ApiOperation(value = "查询产品标签")
    public ResultObject<BirdGirdTableResponse2<ProductDataTag>> search(@ApiParam(value = "搜索关键词(ProductCode)", required = true) @RequestParam String kw,
                                                                       @ApiParam(value = "页数", required = true) @RequestParam Integer page,
                                                                       @ApiParam(value = "每页数量", required = true) @RequestParam Integer pageSize) throws IOException {
        BirdGirdTableResponse2<ProductDataTag> list = iProductDataService.findProductDataTag(kw, page - 1, pageSize);
        return ResultObject.success(list);

    }

    @DeleteMapping("/productDataTag")
    @ApiOperation(value = "删除标签")
    public ResultObject<Object> delete(@ApiParam(value = "删除商品数据标签", required = true) @RequestParam String id) {
        if (iProductDataService.deleteProductDataTagById(id)) {
            return ResultObject.success("删除标签成功", null);
        } else {
            return ResultObject.error("删除标签失败");
        }
    }

    @PostMapping("/productDataTag")
    @ApiOperation(value = "保存标签")
    public ResultObject<Object> saveAllProductDataTag(@ApiParam(value = "删除商品数据标签", required = true) @RequestBody ArrayList<ProductDataTag> productDataTags) {
        if (iProductDataService.saveAllProductDataTag(productDataTags)) {
            return ResultObject.success("保存标签成功", null);
        } else {
            return ResultObject.error("保存标签失败");
        }
    }


}
