package com.ruoyi.mall.service.impl;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.mall.domain.DemandReport;
import com.ruoyi.mall.domain.DemandReportDto;
import com.ruoyi.mall.domain.vo.MemberUserVO;
import com.ruoyi.mall.mapper.DemandReportMapper;
import com.ruoyi.mall.mapper.MallMemberUserMapper;
import com.ruoyi.mall.service.IDemandReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-14
 */
@Service
public class DemandReportServiceImpl implements IDemandReportService {
    @Autowired
    private DemandReportMapper demandReportMapper;
    @Resource
    private MallMemberUserMapper mallMemberUserMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public DemandReport selectDemandReportById(Long id) {
        DemandReport demandReport = demandReportMapper.selectDemandReportById(id);
        if (!demandReport.getProductImages().isEmpty()) {
            List<String> split = Arrays.asList(demandReport.getProductImages().split(";"));
            demandReport.setProductImagesList(split);
        }
        return demandReport;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param demandReport 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<DemandReport> selectDemandReportList(DemandReport demandReport) {
        List<DemandReport> demandReportList = demandReportMapper.selectDemandReportList(demandReport);
        for (DemandReport demandReport1 : demandReportList) {
            if (demandReport1.getProductImages()!=null&&!demandReport1.getProductImages().isEmpty()) {
                List<String> split = Arrays.asList(demandReport1.getProductImages().split(";"));
                demandReport1.setProductImagesList(split);
            }
        }
        return demandReportList;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param demandreportdto 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDemandReport(DemandReportDto demandreportdto) {
        DemandReport demandReport = new DemandReport();
        BeanUtils.copyProperties(demandreportdto, demandReport);
        if (!demandreportdto.getProductImages().isEmpty()) {
            String imagesString = demandreportdto.getProductImages().stream()
                    .collect(Collectors.joining(";"));
            demandReport.setProductImages(imagesString);
        }
        if (!demandreportdto.getUploader().isEmpty()) {
            Long l = Long.valueOf(demandreportdto.getUploader());
            MemberUserVO memberUserVO = mallMemberUserMapper.selectMemberVOUserById(l);
            if (Objects.nonNull(memberUserVO)) {
                demandReport.setUploadername(memberUserVO.getName());
                demandReport.setPhone(memberUserVO.getPhone());
            }
        }
        return demandReportMapper.insertDemandReport(demandReport);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param demandReport 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDemandReport(DemandReport demandReport) {
        return demandReportMapper.updateDemandReport(demandReport);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDemandReportByIds(Long[] ids) {
        return demandReportMapper.deleteDemandReportByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDemandReportById(Long id) {
        return demandReportMapper.deleteDemandReportById(id);
    }
}
