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.PolicyInsurantUpdateRequest;
import com.yd.csf.service.model.PolicyInsurant;
import com.yd.csf.service.model.PolicyPolicyholder;
import com.yd.csf.service.service.PolicyInsurantService;
import com.yd.csf.service.dao.PolicyInsurantMapper;
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.Collections;
import java.util.Date;
import java.util.List;

/**
* @author Zhang Jianan
* @description 针对表【policy_insurant(保单受保人信息表)】的数据库操作Service实现
* @createDate 2025-10-31 13:29:18
*/
@Service
public class PolicyInsurantServiceImpl extends ServiceImpl<PolicyInsurantMapper, PolicyInsurant>
    implements PolicyInsurantService{

    @Override
    public boolean update(PolicyInsurantUpdateRequest policyInsurantUpdateRequest) {
        if (policyInsurantUpdateRequest == null || StringUtils.isBlank(policyInsurantUpdateRequest.getPolicyInsurantBizId())) {
            throw new BusinessException(ResultCode.PARAMS_ERROR.getCode(), "policyInsurantBizId不能为空");
        }
        PolicyInsurant policyInsurant = this.getOne(new QueryWrapper<PolicyInsurant>().eq("policy_insurant_biz_id", policyInsurantUpdateRequest.getPolicyInsurantBizId()));
        if (policyInsurant == null) {
            throw new BusinessException(ResultCode.NULL_ERROR.getCode(), ResultCode.NULL_ERROR.getMessage());
        }
        BeanUtils.copyProperties(policyInsurantUpdateRequest, policyInsurant, "policyBizId", "policyInsurantBizId", "id");
        // 处理地址Json
        if (CollectionUtils.isNotEmpty(policyInsurantUpdateRequest.getAddressList())) {
            policyInsurant.setAddressList(GSONUtil.toJson(policyInsurantUpdateRequest.getAddressList()));
        }
        policyInsurant.setUpdateTime(new Date());
        return this.updateById(policyInsurant);
    }

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

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

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

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




