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

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.stock.domain.WarehouseOperPerf;
import com.ruoyi.web.stock.mapper.WarehouseOperPerfMapper;
import com.ruoyi.web.stock.service.IWarehouseOperPerfService;
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;

@Service
public class WarehouseOperPerfServiceImpl implements IWarehouseOperPerfService {

    @Autowired
    private WarehouseOperPerfMapper warehouseOperPerfMapper;

    @Override
    public WarehouseOperPerf selectById(Long id) {
        return warehouseOperPerfMapper.selectById(id);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(WarehouseOperPerf entity, String operator) {
        validate(entity);
        normalizeAmount(entity);
        if (StringUtils.isEmpty(entity.getSheetNo())) {
            entity.setSheetNo(generateSheetNo(entity.getOrgId(), entity.getFeeDate()));
        }
        entity.setCreateBy(operator);
        entity.setUpdateBy(operator);
        entity.setDelFlag("0");
        return warehouseOperPerfMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(WarehouseOperPerf entity, String operator) {
        if (entity == null || entity.getId() == null) {
            throw new ServiceException("修改失败，id不能为空");
        }
        validate(entity);
        normalizeAmount(entity);
        if (StringUtils.isEmpty(entity.getSheetNo())) {
            entity.setSheetNo(generateSheetNo(entity.getOrgId(), entity.getFeeDate()));
        }
        entity.setUpdateBy(operator);
        return warehouseOperPerfMapper.updateById(entity);
    }

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

    private void validate(WarehouseOperPerf entity) {
        if (entity == null) {
            throw new ServiceException("提交失败，参数不能为空");
        }
        if (entity.getFeeDate() == null) {
            throw new ServiceException("提交失败，费用日期不能为空");
        }
        if (StringUtils.isEmpty(entity.getOrgId())) {
            throw new ServiceException("提交失败，企业标识不能为空");
        }
    }

    private void normalizeAmount(WarehouseOperPerf e) {
        if (e.getRebHead() == null) e.setRebHead(BigDecimal.ZERO);
        if (e.getOthInc() == null) e.setOthInc(BigDecimal.ZERO);
        if (e.getOthExp() == null) e.setOthExp(BigDecimal.ZERO);
        if (e.getTaxSurch() == null) e.setTaxSurch(BigDecimal.ZERO);
        if (e.getImpairLoss() == null) e.setImpairLoss(BigDecimal.ZERO);
        if (e.getNonopInc() == null) e.setNonopInc(BigDecimal.ZERO);
        if (e.getNonopExp() == null) e.setNonopExp(BigDecimal.ZERO);
        if (e.getIncTaxExp() == null) e.setIncTaxExp(BigDecimal.ZERO);
        if (e.getFreightBills() == null) e.setFreightBills(BigDecimal.ZERO);
    }

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

