package com.huimv.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.admin.common.token.TokenSign;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.EnvWarningThreshold;
import com.huimv.admin.entity.GasThreshold;
import com.huimv.admin.mapper.GasThresholdMapper;
import com.huimv.admin.mapper.SysAccountMultilevelMapper;
import com.huimv.admin.service.IGasThresholdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import com.huimv.admin.common.utils.Result;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 气体阈值 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-21
 */
@Service
public class GasThresholdServiceImpl extends ServiceImpl<GasThresholdMapper, GasThreshold> implements IGasThresholdService {

    @Autowired
    private GasThresholdMapper gasThresholdMapper;
    @Autowired
    SysAccountMultilevelMapper sysAccountMultilevelMapper;
    @Override
    public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");

        Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
        List<Integer> userIds =sysAccountMultilevelMapper.getLowerLevel( userId,farmId);
        List<Integer> collect =new ArrayList<>();
        QueryWrapper<GasThreshold> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        List<GasThreshold> gasThresholds = gasThresholdMapper.selectList(queryWrapper);
        String userIds1 = "";
        if (ObjectUtil.isNotEmpty(gasThresholds)){
            userIds1 = gasThresholds.get(0).getUserIds();
        }
        if (StringUtils.isNotBlank(userIds1)){
            collect = Arrays.stream(userIds1.split(",")).map(Integer::valueOf).filter(userIds::contains).collect(Collectors.toList());
        }
        JSONObject jsonObject = new JSONObject();
        if (gasThresholds.size() == 0) {
            jsonObject.put("pigpen", getProtThreshold());
            jsonObject.put("above", getProtThreshold());
            jsonObject.put("under", getProtThreshold());
            jsonObject.put("people", getProtThreshold());

        } else {
            for (GasThreshold gasThreshold : gasThresholds) {
                if (gasThreshold.getGasType() == 1) {
                    jsonObject.put("pigpen", gasThreshold);
                }
                else if (gasThreshold.getGasType() == 2) {
                    jsonObject.put("above", gasThreshold);
                }
                else if (gasThreshold.getGasType() == 3) {
                    jsonObject.put("under", gasThreshold);
                }
                else if (gasThreshold.getGasType() == 4) {
                    jsonObject.put("people", gasThreshold);
                }
            }
            jsonObject.put("userIds", collect);
        }
        return new Result(ResultCode.SUCCESS,jsonObject);
    }
    private JSONObject getProtThreshold() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("nh3N", 0);
        jsonObject.put("jlm", 0);
        jsonObject.put("elht", 0);
        jsonObject.put("ejel", 0);
        jsonObject.put("h2s", 0);
        jsonObject.put("byx", 0);
        jsonObject.put("ch3sh", 0);
        jsonObject.put("sja", 0);
        jsonObject.put("cq", 0);
        return jsonObject;
    }
    @Override
    public Result edit(HttpServletRequest httpServletRequest, List<GasThreshold> gasThresholds) {
        for (GasThreshold gasThreshold : gasThresholds) {
            if (ObjectUtil.isNotEmpty(gasThreshold)) {
                Integer farmId = gasThreshold.getFarmId();
                String userIds = gasThreshold.getUserIds();
                QueryWrapper<GasThreshold> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("farm_id", gasThreshold.getFarmId()).eq("gas_type", gasThreshold.getGasType());
                GasThreshold gasThreshold2 = gasThresholdMapper.selectOne(queryWrapper);

                //去掉属于该人物的id
                Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
                //下属id
                List<Integer> lowerLevelIds = sysAccountMultilevelMapper.getLowerLevel( userId,farmId+"");
                //上传id
                List<Integer> uoloadIds = new ArrayList<>();
                if (StringUtils.isNotBlank(userIds)){
                    uoloadIds = Arrays.stream(userIds.split(",")).map(Integer::valueOf).collect(Collectors.toList());
                }
                //现有id
                String oldUserIds = gasThreshold2.getUserIds();
                List<Integer> oldUsersIds = new ArrayList<>();
                if (StringUtils.isNotBlank(oldUserIds)){
                    oldUsersIds = Arrays.stream(oldUserIds.split(",")).map(Integer::valueOf).collect(Collectors.toList());
                }
                // 现有 -下属 + 上传
                oldUsersIds.removeAll(lowerLevelIds);
                oldUsersIds.addAll(uoloadIds);

                GasThreshold gasThreshold1 = new GasThreshold();
                gasThreshold1.setGasType(gasThreshold.getGasType());
                gasThreshold1.setNh3N(gasThreshold.getNh3N());
                gasThreshold1.setJlm(gasThreshold.getJlm());
                gasThreshold1.setElht(gasThreshold.getElht());
                gasThreshold1.setH2s(gasThreshold.getH2s());
                gasThreshold1.setByx(gasThreshold.getByx());
                gasThreshold1.setCh3sh(gasThreshold.getCh3sh());
                gasThreshold1.setSja(gasThreshold.getSja());
                gasThreshold1.setCq(gasThreshold.getCq());
                gasThreshold1.setUserIds(oldUsersIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
                gasThreshold1.setFarmId(farmId);

                if (ObjectUtil.isEmpty(gasThreshold2)) {
                    gasThresholdMapper.insert(gasThreshold1);
                } else {
                    gasThreshold1.setId(gasThreshold.getId());
                    gasThresholdMapper.updateById(gasThreshold1);
                }
            }

        }
        return new Result(ResultCode.SUCCESS, "修改成功");
    }
}
