package com.yd.csf.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.reflect.TypeToken;
import com.yd.base.feign.client.relobjectcertificate.ApiRelObjectCertificateFeignClient;
import com.yd.base.feign.dto.ApiCertificateDto;
import com.yd.common.exception.BusinessException;
import com.yd.csf.service.common.ErrorCode;
import com.yd.csf.service.dto.DependantData;
import com.yd.csf.service.dto.TaxCountry;
import com.yd.csf.service.model.Fna;
import com.yd.csf.service.model.FnaCustomer;
import com.yd.csf.service.model.FnaForm;
import com.yd.csf.service.service.CustomerService;
import com.yd.csf.service.service.FnaCustomerService;
import com.yd.csf.service.dao.FnaCustomerMapper;
import com.yd.csf.service.service.FnaFormService;
import com.yd.csf.service.service.FnaService;
import com.yd.csf.service.utils.GSONUtil;
import com.yd.csf.service.vo.AddressVO;
import com.yd.csf.service.vo.CustomerVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Zhang Jianan
 * @description 针对表【fna_customer(客户信息表)】的数据库操作Service实现
 * @createDate 2026-03-09 10:45:37
 */
@Service
public class FnaCustomerServiceImpl extends ServiceImpl<FnaCustomerMapper, FnaCustomer>
        implements FnaCustomerService {

    @Resource
    private FnaCustomerService fnaCustomerService;

    @Resource
    private CustomerService customerService;

    @Resource
    private FnaService fnaService;

    @Resource
    private FnaFormService fnaFormService;

    @Resource
    private ApiRelObjectCertificateFeignClient apiRelObjectCertificateFeignClient;


    @Override
    public void validCustomer(FnaCustomer fnaCustomer) {
        if (StringUtils.isBlank(fnaCustomer.getNamePyEn())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR.getCode(), "名字-英文不能为空");
        }
        if (StringUtils.isBlank(fnaCustomer.getMobile())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR.getCode(), "客户手机号不能为空");
        }
        if (StringUtils.isBlank(fnaCustomer.getAge())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR.getCode(), "年龄不能为空");
        }
        if (StringUtils.isBlank(fnaCustomer.getNationality())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR.getCode(), "国籍不能为空");
        }
    }

    @Override
    public int queryDependentsNum(String fnaBizId) {
        Fna fna = fnaService.getByBizId(fnaBizId);
        if (fna == null) {
            return 0;
        }

        FnaForm fnaForm = fnaFormService.getByFnaFormBizId(fna.getFnaFormBizId());
        if (fnaForm != null) {
            if (StringUtils.isNotBlank((CharSequence) fnaForm.getDependantList()) && !"null".equals(fnaForm.getDependantList())) {
                List<DependantData> dependantList = GSONUtil.fromJson((String) fnaForm.getDependantList(), new TypeToken<List<DependantData>>() {
                }.getType());
                return dependantList.size();
            }
        }
        return 0;
    }

    @Override
    public CustomerVO getCustomerVO(FnaCustomer fnaCustomer) {
        if (fnaCustomer == null) {
            return null;
        }

        // 查询受供养人数量
        int dependentsNum = fnaCustomerService.queryDependentsNum(fnaCustomer.getFnaBizId());

        // 获取封装类
        CustomerVO customerVO = new CustomerVO();
        BeanUtils.copyProperties(fnaCustomer, customerVO);
        customerVO.setDependentsNum(dependentsNum);

        if (ObjectUtils.isNotEmpty(fnaCustomer.getAddressList())) {
            List<AddressVO> addressList = GSONUtil.fromJson(fnaCustomer.getAddressList(), new TypeToken<List<AddressVO>>() {
            }.getType());
            customerVO.setAddressList(addressList);
        }
        if (ObjectUtils.isNotEmpty(fnaCustomer.getTaxList())) {
            List<TaxCountry> taxList = GSONUtil.fromJson((String) fnaCustomer.getTaxList(), new TypeToken<List<TaxCountry>>() {
            }.getType());
            customerVO.setApiTaxationDtoList(taxList);
        }
        if (ObjectUtils.isNotEmpty(fnaCustomer.getCertificateList())) {
            List<ApiCertificateDto> certificateList = GSONUtil.fromJson((String) fnaCustomer.getCertificateList(), new TypeToken<List<ApiCertificateDto>>() {
            }.getType());
            customerVO.setApiCertificateDtoList(certificateList);
        }

        return customerVO;
    }
}




