package com.ruoyi.web.work.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.work.domain.Company;
import com.ruoyi.web.work.domain.Recharge;
import com.ruoyi.web.work.domain.Record;
import com.ruoyi.web.work.domain.dto.RechargeDto;
import com.ruoyi.web.work.mapper.RechargeMapper;
import com.ruoyi.web.work.service.ICompanyService;
import com.ruoyi.web.work.service.IRechargeService;
import com.ruoyi.web.work.service.IRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.ServerException;
import java.util.List;

/**
 * @author lsw
 * @date 2024-04-17
 */
@Service
public class RechargeServiceImpl extends ServiceImpl<RechargeMapper, Recharge> implements IRechargeService {
    @Autowired
    private RechargeMapper rechargeMapper;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private IRecordService recordService;

    @Override
    public List<Recharge> selectList(Recharge recharge) {
        return rechargeMapper.selectList(recharge);
    }

    @Override
    public AjaxResult add(Recharge recharge) {
        recharge.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        recharge.setOpBy(SecurityUtils.getLoginUser().getUser().getNickName());
        recharge.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        recharge.setNums(StringUtils.generateNumber());
        recharge.setState(0);
        recharge.setVersion(System.currentTimeMillis());
        if (save(recharge)) {
            return AjaxResult.success();
        }
        return AjaxResult.error("提交申请失败,请联系平台");

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult edit(RechargeDto dto) throws ServerException {
        Recharge recharge = getById(dto.getId());
        if (recharge == null) {
            return AjaxResult.error("充值对象不存在");
        }
        if (recharge.getState() != 0) {
            return AjaxResult.error("不可重复操作");
        }
        recharge.setState(dto.getState());
        recharge.setMsg(dto.getMsg());
        if (!updateById(recharge)) {
            throw new ServerException("充值失败,请联系管理员");
        }
        //审核确认给企业账号充钱
        if (recharge.getState() == 1) {
            Company company = companyService.getById(recharge.getCompanyId());
            if (company == null) {
                throw new ServerException("关联企业不存在");
            }
            company.setMoney(company.getMoney().add(recharge.getMoney()));
            if (!companyService.updateById(company)) {
                throw new ServerException("更新企业余额失败,请联系管理员");
            }
            Record record=new Record();
            record.setType(0);
            record.setCompanyId(recharge.getCompanyId());
            record.setOpId(recharge.getId());
            record.setMoney(recharge.getMoney());
            if (!recordService.save(record)) {
                throw new ServerException("添加充值记录失败,请联系管理员");
            }
        }
        return AjaxResult.success();
    }
}
