package com.izouma.awesomeadmin.service.impl;

import java.util.*;

import com.izouma.awesomeadmin.service.OSSFileService;
import com.izouma.awesomeadmin.util.PropertiesFileLoader;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.UserFileInfo;
import com.izouma.awesomeadmin.service.UserFileInfoService;
import com.izouma.awesomeadmin.dao.UserFileInfoMapper;

/**
 * service接口实现类
 */
@Service
public class UserFileInfoServiceImpl implements UserFileInfoService {

    private static Logger logger = Logger.getLogger(UserFileInfoServiceImpl.class);

    @Autowired
    private UserFileInfoMapper userFileInfoMapper;

    @Autowired
    private OSSFileService oSSFileService;

    @Override
    public List<UserFileInfo> getUserFileInfoList(UserFileInfo record) {

        logger.info("getUserFileInfoList");
        try {

            return userFileInfoMapper.queryAllUserFileInfo(record);
        } catch (Exception e) {
            logger.error("getUserFileInfoList", e);
        }

        return null;
    }

    @Override
    public List<UserFileInfo> getUserFileInfoByPage(Page page, UserFileInfo record) {

        logger.info("getUserFileInfoByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put(AppConstant.PAGE, page);

            return userFileInfoMapper.queryUserFileInfoByPage(parameter);
        } catch (Exception e) {
            logger.error("getUserFileInfoByPage", e);
        }

        return null;
    }

    @Override
    public UserFileInfo getUserFileInfoById(String id) {

        logger.info("getUserFileInfoyId");
        try {

            return userFileInfoMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            logger.error("getUserFileInfoById", e);
        }

        return null;
    }

    @Override
    public UserFileInfo getUserFileInfo(UserFileInfo record) {

        logger.info("getUserFileInfo");
        try {

            return userFileInfoMapper.queryUserFileInfo(record);
        } catch (Exception e) {
            logger.error("getUserFileInfo", e);
        }

        return null;
    }

    @Override
    public boolean createUserFileInfo(UserFileInfo record) {

        logger.info("createUserFileInfo");
        try {

            int updates = userFileInfoMapper.insertSelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("createUserFileInfo", e);
        }

        return false;
    }

    @Override
    public boolean deleteUserFileInfo(UserFileInfo record) {

        logger.info("deleteUserFileInfo");
        try {
            if (record.getId() != null || StringUtils.isNotEmpty(record.getIdStr())) {

                List<UserFileInfo> userFileInfoList = userFileInfoMapper.queryAllUserFileInfo(record);
                for (UserFileInfo fileInfo : userFileInfoList) {
                    if (StringUtils.isNotEmpty(fileInfo.getUrl())) {

                        String artwork = fileInfo.getUrl().replace(PropertiesFileLoader.getProperties("aliImageSever") + "/", "");
                        oSSFileService.deleteObject(artwork);
                    }
                }

            }

            int updates = userFileInfoMapper.delete(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("deleteUserFileInfo", e);
        }

        return false;
    }

    @Override
    public boolean updateUserFileInfo(UserFileInfo record) {

        logger.info("updateUserFileInfo");
        try {

            int updates = userFileInfoMapper.updateByPrimaryKeySelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("updateUserFileInfo", e);
        }

        return false;
    }
}

