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.entity.CommonInfo;
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.pay.service.PayDetailsService;
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 java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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 UserDao userDao;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    @Autowired
    private UserMoneyDao userMoneyDao;
    @Autowired
    private PayDetailsService detailsService;
    @Autowired
    private CommonInfoService commonInfoService;


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

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

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

    @Override
    public UserMoney selectUserMoneyByUserId(Long userId) {
        UserMoney userMoney = baseMapper.selectOne(new QueryWrapper<UserMoney>().eq("user_id", userId));
        if (userMoney!=null){
            userMoney.setAccumulateMoney(detailsService.getUserAccumulateMoney(userId));
        }
        return userMoney;
    }

    @Override
    public Double selectMyProfit(Long userId) {

        return userMoneyDetailsDao.selectMyProfit(userId);
    }


    @Override
    public PageUtils balanceDetailed(Long userId, Page<UserMoneyDetails> pages) {
        return new PageUtils(userMoneyDetailsDao.selectPage(pages, new QueryWrapper<UserMoneyDetails>().eq("user_id", userId).orderByDesc("create_time ")));
    }

    @Override
    public Result profitDetailed(Long userId, Long page, Long limit) {
        //收益明细
        Page<UserMoneyDetails> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(userMoneyDetailsDao.selectMoneyDetails(pages, userId));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result updateCashDeposit(Long userId, BigDecimal money, Integer type) {
        userMoneyDao.updateCashDeposit(userId, money, type);
        UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
        userMoneyDetails.setMoney(money);
        userMoneyDetails.setUserId(userId);
        if (type.equals(1)) {
            userMoneyDetails.setContent("赠送用户保证金：" + money + "元");
            userMoneyDetails.setTitle("系统赠送用户保证金");
        } else if (type.equals(2)) {
            userMoneyDetails.setContent("扣除用户保证金：" + money + "元");
            userMoneyDetails.setTitle("系统扣除用户保证金");
        }
        userMoneyDetails.setUserType(2);
        userMoneyDetails.setType(type);
        userMoneyDetails.setClassify(1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        userMoneyDetails.setCreateTime(simpleDateFormat.format(new Date()));
        userMoneyDetailsService.save(userMoneyDetails);
        return Result.success();
    }


    @Override
    public Result updateUserMoney(Integer type, Long userId, BigDecimal money) {

        int count = userMoneyDao.selectCount(new QueryWrapper<UserMoney>().eq("user_id", userId));
        //如果用户钱包不存在
        if (count == 0) {
            UserMoney userMoney = new UserMoney();
            userMoney.setMoney(BigDecimal.ZERO);
            userMoney.setUserId(userId);
            userMoneyDao.insert(userMoney);
        }
        if (type == 1) {
            userMoneyDao.updateMayMoney(type, userId, money.doubleValue());
        } else if (type == 2) {
            userMoneyDao.updateMayMoney(2, userId, money.doubleValue());

        }
        UserEntity userEntity = userDao.selectById(userId);
        UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
        userMoneyDetails.setUserId(userEntity.getUserId());
        userMoneyDetails.setUserType(1);
        if (type == 1) {
            userMoneyDetails.setTitle("系统赠送用户余额");
            userMoneyDetails.setContent("赠送用户余额:" + money + "元");
            userMoneyDetails.setType(1);

        } else if (type == 2) {
            userMoneyDetails.setTitle("系统扣除用户余额");
            userMoneyDetails.setContent("扣除用户余额:" + money + "元");
            userMoneyDetails.setType(2);
        }
        userMoneyDetails.setMoney(money);
        userMoneyDetails.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        userMoneyDetailsService.save(userMoneyDetails);
        return Result.success();

    }

    @Override
    public IPage<UserEntity> getRiderList(Integer page, Integer limit, UserEntity userEntity) {
        Page<UserEntity> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        CommonInfo deposit = commonInfoService.findOne(261);
        return baseMapper.getRiderList(pages,userEntity,Double.valueOf(deposit.getValue()));

    }



}
