package shop.alien.job.store;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import shop.alien.entity.store.LifeMessage;
import shop.alien.entity.store.LifeReverseGroupBuying;
import shop.alien.entity.store.LifeUser;
import shop.alien.entity.store.LifeUserOrder;
import shop.alien.mapper.LifeMessageMapper;
import shop.alien.mapper.LifeReverseGroupBuyingMapper;
import shop.alien.mapper.LifeUserMapper;
import shop.alien.mapper.LifeUserOrderMapper;
import shop.alien.util.type.LifeMessageUtil;
import shop.alien.util.common.AlipayTradeRefund;
import shop.alien.util.common.DateUtils;

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

/**
 * 反向团购job
 *
 * @author ssk
 * @version 1.0
 * @date 2025/8/6 14:21
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LifeReverseGroupBuyingJob {

    private final LifeUserOrderMapper lifeUserOrderMapper;

    private final AlipayTradeRefund alipayTradeRefund;

    private final LifeReverseGroupBuyingMapper lifeReverseGroupBuyingMapper;

    private final LifeMessageMapper lifeMessageMapper;

    private final LifeMessageUtil lifeMessageUtil;

    private final LifeUserMapper lifeUserMapper;

    /**
     * 反向团购订单自动退款
     */
    @XxlJob("reverseGroupOrder")
    public void reverseGroupOrder() {
        LambdaQueryWrapper<LifeReverseGroupBuying> wrapper = new LambdaQueryWrapper<>();
        //状态, 0:待审核, 1:未通过, 2:已通过, 3:待接单, 4:未接单(已截止), 5:待使用, 6:已完成, 7:已退款
        wrapper.eq(LifeReverseGroupBuying::getStatus, 4);
        List<LifeReverseGroupBuying> list = lifeReverseGroupBuyingMapper.selectList(new LambdaQueryWrapper<>());
        for (LifeReverseGroupBuying lifeReverseGroupBuying : list) {
            //竞价结束
            if (DateUtils.dateCompare(new Date(), lifeReverseGroupBuying.getBiddingEndTime())) {
                LifeUserOrder lifeUserOrder = lifeUserOrderMapper.selectById(lifeReverseGroupBuying.getOrderId());
                String s = alipayTradeRefund.processRefund(lifeUserOrder.getOrderNo(), new BigDecimal(lifeReverseGroupBuying.getGroupMoney()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).toString(), "用户反向团购, 未有商家接单");
                if (s.contains("成功")) {
                    //修改状态
                    lifeReverseGroupBuying.setStatus(7);
                    lifeReverseGroupBuyingMapper.updateById(lifeReverseGroupBuying);
                } else {
                    LifeUser lifeUser = lifeUserMapper.selectById(lifeUserOrder.getUserId());
                    LifeMessage lifeMessage = new LifeMessage();
                    lifeMessage.setSenderName("system");
                    if (lifeUser != null) {
                        lifeMessage.setReceiverId("user_" + lifeUser.getUserPhone());
                        lifeMessage.setContent("你有一笔反向团购退款失败, 请联系管理员");
                        lifeMessage.setType("2");
                        lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
                        lifeMessageMapper.insert(lifeMessage);
                    }

                }
            }
        }
    }
}
