package com.ym.mec.collectfee.service.impl;


import com.ym.mec.collectfee.common.dao.BaseDAO;
import com.ym.mec.collectfee.common.service.impl.BaseServiceImpl;
import com.ym.mec.collectfee.dao.RenewalsDao;
import com.ym.mec.collectfee.entity.Account;
import com.ym.mec.collectfee.entity.MecCourse;
import com.ym.mec.collectfee.entity.Order;
import com.ym.mec.collectfee.entity.Renewals;
import com.ym.mec.collectfee.service.AccountService;
import com.ym.mec.collectfee.service.OrderService;
import com.ym.mec.collectfee.service.RenewalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class RenewalsServiceImpl extends BaseServiceImpl<Integer, Renewals> implements RenewalsService {

    @Autowired
    private RenewalsDao renewalsDao;

    @Autowired
    private OrderService orderService;
    @Autowired
    private RenewalsService renewalsService;
    @Autowired
    private AccountService accountService;

    @Override
    public BaseDAO<Integer, Renewals> getDAO() {
        return renewalsDao;
    }

    @Override
    public void renewalsInsert(ArrayList<Renewals> renewalsArrayList) {
        renewalsDao.renewalsInsert(renewalsArrayList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Order addRenewalsOrder(Renewals renewals, BigDecimal amount, String orderNo, List<MecCourse> mecCourses, Account routingAccount, String remark) {

        ////classType 小课1 大课2
        Order order = new Order();
        order.setBranchId(renewals.getBranchId());
        order.setUserId(renewals.getUserId());
        order.setOrderNo(orderNo);
        order.setAmount(amount);
        order.setAccount(routingAccount.getSellerNo());
        order.setRemark(remark);
        order.setClassId(renewals.getClassId());
        order.setStatus(1);
        order.setUAccount(String.valueOf(routingAccount.getId()));
        order.setCreateTime(new Date());

        Integer type = mecCourses.get(0).getClassType() == 2 ? 2 : 3;
        order.setType(type);
        order.setVoicyPart(mecCourses.get(0).getSubNames());
        order.setClassId(mecCourses.get(0).getClassId());
        order.setUserName(mecCourses.get(0).getSubNames());
        //1、插入订单
        orderService.insert(order);
        
        ArrayList<Renewals> renewalsArrayList = new ArrayList<>();

        for (int i = 0; i < mecCourses.size(); i++) {
            MecCourse course = mecCourses.get(i);
            Renewals rws = new Renewals();
            rws.setUserId(course.getStudentId());
            rws.setBranchId(renewals.getBranchId());
            rws.setClassId(course.getClassId());
            rws.setCourseId(course.getCourseId());
            rws.setWay(30);
            rws.setPay(course.getPrice().multiply(new BigDecimal(course.getBuyCount())));
            rws.setChangeMode(course.getChargeMode());
            rws.setPrice(course.getPrice());
            rws.setBuy(course.getBuyCount());
            rws.setOrderId(order.getId());
            renewalsArrayList.add(rws);
        }

        //2、添加续费记录
        renewalsService.renewalsInsert(renewalsArrayList);
        //3、修改分佣账户已收金额
        //获取分佣账户
        BigDecimal HasRouting = routingAccount.getHasRouting().add(amount);
        routingAccount.setHasRouting(HasRouting);
        accountService.upByIdAndVersion(routingAccount);
        return order;
    }

    @Override
    public Renewals getRenewalsByOrderId(Integer orderId) {
        return renewalsDao.getRenewalsByOrderId(orderId);
    }

}