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

import cn.com.tenlion.systembase.dao.comprehensivecenter.IComprehensiveCenterDao;
import cn.com.tenlion.systembase.pojo.bos.comprehensivecenter.ComprehensiveCenterBO;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.CgImportantEvent;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.ComprehensiveCenter;
import cn.com.tenlion.systembase.pojo.dtos.comprehensivecenter.ComprehensiveCenterDTO;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.pojo.pos.comprehensivecenter.ComprehensiveCenterPO;
import cn.com.tenlion.systembase.pojo.vos.comprehensivecenter.ComprehensiveCenterVO;
import cn.com.tenlion.systembase.pojo.vos.importantevent.ImportantEventVO;
import cn.com.tenlion.systembase.service.comprehensivecenter.IComprehensiveCenterService;
import cn.com.tenlion.systembase.service.grid.IGridService;
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 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: ComprehensiveCenterServiceImpl
 * @Description: 综治中心
 * @Author: CodeFactory
 * @Date: 2021-10-12 15:24:00
 * @Version: 3.0
 **/
@Service
public class ComprehensiveCenterServiceImpl extends DefaultBaseService implements IComprehensiveCenterService {

    @Autowired
    private IComprehensiveCenterDao comprehensiveCenterDao;
    @Autowired
    private IGridService gridService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public void save(ComprehensiveCenterVO comprehensiveCenterVO) {
        saveReturnId(comprehensiveCenterVO);
    }

    @Override
    public void save(String token, ComprehensiveCenterVO comprehensiveCenterVO) {
        saveReturnId(token, comprehensiveCenterVO);
    }

    @Override
    public String saveReturnId(ComprehensiveCenterVO comprehensiveCenterVO) {
        return saveReturnId(null, comprehensiveCenterVO);
    }

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

        saveKafka(comprehensiveCenterId, comprehensiveCenterVO);
        return comprehensiveCenterId;
    }

    @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("comprehensiveCenterIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        comprehensiveCenterDao.remove(params);

        deleteKafka(idsStr);
    }

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

    @Override
    public void update(String comprehensiveCenterId, ComprehensiveCenterVO comprehensiveCenterVO) {
        update(null, comprehensiveCenterId, comprehensiveCenterVO);
    }

    @Override
    public void update(String token, String comprehensiveCenterId, ComprehensiveCenterVO comprehensiveCenterVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(comprehensiveCenterVO);
        params.put("comprehensiveCenterId", comprehensiveCenterId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        comprehensiveCenterDao.update(params);

        updateKafka(comprehensiveCenterId, comprehensiveCenterVO);
    }

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

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

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

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

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

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

    @Override
    public List<ComprehensiveCenterDTO> list(Map<String, Object> params) {
        return comprehensiveCenterDao.list(params);
    }

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

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

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

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

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

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

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

    private ComprehensiveCenter setKafkaData(ComprehensiveCenterVO comprehensiveCenterVO) {
        ComprehensiveCenter comprehensiveCenter = new ComprehensiveCenter();
        // 综治中心名称
        comprehensiveCenter.setcenter_name(comprehensiveCenterVO.getCenterName());
        // 综治中心层级
        comprehensiveCenter.setcenter_level_name(comprehensiveCenterVO.getCenterLevelName());
        // 综治中心层级编码
        comprehensiveCenter.setcenter_level_code(comprehensiveCenterVO.getCenterLevelCode());
        // 详细地址
        comprehensiveCenter.setaddress(comprehensiveCenterVO.getAddress());
        // 区域全名称(/隔开)
        String locationAll = gridService.getLocationAll(comprehensiveCenterVO.getLocationCode());
        comprehensiveCenter.setlocation_all(locationAll);
        // 区域编码
        comprehensiveCenter.setlocation_code(comprehensiveCenterVO.getLocationCode());
        // 区域旗县区名称
        String locationArea = gridService.getLocationArea(comprehensiveCenterVO.getLocationCode());
        comprehensiveCenter.setlocation_area(locationArea);
        // 经度
        comprehensiveCenter.setlongitude(comprehensiveCenterVO.getLongitude().toString());
        // 纬度
        comprehensiveCenter.setlatitude(comprehensiveCenterVO.getLatitude().toString());
        GridDTO gridDTO = gridService.get(comprehensiveCenterVO.getGrid());
        if(null != gridDTO) {
            // 网格名称
            comprehensiveCenter.setgrid_name(gridDTO.getGridName());
            // 网格编码
            comprehensiveCenter.setgrid_code(gridDTO.getGridCode());
        }else {
            // 网格名称
            comprehensiveCenter.setgrid_name("暂无数据");
            // 网格编码
            comprehensiveCenter.setgrid_code("暂无数据");
        }
        return comprehensiveCenter;
    }

}