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

import com.alibaba.fastjson.JSONObject;
import com.tofly.watermeter.custom.dto.AreaCodeDto;
import com.tofly.watermeter.custom.dto.BookCodeDto;
import com.tofly.watermeter.custom.dto.TypeCodeDto;
import com.tofly.watermeter.custom.dto.VendorCodeDto;
import com.tofly.watermeter.custom.entity.marketing.CommonParam;
import com.tofly.watermeter.custom.mapper.DmaCodeMapper;
import com.tofly.watermeter.custom.service.DmaCodeService;
import com.tofly.watermeter.util.CallAPIUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class DmaCodeServiceImpl implements DmaCodeService {
    @Autowired
    private DmaCodeMapper dmaCodeMapper;

    private final static String AREA_TABLENAME = "TF_YWPN_DMACISCBPQ_W";

    private final static String BOOK_TABLENAME = "TF_YWPN_DMACISCBB_W";

    private final static String VENDOR_TABLENAME = "TF_YWPN_DMACISSBCJ_W";


    /**
     * 同步抄表片区
     */
    @Override
    public boolean synchronizationArea() throws Exception {
        CommonParam commonParam = new CommonParam();
        commonParam.setACTION("query");
        Map map = CallAPIUtil.callAPIUtil(commonParam, "/api/Business/MeterReadingAreas");
        //0000代表成功
        if ("0000".equals(map.get("Code"))){
            int count = Integer.parseInt(map.get("count").toString());
            if (count > 0){
                dmaCodeMapper.deleteData(AREA_TABLENAME);
                dmaCodeMapper.insertAreaCode(JSONObject.parseArray(map.get("data").toString(), AreaCodeDto.class));
            }
        }
        return true;
    }

    @Override
    public boolean synchronizationMeterReading() throws Exception {
        CommonParam commonParam = new CommonParam();
        commonParam.setACTION("query");
        Map map = CallAPIUtil.callAPIUtil(commonParam, "/api/Business/MeterReadingBook");
        if ("0000".equals(map.get("Code"))){
            int count = Integer.parseInt(map.get("count").toString());
            if (count > 0){
                dmaCodeMapper.deleteData(BOOK_TABLENAME);
                dmaCodeMapper.insertBookCode(JSONObject.parseArray(map.get("data").toString(), BookCodeDto.class));
            }
        }
        return true;
    }

    @Override
    public boolean synchronizationVendor() throws Exception {
        CommonParam commonParam = new CommonParam();
        commonParam.setACTION("query");
        Map map = CallAPIUtil.callAPIUtil(commonParam, "/api/Business/WaterMaker");
        if ("0000".equals(map.get("Code"))){
            int count = Integer.parseInt(map.get("count").toString());
            if (count > 0){
                dmaCodeMapper.deleteData(VENDOR_TABLENAME);
                dmaCodeMapper.insertVendorCode(JSONObject.parseArray(map.get("data").toString(), VendorCodeDto.class));
            }
        }
        return true;
    }

    @Override
    public boolean synchronizationType() throws Exception {
        CommonParam commonParam = new CommonParam();
        commonParam.setACTION("query");
        Map map = CallAPIUtil.callAPIUtil(commonParam, "/api/Business/WaterType");
        if ("0000".equals(map.get("Code"))){
            int count = Integer.parseInt(map.get("count").toString());
            if (count > 0){
                dmaCodeMapper.deleteData(VENDOR_TABLENAME);
                dmaCodeMapper.insertTypeCode(JSONObject.parseArray(map.get("data").toString(), TypeCodeDto.class));
            }
        }
        return true;
    }
}
