package cn.com.tenlion.systembase.service.importantevent.impl;

import cn.com.tenlion.systembase.pojo.dtos.bigdta.CgImportantEvent;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.util.VerifyUtil;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
import cn.com.tenlion.systembase.dao.importantevent.IImportantEventDao;
import cn.com.tenlion.systembase.pojo.dtos.importantevent.ImportantEventDTO;
import cn.com.tenlion.systembase.pojo.vos.importantevent.ImportantEventVO;
import cn.com.tenlion.systembase.pojo.bos.importantevent.ImportantEventBO;
import cn.com.tenlion.systembase.pojo.pos.importantevent.ImportantEventPO;
import cn.com.tenlion.systembase.service.importantevent.IImportantEventService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: ImportantEventServiceImpl
 * @Description: 重大事件
 * @Author: CodeFactory
 * @Date: 2021-10-11 17:21:48
 * @Version: 3.0
 **/
@Service
public class ImportantEventServiceImpl extends DefaultBaseService implements IImportantEventService {

    @Autowired
    private IImportantEventDao importantEventDao;
    @Autowired
    private IGridService gridService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public void save(ImportantEventVO importantEventVO) {
        saveReturnId(importantEventVO);
    }

    @Override
    public void save(String token, ImportantEventVO importantEventVO) {
        saveReturnId(token, importantEventVO);
    }

    @Override
    public String saveReturnId(ImportantEventVO importantEventVO) {
        return saveReturnId(null, importantEventVO);
    }

    @Override
    public String saveReturnId(String token, ImportantEventVO importantEventVO) {
        String importantEventId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(importantEventVO);
        params.put("importantEventId", importantEventId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        importantEventDao.save(params);

        saveKafka(importantEventId, importantEventVO);
        return importantEventId;
    }

    @Override
    public void remove(List<String> ids, String idsStr) {
        remove(null, ids, idsStr);
    }

    @Override
    public void remove(String token, List<String> ids, String idsStr) {
        Map<String, Object> params = getHashMap(2);
        params.put("importantEventIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        importantEventDao.remove(params);

        deleteKafka(idsStr);
    }

    @Override
    public void delete(List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("importantEventIds", ids);
        importantEventDao.delete(params);
    }

    @Override
    public void update(String importantEventId, ImportantEventVO importantEventVO) {
        update(null, importantEventId, importantEventVO);
    }

    @Override
    public void update(String token, String importantEventId, ImportantEventVO importantEventVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(importantEventVO);
        params.put("importantEventId", importantEventId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        importantEventDao.update(params);

        updateKafka(importantEventId, importantEventVO);
    }

    @Override
    public ImportantEventDTO get(Map<String, Object> params) {
        ImportantEventDTO importantEventDTO = importantEventDao.get(params);
        if(null != importantEventDTO) {
            GridDTO gridDTO = gridService.get(importantEventDTO.getGrid());
            if(null != gridDTO) {
                importantEventDTO.setGridName(gridDTO.getGridName());
            }
        }
        return importantEventDTO;
    }

    @Override
    public ImportantEventDTO get(String importantEventId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("importantEventId", importantEventId);
        return get(params);
    }

    @Override
    public ImportantEventBO getBO(Map<String, Object> params) {
        return importantEventDao.getBO(params);
    }

    @Override
    public ImportantEventBO getBO(String importantEventId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("importantEventId", importantEventId);
        return getBO(params);
    }

    @Override
    public ImportantEventPO getPO(Map<String, Object> params) {
        return importantEventDao.getPO(params);
    }

    @Override
    public ImportantEventPO getPO(String importantEventId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("importantEventId", importantEventId);
        return getPO(params);
    }

    @Override
    public List<ImportantEventDTO> list(Map<String, Object> params) {
        if(params.get("areaCodeLike") != null && !"".equals(params.get("areaCodeLike").toString())){
            String areaCoed = params.get("areaCodeLike").toString();
            String[] areaCodeSplit = VerifyUtil.splitStringByLength(areaCoed, 2);
            String areaCodeLike = "";
            for(int i = 5 ; i >= 0; i--){
                if(!"00".equals(areaCodeSplit[i])){
                    areaCodeLike = areaCoed.substring(0, (i + 1) * 2 );
                    break;
                }
            }
            params.put("areaCodeLike",areaCodeLike);
        }
        return importantEventDao.list(params);
    }

    @Override
    public List<ImportantEventBO> listBO(Map<String, Object> params) {
        return importantEventDao.listBO(params);
    }

    @Override
    public List<ImportantEventPO> listPO(Map<String, Object> params) {
        return importantEventDao.listPO(params);
    }

    @Override
    public SuccessResultList<List<ImportantEventDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<ImportantEventDTO> importantEventDTOs = list(page.getParams());
        PageInfo<ImportantEventDTO> pageInfo = new PageInfo<>(importantEventDTOs);
        return new SuccessResultList<>(importantEventDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer count(Map<String, Object> params) {
        Integer count = importantEventDao.count(params);
        return count == null ? 0 : count;
    }

    @Override
    public void saveKafka(String uid, ImportantEventVO importantEventVO) {
        CgImportantEvent cgImportantEvent = setKafkaData(importantEventVO);
        JSONObject jsonObject = new JSONObject();
        // 实体模板编码
        jsonObject.put("tableNumber", "C0018");
        // 数据主键
        jsonObject.put("uid", uid);
        // 设置数据操作方法
        // action: save/updete/delete
        jsonObject.put("action", "save");
        // 实体
        jsonObject.put("data", cgImportantEvent.saveCheckToJson());
        kafkaTemplate.send("tableSync", jsonObject.toJSONString());
    }

    @Override
    public void updateKafka(String uid, ImportantEventVO importantEventVO) {
        CgImportantEvent cgImportantEvent = setKafkaData(importantEventVO);
        JSONObject jsonObject = new JSONObject();
        // 实体模板编码
        jsonObject.put("tableNumber", "C0018");
        // 数据主键
        jsonObject.put("uid", uid);
        // 设置数据操作方法
        // action: save/updete/delete
        jsonObject.put("action", "update");
        // 实体
        jsonObject.put("data", cgImportantEvent.updateToJson());
        kafkaTemplate.send("tableSync", jsonObject.toJSONString());
    }

    @Override
    public void deleteKafka(String uid) {
        JSONObject jsonObject = new JSONObject();
        // 实体模板编码
        jsonObject.put("tableNumber", "C0018");
        // 数据主键
        jsonObject.put("uid", uid);
        // 设置数据操作方法
        // action: save/updete/delete
        jsonObject.put("action", "delete");
        // 实体
        jsonObject.put("data", new CgImportantEvent());
        kafkaTemplate.send("tableSync", jsonObject.toJSONString());
    }

    private CgImportantEvent setKafkaData(ImportantEventVO importantEventVO) {
        CgImportantEvent cgImportantEvent = new CgImportantEvent();
        cgImportantEvent.setevent_name(importantEventVO.getEventName());
        cgImportantEvent.setevent_date(importantEventVO.getEventDate());
        cgImportantEvent.setevent_site(importantEventVO.getEventSite());
        cgImportantEvent.setevent_level(importantEventVO.getEventLevelText());
        // 事件类型
        cgImportantEvent.setevent_type(importantEventVO.getEventTypeText());
        // 区域全名称(/隔开)
        String locationAll = gridService.getLocationAll(importantEventVO.getLocationCode());
        cgImportantEvent.setlocation_all(locationAll);
        // 区域编码
        cgImportantEvent.setlocation_code(importantEventVO.getLocationCode());
        // 区域旗县区名称
        String locationArea = gridService.getLocationArea(importantEventVO.getLocationCode());
        cgImportantEvent.setlocation_area(locationArea);
        // 经度
        cgImportantEvent.setlongitude(importantEventVO.getLongitude());
        // 纬度
        cgImportantEvent.setlatitude(importantEventVO.getLatitude());
        GridDTO gridDTO = gridService.get(importantEventVO.getGrid());
        if(null != gridDTO) {
            // 网格名称
            cgImportantEvent.setgrid_name(gridDTO.getGridName());
            // 网格编码
            cgImportantEvent.setgrid_code(gridDTO.getGridCode());
        }else {
            // 网格名称
            cgImportantEvent.setgrid_name("暂无数据");
            // 网格编码
            cgImportantEvent.setgrid_code("暂无数据");
        }
        return cgImportantEvent;
    }

}