package com.diagbot.facade;

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

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author:zhaops
 * @time: 2018/11/16 14:31
 */
@Component
public class IntroduceDetailFacade extends IntroduceDetailServiceImpl {
    @Autowired
    private IntroduceInfoFacade introduceInfoFacade;

    /**
     * 获取提示信息明细
     *
     * @param introduceId
     * @return
     */
    public List<IntroduceDetail> getByIntroduceId(Long introduceId) {
        QueryWrapper<IntroduceDetail> introduceDetailQueryWrapper = new QueryWrapper<>();
        introduceDetailQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey()).
                eq("introduce_id", introduceId);
        return this.list(introduceDetailQueryWrapper);
    }

    /**
     * 获取单条提示信息明细
     *
     * @param id
     * @return
     */
    public IntroduceDetail getRecordById(Long id) {
        IntroduceDetail detail = super.getById(id);
        if (detail == null) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "提示信息明细不存在");
        } else if (detail.getIsDeleted().equals(IsDeleteEnum.Y.getKey())) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "提示信息明细已删除");
        }
        return detail;
    }

    /**
     * 单条明细保存
     *
     * @param detailSingleVO
     * @return
     */
    public Boolean saveRecord(IntroduceDetailSingleVO detailSingleVO) {
        if (detailSingleVO.getIntroduceId() == null || detailSingleVO.getIntroduceId().equals(0L)) {
            throw new CommonException(CommonErrorCode.PARAM_ERROR, "请输入提示信息id");
        }
        IntroduceInfo introduceInfo = introduceInfoFacade.getById(detailSingleVO.getIntroduceId());
        if (introduceInfo == null) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "提示信息不存在");
        } else if (introduceInfo.getIsDeleted().equals(IsDeleteEnum.Y.getKey())) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "提示信息已删除");
        }

        String userId = UserUtils.getCurrentPrincipleID();
        Date now = DateUtil.now();
        IntroduceDetail detail = super.getById(detailSingleVO.getId());
        if (!(detail == null || detail.getIsDeleted().equals(IsDeleteEnum.Y.getKey()))) {
            detail.setIsDeleted(IsDeleteEnum.Y.getKey());
            detail.setModifier(userId);
            detail.setGmtModified(now);
            this.updateById(detail);
        }
        detail = new IntroduceDetail();
        detail.setIsReason(detailSingleVO.getIsReason());
        detail.setTitle(detailSingleVO.getTitle());
        detail.setOrderNo(detailSingleVO.getOrderNo());
        detail.setText(detailSingleVO.getText());
        detail.setContent(detailSingleVO.getContent());
        detail.setIntroduceId(detailSingleVO.getIntroduceId());
        detail.setPosition(detailSingleVO.getPosition());
        detail.setCreator(userId);
        detail.setGmtCreate(now);
        detail.setModifier(userId);
        detail.setGmtModified(now);

        this.saveOrUpdate(detail);

        return true;
    }

    /**
     * 单条删除提示信息明细 逻辑删除
     *
     * @param id
     * @return
     */
    public Boolean deleteRecord(Long id) {
        UpdateWrapper<IntroduceDetail> introduceDetailUpdateWrapper = new UpdateWrapper<>();
        introduceDetailUpdateWrapper.eq("id", id).
                eq("is_deleted", IsDeleteEnum.N.getKey()).
                set("is_deleted", IsDeleteEnum.Y.getKey()).
                set("modifier", UserUtils.getCurrentPrincipleID()).
                set("gmt_modified", DateUtil.now());
        this.update(new IntroduceDetail(), introduceDetailUpdateWrapper);
        return true;
    }

    /**
     * 批量删除提示信息明细 逻辑删除
     *
     * @param idVOList
     * @return
     */
    public Boolean deleteRecords(List<IdVO> idVOList) {
        List<Long> ids = idVOList.stream().map(idVO -> idVO.getId()).collect(Collectors.toList());
        UpdateWrapper<IntroduceDetail> introduceDetailUpdateWrapper = new UpdateWrapper<>();
        introduceDetailUpdateWrapper.in("id", ids).
                eq("is_deleted", IsDeleteEnum.N.getKey()).
                set("is_deleted", IsDeleteEnum.Y.getKey()).
                set("modifier", UserUtils.getCurrentPrincipleID()).
                set("gmt_modified", DateUtil.now());
        this.update(new IntroduceDetail(), introduceDetailUpdateWrapper);
        return true;
    }
}