package com.ruansi.wflow.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import com.ruansi.system.api.domain.OrgUser;
import com.ruansi.system.api.domain.SysUser;
import com.ruansi.system.api.domain.UserAgentVo;
import com.ruansi.wflow.bean.do_.DeptDo;
import com.ruansi.wflow.bean.do_.UserDo;
import com.ruansi.wflow.bean.entity.WflowUserAgents;
import com.ruansi.wflow.bean.vo.DeptVo;
import com.ruansi.wflow.bean.vo.OrgTreeVo;
import com.ruansi.wflow.bean.vo.UserVo;
import com.ruansi.wflow.mapper.WflowUserAgentsMapper;
import com.ruansi.wflow.service.OrgRepositoryService;
import com.ruansi.wflow.service.OrgUserAndDeptService;
import com.ruansi.wflow.utils.FR;
import com.ruansi.wflow.utils.UserUtil;
import liquibase.pro.packaged.S;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : willian fu
 * @version : 1.0
 */
@Service
public class OrgUserAndDeptServiceImpl implements OrgUserAndDeptService {

    @Autowired
    private OrgRepositoryService orgRepositoryService;

    @Autowired
    private WflowUserAgentsMapper userAgentsMapper;

    private static final String TIME_FORMAT = "yyyy-MM-dd HH:mm";

    /**
     * 查询组织架构树
     *
     * @param deptId 部门id
     * @param type   类型
     * @return 组织架构树数据
     */
    @Override
    public Object getOrgTreeData(String deptId, String type) {
        if ("role".equals(type)) {
            return FR.ok(orgRepositoryService.getSysAllRoles().stream()
                    .map(r -> OrgTreeVo.builder().id(r.getRoleId()).type("role")
                            .name(r.getRoleName()).build())) ;
        } else {
            List<OrgTreeVo> orgs = new LinkedList<>(orgRepositoryService.getSubDeptById(deptId));
            DeptDo department = orgRepositoryService.getDeptById(deptId);
            if ("user".equals(type) || "org".equals(type)) {
                orgs.addAll(orgRepositoryService.selectUsersByDept(deptId).stream().peek(u -> {
                    u.setIsLeader(null != department && StrUtil.isNotBlank(department.getLeader()) && department.getLeader().equals(u.getId()));
                }).sorted(Comparator.comparing(OrgTreeVo::getIsLeader).reversed()).collect(Collectors.toList()));
            }
            return FR.ok(orgs);
        }
    }

    /**
     * 模糊搜索用户
     *
     * @param userName 用户名/拼音/首字母
     * @return 匹配到的用户
     */
    @Override
    public Object getOrgTreeUser(String userName) {
        return FR.ok(orgRepositoryService.selectUsersByPy(userName));
    }

    @Override
    public List<DeptVo> getOrgUserDept(String userId) {
        return orgRepositoryService.getDeptsByUser(userId)
                .stream().map(d -> new DeptVo(d.getDeptId(), d.getDeptName()))
                .collect(Collectors.toList());
    }

    @Override
    public UserAgentVo getUserAgent(String userId) {
        WflowUserAgents userAgent = userAgentsMapper.selectById(userId);
        if (Objects.nonNull(userAgent)) {
            Date time = GregorianCalendar.getInstance().getTime();
            if (userAgent.getEndTime().after(time)) {
                //在代理期限内或者
                UserDo user = orgRepositoryService.getUserById(userAgent.getAgentUserId());
                return UserAgentVo.builder()
                        .effective(time.after(userAgent.getStartTime()) && time.before(userAgent.getEndTime()))
                        .user(OrgUser.builder().id(user.getUserId())
                                .name(user.getUserName()).type("user")
                                .avatar(user.getAvatar()).build())
                        .timeRange(CollectionUtil.newArrayList(
                                DateUtil.format(userAgent.getStartTime(), TIME_FORMAT),
                                DateUtil.format(userAgent.getEndTime(), TIME_FORMAT)
                        )).build();
            }
        }
        return null;
    }

    @Override
    public void setUserAgent(UserAgentVo agent) {
        String userId = UserUtil.getLoginUserId();
        WflowUserAgents userAgent = userAgentsMapper.selectById(userId);
        WflowUserAgents userAgents = WflowUserAgents.builder().userId(userId)
                .agentUserId(agent.getUser().getId())
                .startTime(DateUtil.parse(agent.getTimeRange().get(0), TIME_FORMAT))
                .endTime(DateUtil.parse(agent.getTimeRange().get(1), TIME_FORMAT))
                .createTime(GregorianCalendar.getInstance().getTime())
                .build();
        if (Objects.nonNull(userAgent)) {
            userAgentsMapper.updateById(userAgents);
        }else {
            userAgentsMapper.insert(userAgents);
        }
    }

    @Override
    public void cleanUserAgent() {
        userAgentsMapper.deleteById(UserUtil.getLoginUserId());
    }

    @Override
    public String getUserSign() {
        return orgRepositoryService.getUserSign(UserUtil.getLoginUserId());
    }

    @Override
    public SysUser getUserDetail(String userId) {
        SysUser userVo = orgRepositoryService.getUserDetail(userId);
        userVo.setUserAgent(getUserAgent(userId));
        return userVo;
    }
}
