package com.yifu.cloud.plus.v1.yifu.social.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.OSSUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.log.annotation.SysLog;
import com.yifu.cloud.plus.v1.yifu.salary.vo.SalaryAttaVo;
import com.yifu.cloud.plus.v1.yifu.social.entity.SysHouseHoldInfo;
import com.yifu.cloud.plus.v1.yifu.social.entity.TAttaInfo;
import com.yifu.cloud.plus.v1.yifu.social.service.FileUploadService;
import com.yifu.cloud.plus.v1.yifu.social.service.TAttaInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fang
 * 文件上传接口
 */
@RestController
@RequestMapping("/fileUpload")
@Tag(name = "文件上传接口")
@Slf4j
public class FileUploadController {

    @Autowired
    private OSSUtil ossUtil;

	@Autowired
	private TAttaInfoService tAttaInfoService;

	@Autowired
	private FileUploadService fileUploadService;

	/**
	 * @Author fxj
	 * @Description
	 * @Date 13:49 2022/6/17
	 * @Param
	 * @return
	**/
	@Operation(description = "OSS文件上传接口" +
			"@ApiImplicitParam(name = \"file\", value = \"Form文件上传\", required = true, dataType = \"__file\", paramType = \"form\"),\n" +
			"\t\t\t@ApiImplicitParam(name = \"filePath\", value = \"文件上传路径\", required = false, dataType = \"String\", paramType = \"form\"),\n" +
			"\t\t\t@ApiImplicitParam(name = \"type\", value = \"关系类型(1户配置的章；2：派单审核附件，税友单个使用)\", required = true, dataType = \"int\", paramType = \"form\"),\n" +
			"\t\t\t@ApiImplicitParam(name = \"domain\", value = \"实体id（传入就插入对应关系,用于编辑）\", required = false, paramType = \"form\")\n" +
			"\t")
	/*@ApiImplicitParams({
			@ApiImplicitParam(name = "file", value = "Form文件上传", required = true, dataType = "__file", paramType = "form"),
			@ApiImplicitParam(name = "filePath", value = "文件上传路径", required = false, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "type", value = "关系类型(0客户附件1联系人附件2跟进记录附件3合同附件4合同归档附件5报价单6招标信息7业务评估8电子档案)", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "domain", value = "实体id（传入就插入对应关系,用于编辑）", required = false, paramType = "form")
	})*/
	@PostMapping(value = "/ossUploadFile")
	public R<T> uploadImg(@RequestBody MultipartFile file, String filePath, Integer type, String domain) throws IOException {
		return fileUploadService.uploadAsso(file,filePath,type,domain,CommonConstants.ZERO_STRING);
	}

	/**
	 * @Author hgw
	 * @Description 改编自ossUploadFile接口，改变了回传信息——专为预入职微信端使用——忽略了token，其余人慎用
	 * @Date 13:49 2022/6/17
	 * @Param relationTypeSub 预入职其他附件的子类型
	 * @return
	**/
	@Operation(description = "OSS文件上传接口" +
			"@ApiImplicitParam(name = \"file\", value = \"Form文件上传\", required = true, dataType = \"__file\", paramType = \"form\"),\n" +
			"\t\t\t@ApiImplicitParam(name = \"filePath\", value = \"文件上传路径\", required = false, dataType = \"String\", paramType = \"form\"),\n" +
			"\t\t\t@ApiImplicitParam(name = \"type\", value = \"关系类型(0员工附件-学历 1 员工附件-伤残 2.员工附件-职业 3.员工附件-不良记录 4.员工附件-合同;5收入证明)\", required = true, dataType = \"int\", paramType = \"form\"),\n" +
			"\t\t\t@ApiImplicitParam(name = \"domain\", value = \"实体id（传入就插入对应关系,用于编辑）\", required = false, paramType = \"form\")\n" +
			"\t")
	@PostMapping(value = "/uploadFileReturnAtta")
	public R<T> uploadFileReturnAtta(@RequestBody MultipartFile file, String filePath, Integer type, String domainId) throws IOException {
		return fileUploadService.uploadAsso(file,filePath,type,domainId,CommonConstants.ONE_STRING);
	}

	/**
	 * @Author fxj
	 * @Description
	 * @Date 13:51 2022/6/17
	 * @Param
	 * @return
	**/
	@Operation(description = "附件删除")
	@SysLog("附件删除")
	@DeleteMapping("/ossFileDelete/{id}")
	public R<Boolean> ossFileDelete(@PathVariable String id) {
		try {
			TAttaInfo attaInfo = tAttaInfoService.getById(id);
			if (null == attaInfo) {
				return R.failed("无对应附件信息");
			} else if (CommonConstants.ONE_STRING.equals(attaInfo.getRelationType())
					&& Common.isNotNull(attaInfo.getDomainId())) {
				// 删除后解除户的章是否有为否
				fileUploadService.updateSysHouseHoldInfoIsSign(attaInfo.getDomainId(), CommonConstants.ONE_STRING);
			}
			if (tAttaInfoService.removeById(id)) {
				ossUtil.deleteObject(null, attaInfo.getAttaSrc());
				return R.ok(null,"删除成功！");
			}

		} catch (Exception e) {
			log.error("附件删除异常：" + e.getMessage());
			return R.failed("附件删除异常！");
		}
		return R.ok(null,"删除失败！");
	}

	/**
	 * 获取附件下载地址
	 *
	 * @param id
	 * @return
	 * @Author fxj
	 * @Date 2019-08-16
	 * @param id  domainId
	 **/
	@Operation(description = "附件预览下载地址 type:关系类型(0员工附件-学历 1 员工附件-伤残 2.员工附件-职业")
	@GetMapping("/ossFileUrl/{id}")
	public R<List<TAttaInfo>> ossFileUrl(@PathVariable String id) {
		List<TAttaInfo> attas = tAttaInfoService.list(Wrappers.<TAttaInfo>query().lambda().eq(TAttaInfo::getDomainId,id));
		if (Common.isEmpty(attas)) {
			return new R<>(attas);
		}
		for (TAttaInfo  atta:attas){
			URL url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
			atta.setAttaUrl(url.toString().replace("http","https"));
		}
		return new R<>(attas);
	}

	/**
	 * 获取附件
	 * @author  hgw
	 * @return R
	 */
	@Operation(summary = "获取薪资原表附件--EKP调用接口", description = "获取薪资原表附件--EKP调用接口")
	@SysLog("获取薪资原表附件--EKP调用接口")
	@GetMapping("/getAttaListBySalaryId")
	public R<List<SalaryAttaVo>> getAttaListBySalaryId(@RequestParam String salaryId) {
		List<SalaryAttaVo> voList = new ArrayList<>();
		List<TAttaInfo> attaList = tAttaInfoService.list(Wrappers.<TAttaInfo>query().lambda()
				.eq(TAttaInfo::getRelationType,11)
				.eq(TAttaInfo::getDomainId,salaryId));
		if (attaList != null && !attaList.isEmpty()) {
			SalaryAttaVo vo;
			URL url;
			for (TAttaInfo  a : attaList){
				url = ossUtil.getObjectUrl(null, a.getAttaSrc());
				vo = new SalaryAttaVo();
				vo.setAttaName(a.getAttaName());
				vo.setAttaUrl(url.toString().replace("http","https"));
				voList.add(vo);
			}
		}
		return R.ok(voList);
	}

	/**
	 * @param domainId 关联id
	 * @param type 类型 1户配置的章；2：派单审核附件，税友单个使用，社保3医保附件；11社保增花名册模板；12社保减花名册模板；13医保增花名册模板；14医保减花名册模板
	 *  4:单个附件_社保_花名册;5:单个附件_医保_花名册;6:单个附件_解除劳动合同证明书（1.7.22版本）
	 * @Description: 获取社保服务的附件信息
	 * @Author: hgw
	 * @Date: 2025/4/28 14:12
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.util.List < com.yifu.cloud.plus.v1.yifu.salary.vo.SalaryAttaVo>>
	 **/
	@Operation(summary = "获取社保服务的附件信息", description = "获取社保服务的附件信息")
	@SysLog("获取社保服务的附件信息")
	@GetMapping("/getAttaListByIdAndType")
	public R<List<TAttaInfo>> getAttaListByIdAndType(@RequestParam String domainId, @RequestParam String type) {
		List<TAttaInfo> attaList = tAttaInfoService.list(Wrappers.<TAttaInfo>query().lambda()
				.eq(TAttaInfo::getRelationType, type)
				.eq(TAttaInfo::getDomainId, domainId));
		if (attaList != null && !attaList.isEmpty()) {
			URL url;
			for (TAttaInfo  a : attaList) {
				url = ossUtil.getObjectUrl(null, a.getAttaSrc());
				if (url != null) {
					a.setAttaUrl(url.toString());
				}
			}
		}
		return R.ok(attaList);
	}

}
