package com.limap.core.basic.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

import com.limap.common.core.domain.AjaxResult;
import com.limap.common.core.domain.entity.SysUser;
import com.limap.common.utils.DateUtils;
import com.limap.common.utils.SecurityUtils;
import com.limap.core.basic.domain.BasicProfitSharingRecord;
import com.limap.core.basic.domain.BasicProfitSharingUser;
import com.limap.core.basic.service.IBasicProfitSharingRecordService;
import com.limap.core.basic.service.IBasicProfitSharingUserService;
import com.limap.core.basic.vo.BasicUserWithdrawVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.limap.common.utils.StringUtils;
import com.limap.core.basic.mapper.BasicUserWithdrawMapper;
import com.limap.core.basic.domain.BasicUserWithdraw;
import com.limap.core.basic.service.IBasicUserWithdrawService;

import javax.annotation.Resource;

/**
 * 提现记录Service业务层处理
 *
 * @author jjb
 * @date 2025-04-11
 */
@Service
public class BasicUserWithdrawServiceImpl extends ServiceImpl<BasicUserWithdrawMapper, BasicUserWithdraw> implements IBasicUserWithdrawService {
    @Resource
    private BasicUserWithdrawMapper basicUserWithdrawMapper;
    @Resource
    private IBasicProfitSharingRecordService basicProfitSharingRecordService;
    @Resource
    private IBasicProfitSharingUserService basicProfitSharingUserService;

    /**
     * 查询提现记录列表
     *
     * @param basicUserWithdraw 提现记录
     * @return 提现记录
     */
    @Override
    public List<BasicUserWithdraw> selectBasicUserWithdrawList(BasicUserWithdraw basicUserWithdraw) {
    	LambdaQueryWrapper<BasicUserWithdraw> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotNull(basicUserWithdraw.getUserId())) {
			queryWrapper.eq(BasicUserWithdraw::getUserId, basicUserWithdraw.getUserId());
		}
        if (StringUtils.isNotEmpty(basicUserWithdraw.getTitle())) {
			queryWrapper.eq(BasicUserWithdraw::getTitle, basicUserWithdraw.getTitle());
		}
        if (StringUtils.isNotNull(basicUserWithdraw.getWithdrawTime())) {
			queryWrapper.eq(BasicUserWithdraw::getWithdrawTime, basicUserWithdraw.getWithdrawTime());
		}
        if (StringUtils.isNotNull(basicUserWithdraw.getNickName())) {
			queryWrapper.like(BasicUserWithdraw::getNickName, basicUserWithdraw.getNickName());
		}
        if (StringUtils.isNotNull(basicUserWithdraw.getTel())) {
			queryWrapper.like(BasicUserWithdraw::getTel, basicUserWithdraw.getTel());
		}
        queryWrapper.orderByDesc(BasicUserWithdraw::getCreateTime);
        return basicUserWithdrawMapper.selectList(queryWrapper);
    }

    /**
     * 用户提现
     *
     * @param basicUserWithdraw
     * @return
     */
    @Override
    public AjaxResult addUserWithdraw(BasicUserWithdrawVo basicUserWithdraw) {
        Date date = DateUtils.getNowDate();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<BasicProfitSharingRecord> records = basicProfitSharingRecordService.selectRecordByWithdraw(basicUserWithdraw);
        if(records.size() == 0){
            return AjaxResult.error("暂无可提现金额");
        }
        BasicProfitSharingUser byId = basicProfitSharingUserService.getById(basicUserWithdraw.getUserId());
        if(byId == null || byId.getBankNumber() == null || byId.getCompany() == null){
            return AjaxResult.error("请先补充打款信息: 点击头像-》个人中心-》基本资料-》分润信息中补充相关信息");
        }
        String title = DateUtils.parseDateToStr("yyyy-MM-dd", date) + "用户提现";
        double sum = records.stream().mapToDouble(e -> e.getAmount().doubleValue()).sum();

        BasicUserWithdraw basicUserWithdraw1 = new BasicUserWithdraw();
        basicUserWithdraw1.setUserId(basicUserWithdraw.getUserId());
        basicUserWithdraw1.setTitle(title);
        basicUserWithdraw1.setWithdrawTime(date);
        basicUserWithdraw1.setStartTime( basicUserWithdraw.getStartTime());
        basicUserWithdraw1.setEndTime(basicUserWithdraw.getEndTime());
        basicUserWithdraw1.setTotalAmount(BigDecimal.valueOf(sum).setScale(2, RoundingMode.HALF_UP));
        basicUserWithdraw1.setBank(byId.getCompany());
        basicUserWithdraw1.setBankNumber(byId.getBankNumber());
        basicUserWithdraw1.setName(byId.getName());
        basicUserWithdraw1.setPaymentStatus("0");
        basicUserWithdraw1.setNickName(user.getNickName());
        basicUserWithdraw1.setTel(user.getPhonenumber());
        this.save(basicUserWithdraw1);
        records.forEach(e -> e.setWithdrawId(basicUserWithdraw1.getId()));
        basicProfitSharingRecordService.updateBatchById(records);
        return AjaxResult.success("提现申请成功，请等待审核");
    }

    /**
     * 更新体现金额
     *
     * @param id
     * @param amount
     * @return
     */
    @Override
    public void updatePayment(Long id, BigDecimal amount) {
        BasicUserWithdraw byId = this.getById(id);
        if(byId == null){
            return;
        }
        BigDecimal paymentAmount = byId.getPaymentAmount();
        if(paymentAmount == null){
            paymentAmount = byId.getTotalAmount();
        }
        paymentAmount = paymentAmount.subtract(amount);
        byId.setPaymentAmount(paymentAmount);
        this.updateById(byId);
    }
}
