package com.yifu.cloud.plus.v1.batch.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.yifu.cloud.plus.v1.batch.entity.TElectronicArchiveDetail;
import com.yifu.cloud.plus.v1.batch.entity.TElectronicArchiveTask;
import com.yifu.cloud.plus.v1.batch.entity.TTaskArchiveRelation;
import com.yifu.cloud.plus.v1.batch.mapper.TElectronicArchiveDetailMapper;
import com.yifu.cloud.plus.v1.batch.mapper.TElectronicArchiveTaskMapper;
import com.yifu.cloud.plus.v1.batch.service.TElectronicArchiveTaskService;
import com.yifu.cloud.plus.v1.batch.service.TTaskArchiveRelationService;
import com.yifu.cloud.plus.v1.batch.vo.TElectronicArchiveTaskSearchVo;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.ClientNameConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电子档案任务表
 *
 * @author huych
 * @date 2025-12-17 11:03:07
 */
@Log4j2
@Service
@RequiredArgsConstructor
public class TElectronicArchiveTaskServiceImpl extends ServiceImpl<TElectronicArchiveTaskMapper, TElectronicArchiveTask> implements TElectronicArchiveTaskService {

	private final TTaskArchiveRelationService relationService;

	private final TElectronicArchiveDetailMapper detailMapper;

    /**
     * 电子档案任务表简单分页查询
     * @param tElectronicArchiveTask 电子档案任务表
     * @return
     */
    @Override
    public IPage<TElectronicArchiveTask> getTElectronicArchiveTaskPage(Page<TElectronicArchiveTask> page, TElectronicArchiveTaskSearchVo tElectronicArchiveTask){
		//权限赋值
		initSearchVo(tElectronicArchiveTask);
		return baseMapper.getTElectronicArchiveTaskPage(page,tElectronicArchiveTask);
    }

	@Override
	public R deleteTaskById(String id) {
		TElectronicArchiveTask task = this.getById(id);
		if (task == null) {
			return R.failed("任务不存在");
		}
		if (!CommonConstants.ZERO_STRING.equals(task.getStatus())) {
			return R.failed("只有待上传状态的任务才可删除！");
		}
		baseMapper.deleteById(id);
		List<TTaskArchiveRelation> relationList = relationService.list(Wrappers.<TTaskArchiveRelation>query().lambda()
				.eq(TTaskArchiveRelation::getTaskId, id));
		if (Common.isNotNull(relationList)) {
			for (TTaskArchiveRelation relation : relationList) {
				//删除关联关系表
				relationService.removeById(relation.getId());
				//删除电子档案明细表
				detailMapper.delete(Wrappers.<TElectronicArchiveDetail>query().lambda()
						.eq(TElectronicArchiveDetail::getStatus,CommonConstants.ZERO_STRING)
						.eq(TElectronicArchiveDetail::getId,relation.getArchiveDetailId()));
			}
		}
		return R.ok();
	}

	public void initSearchVo(TElectronicArchiveTaskSearchVo searchVo) {
		YifuUser user = SecurityUtils.getUser();
		long roleId = 1569585030423158786L;
		boolean isSsc = this.haveRole(user, roleId);
		if (isSsc || CommonConstants.ZERO_STRING.equals(user.getSystemFlag())) {
			searchVo.setCreateBy(null);
		}
	}

	public boolean haveRole(YifuUser user, long roleId) {
		List<Long> roleList = user.getClientRoleMap().get(ClientNameConstants.CLIENT_MVP);
		for (Long role : roleList) {
			if (role == roleId) {
				return true;
			}
		}
		return false;
	}
}
