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.company.domain.*;
import com.fs.company.mapper.*;
import com.fs.company.param.CompanySmsPackageBuyParam;
import com.fs.company.util.OrderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.company.service.ICompanySmsPackageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 短信套餐包Service业务层处理
 *
 * @author fs
 * @date 2023-01-09
 */
@Service
public class CompanySmsPackageServiceImpl implements ICompanySmsPackageService
{
    @Autowired
    private CompanySmsPackageMapper companySmsPackageMapper;
    @Autowired
    private CompanyMoneyLogsMapper moneyLogsMapper;
    @Autowired
    private CompanySmsOrderMapper smsOrderMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private CompanySmsMapper companySmsMapper;
    /**
     * 查询短信套餐包
     *
     * @param packageId 短信套餐包ID
     * @return 短信套餐包
     */
    @Override
    public CompanySmsPackage selectCompanySmsPackageById(Long packageId)
    {
        return companySmsPackageMapper.selectCompanySmsPackageById(packageId);
    }

    /**
     * 查询短信套餐包列表
     *
     * @param companySmsPackage 短信套餐包
     * @return 短信套餐包
     */
    @Override
    public List<CompanySmsPackage> selectCompanySmsPackageList(CompanySmsPackage companySmsPackage)
    {
        return companySmsPackageMapper.selectCompanySmsPackageList(companySmsPackage);
    }

    /**
     * 新增短信套餐包
     *
     * @param companySmsPackage 短信套餐包
     * @return 结果
     */
    @Override
    public int insertCompanySmsPackage(CompanySmsPackage companySmsPackage)
    {
        return companySmsPackageMapper.insertCompanySmsPackage(companySmsPackage);
    }

    /**
     * 修改短信套餐包
     *
     * @param companySmsPackage 短信套餐包
     * @return 结果
     */
    @Override
    public int updateCompanySmsPackage(CompanySmsPackage companySmsPackage)
    {
        return companySmsPackageMapper.updateCompanySmsPackage(companySmsPackage);
    }

    /**
     * 批量删除短信套餐包
     *
     * @param packageIds 需要删除的短信套餐包ID
     * @return 结果
     */
    @Override
    public int deleteCompanySmsPackageByIds(Long[] packageIds)
    {
        return companySmsPackageMapper.deleteCompanySmsPackageByIds(packageIds);
    }

    /**
     * 删除短信套餐包信息
     *
     * @param packageId 短信套餐包ID
     * @return 结果
     */
    @Override
    public int deleteCompanySmsPackageById(Long packageId)
    {
        return companySmsPackageMapper.deleteCompanySmsPackageById(packageId);
    }

    @Override
    @Transactional
    public R buySmsPackage(CompanySmsPackageBuyParam param) {

        Company company=companyMapper.selectCompanyByIdForUpdate(param.getCompanyId());
        CompanySmsPackage smsPackage=companySmsPackageMapper.selectCompanySmsPackageById(param.getPackageId());
        if(!smsPackage.getStatus().equals(1l)){
            return R.error("此套餐已禁用");
        }
        if(company.getMoney().compareTo(smsPackage.getPrice())==-1){
            return R.error("余额不足");
        }

        CompanySmsOrder order=new CompanySmsOrder();
        order.setCompanyId(param.getCompanyId());
        order.setOrderCode(OrderUtils.getOrderNo());
        order.setPackageId(smsPackage.getPackageId());
        order.setSmsCount(smsPackage.getSmsCount());
        order.setPrice(smsPackage.getPrice());
        order.setStatus(1);
        order.setCreateTime(new Date());
        order.setPayTime(new Date());
        order.setPayMoney(smsPackage.getPrice());
        order.setCreateBy(param.getCreateBy());
        smsOrderMapper.insertCompanySmsOrder(order);
        //修改余额
        company.setMoney(company.getMoney().subtract(smsPackage.getPrice()));
        //写入日志
        CompanyMoneyLogs log=new CompanyMoneyLogs();
        log.setCompanyId(order.getCompanyId());
        log.setMoney(smsPackage.getPrice().multiply(new BigDecimal(-1)));
        log.setRemark("购买短信套餐:"+smsPackage.getPrice().multiply(new BigDecimal(-1))+"元");
        log.setCreateTime(new Date());
        log.setLogsType(11);
        log.setBusinessId(order.getOrderId().toString());
        log.setBalance(company.getMoney());
        moneyLogsMapper.insertCompanyMoneyLogs(log);
        companyMapper.updateCompany(company);
        //写入短信数量
        CompanySms companySms=companySmsMapper.selectCompanySmsByCompanyId(param.getCompanyId());
        if(companySms==null){
            companySms=new CompanySms();
            companySms.setCompanyId(param.getCompanyId());
            companySms.setRemainSmsCount(0l);
            companySms.setTotalSmsCount(0l);
            companySmsMapper.insertCompanySms(companySms);

        }
        companySms.setTotalSmsCount(smsPackage.getSmsCount()+companySms.getTotalSmsCount());
        companySms.setRemainSmsCount(smsPackage.getSmsCount()+companySms.getRemainSmsCount());
        companySmsMapper.updateCompanySms(companySms);
        return R.ok("购买成功");
    }
}
