package cn.pluss.platform.controller;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.bestNewActivity.BestNewActivityService;
import cn.pluss.platform.entity.BestNewActivity;
import cn.pluss.platform.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author DJH
 */
@RestController
@RequestMapping("/bestNewActivity")
@Transactional
@Api(value = "BestNewActivity控制类", tags = {"BestNewActivity接口"})
public class BestNewActivityController {

    @Resource
    private BestNewActivityService bestNewActivityService;

    @GetMapping
    @ApiOperation(tags = {"页面-最新活动"}, value = "获取全部", notes = "返回分页过后的数据", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询页码", paramType = "query", dataType = "Integer", defaultValue = "0"),
            @ApiImplicitParam(name = "size", value = "每页数据量", paramType = "query", dataType = "Integer", defaultValue = "0")
    })
    public Result<PageInfo<BestNewActivity>> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, @RequestParam(defaultValue = "3") String type) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", size);
        map.put("offset", (page - 1) * size);
        map.put("type", type);
        List<BestNewActivity> bestNewActivityList = bestNewActivityService.queryBestNewActivityPage(map);
        Integer count = bestNewActivityService.queryBestNewActivityPageCount(map);
        //返回参数
        PageInfo pager = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count, size), page.longValue(), size.longValue(), bestNewActivityList);
        return ResultGenerator.genSuccessResult(pager);
    }

    @PostMapping
    @ApiOperation(value = "添加数据", notes = "添加新的数据", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "BestNewActivity", value = "待添加的BestNewActivity实例", paramType = "body", dataType = "BestNewActivity", required = true)
    })
    public Result add(@RequestBody BestNewActivity bestNewActivity) {
        bestNewActivityService.saveBestNewActivity(bestNewActivity);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据", notes = "根据id删除数据", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询的id实例", paramType = "path", dataType = "Integer", required = true)
    })
    public Result delete(@PathVariable Integer id) {
        BestNewActivity bestNewActivity = new BestNewActivity();
        bestNewActivity.setId(id);
        bestNewActivityService.deleteBestNewActivity(bestNewActivity);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    @ApiOperation(value = "更新数据", notes = "根据内容更新数据", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "BestNewActivity", value = "更新的BestNewActivity实例", paramType = "body", dataType = "BestNewActivity", required = true)
    })
    public Result update(@RequestBody BestNewActivity bestNewActivity) {
        bestNewActivityService.updateBestNewActivity(bestNewActivity);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "获取单个值", notes = "传id", httpMethod = "POST")
    public Result detail(@RequestBody BestNewActivity bestNewActivity) {
        bestNewActivity = bestNewActivityService.queryBestNewActivity(bestNewActivity);
        return ResultGenerator.genSuccessResult(bestNewActivity);
    }

}

