package cn.com.tenlion.materialstore.service.materiallink.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.materiallink.IMaterialLinkDao;
import cn.com.tenlion.materialstore.pojo.dtos.materiallink.MaterialLinkDTO;
import cn.com.tenlion.materialstore.pojo.vos.materiallink.MaterialLinkVO;
import cn.com.tenlion.materialstore.pojo.bos.materiallink.MaterialLinkBO;
import cn.com.tenlion.materialstore.pojo.pos.materiallink.MaterialLinkPO;
import cn.com.tenlion.materialstore.service.materiallink.IMaterialLinkService;
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: MaterialLinkServiceImpl
 * @Description: 链接素材
 * @Author: CodeFactory
 * @Date: 2021-05-28 10:54:44
 * @Version: 3.0
 **/
@Service
public class MaterialLinkServiceImpl extends DefaultBaseService implements IMaterialLinkService {

    @Autowired
    private IMaterialLinkDao materialLinkDao;

    @Override
    public void save(MaterialLinkVO materialLinkVO) {
        saveReturnId(materialLinkVO);
    }

    @Override
    public void save(String token, MaterialLinkVO materialLinkVO) {
        saveReturnId(token, materialLinkVO);
    }

    @Override
    public String saveReturnId(MaterialLinkVO materialLinkVO) {
        return saveReturnId(null, materialLinkVO);
    }

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

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

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

    @Override
    public void update(String materialLinkId, MaterialLinkVO materialLinkVO) {
        update(null, materialLinkId, materialLinkVO);
    }

    @Override
    public void update(String token, String materialLinkId, MaterialLinkVO materialLinkVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(materialLinkVO);
        params.put("materialLinkId", materialLinkId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        materialLinkDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}