package com.iformall.controller.mem;

import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.base.BaseEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxLevelConfig;
import com.iformall.service.WxLevelConfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

@RestController
@RequestMapping("wxLevelConfig")
@Api(description = "等级权益相关接口")
public class WxLevelConfigController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxLevelConfigService wxLevelConfigService;

    @ApiOperation("分页列表接口")
    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "会员等级权益-列表")
    public ResultData list(@ModelAttribute WxLevelConfig wxLevelConfig, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxLevelConfigController::list");
        if (null == wxLevelConfig) wxLevelConfig = new WxLevelConfig();
        wxLevelConfig.setTenantId(getTenantId());
        wxLevelConfig.setSortColumns(BaseEntity.SortField.Points_ASC);
        final PageInfo<WxLevelConfig> page = wxLevelConfigService.listAsPage(wxLevelConfig, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("新增接口")
    @PostMapping("add")
    @SystemControllerLog(description = "会员等级权益-新增")
    public ResultData add(@RequestBody List<WxLevelConfig> levelConfigs) {
        logger.debug("[" + getIpAddr() + "] WxLevelConfigController::add");
        String tenantId = getTenantId();
        try {
            wxLevelConfigService.batchSave(tenantId, levelConfigs);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ResultData(ErrorCode.DB_FAIL);
        }
        return new ResultData();
    }

    @ApiOperation("更改会员权益")
    @PostMapping("updateCapabilities")
    @SystemControllerLog(description = "会员等级权益-更改会员权益")
    public ResultData updateCapabilities(@RequestBody List<WxLevelConfig> levelConfigs) {
        logger.debug("[" + getIpAddr() + "] WxLevelConfigController::add");
        String tenantId = getTenantId();
        try {
            wxLevelConfigService.batchUpdateCapabilities(tenantId, levelConfigs);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ResultData(ErrorCode.DB_FAIL);
        }
        return new ResultData();
    }

    @ApiOperation("根据id删除接口")
    @PostMapping("/del")
    @SystemControllerLog(description = "会员等级权益-删除")
    public ResultData delete(@RequestBody WxLevelConfig wxLevelConfig) {
        logger.debug("[" + getIpAddr() + "] WxLevelConfigController::delete");
        wxLevelConfigService.deleteById(wxLevelConfig.getId());
        return new ResultData(Result.SUCCESS, "删除成功", null);
    }

    @ApiOperation("根据id查询接口")
    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "会员等级权益-获取")
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxLevelConfigController::findById");
        return new ResultData(Result.SUCCESS, "查询成功", wxLevelConfigService.getById(id));
    }

}
