package com.czg.controller.user;

import com.czg.account.entity.UserInfo;
import com.czg.market.dto.MkDistributionUserDTO;
import com.czg.market.dto.MkDistributionWithdrawFlowDTO;
import com.czg.market.entity.MkDistributionUser;
import com.czg.market.entity.MkDistributionWithdrawFlow;
import com.czg.market.service.MkDistributionConfigService;
import com.czg.market.service.MkDistributionFlowService;
import com.czg.market.service.MkDistributionUserService;
import com.czg.market.service.MkDistributionWithdrawFlowService;
import com.czg.market.vo.DistributionCenterShopVO;
import com.czg.market.vo.InviteUserVO;
import com.czg.market.vo.MkDistributionConfigVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.task.DistributionTask;
import com.czg.utils.AssertUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 全民股东相关
 *
 * @author Administrator
 */
@Slf4j
@RestController
@RequestMapping("/user/distribution")
public class UDistributionController {
    @Resource
    private MkDistributionConfigService distributionConfigService;
    @Resource
    private MkDistributionUserService distributionUserService;
    @Resource
    private MkDistributionWithdrawFlowService withdrawFlowService;
    @Resource
    private MkDistributionFlowService distributionFlowService;
    @Resource
    private DistributionTask distributionTask;

    /**
     * 测试方法-分销流水入账
     */
    @GetMapping("/task")
    public CzgResult<String> task(@RequestParam Long shopId) {
        try {
            distributionTask.deliver(shopId);
        } catch (Exception e) {
            return CzgResult.failure(e.getMessage());
        }
        return CzgResult.success("任务执行成功");
    }


    /**
     * 全民股东=-获取配置
     */
    @GetMapping("/getConfig")
    public CzgResult<MkDistributionConfigVO> getConfig(@RequestParam Long shopId) {
        return CzgResult.success(distributionConfigService.detail(shopId));
    }

    /**
     * 全民股东-首页
     */
    @PostMapping("/centerUser")
    public CzgResult<Map<String, Object>> centerUser() {
        return CzgResult.success(distributionUserService.centerUser(StpKit.USER.getLoginIdAsLong()));
    }

    /**
     * 全民股东界-已开通的店铺
     */
    @GetMapping("/centerUser/activates")
    public CzgResult<Page<DistributionCenterShopVO>> activates(@RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "10") Integer size) {
        return CzgResult.success(distributionUserService.activates(StpKit.USER.getLoginIdAsLong(), page, size));
    }

    /**
     * 全民股东-未开通的店铺
     */
    @GetMapping("/centerUser/unActivates")
    public CzgResult<Page<DistributionCenterShopVO>> unActivates(@RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "10") Integer size) {
        return CzgResult.success(distributionUserService.unActivates(StpKit.USER.getLoginIdAsLong(), page, size));
    }

    /**
     * 全民股东-配置信息
     */
    @GetMapping("/centerConfig")
    public CzgResult<Map<String, Object>> centerConfig(@RequestParam Long shopId) {
        return CzgResult.success(distributionUserService.centerConfig(StpKit.USER.getLoginIdAsLong(), shopId));
    }

    /**
     * 全民股东界面-进入过标识
     */
    @GetMapping("/editIn")
    public CzgResult<Boolean> editIn(@RequestParam Long shopUserId) {
        MkDistributionUser distributionUser = new MkDistributionUser();
        distributionUser.setFirstIn(1);
        distributionUserService.update(distributionUser, QueryWrapper.create().eq(MkDistributionUser::getId, shopUserId));
        return CzgResult.success();
    }

    /**
     * 全民股东-获取邀请码
     */
    @GetMapping("/getInviteCode")
    public CzgResult<String> getInviteCode(@RequestParam Long shopId, @RequestParam Long shopUserId) {
        return CzgResult.success(distributionUserService.getInviteCode(shopId, shopUserId));
    }


    /**
     * 全民股东-实名认证
     */
    @PostMapping("/realNameAuth")
    public CzgResult<Map<String, Object>> realNameAuth(@RequestBody UserInfo userInfo) {
        AssertUtil.isNull(userInfo.getId(), "用户Id不能为空");
        AssertUtil.isNull(userInfo.getRealName(), "用户实名不能为空");
        AssertUtil.isNull(userInfo.getIdCard(), "用户身份信息不能为空");
        distributionUserService.realNameAuth(userInfo);
        return CzgResult.success();
    }

    /**
     * 全民股东-绑定邀请人
     */
    @PostMapping("/bindInviteUser")
    public CzgResult<Map<String, Object>> bindInviteUser(@RequestBody MkDistributionUserDTO param) {
        AssertUtil.isNull(param.getId(), "店铺用户ID不能为空");
        AssertUtil.isNull(param.getShopId(), "店铺ID不能为空");
        AssertUtil.isNull(param.getInviteCode(), "邀请码不能为空");
        distributionUserService.bindInviteUser(param);
        return CzgResult.success();
    }

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

    /**
     * 用户提现
     *
     * @param withdrawFlowDTO 提现信息
     * @return 是否成功
     */
    @PostMapping("/withdraw")
    public CzgResult<Boolean> withdraw(@Validated @RequestBody MkDistributionWithdrawFlowDTO withdrawFlowDTO) {
        return CzgResult.success(distributionUserService.withdraw(StpKit.USER.getLoginIdAsLong(), withdrawFlowDTO));
    }

    /**
     * 提现详情
     */
    @GetMapping("/withdraw/detail")
    public CzgResult<Map<String, Object>> withdraw(@RequestParam Long id) {
        return CzgResult.success(distributionUserService.withdrawDetail(StpKit.USER.getLoginIdAsLong(), id));
    }


    /**
     * 提现记录
     */
    @GetMapping("/withdraw/flow")
    public CzgResult<Page<MkDistributionWithdrawFlow>> withArdwFlow() {
        return CzgResult.success(withdrawFlowService.pageInfo(StpKit.USER.getLoginIdAsLong()));
    }


    /**
     * 收益明细
     *
     * @param status pending待入账 success已入账
     */
    @GetMapping("/distributionFlow")
    public CzgResult<Map<String, Object>> distributionFlow(@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime,
                                                           @RequestParam(required = false) Long shopId, @RequestParam(required = false) String status) {
        return CzgResult.success(distributionFlowService.distributionFlow(StpKit.USER.getLoginIdAsLong(), startTime, endTime, shopId, status));
    }

}
