package com.njzscloud.supervisory.money.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.security.util.SecurityUtil;
import com.njzscloud.supervisory.money.contant.MoneyChangeCategory;
import com.njzscloud.supervisory.money.mapper.MoneyAccountMapper;
import com.njzscloud.supervisory.money.pojo.entity.MoneyAccountEntity;
import com.njzscloud.supervisory.money.pojo.entity.MoneyChangeDetailEntity;
import com.njzscloud.supervisory.money.pojo.result.MoneyAccountResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 资金账户表
 * @author ljw
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MoneyAccountService extends ServiceImpl<MoneyAccountMapper, MoneyAccountEntity> implements IService<MoneyAccountEntity> {

    private final MoneyChangeDetailService moneyChangeDetailService;

    /**
     * 新增
     */
    public void add(MoneyAccountEntity moneyAccountEntity) {
//        moneyAccountEntity.setUserId(SecurityUtil.currentUserId());
        this.save(moneyAccountEntity);
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    public void modify(MoneyAccountEntity moneyAccountEntity) {
        if (moneyAccountEntity.getRechargeMoney() == null || moneyAccountEntity.getRechargeMoney().signum() <= 0) {
            throw Exceptions.clierr("充值金额需大于0");
        }
        MoneyAccountResult result = baseMapper.selectMoneyAccountDetailById(moneyAccountEntity.getId());
        if (null == result) {
            throw Exceptions.clierr("未找到该账户");
        }
        // 记录资金变动明细
        MoneyChangeDetailEntity changeDetail = new MoneyChangeDetailEntity()
                .setMoneyAccountId(moneyAccountEntity.getId())
                .setOldMoney(result.getMoney())
                // 充值
                .setDelta(moneyAccountEntity.getRechargeMoney())
                .setMoneyChangeCategory(MoneyChangeCategory.ChongZhi)
                .setFileUrl(moneyAccountEntity.getFileUrl())
                .setMemo("账户充值");

        moneyAccountEntity.setMoney(result.getMoney().add(moneyAccountEntity.getRechargeMoney()));

        changeDetail.setNewMoney(moneyAccountEntity.getMoney());
        changeDetail.setUserId(result.getUserId());
        changeDetail.setCompanyId(result.getStationId());
        this.updateById(moneyAccountEntity);

        moneyChangeDetailService.save(changeDetail);

        log.info("公司账户充值成功，充值金额：{}，余额：{} -> {}",
                moneyAccountEntity.getRechargeMoney(), changeDetail.getOldMoney(), changeDetail.getNewMoney());
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public MoneyAccountResult detail(Long id) {
        return baseMapper.selectMoneyAccountDetailById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<MoneyAccountResult> paging(PageParam pageParam, MoneyAccountEntity moneyAccountEntity) {
        Page<MoneyAccountResult> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        IPage<MoneyAccountResult> result = baseMapper.selectMoneyAccountWithName(page, moneyAccountEntity);
        return PageResult.of(result);
    }

}
