
package com.tmerclub.cloud.distribution.controller.app;

import com.tmerclub.cloud.api.platform.vo.DistributionRecruitConfigApiVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.distribution.constant.DistributionUserStateEnum;
import com.tmerclub.cloud.distribution.model.DistributionUser;
import com.tmerclub.cloud.distribution.service.DistributionConfigService;
import com.tmerclub.cloud.distribution.service.DistributionUserBindService;
import com.tmerclub.cloud.distribution.service.DistributionUserService;
import com.tmerclub.cloud.distribution.vo.DistributionUserInfoVO;
import com.tmerclub.cloud.distribution.vo.DistributionUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 分销员绑定关系
 *
 * @author cl
 * @date 2021-08-09 14:14:09
 */
@RestController("appDistributionUserBindController")
@RequestMapping("/distributionUserBind")
@Tag(name = "分销员绑定关系")
public class DistributionUserBindController {

    private static final Logger logger = LoggerFactory.getLogger(DistributionUserBindController.class);

    @Autowired
    private DistributionUserBindService distributionUserBindService;
    @Autowired
    private DistributionConfigService distributionConfigService;
    @Autowired
    private DistributionUserService distributionUserService;

    @PostMapping("/bindUser")
    @Operation(summary = "绑定用户", description = "根据分销员卡号绑定用户")
    @Parameters(@Parameter(name = "distributionUserId", description = "卡号", required = true))
    public ServerResponseEntity<Void> bindUser(@RequestParam Long distributionUserId) {

        Long userId = AuthUserContext.get().getUserId();
        logger.info("绑定用户distributionUserId:{}", distributionUserId);

        if (Objects.isNull(distributionUserId)) {
            logger.error("获取推广员信息失败");
            return ServerResponseEntity.success();
        }

        DistributionUser shareUser = distributionUserService.getByDistributionUserId(distributionUserId);

        if (shareUser == null) {
            logger.error("获取推广员信息失败");
            return ServerResponseEntity.success();
        }
        if (!Objects.equals(DistributionUserStateEnum.NORMAL.value(), shareUser.getState())) {
            logger.error("推广员状态异常");
            return ServerResponseEntity.success();
        }
        DistributionRecruitConfigApiVO recruitConfigApiVO = distributionConfigService.getDistributionRecruitConfig();
        if (recruitConfigApiVO.getState() == 0) {
            logger.error("推广计划已关闭");
            return ServerResponseEntity.success();
        }
        // 绑定分销员
        int type = 0;
        ServerResponseEntity<DistributionUser> serverResponse = distributionUserBindService.bindDistribution(shareUser, userId, type);
        if (!serverResponse.isSuccess()) {
            logger.error(serverResponse.getMsg());
        }
        return ServerResponseEntity.success();
    }

    @GetMapping("/bindUserList")
    @Operation(summary = "绑定用户列表", description = "获取分销员所绑定的用户列表")
    @Parameters(@Parameter(name = "userType", description = "类型 1 我的用户， 2 我的好友"))
    public ServerResponseEntity<List<DistributionUserInfoVO>> bindUserList(@RequestParam("userType") Integer userType) {
        Long userId = AuthUserContext.get().getUserId();
        DistributionUserVO user = distributionUserService.getByUserId(userId);
        List<DistributionUserInfoVO> resultList = distributionUserService.getDistributionMyFriend(user, userType);
        //distributionUserBindService.bindUserList(pageDTO, userId)
        return ServerResponseEntity.success(resultList);
    }
}
