package com.sqx.modules.userExtension.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.userExtension.dao.UserExtensionDao;
import com.sqx.modules.userExtension.entity.UserExtension;
import com.sqx.modules.userExtension.service.UserExtensionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserExtensionServiceImpl extends ServiceImpl<UserExtensionDao, UserExtension> implements UserExtensionService {

    @Autowired
    private UserExtensionDao extensionDao;
    @Autowired
    private UserService userService;

    @Override
    public Result addUserExtension(UserExtension userExtension) {
        UserEntity userEntity = userService.getById(userExtension.getUserId());
        if (userEntity == null) {
            return Result.error("用户不存在");
        }
        //查询用户是否已经提交过了
        UserExtension extension = extensionDao.selectOne(new QueryWrapper<UserExtension>().eq("user_id", userExtension.getUserId()));
        if (extension != null) {
            return Result.error("请勿重复提交");
        }
        userExtension.setCreateTime(new Date());
        userExtension.setStatus(1);
        return Result.upStatus(extensionDao.insert(userExtension));
    }

    @Override
    public Result updateUserExtension(UserExtension userExtension) {
        UserEntity userEntity = userService.getById(userExtension.getUserId());
        if (userEntity == null) {
            return Result.error("用户不存在");
        }
        //查询用户是否已经提交过了
        UserExtension extension = extensionDao.selectOne(new QueryWrapper<UserExtension>().eq("user_id", userExtension.getUserId()));
        if (extension == null) {
            return Result.error("当前申请信息不存在");
        }
        if (extension.getStatus() == 2) {
            return Result.error("您已通过审核,无需再次提交");
        }
        userExtension.setStatus(1);
        return Result.upStatus(extensionDao.updateById(userExtension));
    }

    @Override
    public UserExtension getUserExtensionInfo(Long userId) {
        return extensionDao.selectOne(new QueryWrapper<UserExtension>().eq("user_id", userId));
    }

    @Override
    public IPage<UserExtension> getExtensionList(Integer page, Integer limit, UserExtension userExtension) {
        Page<UserExtension> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
             pages = new Page<>();
             pages.setSize(-1);
        }
        return extensionDao.selectPage(pages, new QueryWrapper<>(userExtension).orderByDesc("create_time"));
    }

    @Override
    public Result examine(Integer status, Long extensionId, String remarks, String divideScale, String divideTwoScale) {
        UserExtension userExtension = extensionDao.selectById(extensionId);
        if (userExtension == null) {
            return Result.error("当前申请信息不存在");
        }
        userExtension.setRemarks(remarks);
        if (status == 2) {
            if (divideScale == null) {
                return Result.error("请设置推广员分成比例");
            }
            //修改用户分成比例
            UserEntity entity = new UserEntity();
            if (StringUtils.isNotBlank(divideScale)) {
                entity.setDivideScale(divideScale);
            }
            if (StringUtils.isNotBlank(divideTwoScale)) {
                entity.setDivideTwoScale(divideTwoScale);
            }
            entity.setUserId(userExtension.getUserId());
            userService.updateById(entity);
            //修改申请表
            userExtension.setStatus(2);
            extensionDao.updateById(userExtension);
            return Result.success("已同意");
        } else {
            userExtension.setStatus(3);
            extensionDao.updateById(userExtension);
            return Result.success("已拒绝");
        }
    }
}
