package com.ruoyi.xq.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.utils.BeanConvertUtil;
import com.ruoyi.xq.domain.User;
import com.ruoyi.xq.domain.UserInfo;
import com.ruoyi.xq.dto.admin.user.UserInfoDetailAdminVo;
import com.ruoyi.xq.dto.admin.user.UserInfoListAdminVo;
import com.ruoyi.xq.dto.admin.user.req.UpdateUserInfoReq;
import com.ruoyi.xq.dto.admin.user.req.UpdateUserinfoFilterReq;
import com.ruoyi.xq.mapper.UserInfoMapper;
import com.ruoyi.xq.service.UserInfoService;
import com.ruoyi.xq.service.UserService;
import com.ruoyi.xq.util.AgeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户信息Service业务层处理
 *
 * @author 77
 * @date 2024-03-04
 */
@RequiredArgsConstructor
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper,UserInfo> implements UserInfoService {

    @Autowired
    private UserService userService;

    @Override
    public UserInfo getByUsercode(String usercode) {
        return this.getOne(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUsercode, usercode));
    }

    @Override
    public UserInfo getByUserId(Long userId) {
        return this.getOne(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUserId, userId));
    }

    @Override
    public Page<UserInfoListAdminVo> pageAdmin(PageQuery pageQuery, UserInfoListAdminVo bo) {
        Page<UserInfoListAdminVo> page = baseMapper.pageAdmin(pageQuery.build(), bo);
        List<UserInfoListAdminVo> records = page.getRecords();
        for (UserInfoListAdminVo record : records) {
            record.setAge(AgeUtil.getAge(record.getBirthday()));
        }
        return page;
    }

    @Override
    public UpdateUserInfoReq getUserInfoUpdate(Long userId) {
        User user = userService.getById(userId);
        UserInfo userInfo = this.getByUserId(userId);
        UpdateUserInfoReq res = BeanConvertUtil.convertTo(user, UpdateUserInfoReq::new);
        BeanConvertUtil.copyProperties(userInfo, res);
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateInfo(UpdateUserInfoReq bo) {
        User user = new User();
        user.setId(bo.getUserId());
        user.setUpdateTime(LocalDateTime.now());
        BeanConvertUtil.copyProperties(bo, user);
        user.setMobile(null);
        userService.updateById(user);

        UserInfo userInnnn = this.getByUserId(bo.getUserId());
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userInnnn.getId());
        userInfo.setUpdateTime(LocalDateTime.now());
        BeanConvertUtil.copyProperties(bo, userInfo);
        userInfo.setUserId(null);
        this.updateById(userInfo);
    }

    @Override
    public void updateInfoFilter(UpdateUserinfoFilterReq bo) {
        LambdaUpdateWrapper<UserInfo> set = Wrappers.lambdaUpdate(UserInfo.class)
                .eq(UserInfo::getId, bo.getId())
                .set(UserInfo::getFilterAge, bo.getFilterAge())
                .set(UserInfo::getFilterHeight, bo.getFilterHeight())
                .set(UserInfo::getFilterSomatotype, bo.getFilterSomatotype())
                .set(UserInfo::getFilterMarriage, bo.getFilterMarriage())
                .set(UserInfo::getFilterEducation, bo.getFilterEducation())
                .set(UserInfo::getFilterResidence, bo.getFilterResidence())
                .set(UserInfo::getFilterResidenceCode, bo.getFilterResidenceCode())
                .set(UserInfo::getFilterAnnualIncome, bo.getFilterAnnualIncome())
                .set(UserInfo::getFilterChildStatus, bo.getFilterChildStatus())
                .set(UserInfo::getFilterHousingStatus, bo.getFilterHousingStatus())
                .set(UserInfo::getFilterCarStatus, bo.getFilterCarStatus());
        this.update(set);
    }


}
