package cn.com.tenlion.configcontent.service.contentscans.impl;

import ink.wgink.app.AppTokenManager;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.app.AppTokenUser;
import ink.wgink.pojo.bos.UserInfoBO;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.date.DateUtil;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
import cn.com.tenlion.configcontent.dao.contentscans.IContentScansDao;
import cn.com.tenlion.configcontent.pojo.dtos.contentscans.ContentScansDTO;
import cn.com.tenlion.configcontent.pojo.vos.contentscans.ContentScansVO;
import cn.com.tenlion.configcontent.pojo.bos.contentscans.ContentScansBO;
import cn.com.tenlion.configcontent.pojo.pos.contentscans.ContentScansPO;
import cn.com.tenlion.configcontent.service.contentscans.IContentScansService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: ContentScansServiceImpl
 * @Description: CBC内容管理查看记录表
 * @Author: CodeFactory
 * @Date: 2021-08-02 10:42:41
 * @Version: 3.0
 **/
@Service
public class ContentScansServiceImpl extends DefaultBaseService implements IContentScansService {

    @Autowired
    private IContentScansDao contentScansDao;

    @Override
    public void updateGmtCreate(String contentScansId, String gmtCreate) {
        Map<String, Object> params =getHashMap(2);
        params.put("contentScansId", contentScansId);
        params.put("gmtCreate", gmtCreate);
        contentScansDao.updateGmtCreate(params);
    }

    @Override
    public void save(ContentScansVO contentScansVO) {
        saveReturnId(contentScansVO);
    }

    @Override
    public void save(String token, ContentScansVO contentScansVO) {
        saveReturnId(token, contentScansVO);
    }

    @Override
    public String saveReturnId(ContentScansVO contentScansVO) {
        return saveReturnId(null, contentScansVO);
    }

    @Override
    public String saveReturnId(String token, ContentScansVO contentScansVO) {
        String contentScansId = UUIDUtil.getUUID();
        if (token != null) {
            AppTokenUser appTokenUser = AppTokenManager.getInstance().getToken(token).getAppTokenUser();
            contentScansVO.setContentScansUserName(appTokenUser.getName());
            contentScansVO.setContentScansUserIcon(appTokenUser.getAvatar());
        } else {
            UserInfoBO userInfoBO = this.securityComponent.getCurrentUser();
            contentScansVO.setContentScansUserName(userInfoBO.getUserName());
        }
        Map<String, Object> params = HashMapUtil.beanToMap(contentScansVO);
        params.put("contentScansId", contentScansId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        /**
         * 查询是否已查看 , 已查看更新时间 , 未查看保存
         */
        List<ContentScansDTO> list = list(params);
        if (list.size() > 0) {
            for (ContentScansDTO dto : list) {
                updateGmtCreate(dto.getContentScansId(), DateUtil.getTime());
            }
            return "";
        }
        contentScansDao.save(params);
        return contentScansId;
    }

    @Override
    public void remove(List<String> ids) {
        remove(null, ids);
    }

    @Override
    public void remove(String token, List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("contentScansIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        contentScansDao.remove(params);
    }

    @Override
    public void delete(List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("contentScansIds", ids);
        contentScansDao.delete(params);
    }

    @Override
    public void update(String contentScansId, ContentScansVO contentScansVO) {
        update(null, contentScansId, contentScansVO);
    }

    @Override
    public void update(String token, String contentScansId, ContentScansVO contentScansVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(contentScansVO);
        params.put("contentScansId", contentScansId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        contentScansDao.update(params);
    }

    @Override
    public ContentScansDTO get(Map<String, Object> params) {
        return contentScansDao.get(params);
    }

    @Override
    public ContentScansDTO get(String contentScansId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("contentScansId", contentScansId);
        return get(params);
    }

    @Override
    public ContentScansBO getBO(Map<String, Object> params) {
        return contentScansDao.getBO(params);
    }

    @Override
    public ContentScansBO getBO(String contentScansId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("contentScansId", contentScansId);
        return getBO(params);
    }

    @Override
    public ContentScansPO getPO(Map<String, Object> params) {
        return contentScansDao.getPO(params);
    }

    @Override
    public ContentScansPO getPO(String contentScansId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("contentScansId", contentScansId);
        return getPO(params);
    }

    @Override
    public List<ContentScansDTO> list(Map<String, Object> params) {
        return contentScansDao.list(params);
    }

    @Override
    public List<ContentScansBO> listBO(Map<String, Object> params) {
        return contentScansDao.listBO(params);
    }

    @Override
    public List<ContentScansPO> listPO(Map<String, Object> params) {
        return contentScansDao.listPO(params);
    }

    @Override
    public SuccessResultList<List<ContentScansDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<ContentScansDTO> contentScansDTOs = list(page.getParams());
        PageInfo<ContentScansDTO> pageInfo = new PageInfo<>(contentScansDTOs);
        return new SuccessResultList<>(contentScansDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer count(Map<String, Object> params) {
        Integer count = contentScansDao.count(params);
        return count == null ? 0 : count;
    }

}