package com.xiaoniu.system.platformusers.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoniu.common.core.constant.Constants;
import com.xiaoniu.common.core.exception.ServiceException;
import com.xiaoniu.common.core.resdata.RowsData;
import com.xiaoniu.common.core.utils.StringUtils;
import com.xiaoniu.common.core.utils.bean.BeanUtils;
import com.xiaoniu.system.operation.member.domain.TbMember;
import com.xiaoniu.system.operation.member.mapper.TbMemberMapper;
import com.xiaoniu.system.order.storeorder.mapper.TbOrderInfoMapper;
import com.xiaoniu.system.platformusers.user.domain.TbClientUser;
import com.xiaoniu.system.platformusers.user.domain.TbUserInfo;
import com.xiaoniu.system.platformusers.user.dto.ClientUserListQueryDTO;
import com.xiaoniu.system.platformusers.user.dto.EditClientUserDTO;
import com.xiaoniu.system.platformusers.user.dto.LogoutAndBlockDTO;
import com.xiaoniu.system.platformusers.user.dto.UserOrderListQueryDTO;
import com.xiaoniu.system.platformusers.user.mapper.TbClientUserMapper;
import com.xiaoniu.system.platformusers.user.mapper.TbUserInfoMapper;
import com.xiaoniu.system.platformusers.user.service.ITbClientUserService;
import com.xiaoniu.system.platformusers.user.vo.ClientUserDetailsVO;
import com.xiaoniu.system.platformusers.user.vo.ClientUserListVO;
import com.xiaoniu.system.platformusers.user.vo.UserOrderInfoVO;
import com.xiaoniu.system.platformusers.user.vo.UserOrderListVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 用户端用户信息表 服务实现类
 * </p>
 *
 * @author haoxuyang
 * @since 2024-10-14 11:55:30
 */
@Service
public class TbClientUserServiceImpl extends ServiceImpl<TbClientUserMapper, TbClientUser> implements ITbClientUserService {
    @Resource
    private TbUserInfoMapper tbUserInfoMapper;

    @Resource
    private TbOrderInfoMapper tbOrderInfoMapper;

    @Resource
    private TbMemberMapper tbMemberMapper;


    @Override
    public RowsData<ClientUserListVO> clientUserList(ClientUserListQueryDTO dto) {
        try (Page<Object> page = PageHelper.startPage(dto.getPageNum(), dto.getPageSize())) {
            if (StringUtils.isNotEmpty(dto.getSortType())) {
                if ("1".equals(dto.getSortType())) {
                    dto.setSortType("ASC");
                } else if ("2".equals(dto.getSortType())) {
                    dto.setSortType("DESC");
                } else {
                    dto.setSortType(null);
                }
            }
            List<ClientUserListVO> list = baseMapper.getClientUserList(dto);
            return new RowsData<>(list, page.getTotal(), dto.getPageNum());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editClientUser(EditClientUserDTO dto) {
        TbClientUser tbClientUser = baseMapper.selectById(dto.getId());
        if (StringUtils.isNull(tbClientUser)) {
            throw new ServiceException("当前用户信息不存在");
        }
        if (Constants.TWO.equals(tbClientUser.getDelFlag())) {
            throw new ServiceException("当前用户已删除");
        }
        BeanUtils.copyProperties(dto, tbClientUser);
        baseMapper.updateById(tbClientUser);
        TbMember tbMember = tbMemberMapper.selectById(dto.getMemberId());
        //更新用户积分
        TbUserInfo userInfo = tbUserInfoMapper.selectOne(new LambdaQueryWrapper<TbUserInfo>().eq(TbUserInfo::getUserId, tbClientUser.getId()).last("limit 1"));
        if (StringUtils.isNull(userInfo)) {
            TbUserInfo tbUserInfo = new TbUserInfo();
            tbUserInfo.setUserId(tbClientUser.getId());
            if (StringUtils.isNotNull(tbMember)) {
                tbUserInfo.setUserType(tbMember.getMemberName());
            }
            tbUserInfo.setMemberId(dto.getMemberId());
            tbUserInfo.setUserLabel(dto.getUserLabel());
            tbUserInfo.setCreateTime(new Date());
            tbUserInfo.setRemainingPoints(dto.getRemainingPoints());
            tbUserInfoMapper.insert(tbUserInfo);
        } else {
            userInfo.setRemainingPoints(dto.getRemainingPoints());
            userInfo.setMemberId(dto.getMemberId());
            if (StringUtils.isNotNull(tbMember)) {
                userInfo.setUserType(tbMember.getMemberName());
            }
            userInfo.setUserLabel(dto.getUserLabel());
            tbUserInfoMapper.updateById(userInfo);
        }
    }

    @Override
    public ClientUserDetailsVO details(Long id) {
        return baseMapper.details(id);
    }

    @Override
    public void logoutAndBlock(LogoutAndBlockDTO dto) {
        TbClientUser tbClientUser = baseMapper.selectById(dto.getId());
        if (StringUtils.isNull(tbClientUser)) {
            throw new ServiceException("当前用户信息不存在");
        }
        tbClientUser.setStatus(dto.getStatus());
        baseMapper.updateById(tbClientUser);
    }

    @Override
    public RowsData<UserOrderListVO> orderList(UserOrderListQueryDTO dto) {
        try (Page<Object> page = PageHelper.startPage(dto.getPageNum(), dto.getPageSize())) {
            if (StringUtils.isNotEmpty(dto.getSortType())) {
                if ("1".equals(dto.getSortType())) {
                    dto.setSortType("ASC");
                } else if ("2".equals(dto.getSortType())) {
                    dto.setSortType("DESC");
                } else {
                    dto.setSortType("DESC");
                }
            }
            List<UserOrderListVO> list = tbOrderInfoMapper.getUserOrderList(dto);
            return new RowsData<>(list, page.getTotal(), dto.getPageNum());
        }
    }

    @Override
    public UserOrderInfoVO getOrderInfo(Long orderId) {

        return null;
    }
}
