package org.springblade.modules.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.app.client.ZmtClientService;
import org.springblade.modules.app.common.dto.UserDTO;
import org.springblade.modules.app.common.dto.VerifyStationDTO;
import org.springblade.modules.app.common.query.UserQuery;
import org.springblade.modules.app.common.vo.OutApiSyncCheckPointResultVO;
import org.springblade.modules.app.entity.VerifyStationDO;
import org.springblade.modules.app.mapper.VerifyStationMapper;
import org.springblade.modules.app.service.IAppUserService;
import org.springblade.modules.app.service.IVerifyStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 检疫站信息 服务实现类
 * </p>
 *
 * @author fuxinsheng
 * @since 2021-11-08
 */
@Service
public class VerifyStationServiceImpl extends ServiceImpl<VerifyStationMapper, VerifyStationDO> implements IVerifyStationService {

    @Autowired
    private ZmtClientService zmtClientService;

    @Autowired
    private IAppUserService appUserService;

    @Override
    public VerifyStationDTO getVerifyById(Long verifyId) {
        VerifyStationDO verifyStationDO = getById(verifyId);
        return verifyStationDO == null ? null : verifyStationDO.copyTo(VerifyStationDTO.class);
    }

    public void updateVerifyStation() {
        UserQuery userQuery = new UserQuery();
        userQuery.setType(1);
        List<UserDTO> userDTOS = appUserService.listUser(userQuery);
        for (UserDTO userDTO : userDTOS) {
            if (!StringUtil.isEmpty(userDTO.getZmtId()) && userDTO.getZmtId() != 0) {
                List<OutApiSyncCheckPointResultVO> list = zmtClientService.getCheckPoint(userDTO.getZmtId());
                for (OutApiSyncCheckPointResultVO check : list) {
                    VerifyStationDTO exist = this.getVerifyById(check.getCheckpointId());
                    if (StringUtil.isEmpty(exist)) {
                        VerifyStationDO verifyStationDO = new VerifyStationDO();
                        verifyStationDO.setName(check.getCheckpointName());
                        verifyStationDO.setId(check.getCheckpointId());
                        verifyStationDO.setAddress(check.getAddress());
                        verifyStationDO.setRoad("");
                        verifyStationDO.setPhone("00");
                        this.save(verifyStationDO);
                    }

                }
            }
        }

    }
}
