package com.sqx.modules.app.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.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.*;
import com.sqx.modules.app.entity.*;
import com.sqx.modules.app.service.*;
import com.sqx.modules.common.service.CommonInfoService;
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.ViewResumes;
import com.sqx.modules.resumes.service.CompanyService;
import com.sqx.modules.resumes.service.ViewResumesService;
import jodd.util.StringUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Service
@Slf4j
public class UserMoneyServiceImpl extends ServiceImpl<UserMoneyDao, UserMoney> implements UserMoneyService {
    @Autowired
    private UserMoneyDetailsDao userMoneyDetailsDao;
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ViewResumesService viewResumesService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;

    @Override
    public void updateMoney(int i, Long userId, BigDecimal money) {
        baseMapper.updateMayMoney(i, userId, money);
    }

    @Override
    public void updateCustomizationMoney(int i, Long userId, BigDecimal money) {
        baseMapper.updateCustomizationMoney(i, userId, money);
    }

    @Override
    public UserMoney selectUserMoneyByUserId(Long userId) {
        UserMoney userMoney = baseMapper.selectOne(new QueryWrapper<UserMoney>().eq("user_id", userId));
        if (userMoney == null) {
            userMoney = new UserMoney();
            userMoney.setMoney(BigDecimal.ZERO);
            userMoney.setUserId(userId);
            userMoney.setCustomizationMoney(BigDecimal.ZERO);
            baseMapper.insert(userMoney);
        }
        return userMoney;
    }

    @Override
    public Double selectMyProfit(Long userId) {

        return userMoneyDetailsDao.selectMyProfit(userId);
    }

    @Override
    public IPage<UserMoneyDetails> balanceDetailed(Long page, Long limit, UserMoneyDetails userMoneyDetails) {
        Page<UserMoneyDetails> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
        }
        return userMoneyDetailsDao.selectPage(pages, new QueryWrapper<>(userMoneyDetails).orderByDesc("create_time"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result payResumes(Long userId, Long resumesId) {
        UserEntity userEntity = userService.getById(userId);
        if (userEntity == null) {
            return Result.error("用户信息已失效,请重新登录");
        }
        Company company = companyService.getOne(new QueryWrapper<Company>().eq("user_id", userId));
        if (company == null) {
            return Result.error("请先添加企业");
        }
        Integer viewResumes = viewResumesService.count(new QueryWrapper<ViewResumes>().eq("resumes_id", resumesId).eq("company_id", company.getCompanyId()));
        if (viewResumes != null && viewResumes>0) {
            return Result.error("企业已有查看当前简历的权限");
        }
        UserMoney userMoney = baseMapper.selectOne(new QueryWrapper<UserMoney>().eq("user_id", userId));
        BigDecimal money;
        if (userEntity.getIsCompanyVip()!=null && userEntity.getIsCompanyVip() == 1) {
            money = new BigDecimal(commonInfoService.findOne(419).getValue());
        } else {
            money = new BigDecimal(commonInfoService.findOne(420).getValue());
        }
        if (userMoney.getMoney().compareTo(money) < 0) {
            return Result.error("当前钻石不足,请充值后再试");
        }
        baseMapper.updateMayMoney(2, userId, money);
        setResumes(resumesId, company, money,2);

        return Result.success();
    }

    @Override
    public Result countPayResumes(Long userId, Long resumesId) {
        Company company = companyService.getOne(new QueryWrapper<Company>().eq("user_id", userId));
        UserEntity userEntity = userService.getById(userId);
        if (userEntity == null) {
            return Result.error("用户信息已失效,请登录后重试");
        }
        if (company == null) {
            return Result.error("公司信息不存在");
        }
        int vipCount = Integer.parseInt(commonInfoService.findOne(421).getValue());

        int plainCount = Integer.parseInt(commonInfoService.findOne(422).getValue());

        int count = viewResumesService.getUseCount(company.getCompanyId());

        if (userEntity.getIsCompanyVip()!=null && userEntity.getIsCompanyVip() == 1) {
            if (count >= vipCount) {
                return Result.error("免费次数不足");
            }
        } else {
            if (count >= plainCount) {
                return Result.error("免费次数不足");
            }
        }
        setResumes(resumesId, company, BigDecimal.ZERO,1);
        return Result.success();
    }

    private void setResumes(Long resumesId, Company company, BigDecimal money,Integer payType) {
        ViewResumes viewResumes;
        viewResumes = new ViewResumes();
        viewResumes.setResumesId(resumesId);
        viewResumes.setCompanyId(company.getCompanyId());
        viewResumes.setPayMoney(money);
        viewResumes.setPayType(payType);
        viewResumes.setCreateTime(DateUtils.format(new Date()));
        viewResumesService.save(viewResumes);
        UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
        userMoneyDetails.setMoney(money);
        userMoneyDetails.setUserId(company.getUserId());
        userMoneyDetails.setClassify(1);
        userMoneyDetails.setType(2);
        userMoneyDetails.setTitle("购买简历查看权限");
        userMoneyDetails.setTitle("购买简历查看权限成功,支付钻石" + money + "个");
        userMoneyDetails.setCreateTime(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        userMoneyDetailsService.save(userMoneyDetails);
    }


}
