package org.springblade.modules.sign.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.utils.HttpClientUtil;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.modules.sign.dto.SignDTO;
import org.springblade.modules.sign.entity.SignEntity;
import org.springblade.modules.sign.service.ISignService;
import org.springblade.modules.sign.vo.IdentityConfirmVo;
import org.springblade.modules.system.entity.User;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 考勤 定时任务
 */
@Configuration
@EnableScheduling
@AllArgsConstructor
@Slf4j
public class SignTask extends BladeController {

	private final ISignService signService;


	/**
	 * 身份确认(名单库以脸搜脸v2)
	 */
	@Scheduled(cron = "0 0 21 * * ? ")
	private void identityConfirm() {
		//获取用户身份证号集合
		List<Integer> type = new ArrayList<>();
		type.add(1);
		type.add(2);
//		QueryWrapper<User> qe = new QueryWrapper<>();
//		qe.eq("role_id","1677215233615192065");
		List<User> userList = signService.getUserListByRoleId();
		for (User u : userList) {
			if (StringUtils.isNotBlank(u.getSfzh())) {
				String zjhm = u.getSfzh();
				Long userId = u.getId();
				QueryWrapper<SignEntity> wrapper = new QueryWrapper<>();
				wrapper.eq("user_id", userId);
				wrapper.in("type", type);
				wrapper.apply(true, "create_time :: date = current_date");
				List<SignEntity> list = signService.list(wrapper);
				for (SignEntity s : list) {
					if (StringUtils.isNotBlank(s.getQdzp())) {
						String imageData = s.getQdzp().replace("\n", "");
						boolean bdrl = bdrl(imageData, zjhm);
						if (bdrl) {
							s.setBdjg("成功");
							signService.saveOrUpdate(s);
						} else {
							s.setBdjg("失败");
							signService.saveOrUpdate(s);
						}
					}
				}
			}

		}
	}


	public static boolean bdrl(String imageData, String zjhm) {

		String url = "http://135.40.15.68:9195/api/haikang/queryByImageModelWithPage";
		JSONObject json = new JSONObject();
		json.put("pageNo", 1);
		json.put("pageSize", 20);
		json.put("personLibId", "1687665119270");
		json.put("imageData", imageData);
		json.put("minSimilarity", 0.80);
		String result = HttpClientUtil.doPost(url, json);
		log.info("海康返回： " + result);
		JSONObject resultJson = JSONObject.parseObject(result);
		String dataStr1 = resultJson.get("data").toString();
		JSONObject dataJson = JSONObject.parseObject(dataStr1);
		String dataStr2 = dataJson.get("data").toString();
		JSONObject dataJson2 = JSONObject.parseObject(dataStr2);
		JSONArray listJsonArray = dataJson2.getJSONArray("list");
		List<IdentityConfirmVo> list = new ArrayList<>();
		List<String> cardList = new ArrayList<>();
		if (null != listJsonArray && listJsonArray.size() > 0) {
			for (int i = 0; i < listJsonArray.size(); i++) {
				JSONObject jsonArrayObjectItem = JSONObject.parseObject(listJsonArray.get(i).toString());
				String certificateNumber = jsonArrayObjectItem.getString("certificateNumber");
				cardList.add(certificateNumber);
			}
			log.info("******************" + cardList.contains(zjhm));
			return cardList.contains(zjhm);
		} else {
			return false;
		}

	}

	/**
	 * 自动标记旷工
	 */
	@Scheduled(cron = "0 0 23 * * ? ")
//	@Scheduled(cron = "0/30 * * * * ?")
	public void autoAbsent() {
		// 查询今天是不是公休日
		String rqType = signService.getRqType();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 查询所有角色是社区民警/下派民警的人
		List<SignDTO> signDTOS = signService.getSignUser();
		// 自动标记旷工
		for (SignDTO signDTO : signDTOS) {
			SignEntity signEntity = new SignEntity();
			signEntity.setCreateUser(signDTO.getUserId());
			signEntity.setUserId(signDTO.getUserId());
			signEntity.setRq(format.format(new Date()));
			if (rqType.equals("上班日")) {
				signEntity.setType(3); // 旷工
			} else {
				signEntity.setType(6); // 公休
			}
			// 今天没有打卡记录，自动标记一条旷工
			signService.save(signEntity);
		}
	}
}
