/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yifu4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.yifu.cloud.plus.v1.yifu.archives.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.fadada.api.bean.req.template.GetTemplateDetailReq;
import com.fadada.api.bean.rsp.BaseRsp;
import com.fadada.api.bean.rsp.template.GetTemplateDetailRsp;
import com.fadada.api.client.TemplateClient;
import com.fadada.api.exception.ApiException;
import com.yifu.cloud.plus.v1.yifu.archives.entity.FddContractTemplate;
import com.yifu.cloud.plus.v1.yifu.archives.entity.FddContractTemplateFile;
import com.yifu.cloud.plus.v1.yifu.archives.mapper.FddContractTemplateMapper;
import com.yifu.cloud.plus.v1.yifu.archives.service.FddContractTemplateFileService;
import com.yifu.cloud.plus.v1.yifu.archives.service.FddContractTemplateService;
import com.yifu.cloud.plus.v1.yifu.archives.utils.FddUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 法大大员工合同模板
 *
 * @author fxj
 * @date 2022-06-28 16:24:12
 */
@Service
@RequiredArgsConstructor
public class FddContractTemplateServiceImpl extends ServiceImpl<FddContractTemplateMapper, FddContractTemplate> implements FddContractTemplateService {

	@Autowired
	private FddUtil fddUtil;

	@Autowired
	private FddContractTemplateFileService templateFileService;

	/**
	 * 法大大员工合同模板简单分页查询
	 *
	 * @param fddContractTemplate 法大大员工合同模板
	 * @return IPage<FddContractTemplate>
	 */
	@Override
	public IPage<FddContractTemplate> getFddContractTemplatePage(Page<FddContractTemplate> page, FddContractTemplate fddContractTemplate) throws ApiException {
		IPage<FddContractTemplate> fddContractTemplatePage = baseMapper.getFddContractTemplatePage(page, fddContractTemplate);
		List<FddContractTemplate> records = fddContractTemplatePage.getRecords();
		for (FddContractTemplate record : records) {
			//发送请求
			GetTemplateDetailReq req = new GetTemplateDetailReq();
			req.setToken(fddUtil.getToken());
			req.setTemplateId(record.getFddTemplateId());
			TemplateClient client = new TemplateClient(fddUtil.getFadadaApiClient());
			BaseRsp<GetTemplateDetailRsp> rsp = client.getTemplateDetail(req);
			//校验参数
			if (StringUtils.equals(rsp.getCode(), FddUtil.SUCCESS)) {
				String fileId = rsp.getData().getTemplateFiles().get(CommonConstants.ZERO_INT).getFileId();
				FddContractTemplateFile fddContractTemplateFile = templateFileService.getOne(Wrappers.<FddContractTemplateFile>query().lambda().eq(FddContractTemplateFile::getFddTemplateId, record.getFddTemplateId()));
				if(fddContractTemplateFile!=null&&!StringUtils.equals(fddContractTemplateFile.getFddFileId(),fileId)){
					fddContractTemplateFile.setFddFileId(fileId);
					templateFileService.updateById(fddContractTemplateFile);
				}
			}
		}
		return fddContractTemplatePage;
	}
}
