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.MoneyOut;
import com.ruoyi.web.work.domain.Record;
import com.ruoyi.web.work.domain.dto.MoneyOutDto;
import com.ruoyi.web.work.mapper.MoneyOutMapper;
import com.ruoyi.web.work.service.ICommonService;
import com.ruoyi.web.work.service.ICompanyService;
import com.ruoyi.web.work.service.IMoneyOutService;
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-18
 */
@Service
public class MoneyOutServiceImpl extends ServiceImpl<MoneyOutMapper, MoneyOut> implements IMoneyOutService {
    @Autowired
    private MoneyOutMapper moneyOutMapper;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private ICommonService commonService;

    @Autowired
    private IRecordService recordService;

    @Override
    public List<MoneyOut> selectList(MoneyOut moneyOut) {
        return moneyOutMapper.selectList(moneyOut);
    }

    @Override
    public AjaxResult add(MoneyOut moneyOut) throws ServerException {
        commonService.verifyCode(SecurityUtils.getLoginUser().getUser().getPhonenumber(), moneyOut.getCode());
        Company company = companyService.getById(SecurityUtils.getLoginUser().getUser().getCompanyId());
        if (company == null) {
            return AjaxResult.error("账户信息不存在");
        }
        if (moneyOut.getMoney().compareTo(company.getMoney()) > 0) {
            return AjaxResult.error("提现金额不能大于账户余额");
        }
        moneyOut.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        moneyOut.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        moneyOut.setOpBy(SecurityUtils.getLoginUser().getUser().getNickName());
        moneyOut.setNums(StringUtils.generateNumber());
        moneyOut.setState(0);
        moneyOut.setVersion(System.currentTimeMillis());
        if (!save(moneyOut)) {
            throw new ServerException("企业提现申请失败,请联系平台");
        }
        return AjaxResult.success();
    }

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