package io.renren.modules.generator.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.generator.entity.AlgorithmEntity;
import io.renren.modules.generator.service.impl.AlgorithmServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-08-06 15:33:20
 */
@RestController
@RequestMapping("generator/algorithm")

public class AlgorithmController {
    @Autowired
    private AlgorithmServiceImpl algorithmService;

    /**
     * 列表 通过关键字进行获取列表，
     * 这里增加了通过算法种类来进行数据的获取，并进行页面获取
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = algorithmService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{algorithmId}")
    //@RequiresPermissions("generator:algorithm:info")
    public R info(@PathVariable("algorithmId") Long algorithmId) {
        AlgorithmEntity algorithm = algorithmService.getById(algorithmId);

        return R.ok().put("algorithm", algorithm);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("generator:algorithm:save")
    public R save(@RequestBody AlgorithmEntity algorithm) {
        algorithmService.save(algorithm);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //   @RequiresPermissions("generator:algorithm:update")
    public R update(@RequestBody AlgorithmEntity algorithm) {
        System.out.println(algorithm);
        algorithmService.updateById(algorithm);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("generator:algorithm:delete")
    public R delete(@RequestBody Long[] algorithmIds) {
        algorithmService.removeByIds(Arrays.asList(algorithmIds));

        return R.ok();
    }

}
