package com.liangjian.dataplatform.member.controller;

import com.liangjian.dataplatform.member.entity.po.secondary.PointGrowthRule;
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.IPointGrowthRuleService;
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.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 黄子奇
 * @Date: 2019/11/18
 */
@RestController
@RequestMapping("/pointGrowthRule")
@Api(value = "会员积分规则、成长规则控制器", tags = "会员积分规则、成长规则控制器", description = "会员积分规则、成长规则控制器")
public class PointGrowthRuleController {
    @Autowired
    private IPointGrowthRuleService iPointGrowthRuleService;

    @GetMapping("/rules")
    @ApiOperation(value = "查询获取规则")
    public Page<PointGrowthRule> query(@ApiParam(value = "页数", required = true) @RequestParam Integer page,
                                       @ApiParam(value = "每页数量", required = true) @RequestParam Integer size,
                                       @ApiParam(value = "终端编码") @RequestParam(required = false) String terminalCode,
                                       @ApiParam(value = "终端名称") @RequestParam(required = false) String terminalName,
                                       @ApiParam(value = "行为") @RequestParam(required = false) String action,
                                       @ApiParam(value = "是否运行") @RequestParam(required = false) Boolean running) {
        return iPointGrowthRuleService.query(terminalCode, terminalName, action, running, page - 1, size);
    }

    @PostMapping("/rule")
    @ApiOperation(value = "新增规则")
    public ResultObject<Object> addRule(@ApiParam(value = "规则模型", required = true) @RequestBody PointGrowthRule pointGrowthRule) {
        if(iPointGrowthRuleService.addPointGrowthRule(pointGrowthRule)) {
            return ResultObject.success("保存成功", null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @PostMapping("/ruleCondition")
    @ApiOperation(value = "查询获取规则(BirdGird)")
    public BirdGirdTableResponse2<PointGrowthRule> query2(@ApiParam(value = "birdgird的条件请求对象") @RequestBody BirdGirdRequest girdRequest) {
        girdRequest.setPageIndex(girdRequest.getPageIndex() - 1);
        return iPointGrowthRuleService.queryByCondition(girdRequest);
    }

    @PutMapping("/rule")
    @ApiOperation(value = "更新规则")
    public ResultObject<Object> saveRule(@ApiParam(value = "规则模型", required = true) @RequestBody PointGrowthRule pointGrowthRule) {
        if(iPointGrowthRuleService.savePointGrowthRule(pointGrowthRule)) {
            return ResultObject.success("保存成功", null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @PostMapping("/deleteRule")
    @ApiOperation("删除规则")
    public ResultObject<Object> deleteRule(@ApiParam(value = "规则id", required = true) @RequestParam String id) {
        if(iPointGrowthRuleService.deletePointGrowthRule(id)) {
            return ResultObject.success();
        } else {
            return ResultObject.error("删除失败");
        }
    }


}