package com.zsElectric.boot.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsElectric.boot.business.mapper.ThirdPartyInfoMapper;
import com.zsElectric.boot.business.model.entity.ThirdPartyInfo;
import com.zsElectric.boot.business.model.form.ThirdPartyInfoForm;
import com.zsElectric.boot.business.model.query.ThirdPartyInfoQuery;
import com.zsElectric.boot.business.model.vo.PartyStationInfoVO;
import com.zsElectric.boot.business.model.vo.ThirdPartyInfoVO;
import com.zsElectric.boot.business.service.ThirdPartyInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * 第三方对接信息服务实现
 *
 * @author system
 * @since 2025-12-15
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ThirdPartyInfoServiceImpl implements ThirdPartyInfoService {

    private final ThirdPartyInfoMapper thirdPartyInfoMapper;

    @Override
    public Page<ThirdPartyInfoVO> getThirdPartyInfoPage(ThirdPartyInfoQuery queryParams) {
        // 构建分页
        Page<ThirdPartyInfoVO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize());

        // 调用Mapper分页查询
        return thirdPartyInfoMapper.selectThirdPartyInfoPage(page, queryParams);
    }

    @Override
    public ThirdPartyInfoVO getThirdPartyInfoById(Long id) {
        ThirdPartyInfo entity = thirdPartyInfoMapper.selectById(id);
        if (entity == null) {
            return null;
        }

        ThirdPartyInfoVO vo = new ThirdPartyInfoVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public boolean addThirdPartyInfo(ThirdPartyInfoForm form) {
        ThirdPartyInfo entity = new ThirdPartyInfo();
        BeanUtils.copyProperties(form, entity);

        return thirdPartyInfoMapper.insert(entity) > 0;
    }

    @Override
    public boolean updateThirdPartyInfo(ThirdPartyInfoForm form) {
        if (form.getId() == null) {
            throw new IllegalArgumentException("ID不能为空");
        }

        ThirdPartyInfo entity = new ThirdPartyInfo();
        BeanUtils.copyProperties(form, entity);

        return thirdPartyInfoMapper.updateById(entity) > 0;
    }

    @Override
    public boolean deleteThirdPartyInfo(Long id) {
        return thirdPartyInfoMapper.deleteById(id) > 0;
    }

    @Override
    public List<PartyStationInfoVO> getPartyStationInfoList() {
        List<ThirdPartyInfo> thirdPartyInfos = thirdPartyInfoMapper.selectList(null);
        
        // 转换为VO列表
        return thirdPartyInfos.stream()
                .map(info -> {
                    PartyStationInfoVO vo = new PartyStationInfoVO();
                    vo.setId(info.getId());
                    vo.setStationName(info.getEcName());
                    return vo;
                })
                .collect(Collectors.toList());
    }
}
