package com.yiboshi.science.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiboshi.science.base.BaseServiceImpl;
import com.yiboshi.science.dao.ComEvaluationItemDAO;
import com.yiboshi.science.entity.ComEvaluationItem;
import com.yiboshi.science.param.dto.ComEvaluationContentDTO;
import com.yiboshi.science.param.dto.ComEvaluationItemDTO;
import com.yiboshi.science.param.dto.ComEvaluationValueDTO;
import com.yiboshi.science.param.query.ComEvaluationItemQueryVO;
import com.yiboshi.science.service.ComEvaluationItemService;
import com.yiboshi.science.service.ComEvaluationValueService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class ComEvaluationItemServiceImpl extends BaseServiceImpl<ComEvaluationItemDAO, ComEvaluationItemQueryVO, ComEvaluationItemDTO, ComEvaluationItem> implements ComEvaluationItemService {

    @Autowired
    private ComEvaluationItemDAO comEvaluationItemDAO;

    @Autowired
    private ComEvaluationValueService comEvaluationValueService;

    @Override
    protected void setCriteriaForQuery(ComEvaluationItemQueryVO vo, QueryWrapper<ComEvaluationItemQueryVO> criteria) {

    }

    public List<ComEvaluationItemDTO> getEvaluationItemByContentId(String contentId) {
        QueryWrapper<ComEvaluationItem> criteria = new QueryWrapper();
        ComEvaluationItem comEvaluationItem = new ComEvaluationItem();
        comEvaluationItem.setContentId(contentId);
        notNullField(comEvaluationItem, criteria);
        criteria.orderByAsc("show_index");
        return convertEntity2DtoList(this.list(criteria));
    }

    public ComEvaluationItemDTO getEvaluationItemById(String id) {
        return this.dtoById(id);
    }

    public String CheckItemHasValues(ComEvaluationItemDTO dto) {
        ComEvaluationItemDTO model = this.dtoById(dto.getId());
        List<ComEvaluationValueDTO> ValueList = comEvaluationValueService.getEvaluationValueByObjectId(dto.getId());
        if (ValueList.size() > 0)
            return "0|" + model.getValueType().toString() + "|已经有值子项存在，不能修改为打分子项！";
        else
            return "1|" + model.getValueType().toString() + "|可以修改为打分子项！";
    }

    public String insertEvaluationItem(ComEvaluationItemDTO dto) {
        ComEvaluationItem entity = convert2Entity(dto);
        this.insert(entity);
        return "内容子项添加成功！";
    }

    public String updateEvaluationItem(ComEvaluationItemDTO dto) {
        ComEvaluationItem entity = convert2Entity(dto);
        this.update(entity);
        return "内容子项更新成功！";
    }

    public String deleteEvaluationItem(String id){
        this.deleteById(id);
        return "内容子项删除成功！";
    }
}
