package com.iailab.module.model.mcs.pre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.module.model.mcs.pre.dao.DmModuleDao;
import com.iailab.module.model.mcs.pre.entity.DmModuleEntity;
import com.iailab.module.model.mcs.pre.service.DmModuleService;
import com.iailab.module.model.mcs.pre.vo.DmModulePageReqVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author PanZhibao
 * @date 2021年04月23日 8:36
 */
@Service
public class DmModuleServiceImpl extends ServiceImpl<DmModuleDao, DmModuleEntity> implements DmModuleService {

    @Autowired
    private DmModuleDao dmModuleDao;

    @Override
    public PageResult<DmModuleEntity> queryPage(DmModulePageReqVO reqVO) {
        return dmModuleDao.selectPage(reqVO);
    }

    @Override
    public List<DmModuleEntity> list(Map<String, Object> params) {
        QueryWrapper<DmModuleEntity> wrapper = getWrapper(params);
        wrapper.orderByDesc("create_time");
        return dmModuleDao.selectList(wrapper);
    }

    @Override
    public DmModuleEntity info(String id) {
        return dmModuleDao.selectById(id);
    }

    @Override
    public DmModuleEntity getModuleByItemId(String itemId) {
        List<DmModuleEntity> list = dmModuleDao.getModuleByItemId(itemId);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    private QueryWrapper<DmModuleEntity> getWrapper(Map<String, Object> params) {
        String modulename = (String) params.get("modulename");
        String moduletype = (String) params.get("moduletype");

        QueryWrapper<DmModuleEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(modulename), "modulename", modulename)
                .eq(StringUtils.isNotBlank(moduletype), "moduletype", moduletype);

        return wrapper;
    }

    @Override
    public void saveModule(DmModuleEntity entity) {
        entity.setId(UUID.randomUUID().toString());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        dmModuleDao.insert(entity);
    }

    @Override
    public void update(DmModuleEntity entity) {
        entity.setUpdateTime(new Date());
        dmModuleDao.updateById(entity);
    }

    @Override
    public void deleteBatch(String[] moduleIds) {
        dmModuleDao.deleteBatchIds(Arrays.asList(moduleIds));
    }

    @Override
    public List<DmModuleEntity> getModuleByModuleType(String moduletype) {
        Map<String, Object> params = new HashMap<>(1);
        params.put("moduletype", moduletype);
        QueryWrapper<DmModuleEntity> wrapper = getWrapper(params);
        return dmModuleDao.selectList(wrapper);
    }

    @Override
    public void updatePredictTime(String id, Date predictTime) {
        DmModuleEntity entity = dmModuleDao.selectById(id);
        if (entity == null) {
            return;
        }
        entity.setPredicttime(predictTime);
        dmModuleDao.updateById(entity);
    }

    @Override
    public int check(DmModuleEntity module) {
        String id = module.getId();
        String modulename = module.getModulename();
        QueryWrapper<DmModuleEntity> moduleWrapper = new QueryWrapper<>();
        moduleWrapper.ne(StringUtils.isNotBlank(id), "id", id);
        moduleWrapper.and(wrapper -> wrapper.eq("modulename", modulename));

        List<DmModuleEntity> list = dmModuleDao.selectList(moduleWrapper);
        return list.size();
    }

}