/*
 *      Copyright (c) 2018-2028, Chill Zhuang 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 dreamlu.net 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: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.manager.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springblade.core.tool.api.R;
import org.springblade.manager.dto.MakeSealDTO;
import org.springblade.manager.entity.ProjectInfo;
import org.springblade.manager.entity.SignPfxFile;
import org.springblade.manager.service.IProjectInfoService;
import org.springblade.manager.vo.SignPfxFileVO;
import org.springblade.manager.mapper.SignPfxFileMapper;
import org.springblade.manager.service.ISignPfxFileService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.manager.vo.SingPfxManagementVO;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 个人/企业证书信息表 服务实现类
 *
 * @author BladeX
 * @since 2022-06-24
 */
@Service
@AllArgsConstructor
public class SignPfxFileServiceImpl extends BaseServiceImpl<SignPfxFileMapper, SignPfxFile> implements ISignPfxFileService {

	private final IUserClient userClient;

	private final IProjectInfoService projectInfoService;

	@Override
	public IPage<SingPfxManagementVO> singPfxManagementPage(IPage<SingPfxManagementVO> page, SingPfxManagementVO vo) {
		//获取项目列表
		List<SingPfxManagementVO> voResult = this.projectInfoService.singPfxManagementPage(vo);
		//获取项目列表总数
		long count = this.projectInfoService.count(Wrappers.<ProjectInfo>lambdaQuery().eq(ProjectInfo::getIsDeleted, 0));
		page.setTotal(count);

		return page.setRecords(voResult);
	}

	@Override
	public R<Boolean> goRegister(Long id) {
		//获取信息
		SignPfxFile singPfx = this.getById(id);
		if(singPfx == null){
			return R.data(-1, false, "找不到证书");
		}
		if(new Integer("1").equals(singPfx.getCertificateType())){
			//个人证书
			//获取个人信息
			User user = this.userClient.userInfoById(singPfx.getCertificateUserId()).getData();
			if(user == null){
				return R.data(-1, false, "找不到用户");
			}
			if(StringUtils.isEmpty(user.getIdNumber())){
				return R.data(-1, false, "用户身份证号为空");
			}

			boolean ust = false;
			boolean usw = false;

			//判断是否存在个人签名文件
			if(StringUtils.isNotEmpty(singPfx.getSignatureFileUrl()) && !"null".equals(singPfx.getSignatureFileUrl())){
				if(!singPfx.getSignatureFileUrl().contains("png")){
					return R.data(-1, false, "用户签名不是PNG图片");
				}

				ust = true;
				//设置信息
				MakeSealDTO makeSeal = new MakeSealDTO();


			}

		}

		return null;
	}

	@Override
	public IPage<SignPfxFileVO> selectSignPfxFilePage(IPage<SignPfxFileVO> page, SignPfxFileVO vo) {
		long current = (page.getCurrent() - 1L) * page.getSize();
		//汇总
		Integer count = this.baseMapper.countSignPfxFile(vo);
		//设置总数
		page.setTotal(count);

		List<SignPfxFile> signPfxFileList = this.baseMapper.selectSignPfxFilePage(current, page.getSize(), vo);
		List<SignPfxFileVO> result = JSONArray.parseArray(JSONObject.toJSONString(signPfxFileList), SignPfxFileVO.class);
		result.forEach(vos -> {
			vos.setIsRegisterBoolean(new Integer("1").equals(vos.getIsRegister()));
			vos.setCertificateTypeValue(new Integer("1").equals(vos.getCertificateType()) ? "个人证书" : "企业证书");
		});

		return page.setRecords(result);
	}

}
