package com.gic.haoban.manage.service.service.out.impl;

import com.gic.api.base.commons.Page;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.base.api.common.BasePageInfo;
import com.gic.haoban.common.utils.PageUtil;
import com.gic.haoban.common.utils.UuidUtil;
import com.gic.haoban.manage.api.dto.HelpDTO;
import com.gic.haoban.manage.api.service.HelpApiService;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanHelpMapper;
import com.gic.haoban.manage.service.entity.TabHaobanHelp;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by tgs on 2020/2/9.
 */
@Service
public class HelpApiServiceImpl implements HelpApiService {
    @Autowired
    private TabHaobanHelpMapper tabHaobanHelpMapper;

    @Override
    public int saveHelp(HelpDTO dto) {
        if (StringUtils.isBlank(dto.getHelpId())) {
            //空，则新增
            dto.setHelpId(UuidUtil.randomUUID());
            TabHaobanHelp tab = EntityUtil.changeEntity(TabHaobanHelp.class, dto);
            tab.setStatusFlag(1);
            tab.setCreateTime(new Date());
            tab.setUpdateTime(new Date());
            tab.setOpenFlag(1);
            tab.setSort(1);
            TabHaobanHelp tab1 = tabHaobanHelpMapper.selectByHelpTitle(dto.getHelpTitle());
            if (tab1 != null) {
                return 8;
            }
            tabHaobanHelpMapper.insert(tab);
        } else {
            TabHaobanHelp tab = EntityUtil.changeEntity(TabHaobanHelp.class, dto);
            tab.setUpdateTime(new Date());
            tabHaobanHelpMapper.updateByPrimaryKeySelective(tab);
        }
        return 1;
    }

    @Override
    public HelpDTO findOneHelp(String helpId) {
        TabHaobanHelp tab = tabHaobanHelpMapper.selectByPrimaryKey(helpId);
        if (tab == null) {
            return null;
        }
        HelpDTO dto = EntityUtil.changeEntity(HelpDTO.class, tab);
        return dto;
    }

    @Override
    public Page<HelpDTO> pageList(BasePageInfo pageInfo) {
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        return PageUtil.changePageHelperToCurrentPage(tabHaobanHelpMapper.pageList(), HelpDTO.class);
    }

    @Override
    public void deleteOne(String helpId) {
        tabHaobanHelpMapper.deleteByPrimaryKey(helpId);
    }

    @Override
    public List<HelpDTO> getList() {
        List<TabHaobanHelp> list = tabHaobanHelpMapper.getList();
        return EntityUtil.changeEntityListByJSON(HelpDTO.class, list);
    }

}
