package com.diagbot.web;


import com.diagbot.annotation.SysLogger;
import com.diagbot.dto.ProductServiceDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.facade.ProductServiceFacade;
import com.diagbot.facade.ServiceTokenFacade;
import com.diagbot.vo.ServiceTokenVo;
import com.diagbot.vo.ProductServiceSaveVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * <p>
 * 服务令牌表 前端控制器
 * </p>
 *
 * @author zhaops
 * @since 2018-09-17
 */
@RestController
@Api(value="服务令牌API", tags={"服务令牌API"})
@RequestMapping("/serviceToken")
@SuppressWarnings("unchecked")
public class ServiceTokenController {

    @Autowired
    ServiceTokenFacade serviceTokenFacade;
    @Autowired
    ProductServiceFacade productServiceFacade;


    @PostMapping("/hasPermission")
    @ApiOperation(value = "查询当前appkey和secret是否有权限[by:zhoutg]")
    @ApiIgnore
    public RespDTO<Boolean> hasPermission(@RequestBody ServiceTokenVo serviceTokenVo) {
        return serviceTokenFacade.hasPermission(serviceTokenVo);
    }

    @ApiOperation(value = "生成令牌[by:zhaops]",
            notes = "productId：产品Id，必填<br>" +
                    "serviceId：服务Id，必填<br> ")
    @PostMapping("/createServiceToken")
    @SysLogger("createServiceToken")
    @Transactional
    public RespDTO<ProductServiceDTO> createServiceToken(@RequestBody @Valid ProductServiceSaveVO productServiceSaveVO) {
        ProductServiceDTO productServiceDTO = productServiceFacade.genProductService(productServiceSaveVO);
        return RespDTO.onSuc(productServiceDTO);
    }

    @ApiOperation(value = "令牌禁用[by:zhaops]",
            notes = "productServiceId：产品服务id，必填<br>")
    @PostMapping("/disableToken")
    @SysLogger("disableToken")
    @Transactional
    public RespDTO<Integer> disableToken(@RequestParam Long productServiceId) {
        Integer count = serviceTokenFacade.disableToken(productServiceId);
        return RespDTO.onSuc(count);
    }

    @ApiOperation(value = "令牌启用[by:zhaops]",
            notes = "productServiceId：产品服务id，必填<br>")
    @PostMapping("/enableToken")
    @SysLogger("enableToken")
    @Transactional
    public RespDTO<Integer> enableToken(@RequestParam Long productServiceId) {
        Integer count = serviceTokenFacade.enableToken(productServiceId);
        return RespDTO.onSuc(count);
    }

    @ApiOperation(value = "令牌删除[by:zhaops]",
            notes = "productServiceId：产品服务id，必填<br>")
    @PostMapping("/deleteTokenByProductServiceId")
    @SysLogger("deleteTokenByProductServiceId")
    @Transactional
    public RespDTO<Integer> deleteTokenByProductServiceId(@RequestParam Long productServiceId) {
        Integer count = serviceTokenFacade.deleteByProductServiceId(productServiceId);
        return RespDTO.onSuc(count);
    }
}

