AccountController.java 2.29 KB
package vion.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import vion.Global;
import vion.enums.ResultEnum;
import vion.model.Account;
import vion.model.Store;
import vion.service.IAccountSerrvice;
import vion.utils.ResultUtil;

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

@Controller
@RequestMapping(Global.BASE_URL)
@Slf4j
public class AccountController {

    @Autowired
    private IAccountSerrvice accountSerrvice;

    @GetMapping("/accounts")
    @ResponseBody
    public Object getAccountList(@RequestParam(name = "pageNum")Integer pageNum,
                                 @RequestParam(name = "pageSize")Integer pageSize){

        //pageNum当前页,pageSize每页条数
        Page page= PageHelper.startPage(pageNum,pageSize);
        List<Account> accountList=accountSerrvice.list();

        accountList.sort(Comparator.comparing(Account::getId));
        PageInfo pageInfo=new PageInfo<>(page);
        pageInfo.setList(accountList);

        if (accountList != null) {


            return ResultUtil.success(pageInfo);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @GetMapping("/account")
    @ResponseBody
    public Object getProductByID(@RequestParam(name = "id")Integer id){

        Account account=accountSerrvice.getById(id);//accountDao.getAccountById(id);

        if (account != null) {
            return ResultUtil.success(account);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @PostMapping("/accounts")
    @ResponseBody
    public Object saveOrUpdate(@RequestBody Account data) {

        try {
            if(data.getId()==null){
                data.setCreateTime(new Date());
            }else{
                data.setModifyTime(new Date());
            }
            Account account= accountSerrvice.saveAndReturn(data);
            return ResultUtil.success(account);

        }catch (Exception e) {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }
}