package com.fs.company.service.impl;

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

import cn.hutool.core.util.ObjectUtil;
import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.OrderUtils;
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.vo.CompanyVoicePackageOrderListVO;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.company.service.ICompanyVoicePackageOrderService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 套餐订单Service业务层处理
 *
 * @author fs
 * @date 2023-02-23
 */
@Service
@Slf4j
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(rollbackFor = Throwable.class,propagation = Propagation.REQUIRED)
    public R buy(CompanyPackageBuyParam param) {
        // 查找公司和套餐
        CompanyVoice companyVoice = voiceMapper.selectCompanyVoiceByCompanyIdForUpdate(param.getCompanyId());
        if(ObjectUtils.isEmpty(companyVoice)){
            companyVoice = new CompanyVoice();
            companyVoice.setCompanyId(param.getCompanyId());
            companyVoice.setTimes(0L);
            companyVoice.setTotalTimes(0L);
            log.info("新增公司通话信息:{}",companyVoice);
        }
        Company company=companyMapper.selectCompanyByIdForUpdate(param.getCompanyId());
        CompanyVoicePackage companyPackage=voicePackageMapper.selectCompanyVoicePackageById(param.getPackageId());
        if(ObjectUtil.equal(company.getMoney().compareTo(companyPackage.getPrice()),-1)){
            throw new CustomException("余额不足!");
        }

        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());
        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(2);
        log.setBalance(company.getMoney());
        moneyLogsMapper.insertCompanyMoneyLogs(log);
        companyMapper.updateCompany(company);

        companyVoice.setTimes(companyVoice.getTimes() + companyPackage.getTimes());
        companyVoice.setTotalTimes(companyVoice.getTotalTimes() + companyPackage.getTimes());
        voiceMapper.updateCompanyVoice(companyVoice);
        return R.ok("购买成功");
    }
}
