package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SocketContactVo;
import com.ruoyi.system.domain.bo.SocketContactBo;
import com.ruoyi.system.service.ISocketContactService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 通信目录
 *
 * @author hdka
 * @date 2024-10-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/contact")
public class SocketContactController extends BaseController {

    private final ISocketContactService iSocketContactService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询通信目录列表
     */
    @GetMapping("/list")
    public TableDataInfo<SocketContactVo> list(SocketContactBo bo, PageQuery pageQuery) {
        return iSocketContactService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出通信目录列表
     */
    @Log(title = "通信目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SocketContactBo bo, HttpServletResponse response) {
        List<SocketContactVo> list = iSocketContactService.queryList(bo);
        ExcelUtil.exportExcel(list, "通信目录", SocketContactVo.class, response);
    }

    /**
     * 获取通信目录详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<SocketContactVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iSocketContactService.queryById(id));
    }


    /**
     * 新增通信目录
     */
    @Log(title = "通信目录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SocketContactBo bo) {
        return toAjax(iSocketContactService.insertByBo(bo));
    }

    /**
     * 修改通信目录
     */
    @Log(title = "通信目录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SocketContactBo bo) {
        return toAjax(iSocketContactService.updateByBo(bo));
    }

    /**
     * 删除通信目录
     *
     * @param ids 主键串
     */
    @Log(title = "通信目录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iSocketContactService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @GetMapping("/script")
    public R<Void> script() {
        List<SysUser> selectUserList = userService.selectUserList(new SysUser());
        for (int i = 0; i < selectUserList.size(); i++) {
            SysUser sysUser = selectUserList.get(i);
            Long userId = sysUser.getUserId();
            for (int i1 = i+1; i1 < selectUserList.size(); i1++) {
                SocketContactBo socketContactBo = new SocketContactBo();
                socketContactBo.setUserId(userId);
                socketContactBo.setRoomId(0L);
                socketContactBo.setContactUserId(selectUserList.get(i1).getUserId());
                iSocketContactService.insertByBo(socketContactBo);
            }

        }
        return R.ok();
    }
}
