package cn.com.tenlion.materialstore.service.materialmap.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.materialmap.IMaterialMapDao;
import cn.com.tenlion.materialstore.pojo.dtos.materialmap.MaterialMapDTO;
import cn.com.tenlion.materialstore.pojo.vos.materialmap.MaterialMapVO;
import cn.com.tenlion.materialstore.pojo.bos.materialmap.MaterialMapBO;
import cn.com.tenlion.materialstore.pojo.pos.materialmap.MaterialMapPO;
import cn.com.tenlion.materialstore.service.materialmap.IMaterialMapService;
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: MaterialMapServiceImpl
 * @Description: 经纬度素材
 * @Author: CodeFactory
 * @Date: 2021-05-28 11:01:59
 * @Version: 3.0
 **/
@Service
public class MaterialMapServiceImpl extends DefaultBaseService implements IMaterialMapService {

    @Autowired
    private IMaterialMapDao materialMapDao;

    @Override
    public void save(MaterialMapVO materialMapVO) {
        saveReturnId(materialMapVO);
    }

    @Override
    public void save(String token, MaterialMapVO materialMapVO) {
        saveReturnId(token, materialMapVO);
    }

    @Override
    public String saveReturnId(MaterialMapVO materialMapVO) {
        return saveReturnId(null, materialMapVO);
    }

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

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

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

    @Override
    public void update(String materialMapId, MaterialMapVO materialMapVO) {
        update(null, materialMapId, materialMapVO);
    }

    @Override
    public void update(String token, String materialMapId, MaterialMapVO materialMapVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(materialMapVO);
        params.put("materialMapId", materialMapId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        materialMapDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}