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

import cn.com.tenlion.materialstore.pojo.bos.materialaudio.MaterialAudioBO;
import cn.com.tenlion.materialstore.pojo.dtos.materialaudio.MaterialAudioDTO;
import cn.com.tenlion.materialstore.pojo.pos.materialaudio.MaterialAudioPO;
import cn.com.tenlion.materialstore.pojo.vos.materialaudio.MaterialAudioVO;
import cn.com.tenlion.materialstore.dao.materialaudio.IMaterialAudioDao;
import cn.com.tenlion.materialstore.service.materialaudio.IMaterialAudioService;
import cn.com.tenlion.materialstore.util.MaterialTypeEnum;
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 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: MaterialAudioServiceImpl
 * @Description: 音频素材
 * @Author: CodeFactory
 * @Date: 2021-05-28 10:41:09
 * @Version: 3.0
 **/
@Service
public class MaterialAudioServiceImpl extends DefaultBaseService implements IMaterialAudioService {

    @Autowired
    private IMaterialAudioDao materialAudioDao;

    @Override
    public void save(MaterialAudioVO materialAudioVO) {
        saveReturnId(materialAudioVO);
    }

    @Override
    public void save(String token, MaterialAudioVO materialAudioVO) {
        saveReturnId(token, materialAudioVO);
    }

    @Override
    public String saveReturnId(MaterialAudioVO materialAudioVO) {
        return saveReturnId(null, materialAudioVO);
    }

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

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

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

    @Override
    public void update(String materialAudioId, MaterialAudioVO materialAudioVO) {
        update(null, materialAudioId, materialAudioVO);
    }

    @Override
    public void update(String token, String materialAudioId, MaterialAudioVO materialAudioVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(materialAudioVO);
        params.put("materialAudioId", materialAudioId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        materialAudioDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

    @Override
    public Integer count(Map<String, Object> params) {
        MaterialTypeEnum[] arr = MaterialTypeEnum.values();
        Integer count = materialAudioDao.count(params);
        return count == null ? 0 : count;
    }

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

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

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

}