package com.sw.mes.yb.pc.service.impl;

import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.RequestPage;

import com.sw.mes.base.factory.utils.PageUtils;
import com.sw.mes.yb.pc.dao.ExceptionInfoDAO;
import com.sw.mes.yb.pc.entity.ExceptionInfo;
import com.sw.mes.yb.pc.service.ExceptionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class ExceptionInfoServiceImpl extends BaseServiceImpl<ExceptionInfo> implements ExceptionInfoService {

    ExceptionInfoDAO dao;

    @Autowired
    public void setDao(ExceptionInfoDAO exceptionInfoDAO) {
        this.dao = exceptionInfoDAO;
        super.setBaseDao(exceptionInfoDAO);
    }


    @Override
    public List<ExceptionInfo> findListByPage(Specification<ExceptionInfo> specification, RequestPage page) {
        org.springframework.data.domain.Page<ExceptionInfo> springDataPage = dao.findAll(specification, PageUtils.createPageable(page));
        page.setTotal(springDataPage.getTotalElements());
        return springDataPage.getContent();
    }

    @Override
    public void createInfo(Long id, String sectionNo, String displayName, String act, String tip) {
        ExceptionInfo exceptionInfo = new ExceptionInfo();
        exceptionInfo.setPropertyId(id);
        exceptionInfo.setPropertyCode(sectionNo);
        exceptionInfo.setPropertyName(displayName);
        exceptionInfo.setPropertyValue(act);
        exceptionInfo.setExceptionTip(tip);

        createInfo(exceptionInfo);
    }

    @Override
    public void updateUsable(String propertyCode) {
        dao.updateUsable(propertyCode);
    }

    @Override
    public List<ExceptionInfo> getByPropertyCode(String sectionNo) {
        return dao.findByPropertyCode(sectionNo);
    }

    @Override
    public void createInfo(ExceptionInfo info) {
        info.setUsable(true);
        info.setCreateDate(new Date());
        saveOrUpdate(info);
    }

    @Override
    public ExceptionInfo getUnableByPropertyCode(String sectionNo) {
        List<ExceptionInfo> list = dao.getUnableByPropertyCode(sectionNo);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void handleExceptionInfos(List<ExceptionInfo> infos, String sectionNo) {
        List<ExceptionInfo> list = dao.getUnableByPropertyCode(sectionNo);
        List<ExceptionInfo> entities = infos.stream().filter(item -> exceptionCon(list, item.getCause())).collect(Collectors.toList());
        if (entities.size() > 0) {
            dao.saveAll(entities);
        }
    }

    @Override
    public ExceptionInfo getByPropertyCodeAndCause(String sectionNo, String cause) {
        List<ExceptionInfo> list = dao.getByPropertyCodeAndCause(sectionNo, cause);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    private Boolean exceptionCon(List<ExceptionInfo> list, String cause) {
        for (ExceptionInfo item: list) {
            if (Objects.equals(cause, item.getCause())) {
                return false;
            }
        }
        return true;
    }

}
