package selling.sunshine.dao.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import selling.sunshine.dao.AdminDao;
import selling.sunshine.dao.BaseDao;
import selling.sunshine.model.Admin;
import selling.sunshine.model.Role;
import selling.sunshine.model.User;
import selling.sunshine.utils.IDGenerator;
import selling.sunshine.utils.ResponseCode;
import selling.sunshine.utils.ResultData;

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

/**
 * Created by sunshine on 4/21/16.
 */
@Repository
public class AdminDaoImpl extends BaseDao implements AdminDao {
    private Logger logger = LoggerFactory.getLogger(AdminDaoImpl.class);

    private Object lock = new Object();

    @Override
    public ResultData queryAdmin(Map<String, Object> condition) {
        ResultData result = new ResultData();
        try {
            List<Admin> list = sqlSession.selectList("selling.admin.query",
                    condition);
            result.setData(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            result.setResponseCode(ResponseCode.RESPONSE_ERROR);
            result.setDescription(e.getMessage());
        } finally {
            return result;
        }
    }

    @Transactional
    @Override
    public ResultData insertAdmin(Admin admin) {
        ResultData result = new ResultData();
        synchronized (lock) {
            try {
                admin.setAdminId(IDGenerator.generate("MNG"));
                sqlSession.insert("selling.admin.insert", admin);
                User user = new User(admin.getUsername(), admin.getPassword());
                user.setUserId(IDGenerator.generate("USR"));
                Role role = new Role();
                role.setRoleId("ROL00000001");
                user.setRole(role);
                user.setAdmin(admin);
                sqlSession.insert("selling.user.insert", user);
                result.setData(admin);
            } catch (Exception e) {
                logger.error(e.getMessage());
                result.setResponseCode(ResponseCode.RESPONSE_ERROR);
                result = insertAdmin(admin);
            } finally {
                return result;
            }
        }
    }

    @Transactional
    @Override
    public ResultData updateAdmin(Admin admin) {
        ResultData result = new ResultData();
        synchronized (lock) {
            try {
                sqlSession.update("selling.admin.update", admin);
                User user = new User(admin.getUsername(), admin.getPassword());
                user.setAdmin(admin);
                sqlSession.update("selling.user.update", user);
                result.setData(admin);
            } catch (Exception e) {
                logger.debug(e.getMessage());
                result.setResponseCode(ResponseCode.RESPONSE_ERROR);
                result.setDescription(e.getMessage());
            } finally {
                return result;
            }
        }
    }

    @Transactional
    @Override
    public ResultData deleteAdmin(Admin admin) {
        ResultData result = new ResultData();
        synchronized (lock) {
            try {
                User user = new User(admin.getUsername(), admin.getPassword());
                sqlSession.delete("selling.user.delete", user);
                sqlSession.delete("selling.admin.delete", admin);
                result.setData(admin);
            } catch (Exception e) {
                logger.debug(e.getMessage());
                result.setResponseCode(ResponseCode.RESPONSE_ERROR);
                result.setDescription(e.getMessage());
            } finally {
                return result;
            }
        }
    }
}

@@ -791,24 +791,113 @@
  Object();%0A%0A
+    /**%0A     * %E6%A0%B9%E6%8D%AE%E6%9F%A5%E8%AF%A2%E7%9A%84%E6%9D%A1%E4%BB%B6%E6%9F%A5%E6%89%BE%E7%AC%A6%E5%90%88%E6%9D%A1%E4%BB%B6%E7%9A%84%E7%AE%A1%E7%90%86%E5%91%98%E5%88%97%E8%A1%A8%0A     *%0A     * @param condition%0A     * @return%0A     */%0A
     @Overrid
@@ -890,32 +890,32 @@
 /%0A    @Override%0A
-
     public Resul
@@ -1103,28 +1103,8 @@
 ry%22,
-%0A                   
  con
@@ -1392,32 +1392,119 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * %E6%B7%BB%E5%8A%A0%E7%AE%A1%E7%90%86%E5%91%98%E8%AE%B0%E5%BD%95,%E5%90%8C%E6%97%B6%E6%B7%BB%E5%8A%A0%E7%94%A8%E6%88%B7%E8%A1%A8%E4%B8%AD%E7%9A%84%E7%9B%B8%E5%BA%94%E8%AE%B0%E5%BD%95%0A     *%0A     * @param admin%0A     * @return%0A     */%0A
     @Transaction
@@ -2471,32 +2471,113 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * %E6%9B%B4%E6%96%B0%E7%AE%A1%E7%90%86%E5%91%98%E8%AE%B0%E5%BD%95,%E5%90%8C%E6%97%B6%E6%9B%B4%E6%96%B0%E7%94%A8%E6%88%B7%E8%A1%A8%0A     *%0A     * @param admin%0A     * @return%0A     */%0A
     @Transaction
@@ -3311,24 +3311,99 @@
    %7D%0A    %7D%0A%0A
+    /**%0A     * %E5%88%A0%E9%99%A4%E7%AE%A1%E7%90%86%E5%91%98%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AF%0A     *%0A     * @param admin%0A     * @return%0A     */%0A
     @Transac

add comment in admin dao