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

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
import cn.com.tenlion.materialstore.dao.materialstore.IMaterialStoreDao;
import cn.com.tenlion.materialstore.pojo.dtos.materialstore.MaterialStoreDTO;
import cn.com.tenlion.materialstore.pojo.vos.materialstore.MaterialStoreVO;
import cn.com.tenlion.materialstore.pojo.bos.materialstore.MaterialStoreBO;
import cn.com.tenlion.materialstore.pojo.pos.materialstore.MaterialStorePO;
import cn.com.tenlion.materialstore.service.materialstore.IMaterialStoreService;
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: MaterialStoreServiceImpl
 * @Description: CBC素材包
 * @Author: CodeFactory
 * @Date: 2021-08-24 11:13:38
 * @Version: 3.0
 **/
@Service
public class MaterialStoreServiceImpl extends DefaultBaseService implements IMaterialStoreService {

    @Autowired
    private IMaterialStoreDao materialStoreDao;

    @Override
    public void save(MaterialStoreVO materialStoreVO) {
        saveReturnId(materialStoreVO);
    }

    @Override
    public void save(String token, MaterialStoreVO materialStoreVO) {
        saveReturnId(token, materialStoreVO);
    }

    @Override
    public String saveReturnId(MaterialStoreVO materialStoreVO) {
        return saveReturnId(null, materialStoreVO);
    }

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

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

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

    @Override
    public void update(String materialStoreId, MaterialStoreVO materialStoreVO) {
        update(null, materialStoreId, materialStoreVO);
    }

    @Override
    public void update(String token, String materialStoreId, MaterialStoreVO materialStoreVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(materialStoreVO);
        params.put("materialStoreId", materialStoreId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        materialStoreDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

}