package com.ruoyi.info.collection.service.impl;

import com.ruoyi.info.collection.domain.CcdiAssetInfo;
import com.ruoyi.info.collection.domain.dto.CcdiAssetInfoDTO;
import com.ruoyi.info.collection.mapper.CcdiAssetInfoMapper;
import com.ruoyi.info.collection.service.ICcdiAssetInfoService;
import com.ruoyi.common.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 员工资产信息 服务层处理
 *
 * @author ruoyi
 * @date 2026-03-12
 */
@Service
public class CcdiAssetInfoServiceImpl implements ICcdiAssetInfoService {

    @Resource
    private CcdiAssetInfoMapper assetInfoMapper;

    @Override
    public List<CcdiAssetInfo> selectByFamilyId(String familyId) {
        return assetInfoMapper.selectByFamilyId(familyId);
    }

    @Override
    public List<CcdiAssetInfo> selectByFamilyIdAndPersonId(String familyId, String personId) {
        return assetInfoMapper.selectByFamilyIdAndPersonId(familyId, personId);
    }

    @Override
    @Transactional
    public void replaceByFamilyId(String familyId, List<CcdiAssetInfoDTO> assetInfoList) {
        replaceAssets(familyId, familyId, assetInfoList, true);
    }

    @Override
    @Transactional
    public void replaceByFamilyIdAndPersonId(String familyId, String personId, List<CcdiAssetInfoDTO> assetInfoList) {
        replaceAssets(familyId, personId, assetInfoList, false);
    }

    @Override
    public int deleteByFamilyId(String familyId) {
        return assetInfoMapper.deleteByFamilyId(familyId);
    }

    @Override
    public int deleteByFamilyIdAndPersonId(String familyId, String personId) {
        return assetInfoMapper.deleteByFamilyIdAndPersonId(familyId, personId);
    }

    @Override
    public int deleteByFamilyIds(List<String> familyIds) {
        if (familyIds == null || familyIds.isEmpty()) {
            return 0;
        }
        return assetInfoMapper.deleteByFamilyIds(familyIds);
    }

    private void replaceAssets(String familyId, String personId, List<CcdiAssetInfoDTO> assetInfoList, boolean deleteByFamilyOnly) {
        if (deleteByFamilyOnly) {
            assetInfoMapper.deleteByFamilyId(familyId);
        } else {
            assetInfoMapper.deleteByFamilyIdAndPersonId(familyId, personId);
        }
        if (assetInfoList == null || assetInfoList.isEmpty()) {
            return;
        }

        List<CcdiAssetInfo> saveList = assetInfoList.stream()
                .filter(item -> !isEmptyRow(item))
                .map(item -> {
                    validateAsset(item);
                    return toEntity(familyId, personId, item);
                })
                .toList();

        if (!saveList.isEmpty()) {
            assetInfoMapper.insertBatch(saveList);
        }
    }

    private CcdiAssetInfo toEntity(String familyId, String personId, CcdiAssetInfoDTO dto) {
        CcdiAssetInfo assetInfo = new CcdiAssetInfo();
        BeanUtils.copyProperties(dto, assetInfo);
        assetInfo.setFamilyId(familyId);
        assetInfo.setPersonId(personId);
        return assetInfo;
    }

    private boolean isEmptyRow(CcdiAssetInfoDTO dto) {
        return StringUtils.isEmpty(dto.getAssetMainType())
                && StringUtils.isEmpty(dto.getAssetSubType())
                && StringUtils.isEmpty(dto.getAssetName())
                && dto.getCurrentValue() == null
                && StringUtils.isEmpty(dto.getAssetStatus())
                && dto.getOwnershipRatio() == null
                && dto.getPurchaseEvalDate() == null
                && dto.getOriginalValue() == null
                && dto.getValuationDate() == null
                && StringUtils.isEmpty(dto.getRemarks());
    }

    private void validateAsset(CcdiAssetInfoDTO dto) {
        if (StringUtils.isEmpty(dto.getAssetMainType())) {
            throw new RuntimeException("资产大类不能为空");
        }
        if (StringUtils.isEmpty(dto.getAssetSubType())) {
            throw new RuntimeException("资产小类不能为空");
        }
        if (StringUtils.isEmpty(dto.getAssetName())) {
            throw new RuntimeException("资产名称不能为空");
        }
        if (dto.getCurrentValue() == null) {
            throw new RuntimeException("当前估值不能为空");
        }
        if (StringUtils.isEmpty(dto.getAssetStatus())) {
            throw new RuntimeException("资产状态不能为空");
        }
    }
}
