package com.zcloud.basic.info.persistence.repository.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.zcloud.basic.info.persistence.dataobject.UserExpandInfoDO;
import com.zcloud.basic.info.persistence.mapper.UserExpandInfoMapper;
import com.zcloud.basic.info.persistence.repository.UserExpandInfoRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2026-01-05 14:12:19
 */
@Service
@RequiredArgsConstructor
public class UserExpandInfoRepositoryImpl extends BaseRepositoryImpl<UserExpandInfoMapper, UserExpandInfoDO> implements UserExpandInfoRepository {
    private final UserExpandInfoMapper userExpandInfoMapper;

    @Override
    public PageResponse<UserExpandInfoDO> listPage(Map<String, Object> params) {
        IPage<UserExpandInfoDO> iPage = new Query<UserExpandInfoDO>().getPage(params);
        QueryWrapper<UserExpandInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params);
        queryWrapper.orderByDesc("create_time");
        IPage<UserExpandInfoDO> result = userExpandInfoMapper.selectPage(iPage, queryWrapper);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public UserExpandInfoDO getInfoByPhone(String phone) {
        QueryWrapper<UserExpandInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        List<UserExpandInfoDO> userExpandInfoDOS = userExpandInfoMapper.selectList(queryWrapper);
        if(CollUtil.isEmpty(userExpandInfoDOS)) {
            return null;
        }
        return userExpandInfoDOS.get(0);
    }

    @Override
    public void updateByPhone(String phone, Integer flowFlag) {
        //查询
        UserExpandInfoDO userExpandInfoDO = getInfoByPhone(phone);
        if(userExpandInfoDO == null) {
            //新增
            userExpandInfoDO = new UserExpandInfoDO();
            userExpandInfoDO.setPhone(phone);
            userExpandInfoDO.setFlowFlag(flowFlag);
            userExpandInfoMapper.insert(userExpandInfoDO);
        }else{
            userExpandInfoDO.setFlowFlag(flowFlag);
            userExpandInfoMapper.updateById(userExpandInfoDO);
        }

    }
}

