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

import com.ruoyi.web.arap.domain.ArapRefund;
import com.ruoyi.web.arap.mapper.ArapRefundMapper;
import com.ruoyi.web.arap.service.IArapRefundService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

@Service
public class ArapRefundServiceImpl implements IArapRefundService {

    @Autowired
    private ArapRefundMapper arapRefundMapper;

    @Override
    public ArapRefund selectById(Long id) {
        return arapRefundMapper.selectById(id);
    }

    @Override
    public java.util.List<ArapRefund> selectList(ArapRefund query) {
        return arapRefundMapper.selectList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(ArapRefund entity, String operator) {
        validate(entity);
        normalize(entity);

        if (StringUtils.isEmpty(entity.getRefType())) {
            entity.setRefType("CASH");
        }
        entity.setRefType(entity.getRefType().toUpperCase(Locale.ROOT));

        if (StringUtils.isEmpty(entity.getRefNo())) {
            entity.setRefNo(generateNo(entity.getOrgId(), entity.getRefDate()));
        }

        entity.setDelFlag("0");
        entity.setCreateBy(operator);
        entity.setUpdateBy(operator);
        return arapRefundMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(ArapRefund entity, String operator) {
        if (entity == null || entity.getId() == null) {
            throw new ServiceException("修改失败，id不能为空");
        }
        validate(entity);
        normalize(entity);

        if (StringUtils.isEmpty(entity.getRefType())) {
            entity.setRefType("CASH");
        }
        entity.setRefType(entity.getRefType().toUpperCase(Locale.ROOT));

        if (StringUtils.isEmpty(entity.getRefNo())) {
            throw new ServiceException("修改失败，退款编号不能为空");
        }

        entity.setUpdateBy(operator);
        return arapRefundMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id, String orgId, String operator) {
        return arapRefundMapper.deleteById(id, orgId, operator);
    }

    private void validate(ArapRefund e) {
        if (e == null) {
            throw new ServiceException("提交失败，参数不能为空");
        }
        if (e.getRefDate() == null) {
            throw new ServiceException("提交失败，退款日期不能为空");
        }
        if (StringUtils.isEmpty(e.getOrgId())) {
            throw new ServiceException("提交失败，企业标识不能为空");
        }
        if (StringUtils.isEmpty(e.getCustNo())) {
            throw new ServiceException("提交失败，客户编号不能为空");
        }
        if (StringUtils.isEmpty(e.getCustName())) {
            throw new ServiceException("提交失败，客户名称不能为空");
        }
        if (StringUtils.isEmpty(e.getRefBy())) {
            throw new ServiceException("提交失败，退款人不能为空");
        }
        if (StringUtils.isEmpty(e.getBankNo())) {
            throw new ServiceException("提交失败，银行卡号不能为空");
        }
        if (e.getRefAmt() == null) {
            throw new ServiceException("提交失败，退款金额不能为空");
        }
    }

    private void normalize(ArapRefund e) {
        if (e.getRefAmt() == null) e.setRefAmt(BigDecimal.ZERO);
        if (StringUtils.isEmpty(e.getDeducted())) e.setDeducted("0");
    }

    private String generateNo(String orgId, Date refDate) {
        String dateText = new SimpleDateFormat("yyyyMMdd").format(refDate);
        Integer maxSeq = arapRefundMapper.selectTodayMaxSeq(orgId, dateText);
        int nextSeq = (maxSeq == null ? 0 : maxSeq) + 1;
        return dateText + String.format("%04d", nextSeq);
    }
}

