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.SysBlockChainManagementDTO;
import com.fzm.mall.server.admin.system.entity.dto.SysBlockChainManagementSaveDTO;
import com.fzm.mall.server.admin.system.entity.dto.SysBlockChainManagementUpdateDTO;
import com.fzm.mall.server.admin.system.service.ISysBlockChainManagementService;
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("/blockChainManagement")
public class SysBlockChainManagementController {

    @Autowired
    private ISysBlockChainManagementService managementService;

    @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 SysBlockChainManagementSaveDTO saveDTO) {

        managementService.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 SysBlockChainManagementUpdateDTO updateDTO) {

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

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

        managementService.validStateUpdate(blockChainId, validState, updateTime);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-区块链管理-删除")
    @ApiOperationSupport(order = 4)
    @AdminLog(description = "系统中心-区块链管理-删除", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/delete/{blockChainId}")
    public ResponseVO delete(
            @ApiParam(value = "blockChainId", required = true) @PathVariable(value = "blockChainId") String blockChainId) {
        managementService.delete(blockChainId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-区块链管理-主键查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/{blockChainId}")
    public ResponseVO read(
            @ApiParam(value = "blockChainId", required = true) @PathVariable(value = "blockChainId") String blockChainId) {
        SysBlockChainManagementDTO result = managementService.read(blockChainId);
        return resFac.getSuccessResponseWithData(result);
    }

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

        IPage<SysBlockChainManagementDTO> result = managementService.page(page, keyword);
        return resFac.getSuccessResponseWithData(result);
    }
}
