package com.ruoyi.interfaces.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.uuid.IdUtils;
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.PtServiceCate;
import com.ruoyi.interfaces.mapper.PtServiceCateMapper;
import io.swagger.annotations.ApiOperation;
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;


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

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PtServiceCateMapper ptServiceCateMapper;

    @ApiOperation("添加数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxResult insert(PtServiceCate ptServiceCate) {

        ptServiceCate.setCateId(IdUtils.simpleUUID());

        int ret = ptServiceCateMapper.insert(ptServiceCate);

        return AjaxResult.success();
    }

    @ApiOperation("数据列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Page list(@RequestParam(required = false, defaultValue = "1") int page,
                     @RequestParam(required = false, defaultValue = "100")
                             int rows

    ) {
        PageHelper.startPage(page, rows);
        PageHelper.orderBy("CATE_ID desc");
        List<PtServiceCate> list = ptServiceCateMapper.selectAll();

        // 取分页信息
        PageInfo<PtServiceCate> pageInfo = new PageInfo<PtServiceCate>(list);

        return PageUtils.convert(pageInfo);
    }

    @ApiOperation("根据主键获取数据")
    @RequestMapping(value = "/get")
    public ModelAndView get(@RequestParam(required = true) String cateId) {
        ModelAndView mav = new ModelAndView();

        PtServiceCate ptServiceCate = ptServiceCateMapper.selectByPrimaryKey(cateId);
        if (ptServiceCate == null) {
            return null;
        } else {
            mav.addObject("ptServiceCate", ptServiceCate);
            mav.setViewName("mng/pt_service_cate_edit");
            return mav;
        }
    }

    @ApiOperation("根据主键更新数据")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxResult update(PtServiceCate ptServiceCate) {
        int ret = ptServiceCateMapper.updateByPrimaryKeySelective(ptServiceCate);
        return AjaxResult.success();
    }

    @ApiOperation("根据主键删除数据")
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public AjaxResult delete(@RequestParam(required = true) String cateId) {

        int ret = 0;

        if (StringUtils.isNotBlank(cateId)) {
            String[] code = cateId.split(",");
            for (String aid : code) {
                ret = ptServiceCateMapper.deleteByPrimaryKey(aid);
            }
        }
        return AjaxResult.success();

    }

}
