package net.northking.cctp.flowable.api.implement;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.flowable.api.implement.service.ImplementProcessService;
import net.northking.cctp.flowable.db.entity.IpProcess;
import net.northking.cctp.flowable.dto.ip.IpProcessState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@Api(tags = "实施流程定义")
@RequestMapping("/v1")
@RestController
public class ImplementProcessCtrl {

    /**
     * 资源名称
     */
    private static final String RESOURCE_NAME = "/implement";

    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(ImplementProcessCtrl.class);


    @Autowired
    private ImplementProcessService implementService;

    @ApiOperation("保存实施流程")
    @PostMapping(value = RESOURCE_NAME,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> save(
            @RequestBody @Validated IpProcessState dto) {
        // 校验
        dto.flowCheck();
        String processId = implementService.saveProcess(dto);
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        return wrapper.success(processId);
    }

    /**
     * 获取实施流程明细
     *
     * @param uuid 主键
     * @return 接口数据传输对象
     */
    @ApiOperation(value = "获取实施流程明细")
    @GetMapping(
            value = RESOURCE_NAME + "/{uuid}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<IpProcessState> findByPK(@PathVariable("uuid") String uuid) {
        ResultWrapper<IpProcessState> wrapper = new ResultWrapper<>();
        IpProcessState dto = implementService.findByPK(uuid);
        return wrapper.success(dto);
    }

    /**
     * 删除实施流程
     *
     * @param uuid 主键
     * @return 删除的记录数量
     */
    @ApiOperation(value = "删除实施流程")
    @DeleteMapping(
            value = RESOURCE_NAME + "/{uuid}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> deleteByPK(
            @PathVariable("uuid") String uuid) {
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer count = implementService.deleteByPK(uuid);
        return wrapper.success(count);
    }

    /**
     * 批量删除流程
     *
     * @param batch 集合
     * @return 成功的数量
     */
    @ApiOperation(value = "批量删除：状态流程定义")
    @PostMapping(
            value = "/batch" + RESOURCE_NAME + "/delete",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> deleteBatch(
            @RequestBody @Validated List<String> batch) {
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer count = implementService.deleteBatch(batch);
        return wrapper.success(count);
    }

    /**
     * 分页查询
     *
     * @param queryByPage 查询条件
     * @return 数据列表
     */
    @ApiOperation(value = "分页查询列表：状态流程定义")
    @PostMapping(value = "/paging/query" + RESOURCE_NAME,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Pagination<IpProcessState>> pagingQuery(
            @RequestBody @Validated QueryByPage<IpProcess> queryByPage) {
        ResultWrapper<Pagination<IpProcessState>> wrapper = new ResultWrapper<>();
        Pagination<IpProcessState> page = implementService.pagingQuery(queryByPage);
        return wrapper.success(page);
    }


    /**
     * 更改流程是否默认
     */
    @ApiOperation(value = "更改流程是否默认")
    @GetMapping(value = RESOURCE_NAME + "default/{uuid}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> updateDefault(@PathVariable("uuid") String uuid) {
        Integer result = implementService.updateDefault(uuid);
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        return wrapper.success(result);
    }

    @ApiOperation(value = "刷新流程节点到缓存中")
    @GetMapping(value = RESOURCE_NAME + "/cache",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> cacheRefresh() {
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        implementService.reloadProcessCache();
        return wrapper.success();
    }



}
