package com.yida.data.school.userLeave.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.userLeave.EduLeaveRequestProcessConfig;
import com.yida.data.common.core.entity.userLeave.EduLeaveRequestProcessConfigApplicableDepartments;
import com.yida.data.common.core.entity.userLeave.EduLeaveRequestProcessConfigApprovalUser;
import com.yida.data.common.core.entity.userLeave.EduLeaveRequestProcessConfigView;
import com.yida.data.school.dto.userLeave.EduLeaveRequestProcessConfigDTO;
import com.yida.data.school.userLeave.mapper.EduLeaveRequestProcessConfigApprovalUserMapper;
import com.yida.data.school.userLeave.mapper.EduLeaveRequestProcessConfigMapper;
import com.yida.data.school.userLeave.service.EduLeaveRequestProcessConfigApplicableDepartmentsService;
import com.yida.data.school.userLeave.service.EduLeaveRequestProcessConfigApprovalUserService;
import com.yida.data.school.userLeave.service.EduLeaveRequestProcessConfigService;
import com.yida.data.school.userLeave.service.EduLeaveRequestProcessConfigViewService;
import com.yida.data.school.vo.userLeave.EduLeaveRequestProcessConfigVO;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;

/**
 * Service实现
 *
 * @author wjm
 * @date 2022-12-01 10:45:56
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduLeaveRequestProcessConfigServiceImpl extends ServiceImpl
    <EduLeaveRequestProcessConfigMapper, EduLeaveRequestProcessConfig> implements EduLeaveRequestProcessConfigService {

    private final EduLeaveRequestProcessConfigMapper eduLeaveRequestProcessConfigMapper;

    private final EduLeaveRequestProcessConfigApprovalUserService eduLeaveRequestProcessConfigApprovalUserService;

    private final EduLeaveRequestProcessConfigApplicableDepartmentsService eduLeaveRequestProcessConfigApplicableDepartmentsService;

    private final EduLeaveRequestProcessConfigViewService eduLeaveRequestProcessConfigViewService;


    @Override
    public void add(EduLeaveRequestProcessConfigDTO dto) {
        if (ObjectUtil.isNotNull(dto.getId())) {
            deleteBatch(Collections.singletonList(dto.getId()));
            dto.setId(null);
        }
        EduLeaveRequestProcessConfig config = new EduLeaveRequestProcessConfig();
        BeanUtils.copyProperties(dto, config);

        //保存流程主体
        eduLeaveRequestProcessConfigMapper.insert(config);

        //保存流程步骤
        List<EduLeaveRequestProcessConfigApprovalUser> eduLeaveRequestProcessConfigApprovalUserList = new ArrayList<>();

        int index = 0;
        dto.getEduLeaveRequestProcessConfigViewList().forEach(eduLeaveRequestProcessConfigView -> {
            eduLeaveRequestProcessConfigView.setProcessId(config.getId());
            //保存流程步骤
            eduLeaveRequestProcessConfigViewService.save(eduLeaveRequestProcessConfigView);

            eduLeaveRequestProcessConfigView.getEduLeaveRequestProcessConfigApprovalUserList().forEach(user -> {
                user.setProcessId(config.getId());
                user.setViewId(eduLeaveRequestProcessConfigView.getId());
                eduLeaveRequestProcessConfigApprovalUserList.add(user);
            });
        });
        eduLeaveRequestProcessConfigApprovalUserService.saveBatch(eduLeaveRequestProcessConfigApprovalUserList);

        //保存适用人员部门
        List<EduLeaveRequestProcessConfigApplicableDepartments> eduLeaveRequestProcessConfigApplicableDepartmentsList = new ArrayList<>();
        dto.getApplicableDepartmentsList().forEach(eduLeaveRequestProcessConfigApplicableDepartments -> {
            eduLeaveRequestProcessConfigApplicableDepartments.setProcessId(config.getId());
            eduLeaveRequestProcessConfigApplicableDepartmentsList.add(eduLeaveRequestProcessConfigApplicableDepartments);
        });
        eduLeaveRequestProcessConfigApplicableDepartmentsService
            .saveBatch(eduLeaveRequestProcessConfigApplicableDepartmentsList);

    }

    @Override
    public void deleteBatch(List<Long> idList) {
        idList.forEach(id -> {
            eduLeaveRequestProcessConfigApprovalUserService.remove(
                Wrappers.lambdaQuery(new EduLeaveRequestProcessConfigApprovalUser())
                    .eq(EduLeaveRequestProcessConfigApprovalUser::getProcessId, id));
            eduLeaveRequestProcessConfigApplicableDepartmentsService
                .remove(Wrappers.lambdaQuery(new EduLeaveRequestProcessConfigApplicableDepartments())
                    .eq(EduLeaveRequestProcessConfigApplicableDepartments::getProcessId, id));
            eduLeaveRequestProcessConfigViewService.remove(Wrappers.lambdaQuery(new EduLeaveRequestProcessConfigView())
                .eq(EduLeaveRequestProcessConfigView::getProcessId, id));

            this.remove(Wrappers.lambdaQuery(new EduLeaveRequestProcessConfig())
                .eq(EduLeaveRequestProcessConfig::getId, id));
        });
    }

    @Override
    public Page<EduLeaveRequestProcessConfigVO> findPageList(Page<EduLeaveRequestProcessConfig> page, String processName,
        Long schoolId) {
        Page<EduLeaveRequestProcessConfigVO> returnPage = eduLeaveRequestProcessConfigMapper
            .findPage(page, processName, schoolId);
        returnPage.getRecords().forEach(eduLeaveRequestProcessConfigVO -> {
            List<EduLeaveRequestProcessConfigView> eduLeaveRequestProcessConfigViewList = eduLeaveRequestProcessConfigViewService
                .list(Wrappers.lambdaQuery(new EduLeaveRequestProcessConfigView())
                    .eq(EduLeaveRequestProcessConfigView::getProcessId, eduLeaveRequestProcessConfigVO.getId()));
            eduLeaveRequestProcessConfigViewList.forEach(eduLeaveRequestProcessConfigView -> {
                List<EduLeaveRequestProcessConfigApprovalUser> approvalUserList = eduLeaveRequestProcessConfigApprovalUserService
                    .list(Wrappers.lambdaQuery(new EduLeaveRequestProcessConfigApprovalUser())
                        .eq(EduLeaveRequestProcessConfigApprovalUser::getViewId, eduLeaveRequestProcessConfigView.getId()));
                eduLeaveRequestProcessConfigView.setEduLeaveRequestProcessConfigApprovalUserList(approvalUserList);
            });

            List<EduLeaveRequestProcessConfigApplicableDepartments> applicableDepartmentsList = eduLeaveRequestProcessConfigApplicableDepartmentsService
                .list(Wrappers.lambdaQuery(new EduLeaveRequestProcessConfigApplicableDepartments())
                    .eq(EduLeaveRequestProcessConfigApplicableDepartments::getProcessId, eduLeaveRequestProcessConfigVO.getId()));

            eduLeaveRequestProcessConfigVO.setEduLeaveRequestProcessConfigViewList(eduLeaveRequestProcessConfigViewList);
            eduLeaveRequestProcessConfigVO.setApplicableDepartmentsList(applicableDepartmentsList);
        });
        return returnPage;
    }

    @Override
    public List<EduLeaveRequestProcessConfig> findConfigByDeptIds(List<Long> deptIds, Integer type, Long requestDays) {
        return baseMapper.findConfigByDeptIds(deptIds, type, requestDays);
    }


}
