package com.bocloud.ims.booter.controller.resource.f5;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.model.f5.ProfileModel;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.f5.BiProfile;
import com.bocloud.ims.interfaces.resource.f5.F5BiProfileService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "BiProfile", tags = "BiProfile")
@RestController
@RequestMapping("/v1/f5/profiles")
public class F5BiProfileController {
    @Autowired
    private F5BiProfileService f5BiProfileService;

    /**
     * 列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查看BiProfile列表", httpMethod = "GET")
    @GetMapping
    public BocloudResult<GridBean<BiProfile>> list(Pager pager) {
        return f5BiProfileService.list(pager);
    }

    /**
     * 添加
     *
     * @param biProfile
     * @param context
     * @return 添加结果
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "添加BiProfile", httpMethod = "POST")
    @PostMapping
    public BocloudResult create(@RequestBody BiProfile biProfile, @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != biProfile) {
            ProfileModel profileModel = JSONObject.parseObject(
                JSONObject.parseObject(JSONObject.toJSONString(biProfile)).toJSONString(), ProfileModel.class);
            return this.f5BiProfileService.create(biProfile, context.getTarget(), profileModel);
        } else {
            return new BocloudResult(false, "添加失败");
        }
    }

    /**
     * 修改
     *
     * @param biProfile
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改BiProfile", httpMethod = "PUT")
    @PutMapping(value = "/{id}")
    public BocloudResult modify(@RequestBody BiProfile biProfile, @PathVariable(value = Common.ID) Long id,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != biProfile) {
            biProfile.setId(id);
            ProfileModel profileModel = JSONObject.parseObject(
                JSONObject.parseObject(JSONObject.toJSONString(biProfile)).toJSONString(), ProfileModel.class);
            return this.f5BiProfileService.modify(biProfile, context.getTarget(), profileModel);
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 移除
     *
     * @param id
     * @param context
     * @return 操作结果
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除BiProfile", httpMethod = "DELETE")
    @DeleteMapping(value = "/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return this.f5BiProfileService.remove(id, context.getTarget());
    }

    /**
     * 详细信息
     *
     * @param id
     * @return 操作结果
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询BiProfile详情", httpMethod = "GET")
    @GetMapping(value = "/{id}")
    public BocloudResult<BiProfile> detail(@PathVariable(value = Common.ID) Long id) {
        return f5BiProfileService.detail(id);
    }
}
