package com.iformall.controller.market;

import com.github.pagehelper.PageInfo;
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.WxCouponSendConfig;
import com.iformall.enums.EnumCouponSendSendType;
import com.iformall.enums.EnumValidStatus;
import com.iformall.service.WxCouponSendConfigService;
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.*;

import java.util.Date;

@RestController
@RequestMapping("wxCouponSendConfig")
@Api(description = "注劵配置接口")
public class WxCouponSendConfigController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxCouponSendConfigService wxCouponSendConfigService;

    @ApiOperation("获取开关")
    @GetMapping("get")
    @SystemControllerLog(description = "发券开关-获取")
    public ResultData getCouponSendConfig(@ModelAttribute WxCouponSendConfig wxCouponSendConfig) {
        logger.debug("[" + getIpAddr() + "] WxMallConfigController::getCouponSendConfig");
        if (wxCouponSendConfig == null || wxCouponSendConfig.getSendType() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);

        wxCouponSendConfig.setTenantId(getTenantId());
        PageInfo<WxCouponSendConfig> page = wxCouponSendConfigService.listAsPage(wxCouponSendConfig, 1, 1);
        if (page.getSize() > 0) {
            return new ResultData(page.getList().get(0));
        } else {
            //配置不存在，添加默认配置(停用)
            wxCouponSendConfig.setRemark(EnumCouponSendSendType.getEnum(wxCouponSendConfig.getSendType()).getMessage() + "开关");
            wxCouponSendConfig.setValue(1);
            wxCouponSendConfigService.saveOrUpdate(wxCouponSendConfig);
            return new ResultData(wxCouponSendConfig);
        }
    }


    @PostMapping("update")
    @ApiOperation("修改开关")
    @SystemControllerLog(description = "发券开关-修改开关")
    public ResultData updateCouponSendConfig(@RequestBody WxCouponSendConfig wxCouponSendConfig) {
        logger.debug("[" + getIpAddr() + "] WxMallConfigController::updateStopCarConpon");
        if (wxCouponSendConfig == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        if (wxCouponSendConfig.getId() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        if (wxCouponSendConfig.getSendType() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        if (wxCouponSendConfig.getValue() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        wxCouponSendConfig.setTenantId(getTenantId());
        wxCouponSendConfig.setUpdateTime(new Date());
        wxCouponSendConfigService.update(wxCouponSendConfig);
        return new ResultData();
    }




}
