package com.yida.data.form.apply.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.applyForm.CoreApplyFormApprovalProcess;
import com.yida.data.form.apply.mapper.CoreApprovalProcessMapper;
import com.yida.data.form.apply.service.CoreApplyFormApprovalProcessService;
import com.yida.data.form.apply.service.CoreApprovalProcessService;
import com.yida.data.form.apply.service.CoreApprovalProcessUserService;
import com.yida.data.common.core.entity.applyForm.CoreApprovalProcess;
import com.yida.data.common.core.entity.applyForm.CoreApprovalProcessUser;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.customForm.dto.apply.ApprovalProcessSelectPageDTO;
import com.yida.data.customForm.vo.apply.ApprovalProcessInfoVO;
import com.yida.data.customForm.vo.apply.ApprovalProcessSelectPageVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 审核流程表 Service
 *
 * @author ZYJ
 * @date 2023/11/21
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class CoreApprovalProcessServiceImpl extends ServiceImpl<CoreApprovalProcessMapper, CoreApprovalProcess>
        implements CoreApprovalProcessService {

    private final CoreApprovalProcessUserService coreApprovalProcessUserService;
    private final CoreApplyFormApprovalProcessService coreApplyFormApprovalProcessService;

    @Override
    public IPage<ApprovalProcessSelectPageVO> listApprovalProcessPage(ApprovalProcessSelectPageDTO dto) {
        // TODO: 2023/11/21 此处暂时只有学校角色
        dto.setDeptId(FebsUtil.getDeptId());
        IPage<ApprovalProcessSelectPageVO> page = baseMapper.listApprovalProcessPage(dto.toPage(), dto);
        // TODO: 2023/11/21 后续查询关键字段
        if (CollUtil.isNotEmpty(page.getRecords())) {
            page.getRecords().forEach(process -> {
                // 查询审核人员名称
                List<CoreApprovalProcessUser> processUserList = coreApprovalProcessUserService.list(Wrappers.lambdaQuery(new CoreApprovalProcessUser())
                        .eq(CoreApprovalProcessUser::getProcessId, process.getId()));
                if (CollUtil.isNotEmpty(processUserList)) {
                    String names = processUserList.stream().map(CoreApprovalProcessUser::getName).collect(Collectors.joining("、"));
                    process.setApprovalUserName(names);
                }
            });
        }
        return page;
    }

    @Override
    public ApprovalProcessInfoVO getApprovalProcessInfo(Long id) {
        // 返回数据
        ApprovalProcessInfoVO vo = new ApprovalProcessInfoVO();
        CoreApprovalProcess process = getById(id);
        if (Objects.nonNull(process)) {
            BeanUtils.copyProperties(process, vo);
            // 查询人员信息
            List<CoreApprovalProcessUser> list = coreApprovalProcessUserService.list(Wrappers.lambdaQuery(new CoreApprovalProcessUser())
                    .eq(CoreApprovalProcessUser::getProcessId, id));
            vo.setUserList(list);
        }
        return vo;
    }

    @Override
    public void deleteApprovalProcess(Long id) {
        removeById(id);
        // 删除关联用户
        coreApprovalProcessUserService.remove(Wrappers.lambdaQuery(new CoreApprovalProcessUser())
                .eq(CoreApprovalProcessUser::getProcessId, id));
        // TODO: 2023/11/21 删除申请表单对应的审核流程信息 (此处需要判断能否删除)
    }

    @Override
    public void saveApprovalProcess(CoreApprovalProcess process) {
        // TODO: 2023/11/21 需要判断能否修改
        if (Objects.nonNull(process.getId())) {
            // 删除用户信息
            coreApprovalProcessUserService.remove(Wrappers.lambdaQuery(new CoreApprovalProcessUser())
                    .eq(CoreApprovalProcessUser::getProcessId, process.getId()));
        } else {
            process.setDeptId(FebsUtil.getDeptId());
        }
        saveOrUpdate(process);
        // 处理用户信息
        if (CollUtil.isNotEmpty(process.getUserList())) {
            for (CoreApprovalProcessUser coreApprovalProcessUser : process.getUserList()) {
                coreApprovalProcessUser.setId(null);
                coreApprovalProcessUser.setProcessId(process.getId());
                coreApprovalProcessUserService.save(coreApprovalProcessUser);
            }
        }
    }

    @Override
    public List<CoreApprovalProcess> listApprovalProcess(Long formId) {
        // 查询表单已有的流程
        List<CoreApplyFormApprovalProcess> list = coreApplyFormApprovalProcessService.list(Wrappers.lambdaQuery(new CoreApplyFormApprovalProcess())
                .eq(CoreApplyFormApprovalProcess::getApplyFormId, formId));
        List<CoreApprovalProcess> processList = list(Wrappers.lambdaQuery(new CoreApprovalProcess())
                .notIn(CollUtil.isNotEmpty(list), CoreApprovalProcess::getId, list.stream()
                        .map(CoreApplyFormApprovalProcess::getApprovalProcessId).collect(Collectors.toList())));
        if (CollUtil.isNotEmpty(processList)) {
            processList.forEach(process -> {
                // 查询审核人员名称
                List<CoreApprovalProcessUser> processUserList = coreApprovalProcessUserService.list(Wrappers.lambdaQuery(new CoreApprovalProcessUser())
                        .eq(CoreApprovalProcessUser::getProcessId, process.getId()));
                if (CollUtil.isNotEmpty(processUserList)) {
                    String names = processUserList.stream().map(CoreApprovalProcessUser::getName).collect(Collectors.joining("、"));
                    process.setApprovalUserName(names);
                }
            });
        }
        return processList;
    }
}
