package com.xueyi.system.resource.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xueyi.common.web.constant.ResponseCode;
import com.xueyi.common.web.response.MyResponse;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.NumberFormat;
import java.text.ParseException;

/**
 * 静态资源管理 业务处理
 * 含特征值同步接口
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/api/resources")
public class DmResourcesCommonApiController  extends  BaseApiController{

    @RequestMapping(value = "/sync-face", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Object syncFace(@RequestBody JSONObject map, HttpServletRequest request) {

        String deviceId = map.getString("devId");
        String timestamp = map.getString("timestamp");
        String nowTs = map.getString("now_ts");
        int maxCnt = map.getInteger("max_cnt");
        String sign = map.getString("sign");

        MyResponse myResponse = super.commonCheck(deviceId, Double.valueOf(timestamp), sign);
        if (myResponse.getStatus() != 0) {
            return myResponse;
        }

        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        System.err.println(nowTs);
        String ts = nf.format(Double.parseDouble(nowTs));
        /*AccessControl ac = accessControlDao.findAccessControlByDev(deviceId);

        List<AuthMapping> list = authMappingDao.getByDevAndTime(ac.getAccessControlId(), ts, maxCnt);
        JSONObject ja = new JSONObject();
        if (ObjectUtils.anyNotNull(list) && list.size() > 0) {
            int index = 0;
            for (AuthMapping am : list) {
                Emp emp = empService.get(am.getEmpId());
                *//*if (ObjectUtils.anyNotNull(emp) && !(Emp.STATUS_NORMAL.equals(emp.getStatus()))) {//如果人员状态不对，则不下发该人员信息
                    continue;
                }*//*
                JSONObject json = new JSONObject();
                json.put("userId", am.getEmpId());
                if (ObjectUtils.anyNotNull(emp)) {
                    json.put("userName", emp.getRealName());
                    json.put("gender", emp.getGender());
                    json.put("cardNum", emp.getCardNo());
                    json.put("tag", emp.getEmpType());
                } else {
                    json.put("userName", "");
                    json.put("gender", "");
                    json.put("cardNum", "");
                    json.put("tag", "");
                }
                json.put("hasAuth", am.getHasAuth());
                if (ObjectUtils.anyNotNull(emp) && am.getIsUpdFace() == 1) {
                    json.put("feature", ObjectUtils.anyNotNull(emp.getEmpFaceFeature()) ? JSON.parse(emp.getEmpFaceFeature().getFaceFeature()) : new ArrayList<>());
                } else {
                    json.put("feature", new ArrayList<>());
                }
                json.put("timestamp", am.getUpdatedAt());
                ja.put(String.valueOf(index++), json);
            }
        }*/

        JSONObject result = new JSONObject();
        result.put("devId", deviceId);
//        result.put("auth", ja);

        return outputSuccess(result);
    }



}
