package cn.com.tenlion.materialstore.service.materialphoto.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.materialphoto.IMaterialPhotoDao;
import cn.com.tenlion.materialstore.pojo.dtos.materialphoto.MaterialPhotoDTO;
import cn.com.tenlion.materialstore.pojo.vos.materialphoto.MaterialPhotoVO;
import cn.com.tenlion.materialstore.pojo.bos.materialphoto.MaterialPhotoBO;
import cn.com.tenlion.materialstore.pojo.pos.materialphoto.MaterialPhotoPO;
import cn.com.tenlion.materialstore.service.materialphoto.IMaterialPhotoService;
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: MaterialPhotoServiceImpl
 * @Description: 图片素材
 * @Author: CodeFactory
 * @Date: 2021-05-28 11:04:34
 * @Version: 3.0
 **/
@Service
public class MaterialPhotoServiceImpl extends DefaultBaseService implements IMaterialPhotoService {

    @Autowired
    private IMaterialPhotoDao materialPhotoDao;

    @Override
    public void save(MaterialPhotoVO materialPhotoVO) {
        saveReturnId(materialPhotoVO);
    }

    @Override
    public void save(String token, MaterialPhotoVO materialPhotoVO) {
        saveReturnId(token, materialPhotoVO);
    }

    @Override
    public String saveReturnId(MaterialPhotoVO materialPhotoVO) {
        return saveReturnId(null, materialPhotoVO);
    }

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

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

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

    @Override
    public void update(String materialPhotoId, MaterialPhotoVO materialPhotoVO) {
        update(null, materialPhotoId, materialPhotoVO);
    }

    @Override
    public void update(String token, String materialPhotoId, MaterialPhotoVO materialPhotoVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(materialPhotoVO);
        params.put("materialPhotoId", materialPhotoId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        materialPhotoDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}