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.Company;
import com.fs.company.domain.CompanyMoneyLogs;
import com.fs.company.mapper.CompanyMapper;
import com.fs.company.mapper.CompanyMoneyLogsMapper;
import com.fs.company.vo.CompanyRechargeExportVO;
import com.fs.company.vo.CompanyRechargeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.company.mapper.CompanyRechargeMapper;
import com.fs.company.domain.CompanyRecharge;
import com.fs.company.service.ICompanyRechargeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 充值Service业务层处理
 *
 * @author fs
 * @date 2021-10-04
 */
@Service
public class CompanyRechargeServiceImpl implements ICompanyRechargeService
{
    @Autowired
    private CompanyRechargeMapper companyRechargeMapper;
    @Autowired
    private CompanyMoneyLogsMapper moneyLogsMapper;
    @Autowired
    private CompanyMapper companyMapper;
    /**
     * 查询充值
     *
     * @param rechargeId 充值ID
     * @return 充值
     */
    @Override
    public CompanyRecharge selectCompanyRechargeById(Long rechargeId)
    {
        return companyRechargeMapper.selectCompanyRechargeById(rechargeId);
    }

    /**
     * 查询充值列表
     *
     * @param companyRecharge 充值
     * @return 充值
     */
    @Override
    public List<CompanyRecharge> selectCompanyRechargeList(CompanyRecharge companyRecharge)
    {
        return companyRechargeMapper.selectCompanyRechargeList(companyRecharge);
    }

    /**
     * 新增充值
     *
     * @param companyRecharge 充值
     * @return 结果
     */
    @Override
    public int insertCompanyRecharge(CompanyRecharge companyRecharge)
    {
        companyRecharge.setCreateTime(DateUtils.getNowDate());
        return companyRechargeMapper.insertCompanyRecharge(companyRecharge);
    }

    /**
     * 修改充值
     *
     * @param companyRecharge 充值
     * @return 结果
     */
    @Override
    public int updateCompanyRecharge(CompanyRecharge companyRecharge)
    {
        return companyRechargeMapper.updateCompanyRecharge(companyRecharge);
    }

    /**
     * 批量删除充值
     *
     * @param rechargeIds 需要删除的充值ID
     * @return 结果
     */
    @Override
    public int deleteCompanyRechargeByIds(Long[] rechargeIds)
    {
        return companyRechargeMapper.deleteCompanyRechargeByIds(rechargeIds);
    }

    /**
     * 删除充值信息
     *
     * @param rechargeId 充值ID
     * @return 结果
     */
    @Override
    public int deleteCompanyRechargeById(Long rechargeId) {
        return companyRechargeMapper.deleteCompanyRechargeById(rechargeId);
    }

    @Override
    public List<CompanyRechargeVO> selectCompanyRechargeVOList(CompanyRecharge companyRecharge) {
        return companyRechargeMapper.selectCompanyRechargeVOList(companyRecharge);
    }

    @Override
    public CompanyRecharge selectCompanyRechargeByNo(String no) {
        return companyRechargeMapper.selectCompanyRechargeByNo(no);
    }

    @Override
    @Transactional
    public R payNotify(CompanyRecharge recharge) {
        //修改状态
        recharge.setPayTime(new Date());
        recharge.setStatus(1);
        companyRechargeMapper.updateCompanyRecharge(recharge);
        Company company=companyMapper.selectCompanyById(recharge.getCompanyId());
        company.setMoney(company.getMoney().add(recharge.getMoney()));
        //写入日志
        CompanyMoneyLogs log=new CompanyMoneyLogs();
        log.setCompanyId(recharge.getCompanyId());
        log.setMoney(recharge.getMoney());
        log.setRemark("充值金额:"+recharge.getMoney()+"元");
        log.setCreateTime(new Date());
        log.setLogsType(3);
        log.setBalance(company.getMoney());
        moneyLogsMapper.insertCompanyMoneyLogs(log);
        //修改余额

        company.setMoney(company.getMoney().add(recharge.getMoney()));
        companyMapper.updateCompany(company);
        return R.ok();
    }

    @Override
    public BigDecimal selectCompanyRechargeMoney() {
        return companyRechargeMapper.selectCompanyRechargeMoney();
    }

    @Override
    public List<CompanyRechargeExportVO> selectCompanyRechargeExportList(CompanyRecharge companyRecharge) {
        return companyRechargeMapper.selectCompanyRechargeExportList(companyRecharge);
    }
}
