package com.yanzhu.manage.controller;

import com.alibaba.fastjson2.JSONObject;
import com.yanzhu.common.core.utils.DateUtils;
import com.yanzhu.common.core.utils.StringUtils;
import com.yanzhu.common.core.web.domain.AjaxResult;
import com.yanzhu.manage.domain.AttendanceUbiData;
import com.yanzhu.manage.domain.AttendanceUbiDevice;
import com.yanzhu.manage.domain.ProProjectInfoSubdeptsUsers;
import com.yanzhu.manage.service.IAttendanceUbiDataService;
import com.yanzhu.manage.service.IAttendanceUbiDeviceService;
import com.yanzhu.manage.service.IProProjectInfoDeptsService;
import com.yanzhu.manage.service.IProProjectInfoSubdeptsUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/uni/callback")
public class UniCallBackController {

    @Autowired
    private IAttendanceUbiDataService attendanceUbiDataService;

    @Autowired
    private IProProjectInfoSubdeptsUsersService proProjectInfoSubdeptsUsersService;

    @Autowired
    private IAttendanceUbiDeviceService attendanceUbiDeviceService;
    /**
     * 识别回调
     * @param jo
     * @return
     */
    @PostMapping("/recognize")
    public AjaxResult recognize(HttpServletRequest request){
        String eventCode=request.getParameter("eventCode");
        String eventGuid=request.getParameter("eventGuid");
        String data=request.getParameter("eventMsg");
        switch (eventCode){
            case "101004"://上报识别记录
                return doAddAttendance(eventGuid,data);
            case "100002"://设备上线
                return doDeviceOnLine(eventGuid,data);
            case "100003"://设备下线
                return doDeviceOffLine(eventGuid,data);
        }
        return AjaxResult.success();
    }

    /**
     * 设备上线
     * @param eventGuid
     * @param data
     * @return
     */
    private AjaxResult doDeviceOnLine(String eventGuid,String data){
        JSONObject json=JSONObject.parseObject(data);
        String deviceNo= json.getString("deviceNo");
        if(StringUtils.isNotEmpty(deviceNo)){
            AttendanceUbiDevice device=attendanceUbiDeviceService.selectAttendanceUbiDeviceByNo(deviceNo);
            if(device!=null){
                device.setOnlineState(1l);
                attendanceUbiDeviceService.updateAttendanceUbiDevice(device);
                return  AjaxResult.success("同步成功");
            }
        }
        return AjaxResult.error("同步失败");
    }

    /**
     * 设备下线
     * @param eventGuid
     * @param data
     * @return
     */
    private AjaxResult doDeviceOffLine(String eventGuid,String data){
        JSONObject json=JSONObject.parseObject(data);
        String deviceNo= json.getString("deviceNo");
        if(StringUtils.isNotEmpty(deviceNo)){
            AttendanceUbiDevice device=attendanceUbiDeviceService.selectAttendanceUbiDeviceByNo(deviceNo);
            if(device!=null){
                device.setOnlineState(0l);
                attendanceUbiDeviceService.updateAttendanceUbiDevice(device);
                return  AjaxResult.success("同步成功");
            }
        }
        return AjaxResult.error("同步失败");
    }

    /**
     * 考勤记录同步
     * @param eventGuid
     * @param data
     * @return
     */
    private AjaxResult doAddAttendance(String eventGuid,String data){
        JSONObject json=JSONObject.parseObject(data);
        int type=json.getInteger("type");
        if(type==1){// 人员比对结果,1:比对成功 2:比对失败

            String admitGuid=json.getString("admitGuid");
            long projectId=Long.parseLong(json.getString("cardNo"));
            String deviceNo=json.getString("deviceNo");
            String filePath=json.getString("filePath");
            Date showTime=new Date(json.getLong("showTime"));
            AttendanceUbiData where=new AttendanceUbiData();
            where.setAdmitGuid(admitGuid);
            where.setInTime(showTime);
            List<AttendanceUbiData> list= attendanceUbiDataService.queryAttendaceInfo(where);
            AttendanceUbiDevice device=attendanceUbiDeviceService.selectAttendanceUbiDeviceByNo(deviceNo);
            if(device==null){
                return  AjaxResult.error("没有查询到设备No");
            }
            if(list.size()==0){
                //增加操作
                ProProjectInfoSubdeptsUsers userWhere=new ProProjectInfoSubdeptsUsers();
                userWhere.setProjectId(projectId);
                userWhere.setAdmitGuid(admitGuid);
                List<ProProjectInfoSubdeptsUsers> userList=proProjectInfoSubdeptsUsersService.selectProProjectInfoSubdeptsUsersList(userWhere);
                if(userList.size()>0){
                    ProProjectInfoSubdeptsUsers proUser=userList.get(0);
                    if(proUser!=null && proUser.getUser()!=null){
                        AttendanceUbiData addData=new AttendanceUbiData();
                        addData.setAdmitGuid(admitGuid);
                        addData.setComName(proUser.getComName());
                        addData.setProjectName(proUser.getProjectName());
                        addData.setComId(proUser.getComId());
                        addData.setUserId(proUser.getUserId());
                        addData.setProjectId(projectId);
                        addData.setEventGuid(eventGuid);
                        addData.setUserName(proUser.getUser().getUserName());
                        addData.setSubDeptGroup(proUser.getSubDeptGroup());
                        addData.setSubDeptGroupName(proUser.getSubDeptGroupName());
                        addData.setCraftPost(proUser.getCraftPost());
                        addData.setCraftType(proUser.getCraftType());
                        if(device.getDirection()==0) {
                            addData.setInTime(showTime);
                            addData.setInData(data);
                            addData.setInPhoto(filePath);
                        }else{
                            addData.setOutPhoto(filePath);
                            addData.setOutData(data);
                            addData.setOutTime(showTime);
                        }
                        addData.setDeviceNo(deviceNo);
                        attendanceUbiDataService.insertAttendanceUbiData(addData);
                        return AjaxResult.success("增加成功");
                    }
                }
            }else{
                //修改操作
                AttendanceUbiData upData= list.get(0);
                if(upData.getEventGuid().equals(eventGuid)){
                    return  AjaxResult.error("此记录已入库!");
                }
                Date dt1=upData.getInTime();
                Date dt2=upData.getOutTime();
                if(device.getDirection()==0){
                    //入
                    if(dt1==null || showTime.getTime()<dt1.getTime()){
                        upData.setInPhoto(filePath);
                        upData.setInData(data);
                        upData.setInTime(showTime);
                    }else{
                        return  AjaxResult.success();
                    }
                }else{
                    //出
                    if(dt2==null || showTime.getTime()>dt2.getTime()){
                        upData.setOutData(data);
                        upData.setOutPhoto(filePath);
                        upData.setOutTime(showTime);
                    }else {
                        return  AjaxResult.success();
                    }
                }
                attendanceUbiDataService.updateAttendanceUbiData(upData);
                return AjaxResult.success("更新成功");
            }

        }
        return AjaxResult.error("同步失败");
    }
}
