package com.platform.service.impl;

import com.platform.dao.SceneDao;
import com.platform.entity.SceneEntity;
import com.platform.service.SceneService;
import com.platform.utils.R;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-19 17:59:15
 */
@Service
public class SceneServiceImpl implements SceneService {
    private static Logger log = LoggerFactory.getLogger(SceneServiceImpl.class);

    @Autowired
    private SceneDao sceneDao;

    @Override
    public SceneEntity queryObject(Integer id) {
        return sceneDao.queryObject(id);
    }

    @Override
    public List<SceneEntity> queryList(Map<String, Object> map) {
        return sceneDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sceneDao.queryTotal(map);
    }

    @Override
    public int save(SceneEntity brand) {
        SceneEntity SceneEntity = sceneDao.findByName(brand.getName());
        if(SceneEntity != null){
            log.error("场景已经被绑定");
            return 0;
        }
        return sceneDao.save(brand);
    }

    @Override
    public R update(SceneEntity brand) {
        SceneEntity SceneEntity = sceneDao.findByName(brand.getName());
        if(SceneEntity != null && SceneEntity.getId().intValue() != brand.getId().intValue()){
            return R.error("店铺名称重复");
        }
        int update = sceneDao.update(brand);
        if(update != 1){
            return R.error("更新店铺失败");
        }
        return R.ok();
    }

    @Override
    public int delete(Integer id) {
        return sceneDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return sceneDao.deleteBatch(ids);
    }

    @Override
    public SceneEntity queryByScene(String sceneNum) {

        if(StringUtils.isBlank(sceneNum)){
            return null;
        }
        return sceneDao.queryBySceneNum(sceneNum);
    }

    @Override
    public List<SceneEntity> selectByDeptIdsAndExceptSceneNums(List<Long> deptIds, List<String> sceneNums) {
        return sceneDao.selectByDeptIdsAndExceptSceneNums(deptIds,sceneNums);
    }

    @Override
    public void updateSceneUser(Long fromUserId, Long toUserId,Long currentUserId) {
        sceneDao.updateSceneUser(fromUserId,toUserId,currentUserId);
    }

    @Override
    public List<SceneEntity> queryByUserId(Long userId) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("createUserId",userId);
        return sceneDao.queryList(map);
    }
}
