package com.liangjian.dataplatform.member.controller;

import com.liangjian.dataplatform.member.entity.po.primary.JobInfo;
import com.liangjian.dataplatform.member.entity.response.ResultObject;
import com.liangjian.dataplatform.member.entity.vo.TagRuleVo;
import com.liangjian.dataplatform.member.entity.vo.request.IdAndColsParam;
import com.liangjian.dataplatform.member.entity.vo.tagrules.SqlAndResult;
import com.liangjian.dataplatform.member.service.IJobInfoService;
import com.liangjian.dataplatform.member.service.ITagRuleService;
import com.liangjian.dataplatform.member.service.ITagRuleSqlService;
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.*;

import java.util.List;

/**
 * @Author: 黄子奇
 * @Date: 2019/9/9 17:04
 */
@RestController
@RequestMapping("/tagrule")
@Api(value = "标签规则的控制器", tags = "标签规则的控制器", description = "标签规则的控制器, 和任务信息")
public class TagRuleController {

    @Autowired
    private ITagRuleService iTagRuleService;
    @Autowired
    private IJobInfoService iJobInfoService;
    @Autowired
    private ITagRuleSqlService iTagRuleSqlService;

    @GetMapping("/rule")
    @ApiOperation(value = "分页获取标签规则", notes = "面向对象： 1：员工 2：用户 3：商品")
    public Page<TagRuleVo> getRules(@ApiParam(value = "面向对象") @RequestParam(required = false) Integer object,
                                  @ApiParam(value = "规则名称") @RequestParam(required = false) String name,
                                  @ApiParam(value = "任务是否启用") @RequestParam(required = false) Boolean isActive,
                                  @ApiParam(value = "页码", required = true) @RequestParam Integer page,
                                  @ApiParam(value = "每页数量", required = true) @RequestParam Integer size) {
        Page<TagRuleVo> vos = iTagRuleService.queryByCondition(object, name, isActive, page-1, size);
        return vos;
    }

    @PostMapping("/rule")
    @ApiOperation(value = "新增标签规则")
    public ResultObject<Object> addRule(@ApiParam(value = "标签规则对象",required = true) @RequestBody TagRuleVo vo) {
        if(vo.getId() != null) {
            return ResultObject.error("提交的对象不合规");
        }

        if(iTagRuleService.addTagRule(vo)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @PutMapping("/rule")
    @ApiOperation(value = "更新标签规则")
    public ResultObject<Object> updateRule(@ApiParam(value = "标签规则对象", required = true) @RequestBody TagRuleVo vo) {
        if(vo.getId() == null) {
            return ResultObject.error("提交的对象不合规");
        }

        if(iTagRuleService.saveTagRule(vo)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @DeleteMapping("/rule")
    @ApiOperation(value = "删除标签规则")
    public ResultObject<Object> deleteRule(@ApiParam(value = "标签规则Id", required = true) @RequestBody String id) {
        if(iTagRuleService.deleteTagRule(id)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @GetMapping("/jobinfo")
    @ApiOperation(value = "分页查询任务描述", notes = "调度类型： 0：执行一次， 1：秒调度， 2：分调度， 3：时调度， 4：天调度， 5：周调度， 6：月调度， 7：仅重复")
    public Page<JobInfo> getJobInfos(@ApiParam(value = "任务名称") @RequestParam(required = false) String name,
                                     @ApiParam(value = "调度类型") @RequestParam(required = false) Integer scheduleType,
                                     @ApiParam(value = "任务是否启用") @RequestParam(required = false) Boolean isActive,
                                     @ApiParam(value = "页码", required = true) @RequestParam Integer page,
                                     @ApiParam(value = "每页数量", required = true) @RequestParam Integer size) {
        Page<JobInfo> res = iJobInfoService.queryByCondition(name, scheduleType, isActive, page-1, size);
        return res;
    }

    @GetMapping("/jobDetail")
    @ApiOperation(value = "获取作业的详情", notes = "获取作业的详情")
    public ResultObject<JobInfo> getJobDetail(@ApiParam(value = "作业Id") @RequestParam String jobId) {
        JobInfo jobInfo = iJobInfoService.queryById(jobId);
        return ResultObject.success(jobInfo);
    }


    @PostMapping("/jobinfo")
    @ApiOperation(value = "新增任务描述")
    public ResultObject<Object> addJobInfo(@ApiParam(value = "任务描述对象", required = true) @RequestBody JobInfo jobInfo) {
        if(jobInfo.getId() != null) {
            return ResultObject.error("提交的对象不合规");
        }

        if(iJobInfoService.addJobInfo(jobInfo)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @PutMapping("/jobinfo")
    @ApiOperation(value = "更新任务描述")
    public ResultObject<Object> updateJobInfo(@ApiParam(value = "任务描述对象", required = true) @RequestBody JobInfo jobInfo) {
        if(jobInfo.getId() == null) {
            return ResultObject.error("提交的对象不合规");
        }

        if(iJobInfoService.saveJobInfo(jobInfo)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @DeleteMapping("/jobinfo")
    @ApiOperation(value = "删除任务描述")
    public ResultObject<Object> deleteJobInfo(@ApiParam(value = "任务描述id", required = true) @RequestBody String id) {
        if(iJobInfoService.delete(id)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @PostMapping("/sqlAndResult")
    @ApiOperation(value = "获取标签规则的转换Sql和生成标签的结果")
    public ResultObject<List<SqlAndResult>> getSqlAndResult(@ApiParam(value = "标签规则Id和查询的字段", required = true) @RequestBody IdAndColsParam param) {
        String id = param.getId();
        List<String> cols = param.getCols();
        List<SqlAndResult> results = iTagRuleSqlService.getSqlAndResult(id,cols);
        return ResultObject.success(results);
    }
}
