package org.jeecg.modules.cabinet.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RestUtil;
import org.jeecg.modules.api.model.CabinetModel;
import org.jeecg.modules.api.model.SingleTool;
import org.jeecg.modules.api.service.CabinetApiService;
import org.jeecg.modules.cabinet.entity.Cabinet;
import org.jeecg.modules.cabinet.mapper.CabinetMapper;
import org.jeecg.modules.cabinet.service.ICabinetService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDepartRole;
import org.jeecg.modules.system.entity.SysDepartRoleUser;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysDepartRoleMapper;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.tool.entity.Tool;
import org.jeecg.modules.tool.mapper.ToolMapper;
import org.jeecg.modules.toolSheet.entity.ToolSheet;
import org.jeecg.modules.toolSheet.mapper.ToolSheetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.*;

/**
 * @Description: 智能工具柜
 * @Author: jeecg-boot
 * @Date:   2025-01-15
 * @Version: V1.0
 */
@Service
public class CabinetServiceImpl extends ServiceImpl<CabinetMapper, Cabinet> implements ICabinetService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private CabinetApiService cabinetApi;

    @Autowired
    private ToolSheetMapper toolSheetMapper;
    @Autowired
    private ToolMapper toolMapper;
    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private ISysUserService sysUserService;

    @Override
    public void sync(Cabinet cabinet) {
        String departName = "大丰车辆段";
        List<SysUser> users = sysUserMapper.getUser(departName);
        List<CabinetModel> cabinetModels = new ArrayList<>();
        for (SysUser user : users) {
            CabinetModel cabinetModel = new CabinetModel();
            cabinetModel.setUserId(user.getId());
            cabinetModel.setUserName(user.getUsername());
            cabinetModel.setNickName(user.getRealname());
            cabinetModel.setFaceUrl("");
            cabinetModel.setFaceImgStr("");
//            LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<>();
//            query.eq(SysDepart::getOrgCode, user.getOrgCode());
//            SysDepart sysDepart = sysDepartService.getOne(query);
//            cabinetModel.setOrgCode(sysDepart.getOrgCode());
//            cabinetModel.setDepartId(sysDepart.getId());
//            cabinetModel.setDepartName(sysDepart.getDepartName());
            //100 对应我worker
            cabinetModel.setRoles(new ArrayList<>(Arrays.asList("100")));
//            if(StringUtils.isNotBlank(user.getFaceUrl())) {
//                cabinetModel.setFaceImgStr(convertImageToBase64(user.getFaceUrl()));
//            }
            cabinetModels.add(cabinetModel);
        }
        cabinetApi.sendMessages(cabinet, cabinetModels,"receiveUsers");

        //todo 同步工具
        List<SingleTool> tools = new ArrayList<>();
        List<ToolSheet> toolSheets = toolSheetMapper.selectList(new LambdaQueryWrapper<ToolSheet>()
                .eq(ToolSheet::getCabinet, cabinet.getId()));
        for (ToolSheet toolSheet : toolSheets) {
            SingleTool tool = new SingleTool();
            tool.setToolId(toolSheet.getId());
            tool.setToolCode(toolSheet.getCode());
            tool.setRfid(StringUtils.isNotEmpty(toolSheet.getRfid())? toolSheet.getRfid() : "");
            Tool toolType = toolMapper.selectById(toolSheet.getToolId());
            if(toolType == null){
                continue;
            }
            tool.setToolName(toolType.getName() + "(" + toolSheet.getModel() + ")");
            tool.setToolType(toolSheet.getToolId());
            tool.setStatus(toolSheet.getStatus());
            if(toolType.getCheckRequire() == "1"){
                tool.setCalibrationTime(DateUtil.offsetDay(toolSheet.getLastCheckDate(), toolType.getCheckCycle()));
            }
            tools.add(tool);
        }
        cabinetApi.sendToolMessages(cabinet, tools);

        cabinet.setLastSyncTime(DateTime.now());
        updateById(cabinet);
    }

}
