package com.community.huiju.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.dao.TpConfigurationMapper;
import com.community.huiju.dao.TpEquipmentMapper;
import com.community.huiju.dao.TpEquipmentTreeMapper;
import com.community.huiju.model.TpConfiguration;
import com.community.huiju.model.TpEquipment;
import com.community.huiju.model.TpEquipmentTree;
import com.community.huiju.service.ITpEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-04-15
 */
@Service
public class TpEquipmentServiceImpl extends ServiceImpl<TpEquipmentMapper, TpEquipment> implements ITpEquipmentService {

    @Autowired
    private TpEquipmentMapper tpEquipmentMapper;

    @Autowired
    private TpConfigurationMapper tpConfigurationMapper;

    @Autowired
    private TpEquipmentTreeMapper tpEquipmentTreeMapper;

    @Override
    public ResponseBean getAllByCommunityAndEquipmentType(UserElement userElement, Integer equipmentType) {
        ResponseBean responseBean = new ResponseBean();
        List<TpEquipment> tpEquipments = tpEquipmentMapper.selectAllByCommunityAndEquipmentType(userElement.getCommunityId(), equipmentType);
        responseBean.addSuccess(tpEquipments);
        return responseBean;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean saveOrUpdateMonitoring(String parameter, UserElement userElement) {
        ResponseBean responseBean = new ResponseBean();

        // 获取这个小区的根节点树
        QueryWrapper<TpEquipmentTree> equipmentTreeQueryWrapper = new QueryWrapper<>();
        equipmentTreeQueryWrapper.eq("community_id", userElement.getCommunityId());
        TpEquipmentTree tpEquipmentTree = tpEquipmentTreeMapper.selectOne(equipmentTreeQueryWrapper);

        List<TpEquipment> tpEquipments = JSONObject.parseArray(parameter, TpEquipment.class);
        for (int i = 0; i < tpEquipments.size(); i++){
            TpEquipment equipment = tpEquipments.get(i);
            Integer id = equipment.getId();
            if (null == id) {
                // 监控地址
                String address = equipment.getAddress();
                // 监控url
                String url = equipment.getUrl();
                // 保安人员号码
                String securityGuardTel = equipment.getSecurityGuardTel();
                // 保安室号码
                String securityRoomTel = equipment.getSecurityRoomTel();

                // 开始插入设备数据
                // 地址
                TpConfiguration tpConfigurationAddress = insertConfigguration(userElement.getCommunityId(), "监控地址" + i, address, userElement.getId());
                // 监控url
                TpConfiguration tpConfigurationUrl = insertConfigguration(userElement.getCommunityId(), "监控" + i, url, userElement.getId());
                // 保安人员号码
                TpConfiguration tpConfigurationSecurityGuardTel = insertConfigguration(userElement.getCommunityId(), "保安电话" + i, securityGuardTel, userElement.getId());
                // 保安室号码
                TpConfiguration tpConfigurationSecurityRoomTel = insertConfigguration(userElement.getCommunityId(), "安保室电话" + i, securityRoomTel, userElement.getId());

                equipment.setAddressId(tpConfigurationAddress.getId());
                equipment.setUrlId(tpConfigurationUrl.getId());
                equipment.setSecurityGuardTelId(tpConfigurationSecurityGuardTel.getId());
                equipment.setSecurityRoomTelId(tpConfigurationSecurityRoomTel.getId());
                equipment.setEquipmentType("1");
                equipment.setEquipmentTreeId(tpEquipmentTree.getId());
                equipment.setCreateTime(LocalDateTime.now());
                equipment.setCreateUser(userElement.getId());
                equipment.setCommunityId(userElement.getCommunityId());

                tpEquipmentMapper.insert(equipment);
            } else {
                updateConfiguration(equipment.getAddress(), equipment.getAddressId());
                updateConfiguration(equipment.getUrl(), equipment.getUrlId());
                updateConfiguration(equipment.getSecurityGuardTel(), equipment.getSecurityGuardTelId());
                updateConfiguration(equipment.getSecurityRoomTel(), equipment.getSecurityGuardTelId());
                tpEquipmentMapper.updateById(equipment);
            }
        }

        responseBean.addSuccess("操作成功！");
        return responseBean;
    }

    /**
     * 插入数据
     * @param communityId
     * @param name
     * @param value
     * @param userId
     * @return
     */
    private TpConfiguration insertConfigguration(Integer communityId, String name, String value, Integer userId) {

        TpConfiguration tpConfiguration = new TpConfiguration();
        tpConfiguration.setCommunityId(communityId);
        tpConfiguration.setConfigurationName(name);
        tpConfiguration.setConfigurationType(1);
        tpConfiguration.setConfigurationValue(value);
        tpConfiguration.setCreateTiem(LocalDateTime.now());
        tpConfiguration.setCreateUser(userId);
        tpConfigurationMapper.insert(tpConfiguration);

        return tpConfiguration;
    }

    /**
     * 根据id更新 设备配置表的值
     * @param value
     * @param configurationId
     */
    public void updateConfiguration(String value, Integer configurationId) {
        QueryWrapper<TpConfiguration> configurationQueryWrapper = new QueryWrapper<>();
        configurationQueryWrapper.eq("id", configurationId);
        TpConfiguration tpConfiguration = tpConfigurationMapper.selectOne(configurationQueryWrapper);
        tpConfiguration.setConfigurationValue(value);
        tpConfigurationMapper.updateById(tpConfiguration);
    }

    @Override
    public ResponseBean deleteEquipmentById(Integer equipmentId) {
        ResponseBean responseBean = new ResponseBean();
        tpEquipmentMapper.deleteById(equipmentId);
        responseBean.addSuccess("操作成功!");
        return responseBean;
    }
}
