package com.ssm.manage.service.impl;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.manage.dao.SsmStoreParaDao;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.StoreParament;
import com.ssm.manage.query.StoreParaRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmStoreParaService;
import com.ssm.manage.utils.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

@Service
@Slf4j
public class SsmStoreParaServiceImpl extends BaseServiceImpl<StoreParament> implements SsmStoreParaService {
    @Autowired
    public SsmStoreParaDao dao;

    @Override
    public SsmStoreParaDao getDao() {
        return dao;
    }

    @Override
    public List<StoreParament> list(Long storeId) {
        StoreParament storeParament = new StoreParament();
        storeParament.setStoreId(storeId);
        List<StoreParament> list = dao.select(storeParament);
        return list;
    }

    @Override
    public List<StoreParament> list1(Long storeId) {
        StoreParament storeParament = new StoreParament();
        storeParament.setStoreId(storeId);
        List<StoreParament> list = dao.select(storeParament);
        for(StoreParament storeParament1:list){
            if(storeParament1.getParamentName().equals("superPasswd")){
                list.remove(storeParament1);
                break;
            }
        }
        return list;
    }



//    @Override
//    public int addStorePara(StoreParaRequest storeParaRequest,Long storeId,String storeName) {
//        List<StoreParament> list = storeParaRequest.getList();
//        if (list.size() > 0){
//            for (StoreParament storeParament : list) {
//                storeParament.setStoreId(storeId);
//                storeParament.setParamentName(storeName);
//            }
//        }
//        return dao.addStorePara(list);
//    }


    @Override
    public int addStoreParas(Map<String,Object> formMap, Long storeId, String storeName) throws SmsException {
        List<StoreParament> list = new ArrayList<>();
//        Map<String,Object> map = (Map) JSON.parse(formMap);
        for (Map.Entry<String, Object> stringEntry : formMap.entrySet()) {
            String paraName = stringEntry.getKey();
            String paraValue = ObjectUtils.toString(stringEntry.getValue());
            String[] valueRemark = paraValue.split("\\|");
            StoreParament storeParament = new StoreParament();
            storeParament.setStoreId(storeId);
            storeParament.setStoreName(storeName);
            storeParament.setParamentName(paraName);
            storeParament.setParamentValue(valueRemark[0]);
            if (valueRemark.length > 1) {
                storeParament.setRemark(valueRemark[1]);
            }
            list.add(storeParament);
        }

        return dao.addStorePara(list);
    }

//    @Transactional
//    @Override
//    public int updateStorePara(StoreParaRequest storeParaRequest, Long storeId, String storeName) {
//        StoreParament storeParament = new StoreParament();
//        storeParament.setStoreId(storeId);
//        int i = 0;
//         try {
//            i = dao.delete(storeParament);
//         } catch (Exception e){
//             log.error("SsmStoreParaServiceImpl.updateStorePara",e);
//             TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//         }
//        if (i > 0){
//            List<StoreParament> list = storeParaRequest.getList();
//            if (list.size() > 0){
//                for (StoreParament parament : list) {
//                    parament.setStoreId(storeId);
//                    parament.setParamentName(storeName);
//                }
//            }
//            try {
//                i = dao.addStorePara(list);
//            } catch (Exception e){
//                log.error("SsmStoreParaServiceImpl.updateStorePara",e);
//                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//            }
//        }
//        return i;
//    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public int updateStorePara(Map<String,Object> formMap, Long storeId, String storeName) throws SmsException {
        StoreParament storeParament = new StoreParament();
        storeParament.setStoreId(storeId);
        int i = 0;
        try {
            i = dao.delete(storeParament);
        } catch (Exception e){
            log.error("SsmStoreParaServiceImpl.updateStorePara",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.FAILED);
        }
//        if (i > 0){
//            List<StoreParament> list = new ArrayList<>();
//            for (Map.Entry<String, Object> entry : formMap.entrySet()) {
//                String paraName = entry.getKey();
//                String paraValue = ObjectUtils.toString(entry.getValue());
//                String[] valueRemark = paraValue.split("\\|");
//                StoreParament parament = new StoreParament();
//                parament.setStoreId(storeId);
//                parament.setStoreName(storeName);
//                parament.setParamentName(paraName);
//                parament.setParamentValue(valueRemark[0]);
//                parament.setRemark(valueRemark[1]);
//                list.add(parament);
//            }
//            try {
//                i = dao.addStorePara(list);
//            } catch (Exception e){
//                log.error("SsmStoreParaServiceImpl.updateStorePara",e);
//                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//                throw new SmsException(ResponseCode.FAILED);
//            }
//        }
        List<StoreParament> list = new ArrayList<>();
        for (Map.Entry<String, Object> entry : formMap.entrySet()) {
            String paraName = entry.getKey();
            String paraValue = ObjectUtils.toString(entry.getValue());
            String[] valueRemark = paraValue.split("\\|");
            StoreParament parament = new StoreParament();
            parament.setStoreId(storeId);
            parament.setStoreName(storeName);
            parament.setParamentName(paraName);

            parament.setParamentValue(valueRemark[0]);
            parament.setRemark(valueRemark[1]);
            list.add(parament);
        }
        try {
            i = dao.addStorePara(list);
        } catch (Exception e){
            log.error("SsmStoreParaServiceImpl.updateStorePara",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.FAILED);
        }
        return i;
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public int updateStorePara1(Map<String,Object> formMap, List<StoreParament> storeIds) throws SmsException {
        StoreParament storeParament = new StoreParament();
        int i = 0;





        for(StoreParament storeParament1:storeIds){
            storeParament.setStoreId(storeParament1.getStoreId());
            try {
                i += dao.delete(storeParament);
            } catch (Exception e){
                log.error("SsmStoreParaServiceImpl.updateStorePara1",e);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new SmsException(ResponseCode.FAILED);
            }

            List<StoreParament> list = new ArrayList<>();
            for (Map.Entry<String, Object> entry : formMap.entrySet()) {
                String paraName = entry.getKey();
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                StoreParament parament = new StoreParament();

                parament.setStoreId(storeParament1.getStoreId());
                parament.setStoreName(storeParament1.getStoreName());
                if(!paraName.equals("toreIds") && !paraName.equals("storeId")){
                    parament.setParamentName(paraName);
                    parament.setParamentValue(valueRemark.length>=1?valueRemark[0]:"");
                    parament.setRemark(valueRemark.length>=2?valueRemark[1]:"");
                    list.add(parament);
                }
            }
            try {
                i = dao.addStorePara(list);
            } catch (Exception e){
                log.error("SsmStoreParaServiceImpl.updateStorePara",e);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new SmsException(ResponseCode.FAILED);
            }
        }
        return i;
    }

    @Transactional
    @Override
    public int updateStoreParas(List<StoreParament> storeParaments, Long storeId, String storeName) throws SmsException {
        StoreParament storeParament = new StoreParament();
        storeParament.setStoreId(storeId);
        int i = 0;
        try {
            i = dao.delete(storeParament);
        } catch (Exception e){
            log.error("SsmStoreParaServiceImpl.updateStorePara",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.FAILED);
        }
        if (i > 0){
            List<StoreParament> list = new ArrayList<>();
            for (StoreParament storeParament1 : storeParaments) {
                StoreParament parament = new StoreParament();
                parament.setStoreId(storeId);
                parament.setStoreName(storeName);
                parament.setParamentName(storeParament1.getParamentName());
                parament.setParamentValue(storeParament1.getParamentValue());
                parament.setRemark(storeParament1.getRemark());
                list.add(parament);
            }
            try {
                i = dao.addStorePara(list);
            } catch (Exception e){
                log.error("SsmStoreParaServiceImpl.updateStorePara",e);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new SmsException(ResponseCode.FAILED);
            }
        }
        return i;
    }

    //同步其他分店的门店参数


    public String getKey(String phone){
        String key = "";
        if (StringUtils.isNotBlank(phone)){
            key = Constant.staffKey + phone;
        }
        return key;
    }

}
