package com.huimv.face.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huimv.face.domain.FaceUserImgs;
import com.huimv.face.repo.FaceUserImgRepository;
import com.huimv.face.result.Result;
import com.huimv.face.service.FaceUserImgsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.face.service.impl
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2020/12/30 0030 18:25
 **/
@Service
public class FaceUserImgsServiceImpl implements FaceUserImgsService {
    @Autowired
    private FaceUserImgRepository faceUserImgRepository;

    @Override
    public Result upload(Integer ID, String XM, String USERID, MultipartFile file) {
//        try {
        FaceUserImgs faceUserImgs = faceUserImgRepository.findAllByUserId(USERID);
        if (faceUserImgs == null) {
            faceUserImgs = new FaceUserImgs();
        }
        faceUserImgs.setUserId(USERID);
        faceUserImgs.setUserName(XM);
        try {
            faceUserImgs.setImg(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        faceUserImgRepository.save(faceUserImgs);
        return new Result(10001, "添加成功");
//        }catch (Exception e){
//            return new Result(10001,"添加失败");
//        }

    }

    @Override
    public FaceUserImgs findAllByUserId(String userid) {
        return faceUserImgRepository.findAllByUserId(userid);
    }


    @Override
    public JSONObject findAlam() {

        List<Object[]> Alarm = faceUserImgRepository.findAlarm();
        JSONArray userAccountJa = new JSONArray();

        for (int a = 0; a < Alarm.size(); a++) {
            Object[] userAccountObj = (Object[]) Alarm.get(a);
            /* 封装用户和账户关联对象 */
            userAccountJa.add(packageUserAccountRelatedObj(userAccountObj));
        }

        JSONObject resultJo = new JSONObject();
        resultJo.put("code", 10001);
        resultJo.put("data", userAccountJa);
        return resultJo;
    }

    /**
     * 封装对象
     */
    private Object packageUserAccountRelatedObj(Object[] userAccountObj) {
        JSONObject dataJo = new JSONObject();

        dataJo.put("ID", userAccountObj[0].toString());
        dataJo.put("JQID", userAccountObj[1].toString());
        if (userAccountObj[2] == null) {
            dataJo.put("GZLX", "");
        } else {
            dataJo.put("GZLX", userAccountObj[2].toString());
        }
        if (userAccountObj[3] == null) {
            dataJo.put("FSSJ", "");
        } else {
            dataJo.put("FSSJ", userAccountObj[3].toString());
        }
        if (userAccountObj[4] == null) {
            dataJo.put("JSSJ", "");
        } else {
            dataJo.put("JSSJ", userAccountObj[4].toString());
        }
        if (userAccountObj[5] == null) {
            dataJo.put("SBLX", "");
        } else {
            dataJo.put("SBLX", userAccountObj[5].toString());
        }
        if (userAccountObj[6] == null) {
            dataJo.put("SBXH", "");
        } else {
            dataJo.put("SBXH", userAccountObj[6].toString());
        }
        if (userAccountObj[7] == null) {
            dataJo.put("TXBZ", "");
        } else {
            dataJo.put("TXBZ", userAccountObj[7].toString());
        }
        if (userAccountObj[8] == null) {
            dataJo.put("UUID", "");
        } else {
            dataJo.put("UUID", userAccountObj[8].toString());
        }
        if (userAccountObj[9] == null) {
            dataJo.put("gzxx", "");
        } else {
            dataJo.put("gzxx", userAccountObj[9].toString());
        }
        if (userAccountObj[10] == null) {
            dataJo.put("ZSMC", "");
        } else {
            dataJo.put("ZSMC", userAccountObj[10].toString());
        }
        if (userAccountObj[11] == null) {
            dataJo.put("mcmc", "");
        } else {
            dataJo.put("mcmc", userAccountObj[11].toString());
        }
        if (userAccountObj[12] == null) {
            dataJo.put("zsqy", "");
        } else {
            dataJo.put("zsqy", userAccountObj[12].toString());
        }
        return dataJo;
    }

}
