package com.ruoyi.interfaces.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.interfaces.core.page.Page;
import com.ruoyi.interfaces.core.page.PageParam;
import com.ruoyi.interfaces.core.page.PageUtils;
import com.ruoyi.interfaces.domain.*;
import com.ruoyi.interfaces.domain.vo.MdModelInfoVo;
import com.ruoyi.interfaces.mapper.PtServiceMapper;
import com.ruoyi.interfaces.mapper.PtServiceParamMapper;
import com.ruoyi.interfaces.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/pt/service")
public class PtServiceController extends BaseController {

    @Autowired
    private PtServiceService ptServiceService;

    @Autowired
    private PtServiceMapper ptServiceMapper;

    @Autowired
    private PtServiceParamMapper ptServiceParamMapper;

    @Autowired
    private IPtServiceReturnService ptServiceReturnService;

    @Autowired
    private SysCateService sysCateService;

    @Autowired
    private PtServiceFileService ptServiceFileService;

    @Autowired
    private IPtServiceLogService ptServiceLogService;

    @ApiOperation("添加数据")
    @PostMapping(value = "/save")
    public AjaxResult save(@RequestBody PtService ptService) {
        if (ptService.getViewNum() == null) {  //viewNum不能为空
            ptService.setViewNum(0);
        }
        ptService.setAudit("0");
        boolean save = ptServiceService.save(ptService);
        if (save) {
            return AjaxResult.success(ptService);

        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @ApiOperation("根据主键删除数据")
    @DeleteMapping("/{srvId}")
    public AjaxResult delete(@PathVariable String srvId) {
        int ret = 0;
        if (StringUtils.isNotBlank(srvId)) {
            String[] code = srvId.split(",");
            for (String aid : code) {
                ret = ptServiceMapper.deleteByPrimaryKey(aid);
            }
        }
        return AjaxResult.success();
    }

    @ApiOperation("数据列表")
    @RequestMapping(value = "/cate_list", method = RequestMethod.GET)
    public List<SysCate> list(String tabName) {
        SysCate criteria = new SysCate();
        criteria.setCateTable(tabName);
        return sysCateService.findAllO(criteria);
    }

    /**
     * 获取模型关联服务列表
     *
     * @param par
     * @return
     */
    @GetMapping("/modelService")
    public AjaxResult modelService(MdModelInfoVo par) {
        return AjaxResult.success(ptServiceService.modelService(par));
    }


    @ApiOperation("根据主键获取数据")
    @RequestMapping(value = "/get")
    public AjaxResult get(@RequestParam String srvId) {
        PtService ptService = ptServiceMapper.selectByPrimaryKey(srvId);
        ptService.setViewNum(ptService.getViewNum() + 1);
        ptServiceMapper.updateByPrimaryKeySelective(ptService);

        List<PtServiceParam> list = ptServiceParamMapper.selectAll(srvId);
        List<PtServiceReturn> returnList = ptServiceReturnService.selectPtServiceReturnBySrvId(srvId);
        List<PtServiceFile> fileList = ptServiceFileService.selectAll(srvId);
        String url = "/gxpt/api/service/" + ptService.getSrvId() + "/get_data";
        ptService.setUrl(url);
        ptService.setExample(url + "?accessToken='值'");
        Map<String, Object> map = new HashMap<>(3);
        map.put("ptService", ptService);
        map.put("list", list);
        map.put("returnList", returnList);
        map.put("fileList", fileList);
        return AjaxResult.success(map);
    }

    @ApiOperation("根据主键获取数据")
    @RequestMapping(value = "/get/{srvId}")
    public AjaxResult getById(@PathVariable String srvId) {
        PtService ptService = ptServiceMapper.selectByPrimaryKey(srvId);
        List<PtServiceParam> list = ptServiceParamMapper.selectAll(srvId);
        List<PtServiceReturn> returnList = ptServiceReturnService.selectPtServiceReturnBySrvId(srvId);
        List<PtServiceFile> fileList = ptServiceFileService.selectAll(srvId);
        Map<String, Object> map = new HashMap<>(3);
        map.put("ptService", ptService);
        map.put("list", list);
        map.put("returnList", returnList);
        map.put("fileList", fileList);
        return AjaxResult.success(map);
    }

    @ApiOperation("根据主键获取数据")
    @RequestMapping(value = "/get/param")
    public Page getParam(@RequestParam(required = false, defaultValue = "1") int page,
                         @RequestParam(required = false, defaultValue = "100") int rows, String srvId) {
        PageHelper.startPage(page, rows);
        List<PtServiceParam> list = ptServiceParamMapper.selectAll(srvId);
        PageInfo<PtServiceParam> pageInfo = new PageInfo<>(list);
        return PageUtils.convert(pageInfo);
    }

    @PostMapping(value = "/publicList")
    public Page publicList(@RequestBody PageParam<PtService> ptServicePage) {
        PageHelper.startPage(ptServicePage);
        List<PtService> list = ptServiceMapper.selectAllPublic(ptServicePage.getData());
        PageInfo<PtService> pageInfo = new PageInfo<>(list);
        return PageUtils.convert(pageInfo);
    }

    @ApiOperation("根据主键获取数据")
    @PostMapping(value = "/list")
    public Page list(@RequestBody PageParam<PtService> ptServicePage) {
        PageHelper.startPage(ptServicePage);
        PtService service = Optional.ofNullable(ptServicePage.getData()).orElse(new PtService());
        service.setStatus("1");
        List<PtService> list = ptServiceMapper.selectAllPublic(ptServicePage.getData());
        PageInfo<PtService> pageInfo = new PageInfo<>(list);
        return PageUtils.convert(pageInfo);
    }

    @RequestMapping(value = "/getRankingList")
    public AjaxResult getRankingList() {
        return AjaxResult.success(ptServiceService.getRankingList());
    }

    @ApiOperation("查询服务列表")
    @GetMapping(value = "/listBySuccess")
    public AjaxResult listBySuccess(PtService ptService) {
        QueryWrapper<PtService> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "1")
                .like(StringUtils.isNotBlank(ptService.getName()), "name", ptService.getName());
        return AjaxResult.success(ptServiceService.list(queryWrapper));
    }

    @PutMapping()
    public AjaxResult put(@RequestBody PtService ptService) {
        return AjaxResult.success(ptServiceService.put(ptService));
    }



    @PostMapping("/testRun")
    public AjaxResult testRun(@RequestBody PtService ptService) throws IOException {

        PtServiceLog ptServiceLog = new PtServiceLog(ptService);
        Date nowDate = DateUtils.getNowDate();
        ptServiceLog.setTm(nowDate);
        String s = "";
        try {

         s = ptServiceService.testRun(ptService);
        } catch (Exception e){
            return new  AjaxResult(HttpStatus.HTTP_ERROR,s);
        }

        ptServiceLog.setExecTm(
                DateUtils.getNowDate().getTime() - nowDate.getTime());
        ptServiceLog.setReturnText(s);
        ptServiceLogService.insertPtServiceLog(ptServiceLog);
        return success(s);
    }

    @GetMapping("/audit")
    public AjaxResult audit(String[] srvIds,String audit){
        return success(ptServiceService.audit(srvIds,audit));
    }

}
