package com.iformall.controller;

import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.po.*;
import com.iformall.domain.vo.WxCUserVo;
import com.iformall.domain.vo.WxLevelMerchantCVo;
import com.iformall.enums.EnumLevelConfigDiscountStatus;
import com.iformall.service.WxCUserBasicInfoService;
import com.iformall.service.WxCUserService;
import com.iformall.service.WxLevelConfigService;
import com.iformall.service.WxMerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;
import java.util.Map;

/**
 * @author Stormeye
 */
@RestController
@Api(description = "C端会员信息")
@RequestMapping("/api/mem")
public class WxMemController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxCUserService userService;

    @Autowired
    private WxLevelConfigService wxLevelConfigService;

    @Autowired
    private WxCUserBasicInfoService wxCUserBasicInfoService;

    @Autowired
    private WxMerchantService wxMerchantService;

    @ApiOperation(value = "扫C端会员码返回会员信息", notes = "params:{\"memCode\":\"String\"}\n")
    @PostMapping("mem_scan")
    public ResultData memScan(@RequestBody Map<String, String> paramMap) {
        logger.info("memScan" + getIpAddr() + ": " + paramMap.toString());
        String memCode = paramMap.get("memCode");

        if (StringUtils.isBlank(memCode)) {
            logger.error("memCode不能为空");
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "memCode不能为空");
        }

        Long id = null;
        try {
            id = Long.valueOf(memCode);
        } catch (Exception e) {
            logger.error("memCode convert failed");
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "memCode转换失败");
        }

        WxMerchantBUser bUser = getUser();

        WxCUser cUser = userService.getById(id);
        if(cUser == null) {
            return new ResultData(ErrorCode.USER_IS_EMPTY);
        }

        WxCUserVo userVo = new WxCUserVo();
        org.springframework.beans.BeanUtils.copyProperties(cUser, userVo);

        WxCUserBasicInfo wxCUserBasicInfo = wxCUserBasicInfoService.getById(cUser.getId());
        if (wxCUserBasicInfo != null) {
            userVo.setName(wxCUserBasicInfo.getName());
            userVo.setBirthdate(wxCUserBasicInfo.getBirthdate());
            userVo.setSex(wxCUserBasicInfo.getSex());
            userVo.setAddress(wxCUserBasicInfo.getAddress());
        }
        userVo.setLevelName(WxLevelConfigService.DEFAULT_LEVEL);
        userVo.setDiscountRate(WxLevelConfigService.DEFAULT_MERCHANT_DISCOUNT);



        if (userVo.getScore() != null) {
            List<WxLevelConfig> levelList = wxLevelConfigService.getByTenantId(userVo.getTenantId());

            String level = WxLevelConfigService.DEFAULT_LEVEL;
            Long levelId = 0L;
            for(WxLevelConfig levelConfig: levelList) {
                if (userVo.getScore() >= levelConfig.getPoints()) {
                    if(levelConfig.getDiscountEnable().equals(EnumLevelConfigDiscountStatus.ENABLE.getCode()))
                        levelId = levelConfig.getId();
                    level = levelConfig.getLevel();
                }
            }

            userVo.setLevelId(levelId);
            userVo.setLevelName(level);

            WxLevelMerchant levelMerchant = new WxLevelMerchant();
            levelMerchant.setLevelId(levelId);
            levelMerchant.setMerchantId(bUser.getMerchantId());
            List<WxLevelMerchantCVo> levelMerchantList = wxLevelConfigService.findListCVo(levelMerchant);
            if (levelMerchantList.size() > 0) {
                WxLevelMerchantCVo l = levelMerchantList.get(0);
                userVo.setDiscountRate(l.getDiscount());
            } else {
                userVo.setDiscountRate(WxLevelConfigService.DEFAULT_MERCHANT_DISCOUNT);
            }
        }

        return new ResultData(userVo);
    }

}
