package com.fs.company.service.impl;

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

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.company.domain.*;
import com.fs.company.mapper.*;
import com.fs.company.param.CompanyPackageBuyParam;
import com.fs.company.param.CompanyVoicePackageOrderListParam;
import com.fs.company.util.OrderUtils;
import com.fs.company.vo.CompanyVoicePackageOrderListVO;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.company.service.ICompanyVoicePackageOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 套餐订单Service业务层处理
 *
 * @author fs
 * @date 2023-02-23
 */
@Service
public class CompanyVoicePackageOrderServiceImpl implements ICompanyVoicePackageOrderService
{
    @Autowired
    private CompanyVoicePackageOrderMapper companyVoicePackageOrderMapper;
    @Autowired
    private CompanyVoicePackageMapper voicePackageMapper;
    @Autowired
    private CompanyMoneyLogsMapper moneyLogsMapper;
    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private CompanyVoiceMapper voiceMapper;

    /**
     * 查询套餐订单
     *
     * @param orderId 套餐订单ID
     * @return 套餐订单
     */
    @Override
    public CompanyVoicePackageOrder selectCompanyVoicePackageOrderById(Long orderId)
    {
        return companyVoicePackageOrderMapper.selectCompanyVoicePackageOrderById(orderId);
    }

    /**
     * 查询套餐订单列表
     *
     * @param companyVoicePackageOrder 套餐订单
     * @return 套餐订单
     */
    @Override
    public List<CompanyVoicePackageOrder> selectCompanyVoicePackageOrderList(CompanyVoicePackageOrder companyVoicePackageOrder)
    {
        return companyVoicePackageOrderMapper.selectCompanyVoicePackageOrderList(companyVoicePackageOrder);
    }

    /**
     * 新增套餐订单
     *
     * @param companyVoicePackageOrder 套餐订单
     * @return 结果
     */
    @Override
    public int insertCompanyVoicePackageOrder(CompanyVoicePackageOrder companyVoicePackageOrder)
    {
        companyVoicePackageOrder.setCreateTime(DateUtils.getNowDate());
        return companyVoicePackageOrderMapper.insertCompanyVoicePackageOrder(companyVoicePackageOrder);
    }

    /**
     * 修改套餐订单
     *
     * @param companyVoicePackageOrder 套餐订单
     * @return 结果
     */
    @Override
    public int updateCompanyVoicePackageOrder(CompanyVoicePackageOrder companyVoicePackageOrder)
    {
        return companyVoicePackageOrderMapper.updateCompanyVoicePackageOrder(companyVoicePackageOrder);
    }

    /**
     * 批量删除套餐订单
     *
     * @param orderIds 需要删除的套餐订单ID
     * @return 结果
     */
    @Override
    public int deleteCompanyVoicePackageOrderByIds(Long[] orderIds)
    {
        return companyVoicePackageOrderMapper.deleteCompanyVoicePackageOrderByIds(orderIds);
    }

    /**
     * 删除套餐订单信息
     *
     * @param orderId 套餐订单ID
     * @return 结果
     */
    @Override
    public int deleteCompanyVoicePackageOrderById(Long orderId)
    {
        return companyVoicePackageOrderMapper.deleteCompanyVoicePackageOrderById(orderId);
    }

    @Override
    public List<CompanyVoicePackageOrderListVO> selectCompanyVoicePackageOrderListVO(CompanyVoicePackageOrderListParam param) {
        return companyVoicePackageOrderMapper.selectCompanyVoicePackageOrderListVO(param);
    }

    @Override
    @Transactional
    public R buy(CompanyPackageBuyParam param) {
        Company company=companyMapper.selectCompanyByIdForUpdate(param.getCompanyId());
        CompanyVoicePackage companyPackage=voicePackageMapper.selectCompanyVoicePackageById(param.getPackageId());
        if(company.getMoney().compareTo(companyPackage.getPrice())==-1){
            return R.error("余额不足");
        }

        CompanyVoicePackageOrder order=new CompanyVoicePackageOrder();
        order.setTimes(companyPackage.getTimes());
        order.setOrderSn(OrderUtils.getOrderNo());
        order.setIsPay(1);
        order.setPackageId(companyPackage.getPackageId());
        order.setPrice(companyPackage.getPrice());
        order.setStatus(1);
        order.setRemainTimes(companyPackage.getTimes());
        order.setTimes(companyPackage.getTimes());
        order.setCompanyId(company.getCompanyId());
        order.setCreateTime(new Date());
        order.setPayTime(new Date());
        order.setCreateBy(param.getCreateBy());
        companyVoicePackageOrderMapper.insertCompanyVoicePackageOrder(order);
        //修改余额
        company.setMoney(company.getMoney().subtract(companyPackage.getPrice()));

        //写入日志
        CompanyMoneyLogs log=new CompanyMoneyLogs();
        log.setCompanyId(company.getCompanyId());
        log.setMoney(companyPackage.getPrice().multiply(new BigDecimal(-1)));
        log.setRemark("购买套餐:"+companyPackage.getPrice().multiply(new BigDecimal(-1))+"元");
        log.setCreateTime(new Date());
        log.setLogsType(10);
        log.setBalance(company.getMoney());
        moneyLogsMapper.insertCompanyMoneyLogs(log);
        companyMapper.updateCompany(company);
        voiceMapper.updateCompanyVoiceTimes(company.getCompanyId(),companyPackage.getTimes());
        return R.ok("购买成功");
    }
}
