package selling.sunshine.dao.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import selling.sunshine.dao.BaseDao;
import selling.sunshine.dao.CustomerDao;
import selling.sunshine.model.Customer;
import selling.sunshine.model.CustomerAddress;
import selling.sunshine.model.CustomerPhone;
import selling.sunshine.model.Goods;
import selling.sunshine.utils.IDGenerator;
import selling.sunshine.utils.ResponseCode;
import selling.sunshine.utils.ResultData;

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

/**
 * Created by sunshine on 5/5/16.
 */
@Repository
public class CustomerDaoImpl extends BaseDao implements CustomerDao {

    private Logger logger = LoggerFactory.getLogger(CustomerDaoImpl.class);
    private Object lock = new Object();

    @Override
    public ResultData insertCustomer(Customer customer) {
        ResultData result = new ResultData();
        synchronized (lock) {
            try {
                customer.setCustomerId(IDGenerator.generate("CUS"));
                sqlSession.insert("selling.customer.insert", customer);
                CustomerPhone phoneNumber = customer.getPhone();
                CustomerAddress address = customer.getAddress();
                phoneNumber.setPhoneId(IDGenerator.generate("PNM"));
                address.setAddressId(IDGenerator.generate("ADD"));
                sqlSession.insert("selling.customer.phone.insert", phoneNumber);
                sqlSession.insert("selling.customer.address.insert", address);
                result.setData(customer);
            } catch (Exception e) {
                logger.error(e.getMessage());
                result.setResponseCode(ResponseCode.RESPONSE_ERROR);
                result = insertCustomer(customer);
            } finally {
                return result;
            }
        }
    }

	@Override
	public ResultData updateCustomer(Customer customer) {
		ResultData result = new ResultData();
		try {
			
            Map<String, Object> condition=new HashMap<String, Object>();
            condition.put("customerId",customer.getCustomerId());
            condition.put("name",customer.getName());
            condition.put("blockFlag",customer.isBlockFlag());
            
			if(ResponseCode.RESPONSE_OK.equals(queryCustomer(condition).getResponseCode())){
				CustomerPhone phoneNumber=customer.getPhone();
                CustomerAddress address=customer.getAddress();
                
                sqlSession.insert("selling.customer.phone.insert", phoneNumber);
                sqlSession.insert("selling.customer.address.insert", address);
                Customer c=((List<Customer>)queryCustomer(condition).getData()).get(0);
               
                sqlSession.update("selling.customer.phone.block",c.getPhone());
                sqlSession.update("selling.customer.address.block",c.getAddress());                
			}
			result.setData(customer);
			result.setResponseCode(ResponseCode.RESPONSE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage());
            result.setResponseCode(ResponseCode.RESPONSE_ERROR);
        } finally {
            return result;
        }

    }

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

@@ -132,24 +132,89 @@
 Repository;%0A
+import org.springframework.transaction.annotation.Transactional;%0A
 import selli
@@ -892,16 +892,35 @@
 ect();%0A%0A
+    @Transactional%0A
     @Ove
@@ -1974,17 +1974,39 @@
 %0A    %7D%0A%0A
-%09
+    @Transactional%0A    
 @Overrid
@@ -2007,17 +2007,20 @@
 verride%0A
-%09
+    
 public R
@@ -2065,18 +2065,24 @@
 omer) %7B%0A
-%09%09
+        
 ResultDa
@@ -2115,19 +2115,22 @@
 ();%0A
-%09%09
+        
 try %7B%0A
-%09%09%09
 %0A   
@@ -2167,17 +2167,19 @@
 ondition
-=
+ = 
 new Hash
@@ -2240,16 +2240,17 @@
 omerId%22,
+ 
 customer
@@ -2301,16 +2301,17 @@
 (%22name%22,
+ 
 customer
@@ -2361,16 +2361,17 @@
 ckFlag%22,
+ 
 customer
@@ -2383,24 +2383,25 @@
 ockFlag());%0A
+%0A
             
@@ -2404,14 +2404,11 @@
     
-%0A%09%09%09
 if
+ 
 (Res
@@ -2484,14 +2484,27 @@
 ()))
+ 
 %7B%0A
-%09%09%09%09
+                
 Cust
@@ -2524,17 +2524,19 @@
 neNumber
-=
+ = 
 customer
@@ -2587,17 +2587,19 @@
  address
-=
+ = 
 customer
@@ -2613,32 +2613,16 @@
 ress();%0A
-                
 %0A       
@@ -2800,17 +2800,19 @@
 stomer c
-=
+ = 
 ((List%3CC
@@ -2820,16 +2820,17 @@
 stomer%3E)
+ 
 queryCus
@@ -2865,31 +2865,16 @@
 get(0);%0A
-               
 %0A       
@@ -2927,24 +2927,25 @@
 hone.block%22,
+ 
 c.getPhone()
@@ -3014,16 +3014,17 @@
 .block%22,
+ 
 c.getAdd
@@ -3031,16 +3031,17 @@
 ress());
+%0A
         
@@ -3048,21 +3048,22 @@
     
+%7D%0A
     
-%0A%09%09%09%7D%0A%09%09%09
+        
 resu
@@ -3084,19 +3084,28 @@
 tomer);%0A
-%09%09%09
+            
 result.s
@@ -3146,18 +3146,24 @@
 SE_OK);%0A
-%09%09
+        
 %7D catch 
@@ -3182,11 +3182,20 @@
 ) %7B%0A
-%09%09%09
+            
 logg

apply transaction to insert and update method