package com.ym.mec.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ym.mec.biz.dal.dao.SysManualDao;
import com.ym.mec.biz.dal.entity.SysManual;
import com.ym.mec.biz.dal.page.SysManualQueryInfo;
import com.ym.mec.biz.service.SysManualService;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.WrapperUtil;
import com.ym.mec.util.collection.MapUtil;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * (SysManual)表服务实现类
 *
 * @author makejava
 * @since 2021-12-20 17:19:38
 */
@Service("sysManualService")
public class SysManualServiceImpl extends ServiceImpl<SysManualDao, SysManual> implements SysManualService {

    private final static Logger logger = LoggerFactory.getLogger(SysManualServiceImpl.class);

    @Autowired
    private SysManualDao sysManualDao;

    public PageInfo<SysManual> query(SysManualQueryInfo queryInfo) {
        PageInfo<SysManual> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<>();
        MapUtil.populateMap(params, queryInfo);
        List<SysManual> dataList = null;
        int count = sysManualDao.findRecordCount(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = sysManualDao.queryRecord(params);
        }
        if (count == 0) {
            dataList = new ArrayList<>();
        }
        pageInfo.setRows(dataList);

        return pageInfo;
    }

    public Boolean add(SysManual sysManual, Integer userId) {
        logger.info("sysManual save");
        sysManual.setOperatorId(userId);
        sysManual.setCreateTime(new Date());
        sysManual.setUpdateTime(new Date());
        return this.save(sysManual);
    }

    public int update(SysManual sysManual, Integer userId) {
        logger.info("sysManual save");
        sysManual.setOperatorId(userId);
        sysManual.setUpdateTime(new Date());
        return sysManualDao.update(sysManual, new WrapperUtil<SysManual>().hasEq("id_", sysManual.getId()).queryWrapper());
    }

    @Override
    public String listMenuIds() {
        List<String> menuIds = sysManualDao.listMenuIds();
        return StringUtil.join(menuIds, ",");
    }
}

