package cn.iocoder.yudao.module.system.service.worklog;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.api.config.ConfigApi;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptRespVO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceNextOrUpVO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstancePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceRespVO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceSaveReqVO;
import cn.iocoder.yudao.module.system.convert.worklog.LogInstanceConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.dataobject.worklog.*;
import cn.iocoder.yudao.module.system.dal.mysql.worklog.LogInstanceMapper;
import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import cn.iocoder.yudao.module.system.service.worklog.dto.LogReadUserRespDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.ap.internal.util.Strings;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 日志实例的拓展 Service 实现类
 *
 * @author 符溶馨
 */
@Service
@Validated
@Slf4j
public class LogInstanceServiceImpl implements LogInstanceService {

    @Resource
    private LogInstanceMapper logInstanceMapper;

    @Resource
    private DeptService deptService;

    @Resource
    private LogUseService logUseService;

    @Resource
    private LogFormService logFormService;

    @Resource
    private LogRuleService logRuleService;

    @Resource
    private AdminUserService adminUserService;

    @Resource
    private LogReadService logReadService;

    @Resource
    @Lazy
    private LogStatisticsService logStatisticsService;

    @Resource
    private ConfigApi configApi;


    @Override
    public Long createLogInstance(LogInstanceSaveReqVO createReqVO) {

        //校验当前日志类型的日志 是否已提交过
        validateLogInstanceByType(createReqVO);

        LogInstanceDO logInstance = BeanUtils.toBean(createReqVO, LogInstanceDO.class);
        //获取当前登录用户id
        Long userId = getLoginUserId();
        //获取日志类型
        LogFormDO logFormDo = logFormService.getForm(createReqVO.getFormId());

        Long count = logUseService.getCountByUser(logFormDo.getId(), userId);
        if (count == 0L) {
            throw exception(LOG_FORM_NOT_USE);
        }

        //获取部门信息
        AdminUserDO adminUserDO = adminUserService.getUser(userId);
        //获取规则信息
        LogRuleDO logRuleDO = logRuleService.getLogRule(logFormDo.getId());

        //设置发起人用户编号
        logInstance.setStartUserId(userId);
        //设置发起人部门编号
        logInstance.setDeptId(adminUserDO.getDeptId());
        //设置日志类型
        logInstance.setType(logRuleDO.getType());
        //设置日志名称
        logInstance.setName(adminUserDO.getNickname() + createReqVO.getTime() + "的" + logFormDo.getName());

        // 插入提交状态
        Date thisDate = new Date();
        if (logRuleDO.getType() == 1) {
            Date endTime = DateUtils.buildTimeYYYYMMddHHmm(createReqVO.getTime(), logRuleDO.getEndTime());
            logInstance.setStatus(thisDate.getTime() < endTime.getTime() ? 1 : 2);
        } else {
            Date beginTime = DateUtil.beginOfWeek(thisDate);
            Date endTime = DateUtils.buildWeekHHmmTime(logRuleDO.getEndTime());
            logInstance.setStatus(DateUtil.isIn(thisDate, beginTime, endTime) ? 1 : 2);
        }
        // 插入
        logInstanceMapper.insert(logInstance);

        logStatisticsService.saveOrUpdate(new LogStatisticsDO()
                .setFormId(logFormDo.getId())
                .setRuleId(logRuleDO.getId())
                .setUserId(userId)
                .setDeptId(adminUserDO.getDeptId())
                .setLogInstanceExtId(logInstance.getId())
                .setType(logRuleDO.getType())
                .setTime(createReqVO.getTime())
                .setStatus(logInstance.getStatus())
                .setCommitTimeRange(logStatisticsService.getCommitTimeRange(logRuleDO))
        );
        //创建日志时，查询可以查看发起人日志的用户组  用线程控制
        new Thread(() -> {

            try {
//                List<Long> roleIds = Arrays.asList(1L,101L);
//                String roleId = configApi.getConfigKey("system_work_log_role_id").getCheckedData();
//
//                if (Strings.isNotEmpty(roleId)) {
//                    roleIds = Arrays.stream(roleId.split(",")).map(Long::valueOf).collect(Collectors.toList());
//                }
                List<LogReadUserRespDTO> respDTOS = logInstanceMapper.selectRaedUser(userId, adminUserDO.getDeptId());

                //特殊情况， 日志发起人为研发部时 手动添加查看者
                if (adminUserDO.getDeptId() == 128L && adminUserDO.getId() != 126L) {

                    LogReadUserRespDTO dto = new LogReadUserRespDTO();
                    dto.setUserId(126L);
                    dto.setDeptId(128L);
                    respDTOS.add(dto);
                }

                logReadService.createLogRule(respDTOS, logInstance.getId(), logInstance.getStartUserId());
            } catch (Exception e) {
                log.error("获取日志查看用户失败", e);
            }

        }).start();

        // 返回
        return logInstance.getId();
    }

    /**
     * 校验当前日志类型的日志 是否已提交过
     */
    private void validateLogInstanceByType(LogInstanceSaveReqVO createReqVO) {

        List<String> dateList = new ArrayList<>();
        if (createReqVO.getType() == 1) {

            dateList.add(createReqVO.getTime());
        } else {

            String[] times = createReqVO.getTime().split(",");
            dateList = DateUtils.betweenDayStrList(times[0], times[1]);

            createReqVO.setTime(times[1]);
        }

        Long count = logInstanceMapper.getLogByFormId(createReqVO.getFormId(), getLoginUserId(), dateList);
        if (count > 0L) {

            throw exception(ErrorCodeConstants.LOG_TYPE_EXISTS);
        }
    }

    @Override
    public void updateLogInstance(LogInstanceSaveReqVO updateReqVO) {
        // 校验存在
        validateLogInstanceExists(updateReqVO.getId());

        // 校验日志是否已被查看
        if (logReadService.isReadByLogId(updateReqVO.getId())) {
            throw exception(LOG_HAS_READ);
        }

        // 校验要修改的日志是否是当天发送的
        LogInstanceDO logInstanceDO = logInstanceMapper.selectById(updateReqVO.getId());
        if (!logInstanceDO.getCreateTime().toLocalDate().equals(LocalDate.now())) {

            throw exception(LOG_NOT_DATE);
        }

//        LogRuleDO logRuleDO = logRuleService.getLogRuleByFormId(updateReqVO.getFormId());
//        String endTime = logRuleDO.getEndTime();
//        if (logRuleDO.getType() != 1) {
//
//            endTime = endTime.split(",")[1];
//        }
//
//        if (LocalTime.now().isAfter(LocalTime.parse(endTime))) {
//            throw exception(LOG_NOT_UPDATE);
//        }

        // 更新
        LogInstanceDO updateObj = BeanUtils.toBean(updateReqVO, LogInstanceDO.class);
        logInstanceMapper.updateById(updateObj);
    }

    @Override
    public void deleteLogInstance(Long id) {
        // 校验存在
        validateLogInstanceExists(id);
        // 删除
        logInstanceMapper.deleteById(id);
    }

    private void validateLogInstanceExists(Long id) {
        if (logInstanceMapper.selectById(id) == null) {
            throw exception(LOG_INSTANCE_NOT_EXISTS);
        }
    }

    @Override
    public LogInstanceRespVO getLogInstance(Long id) {

        //获取日志详情
        LogInstanceDO logInstanceDO = logInstanceMapper.selectById(id);
//        LogInstanceRespVO logInstanceRespVO = logInstanceMapper.selectPageResult(new LogInstancePageReqVO(), getLoginUserId(), null, id);

        //设置日志详情 模板内容和时间
        return setContentFun(LogInstanceConvert.INSTANCE.convert(logInstanceDO));
    }

    @Override
    public IPage<LogInstanceRespVO> getLogInstancePage(LogInstancePageReqVO pageReqVO, Integer pagingType) {

        List<Long> leaderUserIds = new ArrayList<>();

        //判断特殊情况， 只需查看各部门领导人得日志
        if (pageReqVO.getIsProduce() == null && pageReqVO.getIsBoss() != null && pageReqVO.getIsBoss() == 1) {

            //查询两级以及三级部门得领导人
            //以及岗位为总监或副总监的用户
            leaderUserIds = adminUserService.getUserByBoss();
        }

        Page<LogInstanceRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<LogInstanceRespVO> pageList = new Page<>();
        if (pagingType == 0) {
            pageList = logInstanceMapper.selectPageResult(page, pageReqVO, getLoginUserId(), leaderUserIds);
        }else if (pagingType == 1) {
            pageList = logInstanceMapper.selectMyPageResult(page, pageReqVO, getLoginUserId());
        }

        List<LogInstanceRespVO> records = pageList.getRecords();
        if (!records.isEmpty()) {

            //模版ids过滤
            Set<Long> workFormIds = records.stream().map(LogInstanceRespVO::getFormId).collect(Collectors.toSet());

            //  查询模版列表
            List<LogFormDO> formList = logFormService.getFormList(workFormIds);
            Map<Long, LogFormDO> formMap = formList.stream().collect(Collectors.toMap(LogFormDO::getId, item -> item));

            //查询用户信息列表
            Set<Long> userIds = records.stream().map(LogInstanceRespVO::getStartUserId).collect(Collectors.toSet());
            Map<Long, AdminUserDO> userMap = adminUserService.getUserMap(userIds);

            //遍历
            records.forEach(item -> {

                //设置发起人用户名称和头像
                AdminUserDO userDO = userMap.get(item.getStartUserId());
                if (userDO != null) {
                    item.setStartUserName(userDO.getNickname());
                    item.setAvatar(userDO.getAvatar());
                }

                //设置日志内部分
                LogFormDO logFormDO = formMap.get(item.getFormId());
                List<String> fields = logFormDO.getFields();
                StringBuilder workLogContent = new StringBuilder();
                if (!item.getFormVariables().isEmpty()) {
                    JSONObject workLogContentJson = new JSONObject(item.getFormVariables());
                    for (String fieldItem : fields) {
                        JSONObject fieldJson = new JSONObject(fieldItem);
                        if ("upload".equals(fieldJson.getStr("type"))) {
                            continue;
                        }
                        String fieldStr = fieldJson.getStr("field");
                        String title = fieldJson.getStr("title");
                        if (fieldStr == null || title == null) {
                            continue;
                        }
                        String field = workLogContentJson.getStr(fieldStr);
                        workLogContent.append(title).append(":").append(field).append("  ");
                    }
                }
                item.setWorkLogContent(workLogContent.toString());

                //设置时间
                LocalDate nowDate = LocalDate.now();
                LocalDateTime createTime = LocalDateTime.parse(item.getCreateTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                //日志创建时间是当前日期时
                if (createTime.toLocalDate().equals(nowDate)) {
                    item.setCreateTime("今日 " + createTime.toLocalTime().format(DateTimeFormatter.ofPattern("HH:mm")));
                }
                //日志创建时间是当前日期前一天时
                if (createTime.toLocalDate().equals(nowDate.minusDays(1))) {
                    item.setCreateTime("昨日 " + createTime.toLocalTime().format(DateTimeFormatter.ofPattern("HH:mm")));
                }
            });
        }

        IPage<LogInstanceRespVO> pageListVO = new Page<>();
        pageListVO.setRecords(records);
        pageListVO.setTotal(pageList.getTotal());
        return pageListVO;
    }

    @Override
    public List<DeptRespVO> getDeptInfo() {

        // 根据日志规则 获取当前登录用户可以查看日志的用户信息
        List<LogReadDo> logInstanceIds = logReadService.getReadListByReadUserId(getLoginUserId());
        List<Long> startUserIds = convertList(logInstanceIds, LogReadDo::getStartUserId);
        List<AdminUserDO> adminUserDOS = adminUserService.getUserList(startUserIds);

        // 获得部门信息
        List<Long> deptIds = adminUserDOS.stream()
                .map(AdminUserDO::getDeptId)
                .distinct()
                .collect(Collectors.toList());

        List<DeptDO> deptDOS = deptService.getDeptList(deptIds);
        return BeanUtils.toBean(deptDOS, DeptRespVO.class);
    }

    @Override
    public LogInstanceDO getUpLog(Long formId) {

        PageResult<LogInstanceDO> logInstanceDOS = logInstanceMapper.getUpLog(getLoginUserId(), formId);
        if (!logInstanceDOS.getList().isEmpty()) {

            return logInstanceDOS.getList().get(0);
        }

        return null;
    }

    @Override
    public LogInstanceRespVO setContentFun(LogInstanceRespVO logInstanceRespVO) {

        if (logInstanceRespVO == null) {
            return null;
        }
        //获取模板信息
        LogFormDO logFormDO = logFormService.getForm(logInstanceRespVO.getFormId());
        List<String> fields = logFormDO.getFields();
        List<Map<String, String>> logDetail = new ArrayList<>();

        if (!logInstanceRespVO.getFormVariables().isEmpty()) {
            JSONObject workLogContentJson = new JSONObject(logInstanceRespVO.getFormVariables());
            for (String fieldItem : fields) {
                Map<String, String> detail = new HashMap<>();

                JSONObject fieldJson = new JSONObject(fieldItem);
                String fieldStr = fieldJson.getStr("field");
                String title = fieldJson.getStr("title");
                String field = workLogContentJson.getStr(fieldStr);

                detail.put("title", title);
                detail.put("value", field);
                logDetail.add(detail);
            }
            logInstanceRespVO.setLogDetail(logDetail);
        }
        //设置时间
        LocalDate nowDate = LocalDate.now();
        LocalDateTime createTime = LocalDateTime.parse(logInstanceRespVO.getCreateTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //日志创建时间是当前日期时
        if (createTime.toLocalDate().equals(nowDate)) {
            logInstanceRespVO.setCreateTime("今日 " + createTime.toLocalTime().format(DateTimeFormatter.ofPattern("HH:mm")));
        }
        //日志创建时间是当前日期前一天时
        if (createTime.toLocalDate().equals(nowDate.minusDays(1))) {
            logInstanceRespVO.setCreateTime("昨日 " + createTime.toLocalTime().format(DateTimeFormatter.ofPattern("HH:mm")));
        }

        return logInstanceRespVO;
    }

    @Override
    public LogInstanceNextOrUpVO getNextOrUp(LogInstancePageReqVO dto, Long id, Integer pagingType) {
        LogInstanceNextOrUpVO vo = new LogInstanceNextOrUpVO();
        List<Long> leaderUserIds = new ArrayList<>();
        //判断特殊情况， 只需查看各部门领导人得日志
        if (dto.getIsBoss() != null && dto.getIsBoss() == 1) {
            //查询两级以及三级部门得领导人
            //以及岗位为总监或副总监的用户
            leaderUserIds = adminUserService.getUserByBoss();
        }
        if (pagingType == 0) {
            IPage<LogInstanceRespVO> upLogInstances = logInstanceMapper.getNextOrUp(dto, id, getLoginUserId(), 1, leaderUserIds);
            IPage<LogInstanceRespVO> nextLogInstances = logInstanceMapper.getNextOrUp(dto, id, getLoginUserId(), 0, leaderUserIds);

            if (CollectionUtil.isNotEmpty(upLogInstances.getRecords())) {
                vo.setUpLogInstance(upLogInstances.getRecords().get(0));
            }
            if (CollectionUtil.isNotEmpty(nextLogInstances.getRecords())) {
                vo.setNextLogInstance(nextLogInstances.getRecords().get(0));
            }
        }

        if (pagingType == 1) {
            LogInstanceRespVO upLogInstance = logInstanceMapper.getMyNextOrUp(dto, id, getLoginUserId(), 1, leaderUserIds);
            LogInstanceRespVO nextLogInstance = logInstanceMapper.getMyNextOrUp(dto, id, getLoginUserId(), 0, leaderUserIds);

            vo.setUpLogInstance(upLogInstance);
            vo.setNextLogInstance(nextLogInstance);
        }

        //获取日志详情
        return vo;
    }


    @Override
    public List<LogInstanceDO> getNeedWriteLogInstanceByTimeRange(Long fromId, List<String> time, Long userId) {
        return logInstanceMapper.selectList(new LambdaQueryWrapper<LogInstanceDO>()
                .eq(LogInstanceDO::getFormId, fromId)
                .in(LogInstanceDO::getTime, time)
                .eq(userId != null, LogInstanceDO::getStartUserId, userId));
    }
}