package cn.com.tenlion.materialstore.service.materialvideo.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
import cn.com.tenlion.materialstore.dao.materialvideo.IMaterialVideoDao;
import cn.com.tenlion.materialstore.pojo.dtos.materialvideo.MaterialVideoDTO;
import cn.com.tenlion.materialstore.pojo.vos.materialvideo.MaterialVideoVO;
import cn.com.tenlion.materialstore.pojo.bos.materialvideo.MaterialVideoBO;
import cn.com.tenlion.materialstore.pojo.pos.materialvideo.MaterialVideoPO;
import cn.com.tenlion.materialstore.service.materialvideo.IMaterialVideoService;
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: MaterialVideoServiceImpl
 * @Description: 视频素材
 * @Author: CodeFactory
 * @Date: 2021-05-28 11:07:46
 * @Version: 3.0
 **/
@Service
public class MaterialVideoServiceImpl extends DefaultBaseService implements IMaterialVideoService {

    @Autowired
    private IMaterialVideoDao materialVideoDao;

    @Override
    public void save(MaterialVideoVO materialVideoVO) {
        saveReturnId(materialVideoVO);
    }

    @Override
    public void save(String token, MaterialVideoVO materialVideoVO) {
        saveReturnId(token, materialVideoVO);
    }

    @Override
    public String saveReturnId(MaterialVideoVO materialVideoVO) {
        return saveReturnId(null, materialVideoVO);
    }

    @Override
    public String saveReturnId(String token, MaterialVideoVO materialVideoVO) {
        String materialVideoId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(materialVideoVO);
        params.put("materialVideoId", materialVideoId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        materialVideoDao.save(params);
        return materialVideoId;
    }

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

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

    @Override
    public void update(String materialVideoId, MaterialVideoVO materialVideoVO) {
        update(null, materialVideoId, materialVideoVO);
    }

    @Override
    public void update(String token, String materialVideoId, MaterialVideoVO materialVideoVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(materialVideoVO);
        params.put("materialVideoId", materialVideoId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        materialVideoDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

    @Override
    public List<MaterialVideoDTO> getByMaterialId(String materialId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("materialId", materialId);
        return materialVideoDao.list(params);
    }

    @Override
    public void removeByMaterialId(String materialId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("materialId", materialId);
        materialVideoDao.removeByMaterialId(params);
    }

    @Override
    public void updateCheck(String id) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("materialVideoId", id);
        params.put("materialStatus", "0");
        materialVideoDao.updateCheck(params);
    }

}