/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.distribution.comment.dto.DistributionLevelDto;
import com.yami.shop.distribution.comment.model.DistributionLevelAuditing;
import com.yami.shop.distribution.comment.model.DistributionLevelSet;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.service.DistributionLevelAuditingService;
import com.yami.shop.distribution.comment.service.DistributionLevelService;
import com.yami.shop.distribution.comment.service.DistributionLevelSetService;
import com.yami.shop.distribution.comment.service.DistributionUserService;
import com.yami.shop.distribution.comment.vo.DistributionLevelConditionsSwitchVO;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.api.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 分销等级接口
 */
@RestController
@RequestMapping("/p/distribution/level")
@Api(tags = "分销等级接口")
@AllArgsConstructor
public class DistributionLevelController {

    private final DistributionLevelService distributionLevelService;

    private final DistributionLevelSetService distributionLevelSetService;

    private final DistributionUserService distributionUserService;

    private final DistributionLevelAuditingService distributionLevelAuditingService;

    private final MapperFacade mapperFacade;



    @GetMapping("/page")
    @ApiOperation(value = "查看店铺设置的等级", notes = "查看店铺设置的等级")
    public ResponseEntity<List<DistributionLevelDto>> page() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);


        DistributionLevelSet distributionLevelSet = distributionLevelSetService.getByShopId();
        List<DistributionLevelDto> distributionLevels = mapperFacade.mapAsList(distributionLevelSet.getDistributionLevels(), DistributionLevelDto.class);

        Integer level = distributionUser.getLevel();
        distributionLevels.forEach(distributionLevelDto -> {
            distributionLevelDto.setIsCurrentLevel(Objects.equals(distributionLevelDto.getLevel(), level));
            distributionLevelDto.setLevelSetConditionsSwitch(distributionLevelSet.getLevelSetConditionsSwitch());
        });
        return ResponseEntity.ok(distributionLevels);
    }

    @PostMapping("/upgrade")
    @ApiOperation(value = "申请升级", notes = "根据店铺id申请")
    public ResponseEntity<Void> upgrade() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        YamiUser user = SecurityUtils.getUser();
        String userId = user.getUserId();

        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(userId, shopId);

        if (distributionUser == null || !Objects.equals(distributionUser.getState(), 1)) {
            throw new GlobalException("你还不是分销员，无法进行升级操作");
        }

        DistributionLevelSet distributionLevelSet = distributionLevelSetService.getByShopId();

        // 当前用户等级
        Integer level = distributionUser.getLevel();

        if (distributionLevelSet.getDistributionLevels().size() <= level) {
            throw new GlobalException("已达到等级上限");
        }

        distributionLevelService.tryingToUpgrade(distributionUser, distributionLevelSet);

        distributionUserService.removeCacheByUserIdAndShopId(userId, shopId);
        return ResponseEntity.ok().build();
    }


}
