package com.xdap.self_development.service.impl;


import com.definesys.mpaas.common.exception.MpaasRuntimeException;
import com.xdap.api.moudle.department.pojo.entity.XdapDepartments;
import com.xdap.api.moudle.user.pojo.XdapUsers;
import com.xdap.self_development.common.BusinessDatabase;
import com.xdap.self_development.domain.pojo.ResponsiblePersonPojo;
import com.xdap.self_development.service.ResponsiblePerService;
import com.xdap.self_development.service.XdapDeptUsersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 责任人服务实现类
 *
 * @version 1.0
 * @since 2025
 */
@Slf4j
@Service
public class ResponsiblePerServiceImpl implements ResponsiblePerService {

    @Resource
    private BusinessDatabase bd;
    @Resource
    private XdapDeptUsersService  xdapDeptUsersService;
    /**
     * 根据部门和人员查询责任人
     * 根据部门名称和人员名称查询责任人信息
     *
     * @param department 部门名称
     * @param resPerson  人员名称
     * @return 责任人POJO
     */
    @Override
    public ResponsiblePersonPojo selectByDepAndPer(String department, String resPerson) {
        if (ObjectUtils.isEmpty(department) || ObjectUtils.isEmpty(resPerson)) {
            return null;
        }
        XdapDepartments deptByName = xdapDeptUsersService.selectDeptByName(department);
        List<XdapUsers> userByName = xdapDeptUsersService.selectUserByPersonName(resPerson);
        XdapUsers xdapUsers = xdapDeptUsersService.selectUserByUserListAndDeptName(userByName, deptByName);
        ResponsiblePersonPojo responsiblePerson = null;
        List<ResponsiblePersonPojo> personPojos = bd.getBusinessDatabase()
                .eq("department_id", deptByName.getId())
                .eq("user_id", xdapUsers.getId())
                .doQuery(ResponsiblePersonPojo.class);

        if (personPojos != null && !personPojos.isEmpty()) {
            responsiblePerson = personPojos.get(0);
        }
        return responsiblePerson;
    }


    /**
     * 根据用户ID查询责任人
     * 根据用户列表查询相关责任人信息
     *
     * @param userIds 用户列表
     * @return 责任人POJO列表
     */
    @Override
    public List<ResponsiblePersonPojo> selectByUserId(List<XdapUsers> userIds) {
        if (ObjectUtils.isEmpty(userIds)) {
            return Collections.emptyList();
        }
        List<String> idlist = new ArrayList<>();
        for (XdapUsers userId : userIds) {
            idlist.add(userId.getId());
        }
        return bd.getBusinessDatabase()
                .in("user_id", idlist)
                .doQuery(ResponsiblePersonPojo.class);
    }

    /**
     * 根据部门名称查询责任人
     * 根据部门名称查询相关责任人信息
     *
     * @param departmentName 部门名称
     * @return 责任人POJO列表
     */
    @Override
    public List<ResponsiblePersonPojo> selectByDepName(String departmentName) {
        XdapDepartments xdapDepartments = xdapDeptUsersService.selectDeptByName(departmentName);
        if (ObjectUtils.isEmpty(xdapDepartments)) {
            return Collections.emptyList();
        }
        return bd.getBusinessDatabase()
                .eq("department_id", xdapDepartments.getId())
                .doQuery(ResponsiblePersonPojo.class);
    }

    /**
     * 插入责任人
     * 插入责任人信息到数据库
     *
     * @param responsiblePerson 责任人POJO
     * @return 插入结果
     */
    @Override
    public Object insert(ResponsiblePersonPojo responsiblePerson) {
        Object testDemo = null;
        try {
            testDemo = bd.getBusinessDatabase()
                    .doInsert(responsiblePerson);
            log.info("责任人数据写入成功={}", testDemo);
        } catch (MpaasRuntimeException e) {
            log.warn("唯一索引冲突，跳过插入");
        }
        return testDemo;
    }

    /**
     * 根据子系统和部门查询责任人
     * 根据子系统名称和部门名称查询责任人信息
     *
     * @param subSystem 子系统名称
     * @param dept      部门名称
     * @return 责任人POJO列表
     */
    @Override
    public List<ResponsiblePersonPojo> selectBysubSystemDept(String subSystem, String dept) {
        return bd.getBusinessDatabase()
                .like("subsystem", subSystem)
                .eq("department", dept)
                .doQuery(ResponsiblePersonPojo.class);
    }
}
