package com.xjrsoft.module.concat.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
import com.xjrsoft.module.concat.dto.ConcatTreeDto;
import com.xjrsoft.module.concat.vo.ConcatTreeVo;
import com.xjrsoft.module.organization.entity.Department;
import com.xjrsoft.module.organization.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @title: 通讯录
* @Author dzx
* @Date: 2024年3月25日
* @Version 1.0
*/
@RestController
@RequestMapping("/concat" + "/concat")
@Api(value = "/concat"  + "/concat",tags = "通讯录")
@AllArgsConstructor
public class ConcatController {
    private final IDepartmentService departmentService;


    @GetMapping(value = "/list")
    @ApiOperation(value="教师通讯录")
    @SaCheckPermission("room:detail")
    public RT<List<ConcatTreeVo>> list(ConcatTreeDto dto) throws Exception {
        Long parentId = 1684107782861680000L;
        if(dto.getDeptId() != null){
            parentId = dto.getDeptId();
        }
        List<Department> list = departmentService.list(
            new QueryWrapper<Department>().lambda()
            .eq(Department::getParentId, parentId)
        );
        List<ConcatTreeVo> concatList = new ArrayList<>();
        for (Department department : list) {
            concatList.add(
                new ConcatTreeVo(){{
                    setName(department.getName());
                    setId(department.getId());
                    setType(1);
                }}
            );
        }
        String sql = "SELECT t1.name, t1.mobile,t2.dept_id,t6.name as gender_cn FROM xjr_user t1" +
                " LEFT JOIN xjr_user_dept_relation t2 ON t1.id = t2.user_id" +
                " LEFT JOIN xjr_user_role_relation t3 ON t1.id = t3.user_id" +
                " LEFT JOIN xjr_department t5 ON t2.dept_id = t5.id" +
                " left join xjr_dictionary_detail t6 on t1.gender = t6.code" +
                " WHERE t1.delete_mark = 0" +
                " AND t3.role_id = 2";
//        if(dto.getDeptId() != null){
//            sql += " AND t5.hierarchy LIKE '%" + dto.getDeptId() + "%'";
//        }else if(!StrUtil.isNotEmpty(dto.getName())){
//            sql += " AND t5.id = " + parentId;
//        }

        if(StrUtil.isNotEmpty(dto.getName())&&dto.getDeptId() != null) {
            sql += " AND t5.hierarchy LIKE '%" + dto.getDeptId() + "%'";
        }

        if(!StrUtil.isNotEmpty(dto.getName())) {
            if(dto.getDeptId() != null){
                sql += " AND t5.id = " + dto.getDeptId();
            }else {
                sql += " AND t5.id = " + parentId;
            }
        }

        if(StrUtil.isNotEmpty(dto.getName())){
            concatList = new ArrayList<>();
            sql += "AND (t1.mobile = '%" + dto.getName() + "%' OR t1.name LIKE '%" + dto.getName() + "%')";
        }
        List<Map<String, Object>> userList = SqlRunnerAdapter.db().selectList(sql);
        if(StrUtil.isNotEmpty(dto.getName())){
            concatList = new ArrayList<>();
        }
        for (Map<String, Object> user : userList) {
            concatList.add(
                    new ConcatTreeVo(){{
                        setName(user.get("name").toString());
                        setMobile(user.get("mobile").toString());
                        setType(2);
                        setGender(user.get("gender_cn")==null?"":user.get("gender_cn").toString());
                    }}
            );
        }

        return RT.ok(concatList);
    }
}