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

import cn.com.tenlion.configcontent.pojo.dtos.contentscans.ContentScansDTO;
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.contentdispatch.IContentDispatchDao;
import cn.com.tenlion.configcontent.pojo.dtos.contentdispatch.ContentDispatchDTO;
import cn.com.tenlion.configcontent.pojo.vos.contentdispatch.ContentDispatchVO;
import cn.com.tenlion.configcontent.pojo.bos.contentdispatch.ContentDispatchBO;
import cn.com.tenlion.configcontent.pojo.pos.contentdispatch.ContentDispatchPO;
import cn.com.tenlion.configcontent.service.contentdispatch.IContentDispatchService;
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: ContentDispatchServiceImpl
 * @Description: CBC内容管理转发记录表
 * @Author: CodeFactory
 * @Date: 2021-08-02 10:35:42
 * @Version: 3.0
 **/
@Service
public class ContentDispatchServiceImpl extends DefaultBaseService implements IContentDispatchService {

    @Autowired
    private IContentDispatchDao contentDispatchDao;

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

    @Override
    public void save(ContentDispatchVO contentDispatchVO) {
        saveReturnId(contentDispatchVO);
    }

    @Override
    public void save(String token, ContentDispatchVO contentDispatchVO) {
        saveReturnId(token, contentDispatchVO);
    }

    @Override
    public String saveReturnId(ContentDispatchVO contentDispatchVO) {
        return saveReturnId(null, contentDispatchVO);
    }

    @Override
    public String saveReturnId(String token, ContentDispatchVO contentDispatchVO) {
        String contentDispatchId = UUIDUtil.getUUID();
        if (token != null) {
            AppTokenUser appTokenUser = AppTokenManager.getInstance().getToken(token).getAppTokenUser();
            contentDispatchVO.setContentDispatchUserName(appTokenUser.getName());
            contentDispatchVO.setContentDispatchUserIcon(appTokenUser.getAvatar());
        } else {
            UserInfoBO userInfoBO = this.securityComponent.getCurrentUser();
            contentDispatchVO.setContentDispatchUserName(userInfoBO.getUserName());
        }
        Map<String, Object> params = HashMapUtil.beanToMap(contentDispatchVO);
        params.put("contentDispatchId", contentDispatchId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        /*List<ContentDispatchDTO> list = list(params);
        if (list.size() > 0) {
            for (ContentDispatchDTO dto : list) {
                updateGmtCreate(dto.getContentDispatchId(), DateUtil.getTime());
            }
            return "";
        }*/
        contentDispatchDao.save(params);
        return contentDispatchId;
    }

    @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("contentDispatchIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        contentDispatchDao.remove(params);
    }

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

    @Override
    public void update(String contentDispatchId, ContentDispatchVO contentDispatchVO) {
        update(null, contentDispatchId, contentDispatchVO);
    }

    @Override
    public void update(String token, String contentDispatchId, ContentDispatchVO contentDispatchVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(contentDispatchVO);
        params.put("contentDispatchId", contentDispatchId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        contentDispatchDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

}