package com.bonus.app.service;

import com.bonus.app.entity.*;
import com.bonus.app.mapper.StayPersonMapper;
import com.bonus.common.core.utils.*;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.entity.bracelet.vo.ProjectVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * app 接口实现层
 */
@Service
@Slf4j
public class StayPersonServiceImpl implements IStayPersonService{

    @Autowired
    private StayPersonMapper mapper;


    @Override
    public PageInfo<StayPersonEntity> getStayPersonList(ProjectVo vo) {
            List<StayPersonEntity> list = new ArrayList<>();
            StayPersonEntity bEntity = new StayPersonEntity();
            Map<String, Object> towerMap = new HashMap<>();
            try {
                list = mapper.getStayPersonList(vo);
                for (StayPersonEntity entity : list) {
                    Map<String, Object> map = new HashMap<>();
                    entity.setIdCard(Sm4Utils.decode(entity.getIdCard()));
                    entity.setPhone(Sm4Utils.decode(entity.getPhone()));
                    if(entity.getCertificateNum()>0){
                        entity.setIsCertificate(1);
                    }else{
                        entity.setIsCertificate(0);
                    }
                    towerMap.put("lon",String.valueOf(entity.getSgLon()));
                    towerMap.put("lat",String.valueOf(entity.getSgLat()));
                    String distance = mapper.getDistance();
                    Double distanceValue = 0D;
                    distanceValue = StringUtils.isEmpty(distance) ? 50 : Double.parseDouble(distance);
                    map.put("lon",entity.getLon());
                    map.put("lat",entity.getLat());
                    // 验证施工人员是否超出施工范围
                    Integer isAbnormal = isAbnormal(distanceValue, map, towerMap);
                    map.put("isWarn", isAbnormal);
                    entity.setDistance(distanceValue);
                    entity.setIsWarn((Integer) map.get("isWarn"));
                }
            } catch (Exception e) {
                log.error("查询现场人员列表", e);
            }
            PageInfo<StayPersonEntity> pageInfo = new PageInfo<>(list);
            return pageInfo;
        }

    /**
     * 班组人员/临时人员 是否超出正常施工范围
     *
     * @param distance
     * @param map
     * @return Integer
     */
    public Integer isAbnormal(Double distance, Map<String, Object> map, Map<String, Object> towerMap) {
        Integer isWarn = 0;
        if (map.get("lon") == null || map.get("lat") == null) {
            return isWarn;
        }
        Double lat = Double.parseDouble(String.valueOf(towerMap.get("lat")));
        Double lon = Double.parseDouble(String.valueOf(towerMap.get("lon")));
        Double sgLat = Double.parseDouble(String.valueOf(map.get("lat")));
        Double sgLon = Double.parseDouble(String.valueOf(map.get("lon")));
        double len = HaversineUtil.vincentyDistance(sgLat, sgLon, lat, lon);
        if (len > distance) {
            isWarn = 1;
        }
        return isWarn;
    }

//    /**
//     * 切换时获取工程列表
//     * @param proId
//     * @return
//     */
//    @Override
//    public AjaxResult getProjectList(Long proId) {
//        List<Map<String, Object>>  list = new ArrayList<>();
//        try {
//            list = mapper.getProjectList(proId);
//        } catch (Exception e) {
//            log.error(e.toString(), e);
//        }
//        return AjaxResult.success(list);
//    }

    /**
     * 修改安全帽、马甲编号
     * @param vo
     * @return
     */
    @Override
    public AjaxResult editCodeInfoList(ProjectVo vo) {
        Map<String, Object> map = new HashMap<>();
        try {
            if(StringUtils.isEmpty(vo.getAqmCode()))
            {
                return AjaxResult.error("安全帽编号不能为空，请重新修改");
            }
            if(StringUtils.isEmpty(vo.getMjCode()))
            {
                return AjaxResult.error("马甲编号不能为空，请重新修改");
            }
            ProjectVo num = mapper.isExistCodeAqm(vo);
            if(num!=null){
                return AjaxResult.error("安全帽编号重复，请重新修改");
            }
            ProjectVo num2 = mapper.isExistCodeMj(vo);
            if(num2!=null){
                return AjaxResult.error("马甲编号重复，请重新修改");
            }
            int num3 = mapper.editCodeInfoList(vo);
            if(num3==0){
                return AjaxResult.error("修改失败");
            }else{
                map.put("aqmCode",vo.getAqmCode());
                map.put("mjCode",vo.getMjCode());
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return AjaxResult.success(map);
    }

    /**
     * 单个人员进行人脸验证
     * @param vo
     * @return
     */
    @Override
    public AjaxResult singleRecognition(PeopleInfoEntity vo) {
         ShInfoEntity shData = new ShInfoEntity();
        try {
            shData = mapper.getShInfoList(vo);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return AjaxResult.success(shData);
    }

    /**
     * 多个人员进行人脸验证
     * @param data
     * @return
     */
    @Override
    public AjaxResult allRecognition(ProjectVo data) {
        List<PeopleInfoEntity> list = new ArrayList<>();
        ShInfoEntity shData = new ShInfoEntity();
        List<ShInfoEntity> listSh = new ArrayList<>();
        try {
            list = mapper.getStayPersonInfoList(data);
            if(list.size()!=0){
                for (PeopleInfoEntity vo : list) {
                    shData = mapper.getShInfoList(vo);
                    listSh.add(shData);
                }
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return AjaxResult.success(listSh);
    }
}
