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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.invite.dao.InviteDao;
import com.sqx.modules.invite.entity.Invite;
import com.sqx.modules.invite.service.InviteMoneyService;
import com.sqx.modules.invite.service.InviteService;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.resumes.entity.Company;
import com.sqx.modules.resumes.entity.PostPush;
import com.sqx.modules.resumes.entity.Resumes;
import com.sqx.modules.resumes.service.CompanyService;
import com.sqx.modules.resumes.service.PostPushService;
import com.sqx.modules.resumes.service.ResumesService;
import com.sqx.modules.utils.AmountCalUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 邀请记录
 */
@Service
public class InviteServiceImpl extends ServiceImpl<InviteDao, Invite> implements InviteService {


    @Autowired
    private InviteDao inviteDao;
    @Autowired
    private UserService userService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private ResumesService resumesService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private PostPushService postPushService;


    @Override
    public PageUtils selectInviteUser(int page, int limit, Long userId, Integer state) {
        Page<Map<String, Object>> pages = new Page<>(page, limit);
        return new PageUtils(inviteDao.selectInviteUser(pages, userId, state));
    }

    @Override
    public Integer selectInviteCount(Integer state, Long userId) {
        UserEntity userEntity = userService.selectUserById(userId);
        return userService.queryInviterCount(userEntity.getInvitationCode());
    }


    @Override
    public int saveBody(Long userId, UserEntity userEntity) {
        //给被邀请者增加上级id
        UserEntity user = new UserEntity();
        user.setUserId(userId);
        user.setInviterCode(userEntity.getInvitationCode());
        userService.updateById(user);
        //判断邀请 人数
        String value = commonInfoService.findOne(269).getValue();
        if (!"0".equals(value)) {
            int i = Integer.parseInt(value);
            int counts = userService.count(new QueryWrapper<UserEntity>().eq("inviter_code", userEntity.getInvitationCode()));
            if (i != 0 && counts != 0) {
                if (counts % i == 0) {
                    //能被整除
                    //判断是企业还是个人 个人就刷新简历 企业就刷新项目
                    if (userEntity.getUserType() == null || userEntity.getUserType() == 1) {
                        Resumes resumes = resumesService.selectResumesByUserId(userEntity.getUserId());
                        if (resumes != null) {
                            resumes.setUpdateTime(DateUtils.format(new Date()));
                            resumesService.updateById(resumes);
                        }
                    } else {
                        Company company = companyService.selectCompanyByUserId(userId);
                        if (company != null) {
                            String format = DateUtils.format(new Date());
                            List<PostPush> list = postPushService.list(new QueryWrapper<PostPush>().eq("status", 2).eq("classify", 2).eq("company_id", company.getCompanyId()));
                            for (PostPush postPush : list) {
                                postPush.setUpdateTime(format);
                                postPushService.updateById(postPush);
                            }
                        }
                    }

                }
            }
        }

        return 1;
    }


}
