package com.gxwebsoft.shop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.exception.enums.GlobalErrorCodeConstants;
import com.gxwebsoft.common.core.utils.LoginUserUtil;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.mapper.UserMapper;
import com.gxwebsoft.shop.mapper.ShopVerifyUserMapper;
import com.gxwebsoft.shop.service.ShopVerifyUserService;
import com.gxwebsoft.shop.entity.ShopVerifyUser;
import com.gxwebsoft.shop.param.ShopVerifyUserParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

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

/**
 * 订单核销人管理Service实现
 *
 * @author xm
 * @since 2026-05-25 17:00:03
 */
@Service
@AllArgsConstructor
public class ShopVerifyUserServiceImpl extends ServiceImpl<ShopVerifyUserMapper, ShopVerifyUser> implements ShopVerifyUserService {

    private UserMapper userMapper;

    @Override
    public PageResult<ShopVerifyUser> pageRel(ShopVerifyUserParam param) {
        PageParam<ShopVerifyUser, ShopVerifyUserParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<ShopVerifyUser> list = baseMapper.selectPageRel(page, param);
        if(CollectionUtils.isNotEmpty(list)){
            List<Integer> userIdList = list.stream().map(ShopVerifyUser::getUserId).distinct().collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(userIdList)){
                List<User> userList = userMapper.selectByUserIdList(userIdList);

                for(ShopVerifyUser verifyUser : list){
                    User u = userList.stream().filter(user -> verifyUser.getUserId().equals(user.getUserId())).findFirst().orElse(null);
                    if(u != null){
                        verifyUser.setUserName(StrUtil.isNotEmpty(u.getRealName()) ? u.getRealName() : u.getNickname());
                    }
                }
            }
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<ShopVerifyUser> listRel(ShopVerifyUserParam param) {
        List<ShopVerifyUser> list = baseMapper.selectListRel(param);
        if(CollectionUtils.isNotEmpty(list)){
            List<Integer> userIdList = list.stream().map(ShopVerifyUser::getUserId).distinct().collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(userIdList)){
                List<User> userList = userMapper.selectByUserIdList(userIdList);

                for(ShopVerifyUser verifyUser : list){
                    User u = userList.stream().filter(user -> verifyUser.getUserId().equals(user.getUserId())).findFirst().orElse(null);
                    if(u != null){
                        verifyUser.setUserName(StrUtil.isNotEmpty(u.getRealName()) ? u.getRealName() : u.getNickname());
                    }
                }
            }
        }
        // 排序
        PageParam<ShopVerifyUser, ShopVerifyUserParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public ShopVerifyUser getByIdRel(Integer id) {
        ShopVerifyUserParam param = new ShopVerifyUserParam();
        param.setId(id);
        ShopVerifyUser verifyUser = param.getOne(baseMapper.selectListRel(param));
        if(verifyUser != null){
            User user = userMapper.getById(verifyUser.getUserId());
            if(user != null){
                verifyUser.setUserName(StrUtil.isNotEmpty(user.getRealName()) ? user.getRealName() : user.getNickname());
            }
        }
        return verifyUser;
    }

    @Override
    public ShopVerifyUser getInfo(Integer userId) {
        List<ShopVerifyUser> list = lambdaQuery().eq(ShopVerifyUser::getUserId, userId).list();
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Boolean updateStatus(Integer id) {
        ShopVerifyUser verifyUser = baseMapper.selectById(id);
        if(verifyUser != null){
            Integer status = verifyUser.getStatus();
            if(status == 0){
                verifyUser.setStatus(1);
            }else {
                verifyUser.setStatus(0);
            }
            verifyUser.setUpdateTime(LocalDateTime.now());
            User loginUser = LoginUserUtil.getLoginUser();
            verifyUser.setUpdater(loginUser != null ? loginUser.getUserId() : null);

            return baseMapper.updateById(verifyUser) > 0;
        }else {
            throw new BusinessException(GlobalErrorCodeConstants.NOT_FOUND.getMsg());
        }
    }

    @Override
    public Boolean updateVerifyFlag(Integer id) {
        ShopVerifyUser verifyUser = baseMapper.selectById(id);
        if(verifyUser != null){
            Integer verifyFlag = verifyUser.getVerifyFlag();
            if(verifyFlag == 0){
                verifyUser.setVerifyFlag(1);
            }else {
                verifyUser.setVerifyFlag(0);
            }
            verifyUser.setUpdateTime(LocalDateTime.now());
            User loginUser = LoginUserUtil.getLoginUser();
            verifyUser.setUpdater(loginUser != null ? loginUser.getUserId() : null);

            return baseMapper.updateById(verifyUser) > 0;
        }else {
            throw new BusinessException(GlobalErrorCodeConstants.NOT_FOUND.getMsg());
        }
    }

    @Override
    public Boolean saveInfo(ShopVerifyUser shopVerifyUser) {
        Integer userId = shopVerifyUser.getUserId();
        if(userId == null){
            throw new BusinessException("用户ID必传！");
        }

        List<ShopVerifyUser> list = lambdaQuery().eq(ShopVerifyUser::getUserId, userId).list();
        if(CollectionUtils.isNotEmpty(list)){
            throw new BusinessException("该用户已存在核销人关系!");
        }
        User loginUser = LoginUserUtil.getLoginUser();
        shopVerifyUser.setCreator(loginUser != null ? loginUser.getUserId() : null);
        shopVerifyUser.setCreateTime(LocalDateTime.now());

        return baseMapper.insert(shopVerifyUser) > 0;
    }

}
