package com.ruoyi.interfaces.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.interfaces.core.page.Page;
import com.ruoyi.interfaces.core.page.PageUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.interfaces.domain.PtServiceParam;
import com.ruoyi.interfaces.domain.PtServiceParamKey;
import com.ruoyi.interfaces.mapper.PtServiceParamMapper;
import com.ruoyi.interfaces.service.SysCateService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.stream.Collectors;

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

    @Autowired
    private PtServiceParamMapper ptServiceParamMapper;

    @ApiOperation("添加数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxResult insert(@RequestBody PtServiceParam ptServiceParam) {
        return AjaxResult.success(ptServiceParamMapper.insert(ptServiceParam));
    }

    @ApiOperation("添加数据")
    @RequestMapping(value = "/addList", method = RequestMethod.POST)
    public AjaxResult inserts(@RequestBody List<PtServiceParam> serviceParamList) {
        List<PtServiceParam> ptServiceParamList = serviceParamList.stream()
                .filter(p -> StringUtils.isNotBlank(p.getSrvId()))
                .filter(p -> StringUtils.isNotBlank(p.getParamCode()))
                .filter(p -> StringUtils.isNotBlank(p.getParamName()))
                .filter(p -> StringUtils.isNotBlank(p.getParamType()))
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(ptServiceParamList)) {
            return AjaxResult.error();
        }

        String srvId = ptServiceParamList.get(0).getSrvId();
        ptServiceParamMapper.deleteBySrvId(srvId);
        for (int i = 0; i < ptServiceParamList.size(); i++) {
            ptServiceParamList.get(i).setSort(i);
        }
        ptServiceParamList.forEach(ptServiceParamMapper::insert);
        return AjaxResult.success();
    }

    @ApiOperation("数据列表")
    @RequestMapping(value = "/list")
    public Page list(
            @RequestParam(required = true) String srvId,
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int rows) {
        PageHelper.startPage(page, rows);
        List<PtServiceParam> list = ptServiceParamMapper.listBySrvId(srvId);
        return PageUtils.convert(list);
    }

    @ApiOperation("数据列表")
    @GetMapping(value = "/list/{srvId}")
    public AjaxResult listBysrvId(@PathVariable String srvId) {
        return AjaxResult.success(ptServiceParamMapper.listBySrvId(srvId));
    }

    @ApiOperation("根据主键获取数据")
    @RequestMapping(value = "/get")
    public ModelAndView get(@RequestParam(required = true) String srvId, @RequestParam(required = true) String paramCode) {
        PtServiceParamKey key = new PtServiceParamKey();
        key.setSrvId(srvId);
        key.setParamCode(paramCode);
        PtServiceParam ptServiceParam = ptServiceParamMapper.selectByPrimaryKey(key);
        if (ptServiceParam == null) {
            return null;
        } else {
            ModelAndView mav = new ModelAndView();
            mav.addObject("ptServiceParam", ptServiceParam);
            mav.setViewName("mng/pt_service_param_edit");
            return mav;
        }
    }

    @ApiOperation("根据主键更新数据")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxResult update(@RequestBody PtServiceParam ptServiceParam) {

        return AjaxResult.success(ptServiceParamMapper.updateByPrimaryKeySelective(ptServiceParam));
    }

    @ApiOperation("根据主键删除数据")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public AjaxResult delete(@RequestParam(required = true) String srvId, @RequestParam(required = true) String paramCode) {
        PtServiceParamKey key = new PtServiceParamKey();
        key.setSrvId(srvId);
        key.setParamCode(paramCode);

        ptServiceParamMapper.deleteByPrimaryKey(key);

        return AjaxResult.success();

    }


}
