/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
 *
 */
package net.northking.cctp.testbug.api;

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;

import net.northking.cctp.testbug.api.service.BugSearchApiService;
import net.northking.cctp.testbug.dto.BugSearchAddDto;
import net.northking.cctp.testbug.dto.BugSearchDetailDto;
import net.northking.cctp.testbug.dto.BugSearchUpdateDto;
import net.northking.cctp.testbug.dto.bug.SearchDetailDto;
import net.northking.cctp.testbug.feign.client.PlatFormFeignClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 缺陷条件搜索表 => Restful 类型 HTTP/HTTPS 接口
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */
@Api(tags = "缺陷条件搜索表")
@RequestMapping("/v1/BugSearchs")
@RestController
public class BugSearchRestfulCtrl {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(BugSearchRestfulCtrl.class);

    @Autowired
    private BugSearchApiService apiService;

    @Autowired
    private PlatFormFeignClient platFormFeignClient;

    /**
     * Restful接口-post： 新增缺陷条件搜索表
     *
     * @param inputDto 接口数据传输对象
     * @return 主键
     */
    @ApiOperation(value = "新增：缺陷条件搜索表")
    @RequiredPermissions("ui-mtDefectManage")
    @PostMapping(
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> add(
            @RequestBody BugSearchAddDto inputDto) {
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        String uuid = apiService.add(inputDto);
        return wrapper.success(uuid);
    }

    /**
     * Restful接口-post： 新增缺陷条件搜索表
     *
     * @param inputDto 接口数据传输对象
     * @return 主键
     */
    @ApiOperation(value = "新增：缺陷条件搜索表2")
    @PostMapping(value = "/addSearch",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> addDto(
            @RequestBody SearchDetailDto inputDto) {
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        String uuid = apiService.addDto(inputDto);
        return wrapper.success(uuid);
    }

    /**
     * Restful接口-put： 更新缺陷条件搜索表
     *
     * @param inputDto 接口数据传输对象
     * @return 修改的记录数量
     */
    @ApiOperation(value = "更新：缺陷条件搜索表")
    @PutMapping(
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> update(
            @RequestBody BugSearchUpdateDto inputDto) throws IllegalAccessException {
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer count = apiService.updateByPK(inputDto);
        return wrapper.success(count);
    }

    /**
     * Restful接口-put： 更新缺陷条件搜索表
     *
     * @param inputDto 接口数据传输对象
     * @return 修改的记录数量
     */
    @ApiOperation(value = "更新：缺陷条件搜索表2")
    @PutMapping(value = "/updateSearch",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> updateSearch(
            @RequestBody SearchDetailDto inputDto) throws IllegalAccessException {
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer count = apiService.updateSearch(inputDto);
        return wrapper.success(count);
    }

    /**
     * Restful接口-get： 获取缺陷条件搜索表
     *
     * @param uuid 主键
     * @return 接口数据传输对象
     */
    @ApiOperation(value = "获取：缺陷条件搜索表")
    @GetMapping(
            value = "/{uuid}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<BugSearchDetailDto> findByPK(
            @PathVariable("uuid") String uuid) {
        ResultWrapper<BugSearchDetailDto> wrapper = new ResultWrapper<>();
        BugSearchDetailDto dto = null;
        dto = apiService.findByPK(uuid);
        if (null == dto) {
            return wrapper.success(null);
        }
        BugSearchDetailDto bugSearchDetailDto1 = JSON.parseObject(dto.getExpands(), BugSearchDetailDto.class);
        bugSearchDetailDto1.setExpands(null);
        if ("".equals(bugSearchDetailDto1.getBugFrom())) {
            bugSearchDetailDto1.setBugFrom("0");
        }
        return wrapper.success(bugSearchDetailDto1);
    }

    @ApiOperation(value = "获取：缺陷条件搜索表2")
    @RequiredPermissions("ui-mtDefectManage")
    @GetMapping(
            value = "/getById/{uuid}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<SearchDetailDto> getById(
            @PathVariable("uuid") String uuid) {
        ResultWrapper<SearchDetailDto> wrapper = new ResultWrapper<>();
        BugSearchDetailDto dto = null;
        dto = apiService.findByPK(uuid);
        if (null == dto) {
            return wrapper.success(null);
        }
        System.out.println(JSONObject.parse(dto.getExpands()));
        SearchDetailDto detailDto = JSON.parseObject(dto.getExpands(), SearchDetailDto.class);
        if(StrUtil.isNotBlank(detailDto.getCreator())){
            String creatorName = platFormFeignClient.getUser(detailDto.getCreator());
            detailDto.setCreatorName(creatorName);
        }
        if(StrUtil.isNotBlank(detailDto.getAssignTo())){
            String assignToName = platFormFeignClient.getUser(detailDto.getAssignTo());
            detailDto.setAssignToName(assignToName);
        }

        return wrapper.success(detailDto);
    }

    /**
     * Restful接口-delete： 删除缺陷条件搜索表
     *
     * @param uuid 主键
     * @return 删除的记录数量
     */
    @ApiOperation(value = "删除：缺陷条件搜索表")
    @DeleteMapping(
            value = "/{uuid}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> deleteByPK(
            @PathVariable("uuid") String uuid) {
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer count = apiService.deleteByPK(uuid);
        return wrapper.success(count);
    }
}
