package com.miaxis.app.report.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.app.file.vo.FileVo;
import com.miaxis.app.report.domain.ReportImages;
import com.miaxis.app.report.domain.ReportInfo;
import com.miaxis.app.report.dto.ReportInfoDTO;
import com.miaxis.app.report.mapper.ReportInfoMapper;
import com.miaxis.app.report.service.IReportImagesService;
import com.miaxis.app.report.service.IReportInfoService;
import com.miaxis.common.exception.CustomException;
import com.miaxis.common.utils.DateUtils;
import com.miaxis.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 举报信息Service业务层处理
 *
 * @author miaxis
 * @date 2021-01-06
 */
@Service
public class ReportInfoServiceImpl extends ServiceImpl<ReportInfoMapper, ReportInfo> implements IReportInfoService {
    @Autowired
    private ReportInfoMapper reportInfoMapper;


    @Autowired
    private IReportImagesService reportImagesService;

    /**
     * 查询举报信息
     *
     * @param id 举报信息ID
     * @return 举报信息
     */
    @Override
    public ReportInfo selectReportInfoById(Long id){
        return reportInfoMapper.selectReportInfoById(id);
    }

    /**
     * 查询举报信息列表
     *
     * @param reportInfo 举报信息
     * @return 举报信息
     */
    @Override
    public List<ReportInfo> selectReportInfoList(ReportInfo reportInfo){
        return reportInfoMapper.selectReportInfoList(reportInfo);
    }

    /**
     * 新增举报信息
     * @return 结果
     */
    @Override
    @Transactional
    public void insertReportInfo(ReportInfoDTO reportInfoDTO){
        if (!StringUtils.isMobiPhoneNum(reportInfoDTO.getMobile())){
            throw new CustomException("手机号格式不正确");
        }
        if (!StringUtils.isMobiPhoneNum(reportInfoDTO.getReportContent())){
            throw new CustomException("内容不能为空");
        }
        ReportInfo reportInfo = new ReportInfo();
        BeanUtils.copyProperties(reportInfoDTO,reportInfo);
        reportInfoMapper.insertReportInfo(reportInfo);
        List<FileVo> images = reportInfoDTO.getImages();
        List<ReportImages> reportImagesList = images.stream().map(o->toReportImages(o,reportInfo.getId())).collect(Collectors.toList());
        reportImagesService.saveBatch(reportImagesList);
    }

    private ReportImages toReportImages(FileVo o, Long id) {
        ReportImages reportImages = new ReportImages();
        BeanUtils.copyProperties(o,reportImages);
        reportImages.setReportId(id);
        return  reportImages;
    }

    /**
     * 修改举报信息
     *
     * @param reportInfo 举报信息
     * @return 结果
     */
    @Override
    public int updateReportInfo(ReportInfo reportInfo){
        reportInfo.setUpdateTime(DateUtils.getNowDate());
        return reportInfoMapper.updateReportInfo(reportInfo);
    }

    /**
     * 批量删除举报信息
     *
     * @param ids 需要删除的举报信息ID
     * @return 结果
     */
    @Override
    public int deleteReportInfoByIds(Long[] ids){
        return reportInfoMapper.deleteReportInfoByIds(ids);
    }

    /**
     * 删除举报信息信息
     *
     * @param id 举报信息ID
     * @return 结果
     */
    @Override
    public int deleteReportInfoById(Long id){
        return reportInfoMapper.deleteReportInfoById(id);
    }
}
