package com.huiju.estateagents.property.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.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.dao.TpConfigurationMapper;
import com.huiju.estateagents.property.dao.TpEquipmentMapper;
import com.huiju.estateagents.property.dao.TpEquipmentTreeMapper;
import com.huiju.estateagents.property.model.TpConfiguration;
import com.huiju.estateagents.property.model.TpEquipment;
import com.huiju.estateagents.property.model.TpEquipmentTree;
import com.huiju.estateagents.property.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(TaUser userElement, Integer equipmentType) {
        ResponseBean responseBean = new ResponseBean();
        List<TpEquipment> tpEquipments = tpEquipmentMapper.selectAllByCommunityAndEquipmentType(userElement.getOrgId(), equipmentType);
        responseBean.addSuccess(tpEquipments);
        return responseBean;
    }

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

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

        TpEquipment equipment = JSONObject.parseObject(parameter, TpEquipment.class);
        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.getOrgId(), "监控地址" , address, userElement.getUserId());
                // 监控url
                TpConfiguration tpConfigurationUrl = insertConfigguration(userElement.getOrgId(), "监控", url, userElement.getUserId());
                // 保安人员号码
                TpConfiguration tpConfigurationSecurityGuardTel = insertConfigguration(userElement.getOrgId(), "保安电话" , securityGuardTel, userElement.getUserId());
                // 保安室号码
                TpConfiguration tpConfigurationSecurityRoomTel = insertConfigguration(userElement.getOrgId(), "安保室电话" , securityRoomTel, userElement.getUserId());

                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.getUserId());
                equipment.setCommunityId(userElement.getOrgId());

                tpEquipmentMapper.insert(equipment);
            } else {
                updateConfiguration(equipment.getAddress(), equipment.getAddressId());
                updateConfiguration(equipment.getUrl(), equipment.getUrlId());
                updateConfiguration(equipment.getSecurityGuardTel(), equipment.getSecurityGuardTelId());
                updateConfiguration(equipment.getSecurityRoomTel(), equipment.getSecurityRoomTelId());
                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;
    }

    @Override
    public ResponseBean getMonitoringById(TaUser userElement, Integer type, Integer id) {
        ResponseBean responseBean = new ResponseBean();
        TpEquipment monitoring = tpEquipmentMapper.getMonitoringById(userElement.getOrgId(), type, id);
        responseBean.addSuccess(monitoring);
        return responseBean;
    }

    @Override
    public ResponseBean deleteBachEquipmentById(List<Integer> ids) {
        ResponseBean responseBean = new ResponseBean();
        this.removeByIds(ids);
        responseBean.addSuccess("操作成功！");
        return responseBean;
    }
}
