package com.fjs.scenic.controller.common;

import com.fjs.scenic.service.common.BaseService;
import com.fjs.scenic.utils.ReturnResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * This class is used for ...
 *
 * @author lfj
 * @version 1.0
 * @CreateDate ${date} ${time}
 */
public class BaseController<T> {

    @Autowired
    private BaseService<T> service;

    /**
     * @param record 条件
     * @param currentPage 页码
     * @param pageSize 每页显示数量
     * @return
     */
    @PostMapping("/page")
    public ReturnResult page(T record, int currentPage, int pageSize) {
        currentPage = (currentPage==0)?1:currentPage;
        pageSize = (pageSize==0)?1:pageSize;
        PageHelper.startPage(currentPage, pageSize);
        List<T> manages = service.page(record);
        PageInfo<T> pageInfo = new PageInfo<>(manages);
        return ReturnResult.ok(pageInfo);
    }

    @PostMapping("/add")
    public ReturnResult add(T record) {
        return ReturnResult.ok(service.add(record));
    }

    @PostMapping("/update")
    public ReturnResult update(T record) {
        return ReturnResult.ok(service.update(record));
    }

    @PostMapping("/del")
    @ResponseBody
    public ReturnResult del(int id) {
        return ReturnResult.ok(service.del(id));
    }

    /**
     * 根据ID查询数据信息
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public ReturnResult get(@PathVariable("id") Integer id) {
        return ReturnResult.ok(service.get(id));
    }
}
