package com.jwipc.tender.logic.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.aggregation.ModelAggregationService;
import com.jwipc.constant.FlagConst;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.query.BasePageQuery;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.FileClient;
import com.jwipc.sys.client.dto.FileResposeCDTO;
import com.jwipc.tender.constant.QuestionSendType;
import com.jwipc.tender.convert.TenderQuestionConvert;
import com.jwipc.tender.dto.TenderQuestionBaseDTO;
import com.jwipc.tender.dto.TenderQuestionDetailDTO;
import com.jwipc.tender.entity.TenderQuestion;
import com.jwipc.tender.logic.TenderQuestionLogic;
import com.jwipc.tender.service.ITenderQuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * <p> </p>
 *
 * @author BOGON
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class TenderQuestionLogicImpl implements TenderQuestionLogic {
    private final ITenderQuestionService tenderQuestionService;
    private final TenderQuestionConvert tenderQuestionConvert;
    private final FileClient fileClient;
    private final ModelAggregationService modelAggregationService;

    @Override
    public PageView getList(BasePageQuery page, Long tenderId) {
        Page<TenderQuestion> pg = PageUtil.toPage(page);
        final LambdaQueryWrapper<TenderQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TenderQuestion::getTenderId, tenderId).orderBy(true, false, TenderQuestion::getQuestionTime);
        if (LoginUtil.isSupplier()) {
            //如果是供应商需要查询 公开的，并且是自己提问的
            wrapper.and(wr -> wr.eq(TenderQuestion::getQuestionUserId, LoginUtil.getUserId()).or()
                    .eq(TenderQuestion::getAllFlag, FlagConst.Y.getCode()));
        }
        final List<TenderQuestion> list = tenderQuestionService.list(pg, wrapper);
        List<TenderQuestionBaseDTO> dtos = tenderQuestionConvert.entitiesToDtos(list);
        modelAggregationService.processRelationAggregation(dtos);
        return PageUtil.getPV(dtos, pg.getTotal());
    }

    @Override
    public TenderQuestionDetailDTO getDetail(Long id) {
        final TenderQuestion question = tenderQuestionService.getById(id);
        final String questionFileId = question.getQuestionFileId();
        final List<String> fileIds = StrUtil.split(questionFileId, StrUtil.COMMA);
        final List<FileResposeCDTO> fileInfos = fileClient.getFileInfos(fileIds);
        final TenderQuestionDetailDTO dto = tenderQuestionConvert.entityToDto(question);
        modelAggregationService.processRelationAggregation(dto);
        dto.setFiles(fileInfos);
        return dto;
    }

    @Override
    public void save(TenderQuestionBaseDTO tenderQuestionBaseDTO) {
        final TenderQuestion question = tenderQuestionConvert.dtoToEntity(tenderQuestionBaseDTO);
        if (LoginUtil.isSupplier()) {
            if (question.getId() == 0L) {
                question.setQuestionUserId(LoginUtil.getUserId());
                question.setQuestionTime(LocalDateTime.now());
                question.setSendType(QuestionSendType.SUPPLIER.getType());
            }
        } else {
            question.setAnswerUserId(LoginUtil.getUserId());
            question.setAnswerTime(LocalDateTime.now());
            if (question.getId() == 0L) {
                question.setQuestionUserId(LoginUtil.getUserId());
                question.setQuestionTime(LocalDateTime.now());
                question.setSendType(QuestionSendType.PURCHASE.getType());
                question.setAllFlag(FlagConst.Y.getCode());
            }
        }
        tenderQuestionService.saveOrUpdate(question);
    }

    @Override
    public void remove(Long id) {
        tenderQuestionService.removeById(id);
    }
}