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

import cn.hutool.core.date.DateUtil;
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.artificer.dao.OrdersDao;
import com.sqx.modules.artificer.entity.Artificer;
import com.sqx.modules.artificer.entity.Orders;
import com.sqx.modules.artificer.entity.Period;
import com.sqx.modules.artificer.service.ArtificerService;
import com.sqx.modules.bl.collect.entity.CollectMassage;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.utils.PeriodUtil;
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.web.bind.annotation.RequestAttribute;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserMoneyServiceImpl extends ServiceImpl<UserMoneyDao, UserMoney> implements UserMoneyService {

    @Autowired
    private UserMoneyDetailsDao userMoneyDetailsDao;
    @Autowired
    private UserMoneyArtificerService userMoneyArtificerService;
    @Autowired
    private ArtificerService artificerService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMoneyDetailsServiceImpl userMoneyDetailsService;
    @Autowired
    private OrdersDao ordersDao;
    @Override
    public Result payUserMoneyArtificer(@RequestAttribute Long userId, Long artificerId, BigDecimal money){
        UserMoney userMoney = selectUserMoneyByUserId(userId);
        if(userMoney.getMoney().doubleValue()<money.doubleValue()){
            return Result.error("钱包金额不足！");
        }
        Artificer artificer = artificerService.getById(artificerId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //用户钱包打赏
        updateMoney(2,userId,money.doubleValue());
        UserMoneyDetails userMoneyDetails=new UserMoneyDetails();
        //查询用户当前余额
        UserMoney userNowMoney = selectUserMoneyByUserId(userId);
        //插入余额
        userMoneyDetails.setBalance(userNowMoney.getMoney());
        //插入操作类型为已操作用户余额
        userMoneyDetails.setClassify(8);
        userMoneyDetails.setManipulateType(2);
        userMoneyDetails.setMoney(money);
        userMoneyDetails.setUserId(userId);
        userMoneyDetails.setTitle("钱包给技师打赏");
        userMoneyDetails.setContent("使用钱包给指定技师“"+artificer.getArtificerName()+"”打赏:"+money);
        userMoneyDetails.setType(2);
        userMoneyDetails.setState(2);
        userMoneyDetails.setArtificerId(artificerId);
        userMoneyDetails.setCreateTime(sdf.format(new Date()));
        userMoneyDetails.setBlRole(1);
        userMoneyDetails.setBlFlag(1);
        userMoneyDetailsDao.insert(userMoneyDetails);

//2024-06-15注释掉，user_money_artificer 表一直没用上。用的是 user_money_details 表
//        UserMoneyArtificer userMoneyArtificer = userMoneyArtificerService.getOne(new QueryWrapper<UserMoneyArtificer>().eq("user_id",userId).eq("artificer_id", artificerId));
//        if(userMoneyArtificer==null){
//            userMoneyArtificer=new UserMoneyArtificer();
//            userMoneyArtificer.setUserId(userId);
//            userMoneyArtificer.setArtificerId(artificerId);
//            userMoneyArtificer.setMoney(money);
//            userMoneyArtificer.setCreateTime(DateUtils.format(new Date()));
//            userMoneyArtificerService.save(userMoneyArtificer);
//        }else{
//            userMoneyArtificerService.updateMoney(1,userId,artificerId,money.doubleValue());
//        }

        //技师获得打赏
        UserEntity userEntity = userService.queryByUserId(artificer.getUserId());
        updateMoney(1, userEntity.getUserId(), money.doubleValue());
        UserMoney userNowMoney2 = selectUserMoneyByUserId(userEntity.getUserId());
        UserMoneyDetails userMoneyDetails2 = new UserMoneyDetails();
        userMoneyDetails2.setBalance(userNowMoney2.getMoney());
        userMoneyDetails2.setClassify(8);
        userMoneyDetails2.setManipulateType(2);
        userMoneyDetails2.setUserId(userEntity.getUserId());
        userMoneyDetails2.setTitle("用户钱包打赏");
        userMoneyDetails2.setContent("来自用户“"+userEntity.getUserName()+"”钱包打赏:"+money);
        userMoneyDetails2.setType(1);
        userMoneyDetails2.setState(2);
        userMoneyDetails2.setMoney(money);
        userMoneyDetails2.setCreateTime(sdf.format(new Date()));
        userMoneyDetails2.setBlRole(2);
        userMoneyDetails2.setBlFlag(1);
        userMoneyDetailsDao.insert(userMoneyDetails2);

        return Result.success();
    }




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

    @Override
    public void updateMoney(UserMoney userMoney){
        baseMapper.updateMoney(userMoney);
    }

    @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.setCashDeposit(BigDecimal.ZERO);
            baseMapper.insert(userMoney);
        }
        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 ")));
        return new PageUtils(userMoneyDetailsDao.findUserMontyDetailPage(pages,userId));
    }
    //收益明细
    @Override
    public Result profitDetailed(Long userId, Page page, String startTime,String endTime) {
//        QueryWrapper<Orders> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("artificer_id", userId);
//        queryWrapper.between("end_times", startTime, endTime);
//        //只查询已完成和待评价的订单
//        queryWrapper.in("status", Arrays.asList(5, 3));
//        //只查询根节点数据
//        queryWrapper.eq("parent_id", 0);
//        queryWrapper.orderByDesc("end_times");
//        IPage<Orders> page2 =  ordersDao.selectPage(page1, queryWrapper);

        IPage<Orders> page2 = ordersDao.selectDividedIntoDetails(page, userId,startTime,endTime,null);

        //分成明细添加到返回对象中
        return Result.success().put("data", page2);
    }

    @Override
    public void updateSad(int i, Long userId, double payMoney) {
        baseMapper.updateMaySad(i, userId, payMoney);
    }

}
