/*
 * Copyright (c) 2024 codvision.com All Rights Reserved.
 */

package com.codvision.basic.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codvision.basic.entity.Person;
import com.codvision.basic.entity.PersonAddressRelation;
import com.codvision.basic.entity.dto.PersonAddressRelationDTO;
import com.codvision.basic.entity.vo.PersonAddressRelationVO;
import com.codvision.basic.mapper.PersonAddressRelationMapper;
import com.codvision.basic.mapper.PersonMapper;
import com.codvision.basic.service.PersonAddressRelationService;
import com.codvision.commoncore.utils.Preconditions;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.util.QueryPageUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author KX
 */
@Service
@RequiredArgsConstructor
public class PersonAddressRelationServiceImpl extends ServiceImpl<PersonAddressRelationMapper, PersonAddressRelation> implements PersonAddressRelationService {

    private final PersonMapper personMapper;

    @Override
    public IPage<PersonAddressRelationVO> getPageResult(QueryPage<Map<String, Object>> queryPage) {
        Map<String, Object> paraMap = QueryPageUtil.queryMap(queryPage);
        String flagRelated = (String) paraMap.get("flagRelated");
        if (StringUtils.isBlank(flagRelated)) {
            paraMap.put("flagRelated", "1");
        }
        return getBaseMapper().getPageResult(queryPage.getPage(), paraMap);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(PersonAddressRelationDTO personAddressRelationDto) {
        if (personAddressRelationDto.getPersonId() == null) {
            // 如果人员未新增需要先新增人员
            Person person = Person.builder().idType(personAddressRelationDto.getIdType()).idCard(personAddressRelationDto.getIdCard())
                    .phone(personAddressRelationDto.getPhone()).name(personAddressRelationDto.getName())
                    .sex(personAddressRelationDto.getSex()).personTags(personAddressRelationDto.getPersonTags())
                    .personType(personAddressRelationDto.getPersonType()).oid(personAddressRelationDto.getOid())
                    .age(personAddressRelationDto.getAge())
                    .nativePlace(personAddressRelationDto.getNativePlace()).build();
            personMapper.insert(person);
            personAddressRelationDto.setPersonId(person.getId());
        }

        Long count = getBaseMapper().selectCount(Wrappers.<PersonAddressRelation>lambdaQuery()
                .eq(PersonAddressRelation::getPersonId, personAddressRelationDto.getPersonId())
                .eq(PersonAddressRelation::getAddressId, personAddressRelationDto.getAddressId())
                .eq(PersonAddressRelation::getFlagRelated, '1'));
        Preconditions.checkArgument(count <= 0, "该人员和该户室已绑定，请勿重复绑定！");

        PersonAddressRelation personAddressRelation = PersonAddressRelation.builder().addressId(personAddressRelationDto.getAddressId())
                .personId(personAddressRelationDto.getPersonId()).householderRelation(personAddressRelationDto.getHouseholderRelation())
                .personAddressRelation(personAddressRelationDto.getPersonAddressRelation()).flagRelated("1")
                .addressCode(personAddressRelationDto.getAddressCode()).idType(personAddressRelationDto.getIdType())
                .idCard(personAddressRelationDto.getIdCard()).build();
        getBaseMapper().insert(personAddressRelation);
    }

    @Override
    public void moveOut(List<Integer> ids) {
        getBaseMapper().moveOut(ids);
    }
}
