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

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsElectric.boot.business.mapper.FirmInfoMapper;
import com.zsElectric.boot.business.service.FirmInfoService;
import com.zsElectric.boot.business.model.entity.FirmInfo;
import com.zsElectric.boot.business.model.form.FirmInfoForm;
import com.zsElectric.boot.business.model.query.FirmInfoQuery;
import com.zsElectric.boot.business.model.vo.FirmInfoVO;
import com.zsElectric.boot.business.converter.FirmInfoConverter;

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

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 企业信息服务实现类
 *
 * @author zsElectric
 * @since 2025-12-11 10:10
 */
@Service
@RequiredArgsConstructor
public class FirmInfoServiceImpl extends ServiceImpl<FirmInfoMapper, FirmInfo> implements FirmInfoService {

    private final FirmInfoConverter firmInfoConverter;

    /**
    * 获取企业信息分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<FirmInfoVO>} 企业信息分页列表
    */
    @Override
    public IPage<FirmInfoVO> getFirmInfoPage(FirmInfoQuery queryParams) {
        Page<FirmInfoVO> pageVO = this.baseMapper.getFirmInfoPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取企业信息表单数据
     *
     * @param id 企业信息ID
     * @return 企业信息表单数据
     */
    @Override
    public FirmInfoForm getFirmInfoFormData(Long id) {
        FirmInfo entity = this.getById(id);
        return firmInfoConverter.toForm(entity);
    }
    
    /**
     * 新增企业信息
     *
     * @param formData 企业信息表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveFirmInfo(FirmInfoForm formData) {
        FirmInfo entity = firmInfoConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新企业信息
     *
     * @param id   企业信息ID
     * @param formData 企业信息表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateFirmInfo(Long id,FirmInfoForm formData) {
        FirmInfo entity = firmInfoConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除企业信息
     *
     * @param ids 企业信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteFirmInfos(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的企业信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
