package com.rwk.web.controller.ability;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.rwk.common.core.domain.Ztree;
import com.rwk.common.core.domain.entity.SysDept;
import com.rwk.common.core.domain.entity.SysUser;
import com.rwk.common.utils.ShiroUtils;
import com.rwk.system.domain.RwkAddressGroup;
import com.rwk.system.domain.RwkBookGroup;
import com.rwk.system.service.IRwkAddressGroupService;
import com.rwk.system.service.IRwkBookGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.rwk.common.annotation.Log;
import com.rwk.common.enums.BusinessType;
import com.rwk.system.domain.RwkAddressBook;
import com.rwk.system.service.IRwkAddressBookService;
import com.rwk.common.core.controller.BaseController;
import com.rwk.common.core.domain.AjaxResult;
import com.rwk.common.utils.poi.ExcelUtil;
import com.rwk.common.core.page.TableDataInfo;

/**
 * 通讯录主Controller
 * 
 * @author ruoyi
 * @date 2024-12-11
 */
@Controller
@RequestMapping("/system/addressbook")
public class RwkAddressBookController extends BaseController
{
    private String prefix = "system/addressbook";

    @Autowired
    private IRwkAddressBookService rwkAddressBookService;

    @Autowired
    private IRwkAddressGroupService rwkAddressGroupService;

    @Autowired
    private IRwkBookGroupService rwkBookGroupService;



    @RequiresPermissions("system:addressbook:view")
    @GetMapping()
    public String book(ModelMap map)
    {
        List<RwkAddressGroup> resultGroup = new LinkedList<>();
        SysUser sysUser = ShiroUtils.getSysUser();
        Long deptId = sysUser.getDept().getDeptId();
        // 所有联系人
        List<RwkAddressGroup> rwkAddressGroups1 = rwkAddressGroupService.selectRwkAddressGroupListAllByDept(deptId);
        // 未分组联系人(停用)
//        List<RwkAddressGroup> rwkAddressGroups2 = rwkAddressGroupService.selectRwkAddressGroupListLackByDept(deptId);
//        resultGroup.addAll(rwkAddressGroups2);
        // 分组联系人
        List<RwkAddressGroup> rwkAddressGroups3 = rwkAddressGroupService.selectRwkAddressGroupList(sysUser.getLoginName());
        resultGroup.addAll(rwkAddressGroups3);
        map.put("rwkAddressGroups", resultGroup);
        map.put("allddressGroups", rwkAddressGroups1);
        return prefix + "/addressbook";
    }

    /**
     * 查询通讯录主列表
     */
    @RequiresPermissions("system:book:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RwkAddressBook rwkAddressBook)
    {

        List<RwkAddressBook> list = rwkAddressBookService.selectRwkAddressBookList(rwkAddressBook);
        return getDataTable(list);
    }

    /**
     * 导出通讯录主列表
     */
    @RequiresPermissions("system:book:export")
    @Log(title = "通讯录主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RwkAddressBook rwkAddressBook)
    {
        List<RwkAddressBook> list = rwkAddressBookService.selectRwkAddressBookList(rwkAddressBook);
        ExcelUtil<RwkAddressBook> util = new ExcelUtil<RwkAddressBook>(RwkAddressBook.class);
        return util.exportExcel(list, "通讯录主数据");
    }

//    /**
//     * 新增通讯录人员
//     */
//    @GetMapping("/add")
//    public String add()
//    {
//        return prefix + "/add";
//    }

    /**
     * 新增通讯录分组
     */
    @GetMapping("/addgroup")
    public String addgroup()
    {
        return prefix + "/addgroup";
    }


    /**
     * 新增保存通讯录分组
     */
    @PostMapping("/addgroup/add")
    @ResponseBody
    public AjaxResult addGroupSave(RwkAddressGroup rwkAddressGroup)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        rwkAddressGroup.setGroupByUser(sysUser.getLoginName());
        return toAjax(rwkAddressGroupService.insertRwkAddressGroup(rwkAddressGroup));
    }

    /**
     * 新增通讯录人员
     */
    @GetMapping("/addbook")
    public String addbook(ModelMap map)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        List<RwkAddressGroup> rwkAddressGroups = rwkAddressGroupService.selectRwkAddressGroupList(sysUser.getLoginName());
        map.put("rwkAddressGroups", rwkAddressGroups);
        return prefix + "/addbook";
    }

    /**
     * 新增保存通讯录
     */
    @RequiresPermissions("system:book:add")
    @Log(title = "通讯录主", businessType = BusinessType.INSERT)
    @PostMapping("/address/add")
    @ResponseBody
    public AjaxResult addSave(RwkAddressBook rwkAddressBook)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        SysDept dept = sysUser.getDept();
        rwkAddressBook.setDeptId(dept.getDeptId());
        return toAjax(rwkAddressBookService.insertRwkAddressBook(rwkAddressBook));
    }

    /**
     * 通讯录复制关系
     */
    @PostMapping("/address/copy")
    @ResponseBody
    public AjaxResult copySave(String addressIds, String groupIds)
    {
        String[] addressIdArray = addressIds.split(",");
        String[] groupIdArray = groupIds.split(",");
        List<RwkBookGroup> list=new ArrayList<>();
        for (int i = 0; i < addressIdArray.length; i++){
            for (int j = 0; j < groupIdArray.length; j++){
                RwkBookGroup rbg= new RwkBookGroup();
                rbg.setBookId(Long.valueOf(addressIdArray[i]));
                rbg.setGroupId(Long.valueOf(groupIdArray[j]));
                list.add(rbg);
            }
        }
        return toAjax(rwkBookGroupService.insertRwkBookGroupList(list));
    }

    /**
     * 通讯录移动关系
     */
    @PostMapping("/address/move")
    @ResponseBody
    public AjaxResult moveSave(String addressIds, String byGroupId,String groupIds)
    {
        String[] addressIdArray = addressIds.split(",");
        String[] groupIdArray = groupIds.split(",");
        List<RwkBookGroup> list=new ArrayList<>();
        for (int i = 0; i < addressIdArray.length; i++){
            for (int j = 0; j < groupIdArray.length; j++){
                RwkBookGroup rbg= new RwkBookGroup();
                rbg.setBookId(Long.valueOf(addressIdArray[i]));
                rbg.setGroupId(Long.valueOf(groupIdArray[j]));
                list.add(rbg);
            }
        }
        return toAjax(rwkBookGroupService.insertRwkBookGroupList(list));
    }

    /**
     * 修改通讯录
     */
    @RequiresPermissions("system:book:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RwkAddressBook rwkAddressBook = rwkAddressBookService.selectRwkAddressBookById(id);
        mmap.put("rwkAddressBook", rwkAddressBook);
        return prefix + "/editbook";
    }

    /**
     * 个人通讯录分组树
     * @param
     * @param mmap
     * @return
     */
    @GetMapping("/selectGroupsTree/{addressIds}")
    public String selectDeptTree(ModelMap mmap,@PathVariable("addressIds") String addressIds)
    {

        SysUser sysUser = ShiroUtils.getSysUser();
        mmap.put("groups", rwkAddressGroupService.selectRwkAddressGroupList(sysUser.getLoginName()));
        mmap.put("addressIds", addressIds);
        return prefix + "/groupTree";
    }

    /**
     * 通讯录关系移动
     * @param
     * @param mmap
     * @return
     */
    @GetMapping("/selectGroupsTreeMove/{str}")
    public String selectGroupsTreeMove(ModelMap mmap,@PathVariable("str") String str)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        String[] split = str.split("gId=");
        String ids=split[0];
        String byGroupId=split[1];
        mmap.put("groups", rwkAddressGroupService.selectRwkAddressGroupList(sysUser.getLoginName()));
        mmap.put("addressIds", ids);
        mmap.put("byGroupId", byGroupId);
        return prefix + "/groupTree";
    }


    /**
     * 修改保存通讯录
     */
    @PostMapping("/address/edit")
    @ResponseBody
    public AjaxResult editSave(RwkAddressBook rwkAddressBook)
    {
        return toAjax(rwkAddressBookService.updateRwkAddressBook(rwkAddressBook));
    }

    /**
     * 删除通讯录
     */
    @PostMapping( "/address/delete")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(rwkAddressBookService.deleteRwkAddressBookByIds(ids));
    }

    /**
     * 删除通讯录
     */
    @PostMapping( "/addgroup/delete")
    @ResponseBody
    public AjaxResult removeGroup(String ids)
    {
        return toAjax(rwkBookGroupService.deleteRwkBookGroupByBookIds(ids));
    }

    /**
     * 删除通讯录分组（包含其成员关系）
     */
    @PostMapping( "/addgroup/delete/all")
    @ResponseBody
    public AjaxResult removeGroupAll(Long groupid )
    {
        // 删除组下关系
        rwkBookGroupService.deleteRwkBookGroupByGroupId(groupid);
        return toAjax(rwkAddressGroupService.deleteRwkAddressGroupById(groupid));
    }
}
