package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.entity.TopUpPromotion;
import com.ssm.manage.query.TopUpPromotionRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmTopUpPromotionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;


/**
 * 充值类别(TopUpPromotion)表控制层
 *
 * @author makejava
 * @since 2021-12-24 11:54:40
 */
@Api(value = "充值促销Controller",tags = {"充值促销活动"})
@RestController
@RequestMapping("/demay/ssm/topUpPromotion")
@Slf4j
public class SsmTopUpPromotionController {

    @Autowired
    public SsmTopUpPromotionService ssmTopUpPromotionService;

    @ApiOperation(value = "添加充值促销", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/add")
    @Transactional
    public GenericResponse add(@RequestBody TopUpPromotionRequest topUpPromotionRequest){
        Random random = new Random();
        String result="";
        for (int i=0;i<6;i++)
        {
            result+=random.nextInt(10);
        }
        topUpPromotionRequest.setPromotionNum("cx"+result);
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(topUpPromotionRequest.getPromotionNum())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "促销活动编码");
            return genericResponse;
        }
        if (StringUtils.isBlank(topUpPromotionRequest.getPromotionName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "促销活动名称");
            return genericResponse;
        }
        if (StringUtils.isBlank(topUpPromotionRequest.getDiscountFreeAmount().toString())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "促销活动赠送金额折扣未设置");
            return genericResponse;
        }
        if (StringUtils.isBlank(topUpPromotionRequest.getDiscount().toString())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "促销活动充值金额折扣未设置");
            return genericResponse;
        }
        if (StringUtils.isBlank(topUpPromotionRequest.getRechargeMoney().toString())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "促销活动充值金额未设置");
            return genericResponse;
        }
        if (StringUtils.isBlank(topUpPromotionRequest.getGiftMoney().toString())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "促销活动赠送金额未设置");
            return genericResponse;
        }
        int save = ssmTopUpPromotionService.insert(topUpPromotionRequest);
        if(save>0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }else{
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "分页获取充值促销", httpMethod = "POST", response = PageResponse.class, notes = "分页获取充值促销")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public PageResponse<TopUpPromotion> selectByPage(@RequestBody TopUpPromotionRequest topUpPromotionRequest) {
        PageResponse<TopUpPromotion> pageResponse = new PageResponse<>();
        PageInfo<TopUpPromotion> pageInfo;
        try {
            pageInfo = ssmTopUpPromotionService.selectByPage(topUpPromotionRequest);
        } catch (Exception e) {
            log.error("SsmSysUserController.selectByPage:", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

}
