package com.securitycontrol.inter.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.securitycontrol.common.core.constant.HttpStatus;
import com.securitycontrol.common.core.constant.SecurityConstants;
import com.securitycontrol.common.core.domain.Result;
import com.securitycontrol.common.core.utils.aes.ListHelper;
import com.securitycontrol.common.core.utils.aes.StringHelper;
import com.securitycontrol.common.core.utils.uuid.IdUtils;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.entity.system.SystemGlobal;
import com.securitycontrol.inter.mapper.BdMapper;
import com.securitycontrol.inter.vo.*;
import com.securitycontrol.system.api.RemoteFileService;
import com.securitycontrol.system.api.RemoteSourceService;
import com.securitycontrol.system.api.domain.SysFile;
import com.securitycontrol.system.api.domain.TbSourceFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * 边带业务处理层
 * @author 黑子
 */
@Service
@Slf4j
public class BdServiceImpl {

    @Autowired
    private BdMapper mapper;

    @Resource
    private RemoteSourceService remoteSourceService;
    @Resource
    private RemoteFileService remoteFileService;
    /**
     *
     * @param param
     * @return
     */
    public List<ProjectVo> getProjectByName(InterfaceParamVo param) {
        return mapper.getProjectByName(param);
    }

    public List<SingleProVo> getPepsinInfoByPrcNo(InterfaceParamVo param) {
        return mapper.getPepsinInfoByPrcNo(param);
    }

    /**
     * 查询标段工程信息
     * @param param
     * @return
     */
    public List<BidProjectVo> getSGTenderListBySinList(InterfaceParamVo param) {
                param.setSignList(ListHelper.asList(param.getSingleNoList(),","));
            return mapper.getSGTenderListBySinList(param);
    }

    /**
     * 查询人员信息集合
     * @param param
     * @return
     */
    public List<UserInfoVo> getPeopleInfo(InterfaceParamVo param) {
        List<UserInfoVo> list = mapper.getPeopleInfo(param);
        list.forEach(vo->{
            Result<List<TbSourceFile>> result = remoteSourceService.getSourceFile(vo.getUserId(), SystemGlobal.USER_IMAGE, SecurityConstants.INNER);
            if (result != null && result.getCode() == HttpStatus.SUCCESS && result.getData() != null && result.getData().size() > SystemGlobal.MIN_NUM) {
                List<TbSourceFile> list2 = result.getData();
                Result<SysFile> result2 = remoteFileService.getImgBase64(list2.get(0).getFileId(), SecurityConstants.INNER);
                if (result2 != null && result2.getCode() == HttpStatus.SUCCESS && result2.getData() != null) {
                    String jsonString = JSON.toJSONString(result2.getData());
                    JSONObject item = JSON.parseObject(jsonString);
                    String base64 = item.getString("url");
                    vo.setWorkerPhote(base64);
                }
            }
        });
        return  list;
    }

    /**
     * 边带项目关联 工程
     * @param param
     * @return
     */
    public AjaxResult gatewayDeviceBind(InterfaceParamVo param) {
        int num=mapper.bindBdPro(param);
        if(num>SystemGlobal.MIN_NUM){
          return   AjaxResult.success(1);
        }
        return   AjaxResult.success(0);
    }

    /**
     *
     * @param param
     * @return
     */
    public AjaxResult deviceAdd(DeviceInfoVo param) {
            int num=mapper.getDeviceNumById(param);
        if(num>SystemGlobal.MIN_NUM){
            return   AjaxResult.success(DevResultVo.error(param.getDeviceId(),"设备已存在"));
        }
        int num2=mapper.insertData(param);
        if(num2>SystemGlobal.MIN_NUM){
            return   AjaxResult.success(DevResultVo.success(param.getDeviceId(),"新增成功"));
        }
        return   AjaxResult.success(DevResultVo.error(param.getDeviceId(),"新增失败,参数不正确"));
    }

    /**
     * 删除设备部信息
     * @param param
     * @return
     */
    public AjaxResult deviceDel(InterfaceParamVo param) {
        int num=mapper.deviceDel(param);
        if(num>SystemGlobal.MIN_NUM){
            return   AjaxResult.success(1);
        }
        return   AjaxResult.success(0);
    }

    public AjaxResult deviceStatus(DeviceInfoVo param) {
        int num=mapper.deviceStatus(param);
        if(num>SystemGlobal.MIN_NUM){
            return   AjaxResult.success(1);
        }
        return   AjaxResult.success(0);
    }

    /**
     * 告警信息入库
     * @param param
     * @return
     */
    public AjaxResult devWarn(WarnInfoVo param) {
        String bidCode=mapper.getBidCodeByBd(param);
        param.setBidCode(bidCode);
        int num=mapper.insertWarn(param);
        if(num>SystemGlobal.MIN_NUM){
            return   AjaxResult.success(1);
        }
        return   AjaxResult.success(0);

    }

    /**
     * 设备采集信息上报
     * @param param
     * @return
     */
    public AjaxResult devInfo(DeviceInfoVo param) {
        try{
            String id=param.getDeviceId();
            //json数据
            String json=param.getData();





            JSONObject data=JSON.parseObject(json);
            String isWarn=param.getIsWarn();
            if(StringHelper.isEmpty(isWarn)){
                isWarn=SystemGlobal.WARN_DEFEAT;
            }
            //获取全部key
            Set<String> keys=data.keySet();
            String finalIsWarn = isWarn;
            keys.forEach(key->{
                //查询系统的id
                SelectDataVo  selectDataVo=mapper.getBdDeviceDetailByIdCode(id,key);
                //查询存储值
                String value=data.getString(key);
                selectDataVo.setValue(value);
                selectDataVo.setChangeValue(getChangeValue(selectDataVo));
                selectDataVo.setSourceData(json);
                selectDataVo.setIsWarn(finalIsWarn);
                mapper.insertValue(selectDataVo);
                selectDataVo.setId(IdUtils.getUUId());
                mapper.insertHistoryValue(selectDataVo);
            });


        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return null;
    }


    /**
     * 数据处理
     * @param vo
     * @return
     */
    public  String getChangeValue(SelectDataVo vo){
        try{
            if(StringHelper.isNotEmpty(vo.getDataType())){
                switch (vo.getDataType()){
                    case SystemGlobal.FLOAT_TYPE:
                        if(StringHelper.isNotEmpty(vo.getHistoryValue())){
                            BigDecimal  d1=new BigDecimal(vo.getValue());
                            BigDecimal  d2=new BigDecimal(vo.getHistoryValue());
                            BigDecimal resultSub = d1.subtract(d2);
                            return  resultSub.toString();
                        }
                        return vo.getValue();
                    default:
                        return vo.getValue();
                }
            }
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return "";
    }




}
