package com.iformall.controller.mem;

import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.WxScoreRules;
import com.iformall.enums.EnumCreditLockedStatus;
import com.iformall.service.WxScoreRulesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("wxScoreRules")
@Api(description = "成长值积分规则相关接口")
public class WxScoreRulesController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxScoreRulesService wxScoreRulesService;

    @ApiOperation("成长值配置")
    @GetMapping("setting")
    @SystemControllerLog(description = "成长值-配置")
    public ResultData list() {
        logger.debug("[" + getIpAddr() + "] WxScoreRulesController::list");
        WxScoreRules scoreRules = wxScoreRulesService.getScoreRules(getTenantId());
        if (scoreRules.getCreditLocked() == null) {
            scoreRules.setCreditLocked(EnumCreditLockedStatus.OPEN.getCode());
        }
        return new ResultData(scoreRules);
    }

    @ApiOperation("新增接口")
    @PostMapping("add")
    @SystemControllerLog(description = "成长值-积分-配置-新增")
    public ResultData add(@RequestBody WxScoreRules wxScoreRules) {
        logger.debug("[" + getIpAddr() + "] WxScoreRulesController::add");
        wxScoreRules.setTenantId(getTenantId());
        wxScoreRulesService.saveOrUpdate(wxScoreRules);
        return new ResultData();
    }

    @ApiOperation("积分配置")
    @GetMapping("/credit_rules")
    @SystemControllerLog(description = "积分-配置")
    public ResultData creditRulesList() {
        logger.debug("[" + getIpAddr() + "] WxScoreRulesController::list");
        WxScoreRules scoreRules = wxScoreRulesService.getCreditRules(getTenantId());
        return new ResultData(scoreRules);
    }

    @ApiOperation("更新积分开关状态")
    @PostMapping("updateCreditLocked")
    @SystemControllerLog(description = "更新积分开关状态")
    public ResultData 更新积分开关状态(@RequestBody WxScoreRules wxScoreRules) {
        logger.debug("[" + getIpAddr() + "] WxScoreRulesController::updateCreditLocked");
        if (null == wxScoreRules.getId()) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "id不能为空");
        }
        if (null == wxScoreRules.getCreditLocked()) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "creditLocked不能为空");
        }
        wxScoreRules.setTenantId(getTenantId());
        try {
            wxScoreRulesService.updateCreditLocked(wxScoreRules);
        } catch (Exception e) {
            return new ResultData(ErrorCode.DB_FAIL.getCode(), "更新积分开关状态失败");
        }
        return new ResultData();
    }

}
