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.Level;
import cn.pluss.platform.entity.SystemConfig;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.level.LevelService;
import cn.pluss.platform.systemConfig.SystemConfigService;
import cn.pluss.platform.userApp.UserAppService;
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 lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author DJH
 */
@RestController
@RequestMapping("/level")
@RequiredArgsConstructor
@Api(value = "Level控制类", tags = {"Level接口"})
public class LevelController {

    private final UserAppService userAppService;
    private final LevelService levelService;

    private final SystemConfigService configService;

    @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<Level>> 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<Level> levelList = levelService.queryLevelPage(map);
        Integer count = levelService.queryLevelPageCount(map);
        //返回参数
        PageInfo<Level> pager = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count, size), page.longValue(), size.longValue(), levelList);
        return ResultGenerator.genSuccessResult(pager);
    }

    @PostMapping
    @ApiOperation(value = "添加数据", notes = "添加新的数据", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Level", value = "待添加的Level实例", paramType = "body", dataType = "Level", required = true)
    })
    public Result add(@RequestBody Level level) {
        levelService.saveLevel(level);
        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 Long id) {
        Level level = new Level();
        level.setId(id);
        levelService.deleteLevel(level);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    @ApiOperation(value = "更新数据", notes = "根据内容更新数据", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Level", value = "更新的Level实例", paramType = "body", dataType = "Level", required = true)
    })
    public Result update(@RequestBody Level level) {
        levelService.updateLevel(level);
        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<Object> detail(@PathVariable Long id) {
        Level level = new Level();
        level.setId(id);
        level = levelService.queryLevel(level);
        return ResultGenerator.genSuccessResult(level);
    }


    @GetMapping("/upgradeImg")
    public Result<String> getUpgradeImg() {
        UserApp userApp = userAppService.queryUserAppByToken();

        SystemConfig systemConfig = new SystemConfig();
        if (userApp.getLevel() == 1) {
            systemConfig.setPropertyKey("twoUpVipImg");
        } else if (userApp.getLevel() == 2) {
            systemConfig.setPropertyKey("threeUpVipImg");
        } else {
            return ResultGenerator.genSuccessResult("当前商户已是最高级别", null);
        }

        systemConfig = configService.querySystemConfig(systemConfig);
        return ResultGenerator.genSuccessResult(systemConfig.getPropertyValue());
    }
}

