package com.czg.controller.admin;

import com.czg.annotation.Debounce;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.log.annotation.OperationLog;
import com.czg.market.dto.MkDistributionUserDTO;
import com.czg.market.entity.MkDistributionUser;
import com.czg.market.service.MkDistributionUserService;
import com.czg.market.vo.InviteUserVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import com.czg.validator.group.DefaultGroup;
import com.czg.validator.group.UpdateGroup;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 分销员
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/distribution/user")
public class DistributionUserController {
    @Resource
    private MkDistributionUserService distributionUserService;

    /**
     * 分销员:获取分销员分页列表
     */
    @GetMapping
    public CzgResult<Page<MkDistributionUserDTO>> getDistributionUser(MkDistributionUserDTO param) {
        Long shopId = StpKit.USER.getShopId();
        param.setShopId(shopId);
        return CzgResult.success(distributionUserService.getDistributionUser(param));
    }

    /**
     * 分销员:获取邀请人分页列表
     */
    @GetMapping("/inviteUser")
    public CzgResult<Page<InviteUserVO>> getInviteUser(
            @RequestParam Long id,
            @RequestParam(required = false) Long shopUserId,
            @RequestParam(required = false) Long distributionLevelId,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size) {
        AssertUtil.isNull(id, "邀请人ID");
        Long shopId = StpKit.USER.getShopId();
        return CzgResult.success(distributionUserService.getInviteUser(id,shopId, shopUserId, distributionLevelId, page, size));
    }

    /**
     * 分销员:添加分销员
     *
     * @return 是否成功
     */
    @Debounce
    @PostMapping
    @OperationLog("分销员-添加")
    @SaAdminCheckPermission(parentName = "分销相关",value = "distribution:user:add", name = "分销员添加")
    public CzgResult<Void> addDistributionUser(@RequestBody MkDistributionUser param) {
        Long shopId = StpKit.USER.getShopId();
        param.setShopId(shopId);
        distributionUserService.addDistributionUser(param);
        return CzgResult.success();
    }


    /**
     * 分销员:修改
     */
    @PutMapping
    @OperationLog("分销员-修改")
    @SaAdminCheckPermission(parentName = "分销相关",value = "distribution:user:update", name = "分销员修改/分销员修改")
    public CzgResult<Void> updateDistributionUser(@RequestBody @Validated({UpdateGroup.class, DefaultGroup.class}) MkDistributionUserDTO dto) {
        Long shopId = StpKit.USER.getShopId();
        dto.setShopId(shopId);
        distributionUserService.updateDistributionUserById(dto);
        return CzgResult.success();
    }


    @PostMapping("/resetLevel")
    @OperationLog("分销员-重置分销组")
    @SaAdminCheckPermission(parentName = "分销相关",value = "distribution:user:update", name = "分销员修改/分销员修改")
    public CzgResult<Void> resetDistributionUserLevel(@RequestBody MkDistributionUserDTO dto) {
        Long shopId = StpKit.USER.getShopId();
        dto.setShopId(shopId);
        distributionUserService.resetDistributionUserLevel(dto);
        return CzgResult.success();
    }

    /**
     * 分销员:删除
     */
    @DeleteMapping
    @OperationLog("分销员-删除")
    @SaAdminCheckPermission(parentName = "分销相关",value = "distribution:user:delete", name = "分销员删除")
    public CzgResult<String> deleteDistributionUser(@RequestParam Long id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        Long shopId = StpKit.USER.getShopId();
        distributionUserService.deleteDistributionUser(id,shopId);
        return CzgResult.success();
    }


}
