package top.continew.admin.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.continew.admin.business.mapper.OrderAudioMapper;
import top.continew.admin.business.mapper.OrderHistoryMapper;
import top.continew.admin.business.mapper.OrderThreeMapper;
import top.continew.admin.business.mapper.OrderVideoMapper;
import top.continew.admin.business.model.entity.OrderAudioDo;
import top.continew.admin.business.model.entity.OrderHistoryDo;
import top.continew.admin.business.model.entity.OrderThreeDo;
import top.continew.admin.business.model.entity.OrderVideoDo;
import top.continew.admin.business.model.req.OrderAudioReq;
import top.continew.admin.business.service.OrderAudioService;
import top.continew.admin.system.mapper.UserMapper;
import top.continew.admin.system.model.entity.UserDO;

/**
 * 音频业务实现
 *
 * @author hzy
 * @since 2025/10/22 11:58
 */
@Service
@RequiredArgsConstructor
public class OrderAudioServiceImpl extends ServiceImpl<OrderAudioMapper, OrderAudioDo> implements OrderAudioService {
    @Resource
    private OrderVideoMapper orderVideoMapper;
    @Resource
    private OrderThreeMapper orderThreeMapper;
    @Resource
    private OrderHistoryMapper orderHistoryMapper;
    @Resource
    private UserMapper userMapper;

    /**
     * 保存音频任务订单信息
     *
     * @param req 参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(OrderAudioReq req) {
        OrderAudioDo saveDb = new OrderAudioDo();
        BeanUtil.copyProperties(req, saveDb);
        save(saveDb);
        //保存视频订单任务
        if (req.getOrderType() != null && req.getOrderType() == 2) {
            OrderVideoDo orderVideoDo = new OrderVideoDo();
            BeanUtil.copyProperties(req, orderVideoDo);
            orderVideoMapper.insert(orderVideoDo);
        }
        //保存3D订单任务
        if (req.getOrderType() != null && req.getOrderType() == 3) {
            OrderThreeDo orderThreeDo = new OrderThreeDo();
            BeanUtil.copyProperties(req, orderThreeDo);
            orderThreeMapper.insert(orderThreeDo);
        }
        //保存3D订单任务
        OrderHistoryDo orderHistoryDo = new OrderHistoryDo();
        BeanUtil.copyProperties(req, orderHistoryDo);
        orderHistoryDo.setFormType("支出");
        //剩余点数
        UserDO userDO = userMapper.selectById(saveDb.getCreateUser());
        int payBeans = userDO.getSysBeans();

        orderHistoryDo.setRemainder(payBeans - userDO.getInviteBeans());
        orderHistoryMapper.insert(orderHistoryDo);
    }
}
