package org.springblade.modules.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.springblade.common.base.BaseEntity;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.YesOrNoEnum;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.distribution.entity.Commission;
import org.springblade.modules.distribution.service.ICommissionService;
import org.springblade.modules.distribution.vo.CommisStatisVO;
import org.springblade.modules.groupon.entity.Embers;
import org.springblade.modules.groupon.service.IEmbersService;
import org.springblade.modules.mall.entity.CategoryInfo;
import org.springblade.modules.platform.service.UserAppService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.entity.UserApp;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.UserAppVO;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(CommonConstant.API_URL+"/commission")
@Api(value = "分佣模块接口", tags = "分佣模块接口")
public class CommissionController extends BladeController {

    @Resource
    private ICommissionService commissionService;

    @Resource
    private IUserService userService;

    @Resource
    private UserAppService userAppService;

    @Resource
    private IEmbersService embersService;

    @PostMapping("/bindParentId")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "绑定上级")
    public R bindParentId(@RequestParam(name = "parentId") Long parentId) {
        User user1 = userService.getById(AuthUtil.getUserId());
        Assert.notNull(user1, "未查询到用户信息，请重启小程序后再试");
        if (parentId.equals(getUser().getUserId())){
            return R.success("");
        }

        Embers one = embersService.lambdaQuery()
                .eq(Embers::getBindUserId, getUser().getUserId())
                .eq(Embers::getIsDelete, 0)
                .eq(Embers::getState,0)
                .one();
        if (Objects.nonNull(one)){
            return R.success("");
        }
        Embers embers = new Embers();
        User user = userService.getById(parentId);
        if (Objects.isNull(user)){
            return R.success("");
        }
        embers.setShareUserId(parentId);
        embers.setBindUserId(getUser().getUserId());
        embers.setUserName(getUser().getNickName());
        embers.setUserTel(getUser().getAccount());
        embers.setState(0);
        embersService.save(embers);
        return R.data(embers);
    }

    @PostMapping("/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询分佣列表")
    public R<List<Commission>> list(@RequestBody Query query) {
        LambdaQueryWrapper<Commission> q = new LambdaQueryWrapper<>();
                q.eq(Commission::getBenefitUserId, getUser().getUserId());
                q.eq(Commission::getIsDelete, 0);
        IPage<Commission> page = commissionService.page(Condition.getPage(query), q);
        List<Commission> records = page.getRecords();
        for (Commission commission : records) {
            User user = userService.getById(commission.getBindUserId());
            commission.setBindUserName(user.getName());
        }
        return R.data(page.getRecords());
    }

    @PostMapping("/commissionUser/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询我的客户列表")
    public R<List<UserAppVO>> commissionUser(@RequestBody Query query) {
        LambdaQueryWrapper<Embers> q = new LambdaQueryWrapper<>();
                q.eq(Embers::getShareUserId, getUser().getUserId());
                q.eq(Embers::getIsDelete,0);
                q.eq(Embers::getState, 0).orderByDesc(BaseEntity::getCreateTime);
        IPage<Embers> page = embersService.page(Condition.getPage(query), q);
        List<Embers> records = page.getRecords();
        List<UserAppVO> list = new ArrayList<>();
        for (Embers embers : records) {
            UserAppVO userAppVO = new UserAppVO();
            User user = userService.getById(embers.getBindUserId());
            userAppVO.setParentId(embers.getShareUserId());
            userAppVO.setBindTime(embers.getCreateTime());
            userAppVO.setUserId(embers.getBindUserId());
            userAppVO.setAvatar(user.getAvatar());
            userAppVO.setPhone(user.getPhone());
            userAppVO.setName(user.getName());
            userAppVO.setRealName(user.getRealName());
            list.add(userAppVO);
        }
        return R.data(list);
    }

    @PostMapping("/statistics")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询我的分佣统计")
    public R<CommisStatisVO> statistics() {
        CommisStatisVO commisStatisVO = commissionService.statistics(getUser().getUserId());
        if (Objects.isNull(commisStatisVO)){
            commisStatisVO = new CommisStatisVO();
        }
        return R.data(commisStatisVO);
    }


}
