package com.tofly.watermeter.custom.service.meterBusiness.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.watermeter.custom.dto.VendorCodeDto;
import com.tofly.watermeter.custom.entity.marketing.CommonParam;
import com.tofly.watermeter.custom.mapper.meterBusiness.ConfigManageMapper;
import com.tofly.watermeter.custom.service.meterBusiness.ConfigManageService;
import com.tofly.watermeter.custom.service.meterBusiness.export.ExportCommonCodeService;
import com.tofly.watermeter.service.BwrkdetailService;
import com.tofly.watermeter.util.CallAPIUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class ConfigMangeServiceImpl implements ConfigManageService {

    private final ConfigManageMapper configManageMapper;
    private final ExportCommonCodeService exportCommonCodeService;
    private final BwrkdetailService bwrkdetailService;

    @Override
    public List<Map> vendorConfig(HttpServletResponse response, Long export) {
        List<Map> maps = configManageMapper.codeConfig(2L);
        if (export == 1) {
            exportCommonCodeService.exportCode(response, "vendorConfigService", maps);
        }
        return maps;
    }

    @Override
    public List<Map> calConfig(HttpServletResponse response, Long export) {
        List<Map> maps = configManageMapper.codeConfig(1L);
        if (export == 1) {
            exportCommonCodeService.exportCode(response, "calConfigService", maps);
        }
        return maps;
    }

    @Override
    public List<Map> typeConfig(HttpServletResponse response, Long export) {
        List<Map> maps = configManageMapper.codeConfig(3L);
        if (export == 1) {
            exportCommonCodeService.exportCode(response, "typeConfigService", maps);
        }
        return maps;
    }

    /**
     * 同步厂家
     *
     * @return boolean
     */
    @Override
    public boolean synVendor() {

        CommonParam cp = new CommonParam();
        cp.setACTION("query");
        Map data = null;
        try {
            data = CallAPIUtil.callAPIUtil(cp, "/api/Business/WaterMaker");
        } catch ( Exception e ) {
            e.printStackTrace();
        }
        if (CollectionUtil.isEmpty(data)) {
            throw new ToflyDeniedException("调用森鑫炬接口失败，请确认接口能否访问");
        } else {
            //调用成功，返回
//            if ("600".equals(data.get("statusCode"))) {
            if (Integer.valueOf(data.get("statusCode").toString()) == 600) {
                List<VendorCodeDto> result = JSONObject.parseArray(String.valueOf(data.get("data")), VendorCodeDto.class);
                List<Object> vendorCode = configManageMapper.codeConfig(2L)
                        .stream().filter(Objects::nonNull)
                        .map(m -> m.get("ccode")).collect(Collectors.toList());

                List<VendorCodeDto> vendorData = result.stream().filter(Objects::nonNull)
                        .filter(v -> !vendorCode.contains(v.getSBCJ_BM())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(vendorData)) {
                    Long maxId = bwrkdetailService.getMaxId("TF_YWPN_SYSCHILDCODE_D");
                    for (VendorCodeDto vcd : vendorData) {
                        vcd.setId(maxId);
                        maxId++;
                    }
                    configManageMapper.insertCode(vendorData);
                }
            }
        }
        return true;
    }
}