package org.xyzh.system.service.impl;

import java.util.List;

import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.xyzh.api.system.service.GuestService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageDomain;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.sys.TbGuestDTO;
import org.xyzh.common.dto.sys.TbSysUserRoleDTO;
import org.xyzh.common.utils.id.IdUtil;
import org.xyzh.system.mapper.user.TbGuestMapper;
import org.xyzh.system.mapper.user.TbSysUserRoleMapper;

import java.util.Date;


/**
 * @description    来客服务接口实现
 * @filename    GuestServiceImpl.java
 * @author    yslg
 * @copyright xyzh
 * @since 2025-12-18
 */
@DubboService(
    version = "1.0.0",
    group = "system",
    timeout = 3000,
    retries = 0
)
public class GuestServiceImpl implements GuestService{
    private static final Logger logger = LoggerFactory.getLogger(GuestServiceImpl.class);

    @Autowired
    private TbGuestMapper guestMapper;

    @Autowired
    private TbSysUserRoleMapper userRoleMapper;

    @Override
    @Transactional
    public ResultDomain<TbGuestDTO> createGuest(TbGuestDTO guest) {
        guestMapper.insertGuest(guest);
        
        // 绑定访客角色(role_guest)
        TbSysUserRoleDTO userRole = new TbSysUserRoleDTO();
        userRole.setOptsn(IdUtil.getOptsn());
        userRole.setUserId(guest.getUserId());
        userRole.setRoleId("role_guest");
        userRole.setDeptId("dept_root");
        userRole.setCreateTime(new Date());
        userRoleMapper.insertUserRole(userRole);
        
        return ResultDomain.success("创建成功",guest);
    }

    @Override
    @Transactional
    public ResultDomain<TbGuestDTO> updateGuest(TbGuestDTO guest) {
        guestMapper.updateGuest(guest);
        return ResultDomain.success("更新成功",guest);
    }
    
    @Override
    @Transactional
    public ResultDomain<TbGuestDTO> deleteGuest(String userId) {
        int rows = guestMapper.deleteGuest(userId);
        if (rows > 0) {
            return ResultDomain.success("删除成功");
        }
        return ResultDomain.failure("删除失败，来客不存在");
    }

    @Override
    public ResultDomain<TbGuestDTO> selectGuestList(TbGuestDTO guest) {
        List<TbGuestDTO> list = guestMapper.selectGuestList(guest);
        return ResultDomain.success("查询成功", list);
    }

    @Override
    public ResultDomain<TbGuestDTO> selectGuestOne(TbGuestDTO guest) {
        TbGuestDTO selected = guestMapper.selectGuestOne(guest);
        return ResultDomain.success("查询成功",selected);
    }

    @Override
    public ResultDomain<TbGuestDTO> selectGuestByWechatId(String wechatId){
        TbGuestDTO selected = guestMapper.selectGuestByWechatId(wechatId);
        return ResultDomain.success("查询成功", selected);
    }

    @Override
    public ResultDomain<TbGuestDTO> selectGuestPage(PageRequest<TbGuestDTO> pageRequest) {
        List<TbGuestDTO> guestList = guestMapper.selectGuestPage(pageRequest.getFilter(),pageRequest.getPageParam());
        int total = guestMapper.countGuest(pageRequest.getFilter());
        PageDomain<TbGuestDTO> pageDomain = new PageDomain<>();
        pageDomain.setPageParam(pageRequest.getPageParam());
        pageDomain.getPageParam().setTotal(total);
        
        pageDomain.setDataList(guestList);
        return ResultDomain.success("查询成功",pageDomain);
    }    
    
}
