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

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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.dto.AchievementDataDto;
import com.yami.shop.distribution.comment.dto.DistributionOrderDto;
import com.yami.shop.distribution.comment.dto.DistributionUserDto;
import com.yami.shop.distribution.comment.dto.DistributionUserSimpleDto;
import com.yami.shop.distribution.comment.model.DistributionLevel;
import com.yami.shop.distribution.comment.model.DistributionLevelSet;
import com.yami.shop.distribution.comment.model.DistributionMsg;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.service.DistributionLevelService;
import com.yami.shop.distribution.comment.service.DistributionLevelSetService;
import com.yami.shop.distribution.comment.service.DistributionUserIncomeService;
import com.yami.shop.distribution.comment.service.DistributionUserService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("/p/distribution/user")
@Api(tags = "分销员接口")
@AllArgsConstructor
public class DistributionUserController {

    private final DistributionUserService distributionUserService;

    private final DistributionUserIncomeService distributionUserIncomeService;

    private final DistributionLevelService distributionLevelService;

    private final DistributionLevelSetService distributionLevelSetService;

    private final MapperFacade mapperFacade;

    @GetMapping("/info")
    @ApiOperation(value = "查看分销中心信息")
    public ResponseEntity<AchievementDataDto> info(@RequestParam(value = "shopId",required = false,defaultValue = "0") Long shopId) {
//        Long shopId = 0L;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        if (Objects.isNull(distributionUser)) {
            throw new GlobalException("您还不是销售员");
        }
        return ResponseEntity.ok(distributionUserService.getAchievementDataDtoById(distributionUser.getDistributionUserId()));
    }

    @GetMapping("/distributionUserOrders")
    @ApiOperation(value = "分页查看分销员佣金明细", notes = "传入分销员id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderBy", value = "时间排序(0升序 1降序) 默认1", required = false, dataType = "Integer")
    })
    public ResponseEntity<IPage<DistributionOrderDto>> getDistributionOrderDtosByDistributionUserId(
            PageParam<DistributionOrderDto> page
    ) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        return ResponseEntity.ok(distributionUserIncomeService.getDistributionOrderDtoByDistributionUserId(page, distributionUser.getDistributionUserId()));
    }

    @GetMapping("/distributionUserInfo")
    @ApiOperation(value = "分销员信息")
    public ResponseEntity<DistributionUserDto> isDistributionUser() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        DistributionUserDto distributionUserDto = mapperFacade.map(distributionUser, DistributionUserDto.class);
        if (distributionUserDto == null) {
            return ResponseEntity.ok().build();
        }
        DistributionLevelSet distributionLevelSet = distributionLevelSetService.getByShopId();
        DistributionLevel levelInfo = distributionLevelService.getLevelInfo(distributionUser.getLevel(), distributionLevelSet.getDistributionLevels());
        distributionUserDto.setLevelName(levelInfo.getName());
        return ResponseEntity.ok(distributionUserDto);
    }

    @GetMapping("/subordinate")
    @ApiOperation(value = "查看下级")
    public ResponseEntity<IPage<DistributionUserSimpleDto>> subordinatePage(PageParam<DistributionMsg> page) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        return ResponseEntity.ok(distributionUserService.getDistributionUserSimpleDtoByParentUserIdPage(page, distributionUser.getDistributionUserId()));
    }


}
