package org.security.trans.workHandOverUsers.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.security.common.Constant;
import org.security.desk.feign.IFaceClient;
import org.security.desk.vo.FaceVO;
import org.security.ht.workHandOverUsers.entity.WorkHandoverUsers;
import org.security.ht.workHandOverUsers.vo.WorkHandoverCheckVO;
import org.security.ht.workHandOverUsers.vo.WorkHandoverUsersVO;
import org.security.ht.workHandOverUsers.vo.deptDataVO;
import org.security.ht.workHandOverUsers.dto.WorkHandoverUsersDTO;
import org.security.trans.workHandOverUsers.mapper.WorkHandoverUsersMapper;
import org.security.trans.workHandOverUsers.service.IWorkHandoverUsers;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author ：wzf
 * @since ：Created in 2022/9/2 16:39
 */
@Service
@AllArgsConstructor
public class WorkHandoverUsersService extends ServiceImpl<WorkHandoverUsersMapper, WorkHandoverUsers> implements IWorkHandoverUsers {
	private final IFaceClient faceClient;

	@Override
	public R workHandoverCheck(WorkHandoverCheckVO workHandoverCheckVO) {
		boolean sameUser = false;
		if (workHandoverCheckVO.getHandoverFromIdCard().equals(workHandoverCheckVO.getHandoverToIdCard())) {
			if (Constant.WORK_HANDOVER_USER_TYPE_OUT == workHandoverCheckVO.getHandoverFromType()) {
				if (workHandoverCheckVO.getHandoverFromType() == workHandoverCheckVO.getHandoverFromType()) {
					sameUser = true;
				}
			}
		}
		if (workHandoverCheckVO.getHandoverFromAcc().equals(workHandoverCheckVO.getHandoverToAcc())) {
			if (Constant.WORK_HANDOVER_USER_TYPE_BANK == workHandoverCheckVO.getHandoverFromType()) {
				if (workHandoverCheckVO.getHandoverFromType() == workHandoverCheckVO.getHandoverFromType()) {
					sameUser = true;
				}
			}
		}
		if (sameUser) {
			return R.fail("同一用户无法完成交接");
		}
		QueryWrapper<WorkHandoverUsers> fromQuery = generateCheckWrapper(Constant.WORK_HANDOVER_USER_TYPE_OUT == workHandoverCheckVO.getHandoverFromType(),
			workHandoverCheckVO.getHandoverFrom(), workHandoverCheckVO.getHandoverFromIdCard(), workHandoverCheckVO.getHandoverFromAcc());
		WorkHandoverUsers fromUser = baseMapper.selectOne(fromQuery);
		if (Func.isEmpty(fromUser)) {
			return R.fail("交接人不在交接工作组内！");
		}
		if (0 == fromUser.getFaceState()) {
			return R.fail("交接人尚未完成人脸注册！");
		}

		QueryWrapper<WorkHandoverUsers> toQuery = generateCheckWrapper(Constant.WORK_HANDOVER_USER_TYPE_OUT == workHandoverCheckVO.getHandoverToType(),
			workHandoverCheckVO.getHandoverTo(), workHandoverCheckVO.getHandoverToIdCard(), workHandoverCheckVO.getHandoverToAcc());
		WorkHandoverUsers toUser = baseMapper.selectOne(toQuery);
		if (Func.isEmpty(toUser)) {
			return R.fail("被交接人不在交接工作组内！");
		}
		if (0 == toUser.getFaceState()) {
			return R.fail("被交接人尚未完成人脸注册！");
		}
		return R.status(Boolean.TRUE);
	}

	private QueryWrapper<WorkHandoverUsers> generateCheckWrapper(boolean outsiders, String name, String idCard, String account) {
		QueryWrapper<WorkHandoverUsers> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_name", name);
		if (outsiders) {
			queryWrapper.eq("ID_card", idCard);
		} else {
			queryWrapper.eq("account", account);
		}
		return queryWrapper;
	}

	private int countByNameAndIdCard(String name, String idCard) {
		QueryWrapper<WorkHandoverUsers> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_name", name);
		queryWrapper.eq("ID_card", idCard);
		return baseMapper.selectCount(queryWrapper);
	}

	private int countByNameAndAccount(String name, String account) {
		QueryWrapper<WorkHandoverUsers> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_name", name);
		queryWrapper.eq("account", account);
		return baseMapper.selectCount(queryWrapper);
	}

	@Override
	public WorkHandoverUsersVO getByIdCard(String idCard) {
		QueryWrapper<WorkHandoverUsers> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("ID_card", idCard);
		WorkHandoverUsers workHandoverUsers = baseMapper.selectOne(queryWrapper);
		return BeanUtil.copy(workHandoverUsers, WorkHandoverUsersVO.class);
	}

	@Override
	public boolean saveData(WorkHandoverUsersDTO workHandoverUsersDTO) {
		WorkHandoverUsers workHandoverUsers = Func.copy(workHandoverUsersDTO, WorkHandoverUsers.class);
		//获取工作交接用户的机构信息
		if (Func.isNotEmpty(workHandoverUsersDTO.getUserType()) && Constant.WORK_HANDOVER_USER_TYPE_BANK == workHandoverUsersDTO.getUserType()
			&& Func.isNotEmpty(workHandoverUsersDTO.getIDCard())) {
			deptDataVO dataVO = baseMapper.getDeptData(workHandoverUsersDTO.getIDCard());
			if (Func.isNotEmpty(dataVO)) {
				workHandoverUsers.setDeptId(dataVO.getDeptId());
				workHandoverUsers.setDeptName(dataVO.getDeptName());
			}
		}
		return this.saveOrUpdate(workHandoverUsers);
	}

	@Override
	public IPage<WorkHandoverUsersVO> selectProjectPage(IPage<WorkHandoverUsersVO> page, WorkHandoverUsersDTO workHandoverUsersDTO) {
		List<WorkHandoverUsers> list = this.list(new QueryWrapper<WorkHandoverUsers>().lambda().eq(WorkHandoverUsers::getDelFlag, Constant.DELETE_FLAG_0)
			.eq(0 != workHandoverUsersDTO.getUserType(), WorkHandoverUsers::getUserType, workHandoverUsersDTO.getUserType())
			.eq(9 != workHandoverUsersDTO.getFaceState(), WorkHandoverUsers::getFaceState, workHandoverUsersDTO.getFaceState())
			.like(StringUtils.isNotBlank(workHandoverUsersDTO.getUserName()), WorkHandoverUsers::getUserName, workHandoverUsersDTO.getUserName()));
		return page.setRecords(Func.copy(list, WorkHandoverUsersVO.class));
	}

	@Override
	public R saveWithFace(FaceVO faceVO, MultipartFile file) {
		QueryWrapper<WorkHandoverUsers> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_name", faceVO.getFullName());
		queryWrapper.eq("ID_card", faceVO.getIdCardNo());
		WorkHandoverUsers users = baseMapper.selectOne(queryWrapper);
		if (!Func.isEmpty(users)) {
			if (1 == users.getFaceState()) {
				return R.fail("该交接人信息已存在且通过了人脸认证，请勿重复提交！");
			}
		} else {
			users = new WorkHandoverUsers();
		}
		R<FaceVO> r = faceClient.faceRegister(faceVO, file);
		if (!r.isSuccess()) {
			return r;
		}
		FaceVO faceResult = r.getData();
		users.setUserType(faceResult.isOutsiders() ? Constant.WORK_HANDOVER_USER_TYPE_OUT : Constant.WORK_HANDOVER_USER_TYPE_BANK);
		users.setUserName(faceResult.getFullName());
		users.setIDCard(faceResult.getIdCardNo());
		users.setAccount(faceResult.getEmployeeNo());
		users.setFaceState(1);
		deptDataVO dept = baseMapper.getDeptByCode(faceResult.getDeptNo());
		users.setDeptId(dept.getDeptId());
		users.setDeptName(dept.getDeptName());
		return R.status(this.saveOrUpdate(users));
	}
}
