package io.hmit.modules.fhjw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.page.PageData;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.modules.fhjw.dao.FhjwCommentDao;
import io.hmit.modules.fhjw.dto.FhjwCommentDTO;
import io.hmit.modules.fhjw.entity.FhjwCommentEntity;
import io.hmit.modules.fhjw.service.FhjwAnswerService;
import io.hmit.modules.fhjw.service.FhjwCaseService;
import io.hmit.modules.fhjw.service.FhjwCommentService;
import io.hmit.modules.fhjw.service.FhjwQuestionService;
import io.hmit.modules.fhjw.service.FhjwTempService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 案件评价表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-06-02
 */
@Service
public class FhjwCommentServiceImpl extends CrudServiceImpl<FhjwCommentDao, FhjwCommentEntity, FhjwCommentDTO> implements FhjwCommentService {

    @Autowired
    private FhjwCaseService fhjwCaseService;

    @Override
    public QueryWrapper<FhjwCommentEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<FhjwCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public void save(FhjwCommentDTO dto) {

        Map<String, Object> params = new HashMap<>();
        params.put("caseNum",dto.getCaseNum());
        Long caseId = fhjwCaseService.list(params).get(0).getId();
        dto.getQuestionAnswerList().stream().forEach(item ->{
            FhjwCommentDTO fhjwCommentDTO = new FhjwCommentDTO();
            fhjwCommentDTO.setTempId(dto.getTempId());
            fhjwCommentDTO.setCaseId(caseId);
            fhjwCommentDTO.setCreateDate(new Date());
            fhjwCommentDTO.setUserId(dto.getUserId());
            fhjwCommentDTO.setAnswerId(item.getAnswerId());
            fhjwCommentDTO.setQuestionId(item.getQuestionId());
            fhjwCommentDTO.setRoleId(dto.getRoleId());
            super.save(fhjwCommentDTO);
        });

    }
}
