package com.jsowell.pile.service.impl;

import com.google.common.collect.Lists;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.enums.DelFlagEnum;
import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.SecurityUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.ThirdPartyStationRelation;
import com.jsowell.pile.dto.ThirdPartyStationRelationDTO;
import com.jsowell.pile.mapper.ThirdPartyStationRelationMapper;
import com.jsowell.pile.service.ThirdPartyStationRelationService;
import com.jsowell.pile.vo.base.MerchantInfoVO;
import com.jsowell.pile.vo.base.StationInfoVO;
import com.jsowell.pile.vo.base.ThirdPartyStationRelationVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 站点、第三方推送平台配置对应Service业务层处理
 *
 * @author jsowell
 * @date 2023-06-06
 */
@Service
public class ThirdPartyStationRelationServiceImpl implements ThirdPartyStationRelationService {
    @Autowired
    private ThirdPartyStationRelationMapper thirdPartyStationRelationMapper;

    @Override
    public int insertInfo2DataBase(String thirdPartyType, String stationId) {
        if (StringUtils.isBlank(thirdPartyType) || StringUtils.isBlank(stationId)) {
            return 0;
        }
        ThirdPartyStationRelation relation = new ThirdPartyStationRelation();
        relation.setStationId(Long.parseLong(stationId));
        relation.setThirdPartyType(thirdPartyType);
        if (StringUtils.equals(thirdPartyType, ThirdPlatformTypeEnum.HUA_WEI.getTypeCode())) {
            relation.setStartMode(Constants.ONE);
        }
        ThirdPartyStationRelationVO vo = this.selectRelationInfo(relation);
        if (vo != null) {
            return 0;
        }
        // 新增数据库
        return this.insertThirdPartyStationRelation(relation);
    }

    /**
     * 查询站点、第三方推送平台配置对应
     *
     * @param id 站点、第三方推送平台配置对应主键
     * @return 站点、第三方推送平台配置对应
     */
    @Override
    public ThirdPartyStationRelation selectThirdPartyStationRelationById(Long id) {
        return thirdPartyStationRelationMapper.selectThirdPartyStationRelationById(id);
    }

    /**
     * 查询站点、第三方推送平台配置对应列表
     *
     * @param thirdPartyStationRelation 站点、第三方推送平台配置对应
     * @return 站点、第三方推送平台配置对应
     */
    @Override
    public List<ThirdPartyStationRelation> selectThirdPartyStationRelationList(ThirdPartyStationRelation thirdPartyStationRelation) {
        return thirdPartyStationRelationMapper.selectThirdPartyStationRelationList(thirdPartyStationRelation);
    }

    @Override
    public List<ThirdPartyStationRelation> selectRelationListByStationId(String stationId) {
        return thirdPartyStationRelationMapper.selectRelationListByStationId(stationId);
    }

    @Override
    public List<ThirdPartyStationRelation> selectThirdPartyStationRelationList(String thirdPlatformType) {
        ThirdPartyStationRelation thirdPartyStationRelation = new ThirdPartyStationRelation();
        thirdPartyStationRelation.setThirdPartyType(thirdPlatformType);
        return this.selectThirdPartyStationRelationList(thirdPartyStationRelation);
    }

    /**
     * 通过站点id查询相关配置信息
     * @param stationId 站点id String类型
     * @return 相关配置信息
     */
    @Override
    public ThirdPartyStationRelationVO selectRelationInfo(String stationId) {
        return selectRelationInfo(Long.parseLong(stationId));
    }

    /**
     * 通过站点id查询相关配置信息
     * @param stationId 站点id Long类型
     * @return 相关配置信息
     */
    @Override
    public ThirdPartyStationRelationVO selectRelationInfo(long stationId) {
        ThirdPartyStationRelation relation = new ThirdPartyStationRelation();
        relation.setStationId(stationId);
        return selectRelationInfo(relation);
    }

    /**
     * 查询站点、第三方推送平台配置
     * @param thirdPartyStationRelation
     * @return
     */
    @Override
    public ThirdPartyStationRelationVO selectRelationInfo(ThirdPartyStationRelation thirdPartyStationRelation) {
        return thirdPartyStationRelationMapper.selectRelationInfo(thirdPartyStationRelation);
    }

    /**
     * 通过站点id查询该站点对接了哪些第三方平台
     * @param stationId
     * @return
     */
    @Override
    public List<ThirdPartyStationRelationVO> getRelationInfoList(String stationId) {
        return thirdPartyStationRelationMapper.getRelationInfoList(stationId);
    }

    /**
     * 新增站点、第三方推送平台配置对应
     *
     * @param thirdPartyStationRelation 站点、第三方推送平台配置对应
     * @return 结果
     */
    @Override
    public int insertThirdPartyStationRelation(ThirdPartyStationRelation thirdPartyStationRelation) {
        // stationSettingRelation.setCreateTime(DateUtils.getNowDate());
        thirdPartyStationRelation.setCreateBy(SecurityUtils.getUsername());
        return thirdPartyStationRelationMapper.insertThirdPartyStationRelation(thirdPartyStationRelation);
    }

    /**
     * 修改站点、第三方推送平台配置对应
     *
     * @param dto 站点、第三方推送平台配置对应
     * @return 结果
     */
    @Override
    public int updateThirdPartyStationRelation(ThirdPartyStationRelationDTO dto) {
        String stationId = dto.getStationId();
        // 前端传过来的最新关系
        List<String> thirdPartyTypes = dto.getThirdPartyTypes();
        if (StringUtils.isBlank(stationId) || CollectionUtils.isEmpty(thirdPartyTypes)) {
            throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR);
        }
        // 删除现有的所有站点与第三方平台的活跃关系
        thirdPartyStationRelationMapper.deleteThirdPartyStationRelationByStationId(stationId);
        List<ThirdPartyStationRelation> list = Lists.newArrayList();
        for (String thirdPartyType : thirdPartyTypes) {
            ThirdPartyStationRelation build = ThirdPartyStationRelation.builder()
                    .stationId(Long.parseLong(stationId))
                    .thirdPartyType(thirdPartyType)
                    .delFlag(DelFlagEnum.NORMAL.getValue())
                    .build();
            list.add(build);
        }
        return thirdPartyStationRelationMapper.batchInsert(list);
    }

    /**
     * 批量删除站点、第三方推送平台配置对应
     *
     * @param ids 需要删除的站点、第三方推送平台配置对应主键
     * @return 结果
     */
    @Override
    public int deleteThirdPartyStationRelationByIds(Long[] ids) {
        return thirdPartyStationRelationMapper.deleteThirdPartyStationRelationByIds(ids);
    }

    /**
     * 删除站点、第三方推送平台配置对应信息
     *
     * @param id 站点、第三方推送平台配置对应主键
     * @return 结果
     */
    @Override
    public int deleteThirdPartyStationRelationById(Long id) {
        return thirdPartyStationRelationMapper.deleteThirdPartyStationRelationById(id);
    }

    @Override
    public int updateRelationDelFlag(String stationId, List<String> types) {
        return thirdPartyStationRelationMapper.updateRelationDelFlag(stationId, types);
    }

    @Override
    public List<MerchantInfoVO> selectMerchantList(String thirdPlatformType) {
        return thirdPartyStationRelationMapper.selectMerchantList(thirdPlatformType);
    }

    @Override
    public List<StationInfoVO> selectStationList(String thirdPlatformType) {
        return thirdPartyStationRelationMapper.selectStationList(thirdPlatformType);
    }
}
