package org.jeecg.modules.system.app.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.app.dto.AddFamilyMembersDTO;
import org.jeecg.modules.app.dto.FindFamilyMembersDTO;
import org.jeecg.modules.system.app.dto.FindFamilyMembersResponseDTO;
import org.jeecg.modules.system.app.dto.FindResponseDTO;
import org.jeecg.modules.system.app.entity.FamilyMembers;
import org.jeecg.modules.system.app.enums.DelFlagEnum;
import org.jeecg.modules.system.app.mapper.FamilyMembersMapper;
import org.jeecg.modules.system.app.service.IFamilyMembersService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 家庭成员信息表
 * @Author: jeecg-boot
 * @Date:   2025-07-07
 * @Version: V1.0
 */
@Service
public class FamilyMembersServiceImpl extends ServiceImpl<FamilyMembersMapper, FamilyMembers> implements IFamilyMembersService {

    @Autowired
    FamilyMembersMapper familyMembersMapper;
    @Autowired
    SysUserMapper sysUserMapper;


    @Override
    public Result<FindFamilyMembersResponseDTO> findByUserId(String userId) {
        if (StringUtils.isEmpty(userId)){
            return Result.error("请求参数为空");
        }
        FindFamilyMembersResponseDTO findFamilyMembersResponseDTO = new FindFamilyMembersResponseDTO();

        //查询用户信息
        SysUser sysUser = sysUserMapper.selectById(userId);
        if (sysUser==null){
            return Result.error("未查询到用户信息,请核实是否正确");
        }
        BeanUtils.copyProperties(sysUser,findFamilyMembersResponseDTO);
        //查询家庭成员列表信息
        List<FamilyMembers> familyMembersList = familyMembersMapper.findByUserId(userId);
        findFamilyMembersResponseDTO.setFamilyMembersList(familyMembersList);
        return Result.ok(findFamilyMembersResponseDTO);
    }

    @Override
    public Result<String> addFamilyMembers(AddFamilyMembersDTO addFamilyMembersDTO) {

        if (StringUtils.isEmpty(addFamilyMembersDTO.getPhone())||addFamilyMembersDTO.getPhone().length()>11){
            return Result.error("添加家庭成员失败,手机号不允许为空,或者长度超过11位数");
        }
        if (StringUtils.isEmpty(addFamilyMembersDTO.getIdentityCard())||addFamilyMembersDTO.getIdentityCard().length()>18){
            return Result.error("添加家庭成员失败,身份证号不允许为空,或者长度超过18位数");
        }
        if (!familyMembersMapper.selectList(Wrappers.<FamilyMembers>lambdaQuery()
                .eq(FamilyMembers::getUserId,addFamilyMembersDTO.getUserId())
                .eq(FamilyMembers::getIdentityCard,addFamilyMembersDTO.getIdentityCard())).isEmpty()){
            return Result.error("请勿重复添加，该身份证已填写！");
        }
        FamilyMembers familyMembers = familyMembersMapper.findByUserIdAndPhone(addFamilyMembersDTO.getUserId(),addFamilyMembersDTO.getPhone());
        if (familyMembers==null){
            familyMembers = new FamilyMembers();
            familyMembers.setId(UuidUtils.getUUID());
        }else {
            addFamilyMembersDTO.setId(familyMembers.getId());
        }
        BeanUtils.copyProperties(addFamilyMembersDTO,familyMembers);

        familyMembers.setCreateTime(new Date());
        if (familyMembersMapper.insert(familyMembers)<1){
            return Result.error("添加家庭成员失败，请联系管理员");
        }
        return Result.ok("添加成功");
    }

    @Override
    public Result<String> editFamilyMembers(AddFamilyMembersDTO addFamilyMembersDTO) {
        FamilyMembers familyMembers = new FamilyMembers();
        BeanUtils.copyProperties(addFamilyMembersDTO,familyMembers);
        if (StringUtils.isEmpty(familyMembers.getId())){
            return Result.error("修改家庭成员失败，请选择家庭成员");
        }
        familyMembers.setUpdateTime(new Date());
        if (familyMembersMapper.updateById(familyMembers)<1){
            return Result.error("修改家庭成员失败，请联系管理员");
        }
        return Result.ok("修改成功");
    }

    @Override
    public Result<String> deleteFamilyMembers(String id) {

        if (StringUtils.isEmpty(id)){
            return Result.error("删除失败，请选择需要删除的家庭成员");
        }
        FamilyMembers familyMembers = familyMembersMapper.selectById(id);
        if (familyMembers==null){
            return Result.error("删除失败，未查询到家庭成员");
        }
        familyMembers.setDelFlag(DelFlagEnum.NO_DELETE.getCode());
        familyMembers.setUpdateTime(new Date());
        if (familyMembersMapper.updateById(familyMembers)<1){
            return Result.error("删除家庭成员失败，请联系管理员");
        }
        return Result.ok("删除成功");
    }

    @Override
    public Result<IPage<FamilyMembers>> findFamilyMembers(FindFamilyMembersDTO findFamilyMembersDTO) {
        Page<FindResponseDTO> page = new Page<FindResponseDTO>(findFamilyMembersDTO.getPageNo(), findFamilyMembersDTO.getPageSize());
        return Result.ok(familyMembersMapper.findByPage(page,findFamilyMembersDTO.getUserId()));
    }
}
