package com.zhgd.xmgl.async;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhgd.xmgl.modules.basicdata.entity.AttendanceDev;
import com.zhgd.xmgl.modules.basicdata.entity.AttendanceDevSync;
import com.zhgd.xmgl.modules.basicdata.entity.SystemDictData;
import com.zhgd.xmgl.modules.basicdata.entity.WorkerInfo;
import com.zhgd.xmgl.modules.basicdata.mapper.AttendanceDevMapper;
import com.zhgd.xmgl.modules.basicdata.service.IAttendanceDevSyncService;
import com.zhgd.xmgl.modules.basicdata.service.ISystemDictDataService;
import com.zhgd.xmgl.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 考勤相关信息异步任务
 **/
@Slf4j
@Component
public class AsyncAttendance {

    @Autowired
    private IAttendanceDevSyncService attendanceDevSyncService;

    @Autowired
    private AttendanceDevMapper attendanceDevMapper;

    @Autowired
    private ISystemDictDataService systemDictDataService;

    /**
     * 同步考勤设备信息
     * @param requestUrl
     * @param attendanceDev
     */
    @Async("countAttendanceExecutor")
    public void syncAttendanceDev(String requestUrl, AttendanceDev attendanceDev) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(attendanceDev));
            // 推送同步信息
            String result = HttpUtil.doPost(requestUrl, jsonObject.toString());
            int state = result == null ? 0 : 1;
            // 记录推送信息
            AttendanceDevSync attendanceDevSync = new AttendanceDevSync();
            attendanceDevSync.setMainId(attendanceDev.getCode());
            attendanceDevSync.setRequestUrl(requestUrl);
            attendanceDevSync.setContent(jsonObject.toString());
            attendanceDevSync.setState(state);
            attendanceDevSync.setType(1);
            attendanceDevSyncService.save(attendanceDevSync);
        }catch (Exception e){
            e.printStackTrace();
            log.error("推送考勤设备数据出现异常,请求路径:" + requestUrl + ",设备ID:" + attendanceDev.getId());
        }
    }

    /**
     * 同步考勤人员信息
     * @param workerInfo
     */
    @Async("countAttendanceExecutor")
    public void syncAttendanceWorker(String type, WorkerInfo workerInfo) {
        try {
            // 查询人员项目/工程对应的设备厂商信息
            List<AttendanceDev> attendanceDevList = attendanceDevMapper.selectList(Wrappers.<AttendanceDev>lambdaQuery().eq(AttendanceDev::getProjectSn, workerInfo.getProjectSn())
                    .eq(AttendanceDev::getEngineeringSn, workerInfo.getEngineeringSn()));
            List<SystemDictData> data = systemDictDataService.list(Wrappers.<SystemDictData>lambdaQuery()
                    .eq(SystemDictData::getDictType, type));
            for (AttendanceDev attendanceDev : attendanceDevList) {
                List<SystemDictData> collect = data.stream().filter(d -> d.getDictLabel() == attendanceDev.getProduceId()).collect(Collectors.toList());
                if (collect.size() == 0) {
                    continue;
                }
                String requestUrl = collect.get(0).getRemark();
                JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(workerInfo));
                // 推送同步信息
                String result = HttpUtil.doPost(requestUrl, jsonObject.toString());
                int state = 1;
                if (result == null) {
                    //请求未成功,记录状态
                    state = 0;
                }
                // 记录推送信息
                AttendanceDevSync attendanceDevSync = new AttendanceDevSync();
                attendanceDevSync.setMainId(workerInfo.getId().toString());
                attendanceDevSync.setRequestUrl(requestUrl);
                attendanceDevSync.setContent(jsonObject.toString());
                attendanceDevSync.setState(state);
                attendanceDevSync.setType(2);
                attendanceDevSyncService.save(attendanceDevSync);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("推送考勤人员数据出现异常");
        }
    }
}
