package com.securitycontrol.screen.service.impl;

import com.securitycontrol.common.core.utils.uuid.IdUtils;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.entity.screen.dto.PersonnelControlDto;
import com.securitycontrol.entity.screen.vo.PersonnelControlVo;
import com.securitycontrol.screen.mapper.PersonnelControlMapper;
import com.securitycontrol.screen.service.PersonnelControlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Lenovo
 */
@Slf4j
@Service(value = "PersonnelControlService")
public class PersonnelControlImpl implements PersonnelControlService {

    @Resource(name = "PersonnelControlMapper")
    private PersonnelControlMapper mapper;


    /**
     * 人员管控-穿戴装备
     *
     * @param dto
     * @return AjaxResult
     * @description
     * @author jjLV
     * @date 2024/3/27 17:23
     */
    @Override
    public List<PersonnelControlVo> getWearEquipmentList(PersonnelControlDto dto) {
        return mapper.getWearEquipmentList(dto);
    }

    /**
     * 人员管控-人员列表
     *
     * @param dto
     * @return AjaxResult
     * @description
     * @author jjLV
     * @date 2024/3/27 17:23
     */
    @Override
    public List<PersonnelControlVo> getPersonnelList(PersonnelControlDto dto) {
        return mapper.getPersonnelList(dto);
    }

    /**
     * 人员管控-人员运动趋预警
     *
     * @param dto
     * @return AjaxResult
     * @description
     * @author jjLV
     * @date 2024/3/27 17:23
     */
    @Override
    public List<PersonnelControlVo> getPersonnelMovementList(PersonnelControlDto dto) {
        return mapper.getPersonnelMovementList(dto);
    }

    /**
     * 人员管控-围栏新增
     *
     * @param dto
     * @return AjaxResult
     * @description
     * @author jjLV
     * @date 2024/3/27 17:23
     */
    @Override
    public int addNewFence(PersonnelControlDto dto) {
        String UUID = IdUtils.getUUId();
        dto.setUUID(UUID);
        int result = mapper.addNewFence(dto);
        int insertResult = 0;
        if (result > 0) {
            String[] splitArr = dto.getArrayField().split(",");
            for (int i = 0; i < splitArr.length; i++) {
                String[] coordinates = splitArr[i].split("@");
                dto.setLatitude(coordinates[1]);
                dto.setLongitude(coordinates[0]);
                insertResult = mapper.insertCoordinates(dto);

            }
        }
        return insertResult;
    }

    /**
     * 人员管控-围栏列表
     *
     * @param dto
     * @return AjaxResult
     * @description
     * @author jjLV
     * @date 2024/3/27 17:23
     */
    @Override
    public List<PersonnelControlVo> getNewFenceList(PersonnelControlDto dto) {
        return mapper.getNewFenceList(dto);
    }

    /**
     * 人员管控-状态更新
     *
     * @param dto
     * @return AjaxResult
     * @description
     * @author jjLV
     * @date 2024/3/27 17:23
     */
    @Override
    public AjaxResult subcontractorSwitchListen(PersonnelControlDto dto) {
        try {
            mapper.subcontractorSwitchListen(dto);
        } catch (Exception e) {
            log.error("更新围栏状态失败", e);
            return AjaxResult.error("更新围栏状态失败：" + e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 人员管控-删除围栏
     *
     * @param dto
     * @return AjaxResult
     * @description
     * @author jjLV
     * @date 2024/3/27 17:23
     */
    @Override
    public AjaxResult delSubcontractorById(PersonnelControlDto dto) {
        try {
            mapper.delSubcontractorById(dto);
        } catch (Exception e) {
            log.error("删除围栏失败", e);
            return AjaxResult.error("删除围栏失败：" + e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 人员管控-复现围栏
     *
     * @param dto
     * @return AjaxResult
     * @description
     * @author jjLV
     * @date 2024/3/27 17:23
     */
    @Override
    public List<PersonnelControlVo> getReplicaFence(PersonnelControlDto dto) {

        return mapper.getReplicaFence(dto);
    }

    /**
     * 人员管控-人员坐标点
     *
     * @param dto
     * @return AjaxResult
     * @description
     * @author jjLV
     * @date 2024/3/27 17:23
     */
    @Override
    public List<PersonnelControlVo> personnelCoordinatePoint(PersonnelControlDto dto) {
        return mapper.personnelCoordinatePoint(dto);
    }

    /**
     * 人员管控-人员历史坐标点
     *
     * @param dto
     * @return AjaxResult
     * @description
     * @author jjLV
     * @date 2024/3/27 17:23
     */
    @Override
    public List<PersonnelControlVo> PersonnelHistoricalCoordinates(PersonnelControlDto dto) {
        return mapper.PersonnelHistoricalCoordinates(dto);
    }
}