package com.yiboshi.science.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiboshi.science.base.BaseServiceImpl;
import com.yiboshi.science.base.Pagination;
import com.yiboshi.science.dao.ComEvaluationStandardDAO;
import com.yiboshi.science.entity.ComBatch;
import com.yiboshi.science.entity.ComEvaluationStandard;
import com.yiboshi.science.entity.ComExpertGroup;
import com.yiboshi.science.param.dto.ComBatchDTO;
import com.yiboshi.science.param.dto.ComEvaluationStandardDTO;
import com.yiboshi.science.param.query.ComEvaluationStandardQueryVO;
import com.yiboshi.science.service.ComBatchService;
import com.yiboshi.science.service.ComEvaluationStandardService;
import com.yiboshi.science.service.ComExpertGroupService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class ComEvaluationStandardServiceImpl extends BaseServiceImpl<ComEvaluationStandardDAO, ComEvaluationStandardQueryVO, ComEvaluationStandardDTO, ComEvaluationStandard> implements ComEvaluationStandardService {

    @Autowired
    private ComEvaluationStandardDAO comEvaluationStandardDAO;

    @Autowired
    private ComBatchService comBatchService;

    @Autowired
    private ComExpertGroupService comExpertGroupService;

    @Override
    protected void setCriteriaForQuery(ComEvaluationStandardQueryVO vo, QueryWrapper<ComEvaluationStandardQueryVO> criteria) {
        if (Objects.nonNull(vo.getName())) {
            criteria.like("name", vo.getName());
        }
    }

    @Override
    public Pagination<ComEvaluationStandardDTO> getListByPage(ComEvaluationStandardQueryVO vo) {
        QueryWrapper criteria = new QueryWrapper();
        setCriteriaForQuery(vo, criteria);
        Page<ComEvaluationStandardQueryVO> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<ComEvaluationStandardDTO> dtoList = comEvaluationStandardDAO.getListByPage(page, criteria).getRecords();
        dtoList.forEach(p -> {
            ComBatch entity = comBatchService.getById(p.getBatchId());
            if (null != entity) {
                p.setBatchName(entity.getYear() + "年度第" + entity.getBatch() + "批次");
            }
            ComExpertGroup groupEntity = comExpertGroupService.getById(p.getGroupId());
            if (null != groupEntity) {
                p.setGroupName(groupEntity.getName());
            }
        });
        return new Pagination<>(dtoList, page.getTotal(), vo.getPageSize());
    }

    public ComEvaluationStandardDTO getEvaluationStandardById(String id) {
        return this.dtoById(id);
    }

    public List<ComBatchDTO> getEvaluationYearBatchList() {
        ComBatch entity = new ComBatch();
        return comBatchService.dtoList(entity);
    }

    public String insertEvaluationStandard(ComEvaluationStandardDTO dto) {
        ComEvaluationStandard entity = convert2Entity(dto);
        this.insert(entity);
        return "评审标准添加成功！";
    }

    public String updateEvaluationStandard(ComEvaluationStandardDTO dto) {
        ComEvaluationStandard entity = convert2Entity(dto);
        this.update(entity);
        return "评审标准更新成功！";
    }

    public String deleteEvaluationStandard(String id){
        this.deleteById(id);
        return "评审标准删除成功！";
    }
}
