package com.bonus.bracelet.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bonus.bracelet.mapper.QueryConstructionMapper;
import com.bonus.bracelet.service.IQueryConstructionService;
import com.bonus.common.core.constant.BusinessConstants;
import com.bonus.common.core.constant.HttpStatus;
import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.domain.R;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.entity.bracelet.vo.BaseProject;
import com.bonus.common.entity.bracelet.vo.PersonVo;
import com.bonus.common.entity.bracelet.vo.QueryConstruction;

import com.bonus.common.entity.bracelet.vo.WarnInfoVo;
import com.bonus.system.api.RemoteFileService;
import com.bonus.system.api.domain.SysFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * 岗位信息 服务层处理
 * 
 * @author bonus
 */
@Service
@Slf4j
public class QueryConstructionServiceImpl implements IQueryConstructionService
{
    @Autowired
    private QueryConstructionMapper constructionMapper;

    @Resource
    private RemoteFileService remoteFileService;

    /**
     *
     * @param construction
     * @return
     */
    @Override
    public List<QueryConstruction> selectConstructionList(QueryConstruction construction)
    {
        List<QueryConstruction> list = constructionMapper.selectConstructionList(construction);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setTeamNum(constructionMapper.getTeamNum(list.get(i).getTeamId()));
            list.get(i).setTempNum(constructionMapper.getTempNum(list.get(i).getTeamId()));
        }
        for (QueryConstruction vo : list) {
            vo.setTeamPhone(Sm4Utils.decode(vo.getTeamPhone()));
        }
        return list;
    }

    /**
     * 班组人员设备列表
     * @param data
     * @return
     */
    @Override
    public List<PersonVo> getTeamLists(PersonVo data) {
        List<PersonVo> list = new ArrayList<>();
        data.setSourceType(BusinessConstants.RESOURCE_TYPE_USER);
        try {
            list = constructionMapper.getTeamLists(data);
            for (PersonVo vo : list) {
                vo = handleData(vo);
            }

        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return list;
    }

    /**
     * 临时人员设备列表
     * @param data
     * @return
     */
    @Override
    public List<PersonVo> getTempLists(PersonVo data) {
        List<PersonVo> list = new ArrayList<>();
        data.setSourceType(BusinessConstants.RESOURCE_TYPE_USERTEMP);
        try {
            list = constructionMapper.getTempLists(data);
            for (PersonVo vo : list) {
                vo = handleData(vo);
            }

        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return list;
    }

    public PersonVo handleData(PersonVo vo) {
        vo.setIdCard(Sm4Utils.decode(vo.getIdCard()));
        vo.setPhone(Sm4Utils.decode(vo.getPhone()));
        R<SysFile> result = remoteFileService.getImgBase64(vo.getFilePath(), SecurityConstants.INNER);
        if (result != null && result.getCode() == HttpStatus.SUCCESS && result.getData() != null) {
            String jsonString = JSON.toJSONString(result.getData());
            JSONObject item = JSON.parseObject(jsonString);
            String base64 = item.getString("url");
            vo.setBase64Url(base64);
        }
        return vo;
    }

    /**
     * 施工预警信息
     * @param data
     * @return
     */
    @Override
    public List<WarnInfoVo> getWarnLists(QueryConstruction data) {
        List<WarnInfoVo> list = new ArrayList<>();
        try {
            list = constructionMapper.getWarnLists(data);

        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return list;
    }

}
