package com.liquidnet.service.adam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.commons.lang.util.BsonUtil;
import com.liquidnet.service.adam.dto.vo.AdamAddressesVo;
import com.liquidnet.service.adam.entity.AdamAddresses;
import com.liquidnet.service.adam.mapper.AdamAddressesMapper;
import com.liquidnet.service.adam.service.IAdamAddressesService;
import com.liquidnet.service.adam.service.IAdamRdmService;
import com.mongodb.BasicDBObject;
import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.ReturnDocument;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 收货地址 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2021-05-11
 */
@Service
public class AdamAddressesServiceImpl extends ServiceImpl<AdamAddressesMapper, AdamAddresses> implements IAdamAddressesService {
    @Autowired
    AdamAddressesMapper adamAddressesMapper;
    @Autowired
    MongoConverter mongoConverter;
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    IAdamRdmService adamRdmService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(AdamAddresses adamAddresses) {
        List<AdamAddresses> addressesList = adamRdmService.getAddressesByUid(adamAddresses.getUid());

        adamAddresses.setIsDefault(CollectionUtils.isEmpty(addressesList));

        mongoTemplate.insert(adamAddresses, AdamAddresses.class.getSimpleName());

        // TODO: 2021/5/14
        List<Object> objectList = Arrays.asList();
//        rabbitTemplate.convertAndSend(MQConst.EXCHANGES_LIQUIDNET_SQL, MQConst.ROUTING_KEY_SQL,
//                SqlMapping.get("adam_addresses.add", objectList.toArray()));

        addressesList.add(adamAddresses);
        adamRdmService.setAddressesByUid(adamAddresses.getUid(), addressesList);
    }

//    @Override
//    public List<AdamAddresses> queryByUid(String uid) {
//        List<AdamAddresses> infoList = new ArrayList<>();
//
//        Map<Object, Object> objectMap = redisUtil.hmget(AdamRedisConst.INFO_ADDRESSES.concat(uid));
//
//        if (CollectionUtils.isEmpty(objectMap)) {
//            infoList = mongoTemplate.find(Query.query(Criteria.where("uid").is(uid).and("state").is(1)), AdamAddresses.class, AdamAddresses.class.getSimpleName());
//        } else {
//            Collection<Object> values = objectMap.values();
//            for (Object o : values) {
//                infoList.add((AdamAddresses) o);
//            }
//        }
//
//        return infoList;
//    }

//    @Override
//    public AdamAddresses query(String uid, String addressesId) {
//        AdamAddresses info = (AdamAddresses) redisUtil.hget(AdamRedisConst.INFO_ADDRESSES.concat(uid), addressesId);
//
//        if (null == info) {
//            info = mongoTemplate.findOne(
//                    Query.query(Criteria.where("entersId").is(addressesId).and("state").is(1).and("uid").is(uid)
//                    ), AdamAddresses.class, AdamAddresses.class.getSimpleName());
//
//            if (null != info) redisUtil.hset(AdamRedisConst.INFO_ADDRESSES.concat(uid), addressesId, info);
//        }
//
//        return info;
//    }

    @Override
    public void def(String uid, String addressesId) {
        LocalDateTime now = LocalDateTime.now();
        LinkedList<Object[]> linkedList = new LinkedList<>();
        List<AdamAddresses> addressesList = adamRdmService.getAddressesByUid(uid);
        if (addressesList.size() > 1) {// 取消原默认
            AdamAddresses unDeaultAddresses = new AdamAddresses();
            unDeaultAddresses.setIsDefault(false);
            unDeaultAddresses.setUpdatedAt(now);
            BasicDBObject object = new BasicDBObject("$set", mongoConverter.convertToMongoType(unDeaultAddresses));
            Document doc = mongoTemplate.getCollection(AdamAddresses.class.getSimpleName()).findOneAndUpdate(
                    Query.query(Criteria.where("uid").is(uid).and("isDefault").is(true)).getQueryObject(),
                    object, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)
            );
            if (null != doc) {
                AdamAddresses unDefaultAfterAddresses = BsonUtil.toBean(doc, AdamAddresses.class);

                List<Object> paramList = new ArrayList<>();
                paramList.add(unDeaultAddresses.getIsDefault());
                paramList.add(unDeaultAddresses.getUpdatedAt());
                paramList.add(unDefaultAfterAddresses.getAddressesId());
                linkedList.add(paramList.toArray());

                addressesList.removeIf(r -> r.getAddressesId().equals(unDefaultAfterAddresses.getAddressesId()));
                addressesList.add(unDefaultAfterAddresses);
            }
        }
        {// 设置新默认
            AdamAddresses defaultAddresses = new AdamAddresses();
            defaultAddresses.setIsDefault(true);
            defaultAddresses.setUpdatedAt(now);
            BasicDBObject object = new BasicDBObject("$set", mongoConverter.convertToMongoType(defaultAddresses));
            Document doc = mongoTemplate.getCollection(AdamAddresses.class.getSimpleName()).findOneAndUpdate(
                    Query.query(Criteria.where("uid").is(uid).and("addressesId").is(addressesId)).getQueryObject(),
                    object, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)
            );
            AdamAddresses addresses = BsonUtil.toBean(doc, AdamAddresses.class);

            List<Object> paramList = new ArrayList<>();
            paramList.add(defaultAddresses.getIsDefault());
            paramList.add(defaultAddresses.getUpdatedAt());
            paramList.add(addressesId);
//            rabbitTemplate.convertAndSend(MQConst.EXCHANGES_LIQUIDNET_SQL, MQConst.ROUTING_KEY_SQL,
//                    SqlMapping.get("adam_enters.update.is_default", paramList.toArray()));
            linkedList.add(paramList.toArray());

            addressesList.removeIf(r -> r.getAddressesId().equals(addresses.getAddressesId()));
            addressesList.add(addresses);
        }
        adamRdmService.setAddressesByUid(uid, addressesList);

//        rabbitTemplate.convertAndSend(MQConst.EXCHANGES_LIQUIDNET_SQL, MQConst.ROUTING_KEY_SQL,
//                SqlMapping.get("adam_addresses.update.is_default", linkedList));

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void edit(AdamAddresses info) {
        BasicDBObject object = new BasicDBObject("$set", mongoConverter.convertToMongoType(info));
        Document doc = mongoTemplate.getCollection(AdamAddresses.class.getSimpleName()).findOneAndUpdate(
                Query.query(Criteria.where("addressesId").is(info.getAddressesId())).getQueryObject(),
                object, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)
        );

        // TODO: 2021/5/14
        List<Object> paramList = Arrays.asList();
//        rabbitTemplate.convertAndSend(MQConst.EXCHANGES_LIQUIDNET_SQL, MQConst.ROUTING_KEY_SQL,
//                SqlMapping.get("adam_addresses.edit", paramList.toArray()));

        List<AdamAddresses> list = adamRdmService.getAddressesByUid(info.getUid());
        list.removeIf(r -> r.getAddressesId().equals(info.getAddressesId()));
        list.add(info);
        adamRdmService.setAddressesByUid(info.getUid(), list);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void remove(String uid, String addressesId) {
        AdamAddresses addresses = new AdamAddresses();
        addresses.setUpdatedAt(LocalDateTime.now());
        addresses.setDeletedAt(addresses.getUpdatedAt());
        addresses.setState(2);

        BasicDBObject object = new BasicDBObject("$set", mongoConverter.convertToMongoType(addresses));
        UpdateResult updateResult = mongoTemplate.getCollection(AdamAddresses.class.getSimpleName())
                .updateOne(Query.query(Criteria.where("addressesId").is(addressesId)).getQueryObject(), object);

        List<Object> paramList = Arrays.asList(addresses.getUpdatedAt(), addresses.getDeletedAt(), addressesId);
//        rabbitTemplate.convertAndSend(MQConst.EXCHANGES_LIQUIDNET_SQL, MQConst.ROUTING_KEY_SQL,
//                SqlMapping.get("adam_addresses.remove", paramList.toArray()));

        List<AdamAddresses> list = adamRdmService.getAddressesByUid(uid);
        list.removeIf(r -> r.getAddressesId().equals(addressesId));
        adamRdmService.setAddressesByUid(uid, list);
    }

    @Override
    public AdamAddressesVo queryDefault(String uid) {
        List<AdamAddresses> addressesList = adamRdmService.getAddressesByUid(uid);

        if (!CollectionUtils.isEmpty(addressesList)) {
            AdamAddresses defaultAddr = null;
            for (AdamAddresses addresses : addressesList) {
                if (addresses.getIsDefault()) {
                    return AdamAddressesVo.getNew().copy(addresses);
                }
                defaultAddr = null == defaultAddr ? addresses :
                        addresses.getCreatedAt().isAfter(defaultAddr.getCreatedAt()) ? addresses : defaultAddr;
            }
            return AdamAddressesVo.getNew().copy(defaultAddr);
        }
        return null;
    }
}
