package com.ruoyi.interfaces.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.PtAlgorithm;
import com.ruoyi.interfaces.domain.SysCate;
import com.ruoyi.interfaces.mapper.PtAlgorithmMapper;
import com.ruoyi.interfaces.service.SysCateService;
import io.swagger.annotations.ApiOperation;

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

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/pt/algorithm")
public class PtAlgorithmController extends BaseController {


    @Autowired
    private PtAlgorithmMapper ptMapper;

    @Autowired
    private SysCateService sysCateService;

    @ApiOperation("添加算法")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public AjaxResult insert(PtAlgorithm p) {
        p.setDate(new Date());
        p.setId(ptMapper.selectMaxId());
        int insert = ptMapper.insert(p);
        if (insert > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


    @ApiOperation("去编辑算法页面")
    @RequestMapping(value = "/to_edit", method = RequestMethod.GET)
    public ModelAndView toEdit(Integer id) {
        ModelAndView model = new ModelAndView();
        PtAlgorithm pt = new PtAlgorithm();
        pt.setId(id);
        model.addObject("ptalorithm", ptMapper.selectAll(pt).get(0));
        model.setViewName("mng/pt_alorithm_edit");
        return model;
    }


    @ApiOperation("修改算法")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxResult update(PtAlgorithm p) {
        p.setDate(new Date());
        int update = ptMapper.update(p);
        if (update > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


    @ApiOperation("删除算法")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public AjaxResult delete(String ids) {
        String[] strs = ids.split(",");
        int deletes = 0;
        for (String str : strs) {
            deletes += ptMapper.delete(Integer.parseInt(str));
        }
        if (deletes == strs.length) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


    @ApiOperation("数据列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Page getList(@RequestParam(required = false, defaultValue = "1") int page,
                        @RequestParam(required = false, defaultValue = "100") int rows, PtAlgorithm ptAlgorithm) {
        PageHelper.startPage(page, rows);
        PageHelper.orderBy("TM desc");
        List<PtAlgorithm> list = ptMapper.selectAll(ptAlgorithm);//dao.(criteria);//查询
        // 取分页信息
        PageInfo<PtAlgorithm> pageInfo = new PageInfo<PtAlgorithm>(list);
        return PageUtils.convert(pageInfo);
    }

    @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);//.selectByCriteria(criteria);//查询
    }
}
