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

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.arap.domain.ArapRecv;
import com.ruoyi.web.arap.mapper.ArapRecvMapper;
import com.ruoyi.web.arap.service.IArapRecvService;
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 ArapRecvServiceImpl implements IArapRecvService {

    @Autowired
    private ArapRecvMapper arapRecvMapper;

    @Override
    public ArapRecv selectById(Long id) {
        return arapRecvMapper.selectById(id);
    }

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

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

        if (StringUtils.isEmpty(entity.getRecvType())) {
            entity.setRecvType("CASH");
        }
        entity.setRecvType(entity.getRecvType().toUpperCase(Locale.ROOT));

        if (StringUtils.isEmpty(entity.getRecvNo())) {
            entity.setRecvNo(generateNo(entity.getOrgId(), entity.getRecvDate()));
        }

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

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

        if (StringUtils.isEmpty(entity.getRecvType())) {
            entity.setRecvType("CASH");
        }
        entity.setRecvType(entity.getRecvType().toUpperCase(Locale.ROOT));

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

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

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

    private void validate(ArapRecv e) {
        if (e == null) {
            throw new ServiceException("提交失败，参数不能为空");
        }
        if (e.getRecvDate() == 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.getRecvBy())) {
            throw new ServiceException("提交失败，收款人不能为空");
        }
        if (StringUtils.isEmpty(e.getBankNo())) {
            throw new ServiceException("提交失败，银行卡号不能为空");
        }
        if (e.getRecvAmt() == null) {
            throw new ServiceException("提交失败，收款金额不能为空");
        }
    }

    private void normalize(ArapRecv e) {
        if (e.getRecvAmt() == null) e.setRecvAmt(BigDecimal.ZERO);
        if (StringUtils.isEmpty(e.getDeducted())) e.setDeducted("0");
    }

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

