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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yd.common.enums.ResultCode;
import com.yd.common.exception.BusinessException;
import com.yd.common.utils.ChineseTextConverter;
import com.yd.common.utils.EnglishTextUtil;
import com.yd.csf.service.dto.PolicyPolicyholderUpdateRequest;
import com.yd.csf.service.model.PolicyPolicyholder;
import com.yd.csf.service.service.PolicyPolicyholderService;
import com.yd.csf.service.dao.PolicyPolicyholderMapper;
import com.yd.csf.service.utils.GSONUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author Zhang Jianan
* @description 针对表【policy_policyholder(保单投保人信息表)】的数据库操作Service实现
* @createDate 2025-10-31 11:43:10
*/
@Service
public class PolicyPolicyholderServiceImpl extends ServiceImpl<PolicyPolicyholderMapper, PolicyPolicyholder>
    implements PolicyPolicyholderService{

    @Override
    public boolean update(PolicyPolicyholderUpdateRequest policyPolicyholderUpdateRequest) {
        if (policyPolicyholderUpdateRequest == null || policyPolicyholderUpdateRequest.getPolicyPolicyholderBizId() == null) {
            throw new BusinessException(ResultCode.PARAMS_ERROR.getCode(), ResultCode.PARAMS_ERROR.getMessage());
        }
        PolicyPolicyholder policyPolicyholder = this.getOne(new QueryWrapper<PolicyPolicyholder>().eq("policy_policyholder_biz_id", policyPolicyholderUpdateRequest.getPolicyPolicyholderBizId()));
        if (policyPolicyholder == null) {
            throw new BusinessException(ResultCode.NULL_ERROR.getCode(), ResultCode.NULL_ERROR.getMessage());
        }
        BeanUtils.copyProperties(policyPolicyholderUpdateRequest, policyPolicyholder, "policyBizId", "policyPolicyholderBizId", "id");
        // 处理地址Json
        if (CollectionUtils.isNotEmpty(policyPolicyholderUpdateRequest.getAddressList())) {
            policyPolicyholder.setAddressList(GSONUtil.toJson(policyPolicyholderUpdateRequest.getAddressList()));
        }
        policyPolicyholder.setUpdateTime(new Date());
        return this.updateById(policyPolicyholder);
    }

    @Override
    public PolicyPolicyholder queryOne(String policyBizId) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<PolicyPolicyholder>()
                .eq(PolicyPolicyholder::getPolicyBizId,policyBizId).last(" limit 1 "));
    }

    @Override
    public Map<String, PolicyPolicyholder> queryPolicyholderMap(Collection<?> policyNoList) {
        if (CollectionUtils.isEmpty(policyNoList)) {
            return Collections.emptyMap();
        }

        List<PolicyPolicyholder> policyholderList = this.lambdaQuery()
                .in(PolicyPolicyholder::getPolicyNo, policyNoList)
                .list();

        return policyholderList.stream().collect(Collectors.toMap(PolicyPolicyholder::getPolicyNo, a -> a, (oldValue, newValue) -> newValue));
    }

    @Override
    public List<PolicyPolicyholder> queryList(String policyHolder) {
        // 输入为空时直接返回空列表，避免无意义的全表查询
        if (StringUtils.isBlank(policyHolder)) {
            return Collections.emptyList();
        }

        // 中文转简体
        String nameSc = ChineseTextConverter.traditionalToSimplified(policyHolder);
        // 英文转小写
        String nameEnLc = EnglishTextUtil.toLowerCaseSafe(policyHolder);

        return this.baseMapper.selectList(new LambdaQueryWrapper<PolicyPolicyholder>()
                .and(wrapper -> wrapper
                        .like(StringUtils.isNotBlank(nameSc), PolicyPolicyholder::getNameSc, nameSc)
                        .or()
                        .like(StringUtils.isNotBlank(nameEnLc), PolicyPolicyholder::getNameEnLc, nameEnLc)
                )
        );
    }

    @Override
    public boolean updateName(String policyBizId, String name) {
        String nameSc = ChineseTextConverter.traditionalToSimplified(name);
        return this.update(new UpdateWrapper<PolicyPolicyholder>()
                .set("name", name)
                .set("name_sc", nameSc)
                .eq("policy_biz_id", policyBizId));
    }
}




