package ink.wgink.module.file.media.service.rtmp.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.module.file.media.dao.rtmp.IRtmpClientDao;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.date.DateUtil;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
import ink.wgink.module.file.media.pojo.dtos.rtmp.RtmpClientDTO;
import ink.wgink.module.file.media.pojo.vos.rtmp.RtmpClientVO;
import ink.wgink.module.file.media.pojo.bos.rtmp.RtmpClientBO;
import ink.wgink.module.file.media.pojo.pos.rtmp.RtmpClientPO;
import ink.wgink.module.file.media.service.rtmp.IRtmpClientService;
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: ClientServiceImpl
 * @Description: rtmp客户端
 * @Author: CodeFactory
 * @Date: 2022-04-26 22:03:44
 * @Version: 3.0
 **/
@Service
public class RtmpClientServiceImpl extends DefaultBaseService implements IRtmpClientService {

    @Autowired
    private IRtmpClientDao clientDao;

    @Override
    public void save(RtmpClientVO rtmpClientVO) {
        saveReturnId(rtmpClientVO);
    }

    @Override
    public void save(String token, RtmpClientVO rtmpClientVO) {
        saveReturnId(token, rtmpClientVO);
    }

    @Override
    public String saveReturnId(RtmpClientVO rtmpClientVO) {
        return saveReturnId(null, rtmpClientVO);
    }

    @Override
    public String saveReturnId(String token, RtmpClientVO rtmpClientVO) {
        String clientId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(rtmpClientVO);
        params.put("clientId", clientId);
        params.put("clientCode", getLatestClientCode());
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        clientDao.save(params);
        return clientId;
    }

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

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

    @Override
    public void update(String clientId, RtmpClientVO rtmpClientVO) {
        update(null, clientId, rtmpClientVO);
    }

    @Override
    public void update(String token, String clientId, RtmpClientVO rtmpClientVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(rtmpClientVO);
        params.put("clientId", clientId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        clientDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

    /**
     * 获取最新的客户端编码
     *
     * @return
     */
    private String getLatestClientCode() {
        String datetime = DateUtil.getDays();
        String latestClientCode = clientDao.latestClientCode(datetime);
        int todayLatestIndex = 0;
        if (!StringUtils.isBlank(latestClientCode)) {
            todayLatestIndex = Integer.parseInt(latestClientCode.replace(datetime, ""));
        }
        return String.format("%s%06d", datetime, todayLatestIndex + 1);
    }

}