package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.BMainFile;
import cn.cecep.talroad.domain.BMainSysFeedback;
import cn.cecep.talroad.enums.FileConstant;
import cn.cecep.talroad.mapper.BMainSysFeedbackMapper;
import cn.cecep.talroad.service.IBMainFileService;
import cn.cecep.talroad.service.IBMainSysFeedbackService;
import cn.cecep.talroad.system.api.model.LoginUser;
import cn.cecep.talroad.vo.BMainFileVo;
import cn.cecep.talroad.vo.BMainSysFeedbackVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 系统使用反馈Service业务层处理
 */
@Service
public class BMainSysFeedbackServiceImpl extends ServiceImpl<BMainSysFeedbackMapper, BMainSysFeedback> implements IBMainSysFeedbackService {

    @Autowired
    private IBMainFileService fileService;

    @Override
    @Transactional
    public void add(BMainSysFeedbackVo.AddForm addForm) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        BMainSysFeedback entity = new BMainSysFeedback();
        BeanUtils.copyProperties(addForm, entity);
        entity.setStatus("1"); // 默认未读
        entity.setCreateBy(loginUser.getUserid().toString());
        entity.setCreateByName(loginUser.getUsername());
        entity.setCreateTime(new Date());

        save(entity);

        // 保存反馈关联的附件
        if (addForm.getFiles() != null && !addForm.getFiles().isEmpty()) {
            List<BMainFile> collect = addForm.getFiles().stream().map(o -> {
                BMainFile file = new BMainFile();
                file.setFilePath(o.getFilePath());
                file.setFileName(o.getFileName());
                file.setBusinessId(entity.getId());
                file.setBusinessSource(FileConstant.Source.SysFeedback.getCode());
                file.setCreateTime(entity.getCreateTime());
                file.setCreateBy(entity.getCreateBy());
                return file;
            }).collect(Collectors.toList());

            fileService.saveBatch(collect);
        }
    }

    @Override
    public List<BMainSysFeedbackVo> selectSysFeedbackList(Page<BMainSysFeedbackVo> page, BMainSysFeedbackVo.SearchVo searchVo) {
        List<BMainSysFeedbackVo> list = baseMapper.selectSysFeedbackList(page, searchVo);
        page.setRecords(list);
        return list;
    }

    @Override
    public BMainSysFeedbackVo getInfo(String id) {
        BMainSysFeedback byId = getById(id);
        BMainSysFeedbackVo vo = new BMainSysFeedbackVo();
        BeanUtils.copyProperties(byId, vo);

        List<BMainFile> mainFiles = fileService.list(new QueryWrapper<BMainFile>().eq("business_id", id));

        vo.setFiles(mainFiles.stream().map(o -> {
            BMainFileVo.FormVo formVo = new BMainFileVo.FormVo();
            formVo.setFileName(o.getFileName());
            formVo.setFilePath(o.getFilePath());
            return formVo;
        }).collect(Collectors.toList()));

        return vo;
    }

}
