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.ComEvaluationContentDAO;
import com.yiboshi.science.entity.ComEvaluationContent;
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.ComEvaluationContentQueryVO;
import com.yiboshi.science.service.ComEvaluationContentService;
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 ComEvaluationContentServiceImpl extends BaseServiceImpl<ComEvaluationContentDAO, ComEvaluationContentQueryVO, ComEvaluationContentDTO, ComEvaluationContent> implements ComEvaluationContentService {

    @Autowired
    private ComEvaluationContentDAO comEvaluationContentDAO;

    @Autowired
    private ComEvaluationItemService comEvaluationItemService;

    @Autowired
    private ComEvaluationValueService comEvaluationValueService;

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

    }

    public List<ComEvaluationContentDTO> getEvaluationContentByEvalId(String EvaluationId) {
        QueryWrapper<ComEvaluationContent> criteria = new QueryWrapper();
        ComEvaluationContent comEvaluationContent = new ComEvaluationContent();
        comEvaluationContent.setEvaluationId(EvaluationId);
        notNullField(comEvaluationContent, criteria);
        criteria.orderByAsc("show_index");
        List<ComEvaluationContentDTO> ContentList = convertEntity2DtoList(this.list(criteria));
        ContentList.forEach(c -> {
            if (c.getHasSubitem()) {
                List<ComEvaluationItemDTO> ItemList = comEvaluationItemService.getEvaluationItemByContentId(c.getId());
                ItemList.forEach(p -> {
                    if (p.getValueType() == 1 || p.getValueType() == 2) {
                        List<ComEvaluationValueDTO> ItemValueList = comEvaluationValueService.getEvaluationValueByObjectId(p.getId());
                        ItemValueList.forEach(m -> {
                            m.setHasAdd(1);
                        });
                        p.setItemValues(ItemValueList);
                    }
                });
                c.setContentItems(ItemList);
            } else {
                List<ComEvaluationValueDTO> ItemValueList = comEvaluationValueService.getEvaluationValueByObjectId(c.getId());
                ItemValueList.forEach(p -> {
                    p.setHasAdd(1);
                });
                c.setContentValues(ItemValueList);
            }
        });
        return ContentList;
    }

    public ComEvaluationContentDTO getEvaluationContentById(String id) {
        return this.dtoById(id);
    }

    public String CheckContentHasItemsOrValues(ComEvaluationContentDTO dto) {
        if (dto.getHasSubitem()) {
            List<ComEvaluationValueDTO> ValueList = comEvaluationValueService.getEvaluationValueByObjectId(dto.getId());
            if (ValueList.size() > 0)
                return "0|已经有值子项存在，不能修改为内容子项！";
            else
                return "1|可以修改为内容子项！";
        } else {
            List<ComEvaluationItemDTO> ItemList = comEvaluationItemService.getEvaluationItemByContentId(dto.getId());
            if (ItemList.size() > 0)
                return "0|已经有内容子项存在，不能修改为值子项！";
            else
                return "1|可以修改为值子项！";
        }
    }

    public String CheckContentHasValues(ComEvaluationContentDTO dto) {
        ComEvaluationContentDTO 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 insertEvaluationContent(ComEvaluationContentDTO dto) {
        ComEvaluationContent entity = convert2Entity(dto);
        this.insert(entity);
        return "评审内容添加成功！";
    }

    public String updateEvaluationContent(ComEvaluationContentDTO dto) {
        ComEvaluationContent entity = convert2Entity(dto);
        this.update(entity);
        return "评审内容更新成功！";
    }

    public String deleteEvaluationContent(String id) {
        ComEvaluationContentDTO model = this.dtoById(id);
        if (model.getHasSubitem()) {
            List<ComEvaluationItemDTO> ItemList = comEvaluationItemService.getEvaluationItemByContentId(id);
            if (ItemList.size() > 0) {
                return "0|评审内容子项存在，不能删除评审内容！";
            } else {
                this.deleteById(id);
                return "1|评审内容删除成功！";
            }
        } else {
            if (model.getValueType() == 3) {
                this.deleteById(id);
                return "1|评审内容删除成功！";
            } else {
                List<ComEvaluationValueDTO> ValueList = comEvaluationValueService.getEvaluationValueByObjectId(id);
                if (ValueList.size() > 0) {
                    return "0|评审内容子项存在，不能删除评审内容！";
                } else {
                    this.deleteById(id);
                    return "1|评审内容删除成功！";
                }
            }
        }
    }
}
