package com.ruixuan.jinniu.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.domain.entity.SysRole;
import com.ruixuan.common.core.domain.entity.SysUser;
import com.ruixuan.common.utils.Check;
import com.ruixuan.common.utils.Result;
import com.ruixuan.jinniu.entity.KuaishouProject;
import com.ruixuan.jinniu.entity.KuaishouProjectMember;
import com.ruixuan.jinniu.mapper.KuaishouProjectMemberMapper;
import com.ruixuan.jinniu.service.IKuaishouProjectMemberService;
import com.ruixuan.jinniu.service.IKuaishouProjectService;
import com.ruixuan.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 项目人员信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-16
 */
@Service
public class KuaishouProjectMemberServiceImpl implements IKuaishouProjectMemberService {
    @Autowired
    private KuaishouProjectMemberMapper kuaishouProjectMemberMapper;
    @Autowired
    private IKuaishouProjectService projectMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询项目人员信息
     *
     * @param id 项目人员信息主键
     * @return 项目人员信息
     */
    @Override
    public KuaishouProjectMember selectKuaishouProjectMemberById(Long id) {
        return kuaishouProjectMemberMapper.selectKuaishouProjectMemberById(id);
    }

    /**
     * 查询项目人员信息列表
     *
     * @param kuaishouProjectMember 项目人员信息
     * @return 项目人员信息
     */
    @Override
    public List<KuaishouProjectMember> selectKuaishouProjectMemberList(KuaishouProjectMember kuaishouProjectMember) {
        return kuaishouProjectMemberMapper.selectKuaishouProjectMemberList(kuaishouProjectMember);
    }

    /**
     * 新增项目人员信息
     *
     * @param kuaishouProjectMember 项目人员信息
     * @return 结果
     */
    @Override
    public int insertKuaishouProjectMember(KuaishouProjectMember kuaishouProjectMember) {
        return kuaishouProjectMemberMapper.insertKuaishouProjectMember(kuaishouProjectMember);
    }

    /**
     * 修改项目人员信息
     *
     * @param kuaishouProjectMember 项目人员信息
     * @return 结果
     */
    @Override
    public int updateKuaishouProjectMember(KuaishouProjectMember kuaishouProjectMember) {
        return kuaishouProjectMemberMapper.updateKuaishouProjectMember(kuaishouProjectMember);
    }

    /**
     * 删除项目人员信息信息
     *
     * @param id 项目人员信息主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouProjectMemberById(Long id) {
        return kuaishouProjectMemberMapper.deleteKuaishouProjectMemberById(id);
    }

    @Override
    public Result<Object> queryProjectUsers(Long projectId) {
        List<KuaishouProjectMember> list = kuaishouProjectMemberMapper.queryProjectUsers(projectId);
        return Result.success(list);
    }

    @Override
    public Result<Object> addProjectUsers(JSONObject o) {
        Long projectId = o.getLong("projectId");
        JSONArray userIds = o.getJSONArray("userIds");
        if (Check.isNull(projectId) || Check.isNull(userIds)) {
            return Result.error("缺少参数!");
        }
        String projectName = o.getString("projectName");
        if (Check.isNull(projectName)) {
            KuaishouProject project = projectMapper.selectKuaishouProjectById(projectId);
            projectName = project.getProjectName();
        }
        List<KuaishouProjectMember> list = new ArrayList<>();
        for (Object userId : userIds) {
            KuaishouProjectMember projectMember = new KuaishouProjectMember();
            projectMember.setProjectId(projectId);
            projectMember.setProjectName(projectName);
            SysUser sysUser = sysUserService.selectUserById(Long.valueOf(userId.toString()));
            projectMember.setUserId(sysUser.getUserId());
            projectMember.setUserName(sysUser.getNickName());
            List<SysRole> roles = sysUser.getRoles();
            if (Check.isNotNull(roles)) {
                projectMember.setRoleKey(roles.get(0).getRoleKey());
                projectMember.setRoleName(roles.get(0).getRoleName());
            }
            list.add(projectMember);
        }
        if (Check.isNotNull(list)) {
            kuaishouProjectMemberMapper.replaceBatch(list);
        }
        return Result.success();
    }

    @Override
    public Result<Object> deleteProjectUsers(Long projectId, Long userId) {
        kuaishouProjectMemberMapper.deleteProjectUsers(projectId, userId);
        return Result.success();
    }
}
