package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysInterfaceForeign;
import com.ruoyi.system.mapper.SysInterfaceForeignMapper;
import com.ruoyi.system.utils.IpUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AkmDataMapper;
import com.ruoyi.system.domain.AkmData;
import com.ruoyi.system.service.IAkmDataService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 安康码扫码地点Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-10
 */
@Service
public class AkmDataServiceImpl implements IAkmDataService 
{
    @Autowired
    private AkmDataMapper akmDataMapper;

    @Autowired
    private SysInterfaceForeignMapper sysInterfaceForeignMapper;

    /**
     * 查询安康码扫码地点
     * 
     * @param id 安康码扫码地点主键
     * @return 安康码扫码地点
     */
    @Override
    public AkmData selectAkmDataById(Long id)
    {
        return akmDataMapper.selectAkmDataById(id);
    }


    /**
     * 查询安康码扫码地点列表
     * 
     * @param akmData 安康码扫码地点
     * @return 安康码扫码地点
     */
    @Override
    public List<AkmData> selectAkmDataList(AkmData akmData)
    {
        return akmDataMapper.selectAkmDataList(akmData);
    }

    @Override
    public AjaxResult getForeignList(HttpServletRequest request, String secretKey, AkmData akmData) {

        //根据秘钥查询配置信息
        SysInterfaceForeign sysInterfaceForeign = sysInterfaceForeignMapper.getForeignByKey(secretKey);
        if(sysInterfaceForeign==null){
            return AjaxResult.error("秘钥错误");
        }


        //获取客户端ip
        System.out.println("访问接口ip--------------------------------------------------------");
        String ip = IpUtils.getIp2(request);
        System.out.println("访问接口ip---"+ip);
        if(!sysInterfaceForeign.getIp().contains(ip)){
            return AjaxResult.error("未授权ip");
        }


        //获取授权的设备id
        List<String> equipIdList = Arrays.asList(sysInterfaceForeign.getEquipId().split(","));
        if(equipIdList==null || equipIdList.size()<=0){
            return AjaxResult.success("无授权设备");
        }

        //判断是否还有次数
        if(sysInterfaceForeign.getRemainingNumber()==0){
            return AjaxResult.success("当天查询次数已用完");
        }
        akmData.setEquipIdList(equipIdList);
        List<AkmData> akmDataList = akmDataMapper.selectAkmDataList(akmData);

        if(sysInterfaceForeign.getRemainingNumber()>0){
            sysInterfaceForeign.setRemainingNumber(sysInterfaceForeign.getRemainingNumber()-1);
            sysInterfaceForeignMapper.updateSysInterfaceForeign(sysInterfaceForeign);
        }
        return AjaxResult.success(akmDataList);
    }

    /**
     * 新增安康码扫码地点
     * 
     * @param akmData 安康码扫码地点
     * @return 结果
     */
    @Override
    public AjaxResult insertAkmData(AkmData akmData)
    {
        try {

            if(akmData.getAkmDataList()!=null && akmData.getAkmDataList().size()>0){
                for (AkmData data : akmData.getAkmDataList()) {
                    if(StringUtils.isEmpty(data.getName())){
                        AkmData akm = akmDataMapper.selectAkmDataName(data.getIdCard());
                        if(akm!=null && StringUtils.isEmpty(akm.getName())){
                            data.setName(akm.getName());
                        }
                    }
                    akmDataMapper.insertAkmData(data);
                }
            }

            if(StringUtils.isNotEmpty(akmData.getIdCard())){
                if(akmData.getIdCard().length()>18){
                    return AjaxResult.error("身份证格式错误");
                }
                akmDataMapper.insertAkmData(akmData);
            }

            return AjaxResult.success("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }

    }

    /**
     * 修改安康码扫码地点
     * 
     * @param akmData 安康码扫码地点
     * @return 结果
     */
    @Override
    public int updateAkmData(AkmData akmData)
    {
        return akmDataMapper.updateAkmData(akmData);
    }

    /**
     * 批量删除安康码扫码地点
     * 
     * @param ids 需要删除的安康码扫码地点主键
     * @return 结果
     */
    @Override
    public int deleteAkmDataByIds(Long[] ids)
    {
        return akmDataMapper.deleteAkmDataByIds(ids);
    }

    /**
     * 删除安康码扫码地点信息
     * 
     * @param id 安康码扫码地点主键
     * @return 结果
     */
    @Override
    public int deleteAkmDataById(Long id)
    {
        return akmDataMapper.deleteAkmDataById(id);
    }

}
