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.constants.ProjectConstant;
import cn.pluss.platform.entity.LeshuaMakeMoney;
import cn.pluss.platform.leshuaMakeMoney.LeshuaMakeMoneyService;
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.web.bind.annotation.*;

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


@RestController
@RequestMapping("/leshuaMakeMoney")
@Api(value = "LeshuaMakeMoney控制类", tags = {"LeshuaMakeMoney接口"})
public class LeshuaMakeMoneyController {

    @Resource
    private LeshuaMakeMoneyService leshuaMakeMoneyService;

    @GetMapping
    @ApiOperation(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<LeshuaMakeMoney>> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        if (size == 0) {
            size = ProjectConstant.PAGE_SIZE;
        }
        if (page == 0) {
            page = 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", size);
        map.put("offset", (page - 1) * size);
        List<LeshuaMakeMoney> leshuaMakeMoneyList = leshuaMakeMoneyService.queryLeshuaMakeMoneyPage(map);
        Integer count = leshuaMakeMoneyService.queryLeshuaMakeMoneyPageCount(map);
        //返回参数
        PageInfo<LeshuaMakeMoney> pager = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count, size), page.longValue(), size.longValue(), leshuaMakeMoneyList);
        return ResultGenerator.genSuccessResult(pager);
    }

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

    @GetMapping("/{id}")
    @ApiOperation(value = "获取单个值", notes = "查看单个项目的内容", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询的id", paramType = "path", required = true, dataType = "Integer", defaultValue = "0")
    })
    public Result detail(@PathVariable Long id) {
        LeshuaMakeMoney leshuaMakeMoney = new LeshuaMakeMoney();
        leshuaMakeMoney.setId(id);
        leshuaMakeMoney = leshuaMakeMoneyService.queryLeshuaMakeMoney(leshuaMakeMoney);
        return ResultGenerator.genSuccessResult(leshuaMakeMoney);
    }

}

