package cn.com.tenlion.materialstore.service.materialtext.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.materialtext.IMaterialTextDao;
import cn.com.tenlion.materialstore.pojo.dtos.materialtext.MaterialTextDTO;
import cn.com.tenlion.materialstore.pojo.vos.materialtext.MaterialTextVO;
import cn.com.tenlion.materialstore.pojo.bos.materialtext.MaterialTextBO;
import cn.com.tenlion.materialstore.pojo.pos.materialtext.MaterialTextPO;
import cn.com.tenlion.materialstore.service.materialtext.IMaterialTextService;
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: MaterialTextServiceImpl
 * @Description: 文字素材
 * @Author: CodeFactory
 * @Date: 2021-05-28 11:06:15
 * @Version: 3.0
 **/
@Service
public class MaterialTextServiceImpl extends DefaultBaseService implements IMaterialTextService {

    @Autowired
    private IMaterialTextDao materialTextDao;

    @Override
    public void save(MaterialTextVO materialTextVO) {
        saveReturnId(materialTextVO);
    }

    @Override
    public void save(String token, MaterialTextVO materialTextVO) {
        saveReturnId(token, materialTextVO);
    }

    @Override
    public String saveReturnId(MaterialTextVO materialTextVO) {
        return saveReturnId(null, materialTextVO);
    }

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

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

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

    @Override
    public void update(String materialTextId, MaterialTextVO materialTextVO) {
        update(null, materialTextId, materialTextVO);
    }

    @Override
    public void update(String token, String materialTextId, MaterialTextVO materialTextVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(materialTextVO);
        params.put("materialTextId", materialTextId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        materialTextDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}