package com.friday.custody.service.basicService.impl;

/*import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;*/

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.friday.custody.dao.ProductDataDao;
import com.friday.custody.entity.ProductDataEntity;
import com.friday.custody.service.basicService.ProductDataService;
import com.friday.custody.utils.PageUtils;
import com.friday.custody.utils.Query;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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


@Service("productDataService")
@Slf4j
public class ProductDataServiceImpl extends ServiceImpl<ProductDataDao, ProductDataEntity> implements ProductDataService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductDataEntity> page = this.page(
                new Query<ProductDataEntity>().getPage(params),
                new QueryWrapper<ProductDataEntity>()
        );

        return new PageUtils(page);
    }
    @Override
    public List<ProductDataEntity> saveProductData(JSONObject jsonObject){
        boolean status=false;
        List<JSONObject> value = (List<JSONObject>) jsonObject.get("value");
        ObjectMapper objectMapper = new ObjectMapper();
        ArrayList<ProductDataEntity> list = new ArrayList<>();
        value = objectMapper.convertValue(value, new TypeReference<List<JSONObject>>() {});
        ArrayList<ProductDataEntity> productDataEntities = new ArrayList<>();
        Map<String, String> remoteDataMap;
        String business_type;
        for (JSONObject object : value) {
            remoteDataMap = JSONObject.toJavaObject(object, Map.class);
            ProductDataEntity build = ProductDataEntity.builder()
                    .fundName(remoteDataMap.get("fund_name"))
                    .fundCode(remoteDataMap.get("fund_code"))
                    .fundCreationTime(remoteDataMap.get("fund_creation_time"))
                    .filingTime(remoteDataMap.get("filing_time"))
                    .filingStage(remoteDataMap.get("Filing_stage"))
                    .fundType(remoteDataMap.get("fund_type"))
                    .currency(remoteDataMap.get("currency"))
                    .adminType(remoteDataMap.get("admin_type"))
                    .trustName(remoteDataMap.get("trust_name"))
                    .operationStatus(remoteDataMap.get("operation_status"))
                    .fundLastUpdated(remoteDataMap.get("fund_last_updated"))
                    .tenantName(remoteDataMap.get("tenant_name"))
                    .build();
            if (null != build){
                QueryWrapper<ProductDataEntity> objectQueryWrapper = new QueryWrapper<>();
                objectQueryWrapper.eq("tenant_name",remoteDataMap.get("tenant_name"));
                objectQueryWrapper.eq("fund_code",remoteDataMap.get("fund_code"));
                List<ProductDataEntity> dataEntities = baseMapper.selectList(objectQueryWrapper);
                int i=0;
                if (null!=dataEntities && dataEntities.size()>0){
                    ProductDataEntity productDataEntity = dataEntities.get(0);
                    build.setId(productDataEntity.getId());
                    i = baseMapper.updateById(build);
                }else {
                    i= baseMapper.insert(build);
                }
                if (i==1){
                    ProductDataEntity productDataEntity = new ProductDataEntity(remoteDataMap.get("fund_code"));
                    list.add(productDataEntity);
                }
            }

        }

        return list;
    }
}