package com.buy507.mall.controller;

import cn.hutool.core.date.DateTime;
import com.buy507.mall.common.api.CommonPage;
import com.buy507.mall.common.api.CommonResult;
import com.buy507.mall.model.TmsAdminIstrator;
import com.buy507.mall.service.TmsAdminIstratorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@Api(tags = "TmsAdminIstratorController", description = "管理员信息")
@RequestMapping("/administrator")

public class TmsAdminIstratorController {


    @Autowired
    private TmsAdminIstratorService Service;
    public HttpServletRequest request;

    @ApiOperation("分页查询管理员信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<TmsAdminIstrator>> list(
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        List<TmsAdminIstrator> list = Service.list(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("添加管理员")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody TmsAdminIstrator advertise){
        Date date = new Date();
        advertise.setCreateTime(date);
        int count = Service.create(advertise);
         if (count > 0){
            return CommonResult.success(count);
        }else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("获取管理员详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<TmsAdminIstrator> getItem(@PathVariable Long id) {
        TmsAdminIstrator advertise = Service.getItem(id);
        return CommonResult.success(advertise);
    }

    @ApiOperation("修改管理员数据")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody TmsAdminIstrator advertise) {
        int count = Service.update(id, advertise);
        if (count > 0)
        {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除管理员")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = Service.delete(ids);
        if (count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

}
