package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.diagbot.entity.IntroduceInfo;
import com.diagbot.entity.IntroduceMap;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.IntroduceMapServiceImpl;
import com.diagbot.util.DateUtil;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.IntroduceMapVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author:zhaops
 * @time: 2018/11/16 14:31
 */
@Component
public class IntroduceMapFacade extends IntroduceMapServiceImpl {

    @Autowired
    IntroduceInfoFacade introduceInfoFacade;

    /**
     * 保存标签提示信息映射关系
     *
     * @param introduceMapVO
     * @return
     */
    public Boolean saveIntroduceMap(IntroduceMapVO introduceMapVO) {
        IntroduceInfo introduceInfo = introduceInfoFacade.getById(introduceMapVO.getIntroduceId());
        if (introduceInfo == null) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "提示信息未添加");
        } else if (introduceInfo.getIsDeleted().equals(IsDeleteEnum.Y.getKey())) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "提示信息已删除");
        }
        QueryWrapper<IntroduceMap> introduceMapQueryWrapper = new QueryWrapper<>();
        introduceMapQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey()).
                eq("question_id", introduceMapVO.getQuestionId()).
                eq("type", introduceMapVO.getType()).
                eq("introduce_id", introduceMapVO.getIntroduceId());
        IntroduceMap introduceMap = this.getOne(introduceMapQueryWrapper);
        if (introduceMap == null) {
            //删除该标签关联的别的提示信息，插入新的提示信息
            UpdateWrapper<IntroduceMap> introduceMapUpdateWrapper = new UpdateWrapper<>();
            introduceMapUpdateWrapper.eq("is_deleted", IsDeleteEnum.N.getKey()).
                    eq("question_id", introduceMapVO.getQuestionId()).
                    eq("type", introduceMapVO.getType()).
                    set("is_deleted", IsDeleteEnum.Y.getKey()).
                    set("modifier", UserUtils.getCurrentPrincipleID()).
                    set("gmt_modified", DateUtil.now());
            this.update(new IntroduceMap(), introduceMapUpdateWrapper);

            //插入新的关系
            introduceMap = new IntroduceMap();
            introduceMap.setQuestionId(introduceMapVO.getQuestionId());
            introduceMap.setIntroduceId(introduceMapVO.getIntroduceId());
            introduceMap.setType(introduceMapVO.getType());
            introduceMap.setCreator(UserUtils.getCurrentPrincipleID());
            introduceMap.setGmtCreate(DateUtil.now());
        }
        //已有关系更新时间
        introduceMap.setGmtModified(DateUtil.now());
        introduceMap.setModifier(UserUtils.getCurrentPrincipleID());

        this.saveOrUpdate(introduceMap);
        return true;
    }

    /**
     * 删除标签提示信息映射关系
     *
     * @param introduceMapVO
     * @return
     */
    public Boolean delIntroduceMap(IntroduceMapVO introduceMapVO) {
        UpdateWrapper<IntroduceMap> introduceMapUpdateWrapper = new UpdateWrapper<>();
        introduceMapUpdateWrapper.eq("is_deleted", IsDeleteEnum.N.getKey()).
                eq("introduce_id", introduceMapVO.getIntroduceId()).
                eq("question_id", introduceMapVO.getQuestionId()).
                eq("type", introduceMapVO.getType()).
                set("is_deleted", IsDeleteEnum.Y.getKey()).
                set("modifier", UserUtils.getCurrentPrincipleID()).
                set("gmt_modified", DateUtil.now());
        this.update(new IntroduceMap(), introduceMapUpdateWrapper);
        return true;
    }
}