package com.fzm.mall.server.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.fzm.mall.server.admin.system.entity.dto.SysBlockChainPassDTO;
import com.fzm.mall.server.admin.system.entity.dto.SysBlockChainPassSaveDTO;
import com.fzm.mall.server.admin.system.entity.dto.SysBlockChainPassUpdateDTO;
import com.fzm.mall.server.admin.system.service.ISysBlockChainPassService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 系统中心-通证-通证 前端控制器
 * </p>
 *
 * @author fzm
 * @since 2021-08-12
 */
@Api(tags = "系统中心-区块链管理")
@RestController
@RequestMapping("/blockChainPass")
public class SysBlockChainPassController {

    @Autowired
    private ISysBlockChainPassService passService;

    @Autowired
    private ResponseFactory resFac;

    @ApiOperation(value = "系统中心-通证-新增")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "系统中心-通证-新增", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.ADD)
    @PostMapping("/save")
    public ResponseVO save(@RequestBody @Valid SysBlockChainPassSaveDTO saveDTO) {

        passService.save(saveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-通证-编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "系统中心-通证-编辑", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/update")
    public ResponseVO update(@RequestBody @Valid SysBlockChainPassUpdateDTO updateDTO) {

        passService.update(updateDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-通证-启用状态-编辑")
    @ApiOperationSupport(order = 3)
    @AdminLog(description = "系统中心-通证-启用状态-编辑", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/validState/update/{passId}")
    public ResponseVO validStateUpdate(@ApiParam(value = "passId", required = true) @PathVariable(value = "passId") String passId,
                                       @ApiParam(value = "是否可用(2--停用 1--可用 )") @RequestParam Integer validState,
                                       @ApiParam(value = "信息修改时间") @RequestParam Long updateTime) {

        passService.validStateUpdate(passId, validState, updateTime);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-通证-删除")
    @ApiOperationSupport(order = 4)
    @AdminLog(description = "系统中心-通证-删除", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/delete/{passId}")
    public ResponseVO delete(
            @ApiParam(value = "passId", required = true) @PathVariable(value = "passId") String passId) {
        passService.delete(passId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-通证-主键查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/{passId}")
    public ResponseVO read(
            @ApiParam(value = "passId", required = true) @PathVariable(value = "passId") String passId) {
        SysBlockChainPassDTO result = passService.read(passId);
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "系统中心-通证-列表查询")
    @ApiOperationSupport(order = 6)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "区块链编号", required = true) @RequestParam String blockChainId,
                           @ApiParam(value = "通证名称关键字或通证标识") @RequestParam(required = false) String keyword,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long pageSize,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long pageNum) {
        Page<SysBlockChainPassDTO> page = new Page<>();
        if (ObjectUtils.isNotEmpty(pageSize) && ObjectUtils.isNotEmpty(pageNum)) {
            page.setSize(pageSize);
            page.setCurrent(pageNum);
        }

        IPage<SysBlockChainPassDTO> result = passService.page(page, blockChainId, keyword);
        return resFac.getSuccessResponseWithData(result);
    }
}
